mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-01 10:11:20 +08:00
Added l2_normalize function for sparsevec
This commit is contained in:
@@ -848,6 +848,48 @@ sparsevec_l2_norm(PG_FUNCTION_ARGS)
|
||||
PG_RETURN_FLOAT8(sqrt(norm));
|
||||
}
|
||||
|
||||
/*
|
||||
* Normalize a sparse vector with the L2 norm
|
||||
*/
|
||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(sparsevec_l2_normalize);
|
||||
Datum
|
||||
sparsevec_l2_normalize(PG_FUNCTION_ARGS)
|
||||
{
|
||||
SparseVector *a = PG_GETARG_SPARSEVEC_P(0);
|
||||
float *ax = SPARSEVEC_VALUES(a);
|
||||
double norm = 0;
|
||||
SparseVector *result;
|
||||
float *rx;
|
||||
|
||||
result = InitSparseVector(a->dim, a->nnz);
|
||||
rx = SPARSEVEC_VALUES(result);
|
||||
|
||||
/* Auto-vectorized */
|
||||
for (int i = 0; i < a->nnz; i++)
|
||||
norm += (double) ax[i] * (double) ax[i];
|
||||
|
||||
norm = sqrt(norm);
|
||||
|
||||
/* Return zero vector for zero norm */
|
||||
if (norm > 0)
|
||||
{
|
||||
for (int i = 0; i < a->nnz; i++)
|
||||
{
|
||||
result->indices[i] = a->indices[i];
|
||||
rx[i] = ax[i] / norm;
|
||||
}
|
||||
|
||||
/* Check for overflow */
|
||||
for (int i = 0; i < a->nnz; i++)
|
||||
{
|
||||
if (isinf(rx[i]))
|
||||
float_overflow_error();
|
||||
}
|
||||
}
|
||||
|
||||
PG_RETURN_POINTER(result);
|
||||
}
|
||||
|
||||
/*
|
||||
* Internal helper to compare sparse vectors
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user