Vectors, Matrices, and Tensors
Fixed-size numeric arrays carry dimensions in their types.
Float[#3]
Float[#3] is a vector with exactly three Float values.
point: Float[#3] = {1.0, 2.0, 3.0}
-- point[2] == 3.0
Compile-Time Bounds
Indexing a fixed-size array with a proven valid index can be checked before runtime.
func main(args: List[String]) -> Void:
point: Float[#3] = {1.0, 2.0, 3.0}
z: Float = point[2]
-- z == 3.0
Dot Product
vector.dot requires both operands to have the same element type and dimension.
import:
vector: dot
func main(args: List[String]) -> Void:
a: Float[#3] = {1.0, 2.0, 3.0}
b: Float[#3] = {4.0, 5.0, 6.0}
score: Float = dot(a, b)
-- score == 32.0
Matrix Basics
Matrix helpers such as multiply_vector preserve dimensions in their result types.
m: Float[#2, #2] = {
{1.0, 0.0},
{0.0, 1.0},
}
-- m[1, 1] == 1.0
Example
vectors.brp
import:
float: sqrt
matrix: multiply_vector
vector: dot
pure func distance(a: Float[#3], b: Float[#3]) -> Float:
delta: Float[#3] = a - b
sqrt(dot(delta, delta))
func main(args: List[String]) -> Void:
p: Float[#3] = {1.0, 2.0, 3.0}
q: Float[#3] = {1.0, 4.0, 3.0}
transform: Float[#2, #2] = {
{1.0, 2.0},
{3.0, 4.0},
}
point: Float[#2] = {5.0, 6.0}
moved: Float[#2] = multiply_vector(transform, point)
print(distance(p, q)) -- prints: 2
print(moved[0]) -- prints: 17
print(moved[1]) -- prints: 39
Try it
terminal
blorp run vectors.brp