mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-02 02:31:16 +08:00
Exclude zero vectors for cosine distance to be consistent with other types [skip ci]
This commit is contained in:
@@ -204,6 +204,10 @@ HnswNormValue(FmgrInfo *procinfo, Oid collation, Datum *value, HnswType type)
|
||||
|
||||
*value = PointerGetDatum(result);
|
||||
}
|
||||
else if (type == HNSW_TYPE_INTVEC)
|
||||
{
|
||||
/* Do nothing */
|
||||
}
|
||||
else
|
||||
elog(ERROR, "Unsupported type");
|
||||
|
||||
|
||||
18
src/intvec.c
18
src/intvec.c
@@ -588,3 +588,21 @@ intvec_l1_distance(PG_FUNCTION_ARGS)
|
||||
|
||||
PG_RETURN_FLOAT8((double) distance);
|
||||
}
|
||||
|
||||
/*
|
||||
* Get the L2 norm of an int vector
|
||||
*/
|
||||
PGDLLEXPORT PG_FUNCTION_INFO_V1(intvec_l2_norm);
|
||||
Datum
|
||||
intvec_l2_norm(PG_FUNCTION_ARGS)
|
||||
{
|
||||
IntVector *a = PG_GETARG_INTVEC_P(0);
|
||||
int8 *ax = a->x;
|
||||
int norm = 0;
|
||||
|
||||
/* Auto-vectorized */
|
||||
for (int i = 0; i < a->dim; i++)
|
||||
norm += ax[i] * ax[i];
|
||||
|
||||
PG_RETURN_FLOAT8(sqrt((double) norm));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user