Vector Examples

Some basic examples with doubles.

import vecxt.all.*
import vecxt.BoundsCheck.DoBoundsCheck.yes

val v1 = Array[Double](1, 2, 3)
// v1: Array[Double] = Array(1.0, 2.0, 3.0)
val v2 = Array[Double](4, 5, 6)
// v2: Array[Double] = Array(4.0, 5.0, 6.0)

v1.dot(v2)
// res0: Double = 32.0

cosineSimilarity(v1, v2)
// res1: Double = 0.9746318461970762

(v1 + v2).printArr
// res2: String = "[5.0,7.0,9.0]"

(v1 - v2).printArr
// res3: String = "[-3.0,-3.0,-3.0]"

(v1 * 2.0).printArr
// res4: String = "[2.0,4.0,6.0]"

(v1 / 2.0).printArr
// res5: String = "[0.5,1.0,1.5]"

(v1 > 2).printArr
// res6: String = "[false,false,true]"
(v1 >= 2).printArr
// res7: String = "[false,true,true]"

(v1 < 2).printArr
// res8: String = "[true,false,false]"
(v1 <= 2).printArr
// res9: String = "[true,true,false]"

(v1(v1 <= 2)).printArr
// res10: String = "[1.0,2.0]"

(v1.outer(v2)).printMat
// res11: String = """4.0 5.0 6.0
// 8.0 10.0 12.0
// 12.0 15.0 18.0"""

And Ints. Note that the API here is more limited at the moment.

import vecxt.all.*
import narr.*
import vecxt.BoundsCheck.DoBoundsCheck.yes

val v1 = NArray(1, 2, 3)
// v1: Array[Int] = Array(1, 2, 3)
val v2 = NArray(4, 5, 6)
// v2: Array[Int] = Array(4, 5, 6)


v1.dot(v2)
// res13: Int = 32

(v1 + v2).printArr
// res14: String = "[5,7,9]"

(v1 - v2).printArr
// res15: String = "[-3,-3,-3]"

(v1 > 2).printArr
// res16: String = "[false,false,true]"
(v1 >= 2).printArr
// res17: String = "[false,true,true]"

(v1 < 2).printArr
// res18: String = "[true,false,false]"
(v1 <= 2).printArr
// res19: String = "[true,true,false]"

(v1(v1 <= 2)).printArr
// res20: String = "[1,2]"
In this article