Skip to main content
Functions for measuring the length of a vector (norms) and the distance or similarity between two equal-length vectors, useful for embeddings, feature scoring, and nearest-neighbor style comparisons. Vectors are passed as arrays or tuples of numeric values, and paired functions require both inputs to have the same length.

cosineDistance

Computes the cosine distance between two vectors, defined as one minus their cosine similarity. Values range from 0 (identical direction) to 2 (opposite direction), so smaller values mean more similar orientation regardless of magnitude. Syntax
Arguments
  • vector1 — first vector, an array or tuple of numeric values.
  • vector2 — second vector, an array or tuple of the same length.
Returned value
  • The cosine distance as a Float (Float32 or Float64 depending on the input types).
Example
Cosine distance between two embeddings

L2Distance / L2Norm

L2Distance returns the Euclidean distance between two vectors, the square root of the sum of the squared differences of their elements. L2Norm returns the Euclidean length of a single vector, the square root of the sum of the squares of its elements. Syntax
Arguments
  • vector, vector1, vector2 — arrays or tuples of numeric values. The two arguments to L2Distance must have the same length.
Returned value
  • L2Distance: the Euclidean distance between the two vectors as a Float.
  • L2Norm: the Euclidean length of the vector as a Float.
Example
Euclidean distance and L2 norm

L2SquaredDistance / L2SquaredNorm

L2SquaredDistance returns the sum of the squared differences between corresponding elements of two vectors (the squared Euclidean distance). L2SquaredNorm returns the sum of the squares of a single vector’s elements (the squared L2 norm). Skipping the square root makes these cheaper than their L2 counterparts and is sufficient when you only need to rank or compare distances. Syntax
Arguments
  • vector, vector1, vector2 — arrays or tuples of numeric values. The two arguments to L2SquaredDistance must have the same length.
Returned value
  • L2SquaredDistance: the sum of squared element-wise differences.
  • L2SquaredNorm: the sum of the squared element values.
Both return an integer or Float value depending on the input types. Example
Squared Euclidean distance and squared L2 norm

dotProduct

Computes the scalar (dot) product of two vectors: the sum of the products of their corresponding elements. Syntax
Arguments
  • vector1 — first vector, an array or tuple of numeric values.
  • vector2 — second vector, an array or tuple of the same length.
Returned value
  • The scalar product as a single numeric value whose type follows the input element types.
Example
Dot product of two vectors

L1Distance / L1Norm

L1Distance returns the Manhattan (taxicab) distance between two vectors, the sum of the absolute differences of their elements. L1Norm returns the L1 length of a single vector, the sum of the absolute values of its elements. Syntax
Arguments
  • vector, vector1, vector2 — arrays or tuples of numeric values. The two arguments to L1Distance must have the same length.
Returned value
  • L1Distance: the sum of absolute element-wise differences.
  • L1Norm: the sum of the absolute element values.
Both return an integer or Float value depending on the input types. Example
Manhattan distance and L1 norm

LinfDistance / LinfNorm

LinfDistance returns the Chebyshev (L-infinity) distance between two vectors, the maximum absolute difference across their elements. LinfNorm returns the L-infinity length of a single vector, the maximum absolute value among its elements. Syntax
Arguments
  • vector, vector1, vector2 — arrays or tuples of numeric values. The two arguments to LinfDistance must have the same length.
Returned value
  • LinfDistance: the largest absolute element-wise difference, as a Float.
  • LinfNorm: the largest absolute element value, as a Float.
Example
Chebyshev distance and L-infinity norm