Categories:

Vector similarity functions

VECTOR_L1_DISTANCE

Computes the L1 distance between two vectors.

L1 distance, also known as the Taxicab or Manhattan distance, is a measure of the distance between two points in a vector space. The distance is calculated by taking the sum of the absolute value of the differences of vector elements. The result is a value of zero or higher. If the distance is zero, the vectors are identical. The larger the distance, the farther apart the vectors are.

See also:

VECTOR_INNER_PRODUCT , VECTOR_L2_DISTANCE , VECTOR_COSINE_SIMILARITY , Vector Embeddings

Syntax

VECTOR_L1_DISTANCE( <vector>, <vector> )
Copy

Arguments

vector

The VECTOR value to calculate the distance from.

vector

The VECTOR value to calculate the distance to.

Returns

Returns the L1 distance between the two input vectors as a FLOAT value.

Usage notes

  • Due to computational optimizations in the vector comparison functions, floating-point errors may be slightly larger than usual (e.g. about 1e-4).

Examples

This example uses the VECTOR_L1_DISTANCE function to determine which vectors in the table are closest to each other between columns a and b:

CREATE TABLE vectors (a VECTOR(FLOAT, 3), b VECTOR(FLOAT, 3));
INSERT INTO vectors SELECT [1.1,2.2,3]::VECTOR(FLOAT,3), [1,1,1]::VECTOR(FLOAT,3);
INSERT INTO vectors SELECT [1,2.2,3]::VECTOR(FLOAT,3), [4,6,8]::VECTOR(FLOAT,3);

SELECT VECTOR_L1_DISTANCE(a, b) FROM vectors;
Copy
+--------------+
| 3.300000191  |
|--------------|
| 11.800000191 |
+--------------+