mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-16 17:32:17 +08:00
Updated SparsevecInnerProduct [skip ci]
This commit is contained in:
@@ -895,28 +895,24 @@ SparsevecInnerProduct(SparseVector * a, SparseVector * b)
|
|||||||
float *ax = SPARSEVEC_VALUES(a);
|
float *ax = SPARSEVEC_VALUES(a);
|
||||||
float *bx = SPARSEVEC_VALUES(b);
|
float *bx = SPARSEVEC_VALUES(b);
|
||||||
float distance = 0.0;
|
float distance = 0.0;
|
||||||
int bpos = 0;
|
int i = 0;
|
||||||
|
int j = 0;
|
||||||
|
|
||||||
for (int i = 0; i < a->nnz; i++)
|
while (i < a->nnz && j < b->nnz)
|
||||||
{
|
{
|
||||||
int ai = a->indices[i];
|
int ai = a->indices[i];
|
||||||
|
int bi = b->indices[j];
|
||||||
|
|
||||||
for (int j = bpos; j < b->nnz; j++)
|
if (ai == bi)
|
||||||
{
|
{
|
||||||
int bi = b->indices[j];
|
distance += ax[i] * bx[j];
|
||||||
|
i++;
|
||||||
/* Only update when the same index */
|
j++;
|
||||||
if (ai == bi)
|
|
||||||
distance += ax[i] * bx[j];
|
|
||||||
|
|
||||||
/* Update start for next iteration */
|
|
||||||
if (ai >= bi)
|
|
||||||
bpos = j + 1;
|
|
||||||
|
|
||||||
/* Found or passed it */
|
|
||||||
if (bi >= ai)
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
else if (ai < bi)
|
||||||
|
i++;
|
||||||
|
else
|
||||||
|
j++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return distance;
|
return distance;
|
||||||
|
|||||||
Reference in New Issue
Block a user