mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-05 20:20:59 +08:00
Added halfvec_spherical_distance function [skip ci]
This commit is contained in:
@@ -879,6 +879,32 @@ halfvec_cosine_distance(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_FLOAT8(1 - similarity);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the distance for spherical k-means
|
||||
* Currently uses angular distance since needs to satisfy triangle inequality
|
||||
* Assumes inputs are unit vectors (skips norm)
|
||||
*/
|
||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(halfvec_spherical_distance);
|
||||
Datum
|
||||
halfvec_spherical_distance(PG_FUNCTION_ARGS)
|
||||
{
|
||||
HalfVector *a = PG_GETARG_HALFVEC_P(0);
|
||||
HalfVector *b = PG_GETARG_HALFVEC_P(1);
|
||||
double distance;
|
||||
|
||||
CheckDims(a, b);
|
||||
|
||||
distance = (double) HalfvecInnerProduct(a->dim, a->x, b->x);
|
||||
|
||||
/* Prevent NaN with acos with loss of precision */
|
||||
if (distance > 1)
|
||||
distance = 1;
|
||||
else if (distance < -1)
|
||||
distance = -1;
|
||||
|
||||
PG_RETURN_FLOAT8(acos(distance) / M_PI);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the L1 distance between two half vectors
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user