Vec
The Vec module provides numeric vector operations for linear algebra and SIMD-style computations. These functions operate on Lists, treating them as mathematical vectors for element-wise arithmetic, dot products, normalization, and statistical operations.
Vectors are Lists
Vec functions operate on regular Kit Lists. There is no separate Vec type.
Use standard list literals like [1.0, 2.0, 3.0] to create vectors.
Creating Vectors
Vec.new
List a -> List a
Creates a vector from a list. This is an identity function that can be used for clarity.
v = Vec.new [1.0, 2.0, 3.0]
println v
# => [1.0, 2.0, 3.0]
Vec.splat
Int -> a -> List a
Creates a vector of the given size with all elements set to the same value.
v = Vec.splat 4 1.0
println v
# => [1.0, 1.0, 1.0, 1.0]
Element-wise Arithmetic
Vec.add
List a -> List a -> List a
Performs element-wise addition of two vectors. Vectors must have the same length.
v1 = [1.0, 2.0, 3.0]
v2 = [4.0, 5.0, 6.0]
println (Vec.add v1 v2)
# => [5.0, 7.0, 9.0]
Vec.sub
List a -> List a -> List a
Performs element-wise subtraction of two vectors.
v1 = [10.0, 20.0, 30.0]
v2 = [1.0, 2.0, 3.0]
println (Vec.sub v1 v2)
# => [9.0, 18.0, 27.0]
Vec.mul
List a -> List a -> List a
Performs element-wise multiplication of two vectors (Hadamard product).
v1 = [2.0, 3.0, 4.0]
v2 = [5.0, 6.0, 7.0]
println (Vec.mul v1 v2)
# => [10.0, 18.0, 28.0]
Vec.div
List a -> List a -> List a
Performs element-wise division of two vectors.
v1 = [20.0, 30.0, 40.0]
v2 = [2.0, 3.0, 4.0]
println (Vec.div v1 v2)
# => [10.0, 10.0, 10.0]
Vec.scale
List Float -> Float -> List Float
Multiplies each element of a vector by a scalar value.
v = [1.0, 2.0, 3.0]
println (Vec.scale v 2.0)
# => [2.0, 4.0, 6.0]
Reduction Operations
Vec.sum
List a -> a
Returns the sum of all elements in the vector.
println (Vec.sum [1.0, 2.0, 3.0, 4.0, 5.0])
# => 15.0
Vec.product
List a -> a
Returns the product of all elements in the vector.
println (Vec.product [2.0, 3.0, 4.0])
# => 24.0
Vec.min
List a -> a
Returns the minimum element in the vector.
println (Vec.min [5.0, 2.0, 8.0, 1.0, 9.0])
# => 1.0
Vec.max
List a -> a
Returns the maximum element in the vector.
println (Vec.max [5.0, 2.0, 8.0, 1.0, 9.0])
# => 9.0
Linear Algebra
Vec.dot
List Float -> List Float -> Float
Computes the dot product of two vectors.
v1 = [1.0, 2.0, 3.0]
v2 = [4.0, 5.0, 6.0]
println (Vec.dot v1 v2)
# => 32.0 (1*4 + 2*5 + 3*6)
Vec.magnitude
List Float -> Float
Computes the Euclidean magnitude (length) of a vector.
println (Vec.magnitude [3.0, 4.0])
# => 5.0
println (Vec.magnitude [1.0, 2.0, 2.0])
# => 3.0
Vec.normalize
List Float -> List Float
Returns a unit vector (magnitude 1) in the same direction.
v = [3.0, 4.0]
unit = Vec.normalize v
println unit
# => [0.6, 0.8]
println (Vec.magnitude unit)
# => 1.0
Vec.distance
List Float -> List Float -> Float
Computes the Euclidean distance between two vectors.
p1 = [0.0, 0.0]
p2 = [3.0, 4.0]
println (Vec.distance p1 p2)
# => 5.0
Statistics
Vec.mean
List Float -> Float
Computes the arithmetic mean (average) of a vector.
println (Vec.mean [1.0, 2.0, 3.0, 4.0, 5.0])
# => 3.0
Vec.std
List Float -> Float
Computes the standard deviation of a vector.
println (Vec.std [2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0])
# => 2.0
Access
Vec.get
List a -> Int -> a
Returns the element at the given index (0-based).
v = [10.0, 20.0, 30.0]
println (Vec.get v 1)
# => 20.0
Vec.length
List a -> Int
Returns the number of elements in the vector.
println (Vec.length [1.0, 2.0, 3.0])
# => 3
Vec.to-list
List a -> List a
Converts a vector to a list. Since vectors are lists, this is an identity function.
v = [1.0, 2.0, 3.0]
lst = Vec.to-list v
println lst
# => [1.0, 2.0, 3.0]
Usage Example
# Calculate the angle between two vectors
angle-between = fn(v1, v2) =>
cos-theta = Vec.dot v1 v2 / (Vec.magnitude v1 * Vec.magnitude v2)
Float.acos cos-theta
v1 = [1.0, 0.0]
v2 = [0.0, 1.0]
println (angle-between v1 v2)
# => 1.5707963... (pi/2, or 90 degrees)