Skip to content

math::linalg

The linalg module provides vector operations on plain Array<number> values: dot/cross products, norms, normalization, distance, and element-wise arithmetic.

use std::math::linalg

Dot product of two vectors of equal length.

linalg::dot([1, 2, 3], [4, 5, 6]) // 32

Cross product of two 3D vectors.

linalg::cross([1, 0, 0], [0, 1, 0]) // [0, 0, 1]

Euclidean (L2) length of a vector.

linalg::norm([3, 4]) // 5

Scale a vector to unit length. Returns the input unchanged when its norm is zero.

linalg::normalize([3, 4]) // [0.6, 0.8]

Euclidean distance between two vectors.

linalg::distance([0, 0], [3, 4]) // 5

Multiply every element of v by scalar s.

linalg::scale([1, 2, 3], 2) // [2, 4, 6]

Element-wise addition.

linalg::add([1, 2], [3, 4]) // [4, 6]

Element-wise subtraction (a - b).

linalg::sub([3, 4], [1, 2]) // [2, 2]