3030#include " space_sparse_vector.h"
3131#include " distcomp.h"
3232
33- #define SPACE_SPARSE_COSINE_SIMILARITY " cosinesimil_sparse"
34- #define SPACE_SPARSE_ANGULAR_DISTANCE " angulardist_sparse"
33+ #define SPACE_SPARSE_COSINE_SIMILARITY " cosinesimil_sparse"
34+ #define SPACE_SPARSE_ANGULAR_DISTANCE " angulardist_sparse"
35+ #define SPACE_SPARSE_NEGATIVE_SCALAR " negdotprod_sparse"
36+ #define SPACE_SPARSE_QUERY_NORM_NEGATIVE_SCALAR " querynorm_negdotprod_sparse"
3537
3638namespace similarity {
3739
@@ -54,7 +56,7 @@ class SpaceSparseAngularDistance : public SpaceSparseVectorSimpleStorage<dist_t>
5456 struct SpaceAngularDist {
5557 dist_t operator ()(const dist_t * x, const dist_t * y, size_t length) const {
5658 dist_t val = AngularDistance (x, y, length);
57- // TODO: @leo shouldn't happen any more , but let's keep this check here for a while
59+ // This shouldn't normally happen , but let's keep this check
5860 if (std::isnan (val)) throw runtime_error (" SpaceAngularDist Bug: NAN dist!!!!" );
5961 return val;
6062 }
@@ -82,7 +84,7 @@ class SpaceSparseCosineSimilarity : public SpaceSparseVectorSimpleStorage<dist_t
8284 struct SpaceCosineSimilarityDist {
8385 dist_t operator ()(const dist_t * x, const dist_t * y, size_t length) const {
8486 dist_t val = CosineSimilarity (x, y, length);
85- // TODO: @leo shouldn't happen any more , but let's keep this check here for a while
87+ // This shouldn't normally happen , but let's keep this check
8688 if (std::isnan (val)) throw runtime_error (" SpaceCosineSimilarityDist Bug: NAN dist!!!!" );
8789 return val;
8890 }
@@ -91,6 +93,59 @@ class SpaceSparseCosineSimilarity : public SpaceSparseVectorSimpleStorage<dist_t
9193 DISABLE_COPY_AND_ASSIGN (SpaceSparseCosineSimilarity);
9294};
9395
96+ template <typename dist_t >
97+ class SpaceSparseNegativeScalarProduct : public SpaceSparseVectorSimpleStorage <dist_t > {
98+ public:
99+ explicit SpaceSparseNegativeScalarProduct () {}
100+ virtual ~SpaceSparseNegativeScalarProduct () {}
101+
102+ virtual std::string StrDesc () const {
103+ return " NegativeScalarProduct" ;
104+ }
105+
106+ protected:
107+ virtual dist_t HiddenDistance (const Object* obj1, const Object* obj2) const {
108+ return SpaceSparseVectorSimpleStorage<dist_t >::
109+ ComputeDistanceHelper (obj1, obj2, distObjNegativeScalarProduct_);
110+ }
111+ private:
112+ struct SpaceNegativeScalarDist {
113+ dist_t operator ()(const dist_t * x, const dist_t * y, size_t length) const {
114+ return -ScalarProductSIMD (x, y, length);
115+ }
116+ };
117+ SpaceNegativeScalarDist distObjNegativeScalarProduct_;
118+ DISABLE_COPY_AND_ASSIGN (SpaceSparseNegativeScalarProduct);
119+ };
120+
121+ template <typename dist_t >
122+ class SpaceSparseQueryNormNegativeScalarProduct : public SpaceSparseVectorSimpleStorage <dist_t > {
123+ public:
124+ explicit SpaceSparseQueryNormNegativeScalarProduct () {}
125+ virtual ~SpaceSparseQueryNormNegativeScalarProduct () {}
126+
127+ virtual std::string StrDesc () const {
128+ return " QueryNormNegativeScalarProduct" ;
129+ }
130+
131+ protected:
132+ virtual dist_t HiddenDistance (const Object* obj1, const Object* obj2) const {
133+ return SpaceSparseVectorSimpleStorage<dist_t >::
134+ ComputeDistanceHelper (obj1, obj2, distObjQueryNormNegativeScalarProduct_);
135+ }
136+ private:
137+ struct SpaceNegativeQueryNormScalarDist {
138+ dist_t operator ()(const dist_t * x, const dist_t * y, size_t length) const {
139+ // This shouldn't normally happen, but let's keep this check
140+ dist_t val = QueryNormScalarProduct (x, y, length);
141+ if (std::isnan (val)) throw runtime_error (" SpaceNegativeQueryNormScalarDist Bug: NAN dist!!!!" );
142+ return -val;
143+ }
144+ };
145+ SpaceNegativeQueryNormScalarDist distObjQueryNormNegativeScalarProduct_;
146+ DISABLE_COPY_AND_ASSIGN (SpaceSparseQueryNormNegativeScalarProduct);
147+ };
148+
94149
95150} // namespace similarity
96151
0 commit comments