Updated on 2024-06-03 GMT+08:00

Vector Distance Calculation Interface

l2_distance

Function: Calculates the Euclidean distance between two vectors.

Type of input parameter 1: floatvector

Type of input parameter 2: floatvector

Output parameter type: float8

Code example:

gaussdb=# SELECT l2_distance(floatvector('[1,2,3]'), floatvector('[5,-1,3.5]'));
gaussdb=# SELECT l2_distance('[1,2,3]',  '[5,-1,3.5]');

vector_l2_squared_distance

Function: Calculates the square of the Euclidean distance between two vectors.

Type of input parameter 1: floatvector

Type of input parameter 2: floatvector

Output parameter type: float8

Code example:

gaussdb=# SELECT vector_l2_squared_distance(floatvector('[1,2,3]'), floatvector('[5,-1,3.5]'));
gaussdb=# SELECT vector_l2_squared_distance('[1,2,3]',  '[5,-1,3.5]');

cosine_distance

Function: Calculates the cosine distance between two vectors.

Type of input parameter 1: floatvector

Type of input parameter 2: floatvector

Output parameter type: float8

Code example:

gaussdb=# select cosine_distance(floatvector('[1,2,3]'), floatvector('[5,-1,3.5]'));
gaussdb=# select cosine_distance('[1,2,3]', '[5,-1,3.5]');

vector_spherical_distance

Function: Calculates the spherical distance between two normalized vectors (represented by the radian system of the cosine angle).

Type of input parameter 1: floatvector

Type of input parameter 2: floatvector

Output parameter type: float8

Code example:

gaussdb=# gaussdb=# select vector_spherical_distance('[1,0,0,0]', '[0,0,0,1]');

If the input vector is not normalized, the correct calculation result cannot be obtained.