Skip to content

Commit

Permalink
small updates for Poincare and Lorentz
Browse files Browse the repository at this point in the history
  • Loading branch information
masajiro committed Feb 3, 2021
1 parent da33d2b commit ec54799
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 102 deletions.
4 changes: 2 additions & 2 deletions bin/ngt/README-jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ ANNGやBKNNGを指定した場合には登録データ(ノード)からエ
- __C__: 正規化コサイン類似度。指定されたデータを正規化した上で保存します。
- __h__: ハミング距離。データオブジェクトの型は1バイト整数を指定してください。
- __j__: ジャッカード距離。データオブジェクトの型は1バイト整数を指定してください。
- __p__: ポアンカレ距離
- __l__: ローレンツ距離
- __p__: ポアンカレ距離
- __l__: ローレンツ距離

**-n** *no\_of\_registration\_data*
登録するデータ数を指定します。指定しない場合には指定されたファイル中のすべてのデータを登録します。
Expand Down
4 changes: 2 additions & 2 deletions bin/ngt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ Specify the distance function as follows.
- __C__: Normalized cosine similarity. The specified data are automatically normalized to be appended to the index.
- __h__: Hamming distance. 1 byte unsigned integer should be specified for the data object type.
- __j__: Jaccard distance. 1 byte unsigned integer should be specified for the data object type.
- __p__: Poincare distance.
- __l__: Lorentz distance.
- __p__: Poincare distance
- __l__: Lorentz distance

**-n** *no\_of\_registration\_data*
Specify the number of data items to be registered. If not specified, all data in the specified file will be registered.
Expand Down
4 changes: 2 additions & 2 deletions lib/NGT/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ using namespace std;
case 'E':
property.distanceType = NGT::Index::Property::DistanceType::DistanceTypeNormalizedL2;
break;
case 'p': // added by Nyapicom
case 'p': // added by Nyapicom
property.distanceType = NGT::Index::Property::DistanceType::DistanceTypePoincare;
break;
case 'l': // added by Nyapicom
case 'l': // added by Nyapicom
property.distanceType = NGT::Index::Property::DistanceType::DistanceTypeLorentz;
break;
default:
Expand Down
18 changes: 15 additions & 3 deletions lib/NGT/Graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ NeighborhoodGraph::Search::sparseJaccardFloat(NeighborhoodGraph &graph, NGT::Sea
void
NeighborhoodGraph::Search::poincareFloat(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds)
{
graph.searchReadOnlyGraph<PrimitiveComparator::poincareFloat, DistanceCheckedSet>(sc, seeds);
graph.searchReadOnlyGraph<PrimitiveComparator::PoincareFloat, DistanceCheckedSet>(sc, seeds);
}

// added by Nyapicom
void
NeighborhoodGraph::Search::lorentzFloat(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds)
{
graph.searchReadOnlyGraph<PrimitiveComparator::lorentzFloat, DistanceCheckedSet>(sc, seeds);
graph.searchReadOnlyGraph<PrimitiveComparator::LorentzFloat, DistanceCheckedSet>(sc, seeds);
}

void
Expand Down Expand Up @@ -197,7 +197,19 @@ NeighborhoodGraph::Search::normalizedL2FloatForLargeDataset(NeighborhoodGraph &g
void
NeighborhoodGraph::Search::sparseJaccardFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds)
{
graph.searchReadOnlyGraph<PrimitiveComparator::SparseJaccardFloat, DistanceCheckedSet>(sc, seeds);
graph.searchReadOnlyGraph<PrimitiveComparator::SparseJaccardFloat, DistanceCheckedSetForLargeDataset>(sc, seeds);
}

void
NeighborhoodGraph::Search::poincareFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds)
{
graph.searchReadOnlyGraph<PrimitiveComparator::PoincareFloat, DistanceCheckedSetForLargeDataset>(sc, seeds);
}

void
NeighborhoodGraph::Search::lorentzFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds)
{
graph.searchReadOnlyGraph<PrimitiveComparator::LorentzFloat, DistanceCheckedSetForLargeDataset>(sc, seeds);
}

void
Expand Down
8 changes: 6 additions & 2 deletions lib/NGT/Graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ namespace NGT {
case NGT::ObjectSpace::DistanceTypeL2 : return l2Float;
case NGT::ObjectSpace::DistanceTypeL1 : return l1Float;
case NGT::ObjectSpace::DistanceTypeSparseJaccard : return sparseJaccardFloat;
case NGT::ObjectSpace::DistanceTypePoincare : return poincareFloat; // added by Nyapicom
case NGT::ObjectSpace::DistanceTypeLorentz : return lorentzFloat; // added by Nyapicom
case NGT::ObjectSpace::DistanceTypePoincare : return poincareFloat; // added by Nyapicom
case NGT::ObjectSpace::DistanceTypeLorentz : return lorentzFloat; // added by Nyapicom
default: return l2Float;
}
break;
Expand Down Expand Up @@ -330,6 +330,8 @@ namespace NGT {
case NGT::ObjectSpace::DistanceTypeL2 : return l2FloatForLargeDataset;
case NGT::ObjectSpace::DistanceTypeL1 : return l1FloatForLargeDataset;
case NGT::ObjectSpace::DistanceTypeSparseJaccard : return sparseJaccardFloatForLargeDataset;
case NGT::ObjectSpace::DistanceTypePoincare : return poincareFloatForLargeDataset;
case NGT::ObjectSpace::DistanceTypeLorentz : return lorentzFloatForLargeDataset;
default: return l2FloatForLargeDataset;
}
break;
Expand Down Expand Up @@ -373,6 +375,8 @@ namespace NGT {
static void normalizedCosineSimilarityFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds);
static void normalizedAngleFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds);
static void normalizedL2FloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds);
static void poincareFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds);
static void lorentzFloatForLargeDataset(NeighborhoodGraph &graph, NGT::SearchContainer &sc, ObjectDistances &seeds);
};
#endif

Expand Down
4 changes: 2 additions & 2 deletions lib/NGT/Index.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ namespace NGT {
case DistanceType::DistanceTypeNormalizedAngle: p.set("DistanceType", "NormalizedAngle"); break;
case DistanceType::DistanceTypeNormalizedCosine: p.set("DistanceType", "NormalizedCosine"); break;
case DistanceType::DistanceTypeNormalizedL2: p.set("DistanceType", "NormalizedL2"); break;
case DistanceType::DistanceTypePoincare: p.set("DistanceType", "Poincare"); break; // added by Nyapicom
case DistanceType::DistanceTypeLorentz: p.set("DistanceType", "Lorentz"); break; // added by Nyapicom
case DistanceType::DistanceTypePoincare: p.set("DistanceType", "Poincare"); break; // added by Nyapicom
case DistanceType::DistanceTypeLorentz: p.set("DistanceType", "Lorentz"); break; // added by Nyapicom
default : std::cerr << "Fatal error. Invalid distance type. " << distanceType << std::endl; abort();
}
switch (indexType) {
Expand Down
4 changes: 2 additions & 2 deletions lib/NGT/ObjectSpace.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ namespace NGT {
DistanceTypeJaccard = 7,
DistanceTypeSparseJaccard = 8,
DistanceTypeNormalizedL2 = 9,
DistanceTypePoincare = 100, // added by Nyapicom
DistanceTypeLorentz = 101 // added by Nyapicom
DistanceTypePoincare = 100, // added by Nyapicom
DistanceTypeLorentz = 101 // added by Nyapicom
};

enum ObjectType {
Expand Down
98 changes: 49 additions & 49 deletions lib/NGT/ObjectSpaceRepository.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,47 +240,47 @@ namespace NGT {
#endif
};

class ComparatorPoincareDistance : public Comparator { // added by Nyapicom
public:
#ifdef NGT_SHARED_MEMORY_ALLOCATOR
ComparatorPoincareDistance(size_t d, SharedMemoryAllocator &a) : Comparator(d, a) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
double operator()(Object &objecta, PersistentObject &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
double operator()(PersistentObject &objecta, PersistentObject &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta.at(0, allocator), (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
#else
ComparatorPoincareDistance(size_t d) : Comparator(d) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
#endif
};

class ComparatorLorentzDistance : public Comparator { // added by Nyapicom
public:
#ifdef NGT_SHARED_MEMORY_ALLOCATOR
ComparatorLorentzDistance(size_t d, SharedMemoryAllocator &a) : Comparator(d, a) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
double operator()(Object &objecta, PersistentObject &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
double operator()(PersistentObject &objecta, PersistentObject &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta.at(0, allocator), (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
#else
ComparatorLorentzDistance(size_t d) : Comparator(d) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
#endif
};
class ComparatorPoincareDistance : public Comparator { // added by Nyapicom
public:
#ifdef NGT_SHARED_MEMORY_ALLOCATOR
ComparatorPoincareDistance(size_t d, SharedMemoryAllocator &a) : Comparator(d, a) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
double operator()(Object &objecta, PersistentObject &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
double operator()(PersistentObject &objecta, PersistentObject &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta.at(0, allocator), (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
#else
ComparatorPoincareDistance(size_t d) : Comparator(d) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::comparePoincareDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
#endif
};

class ComparatorLorentzDistance : public Comparator { // added by Nyapicom
public:
#ifdef NGT_SHARED_MEMORY_ALLOCATOR
ComparatorLorentzDistance(size_t d, SharedMemoryAllocator &a) : Comparator(d, a) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
double operator()(Object &objecta, PersistentObject &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
double operator()(PersistentObject &objecta, PersistentObject &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta.at(0, allocator), (OBJECT_TYPE*)&objectb.at(0, allocator), dimension);
}
#else
ComparatorLorentzDistance(size_t d) : Comparator(d) {}
double operator()(Object &objecta, Object &objectb) {
return PrimitiveComparator::compareLorentzDistance((OBJECT_TYPE*)&objecta[0], (OBJECT_TYPE*)&objectb[0], dimension);
}
#endif
};

ObjectSpaceRepository(size_t d, const std::type_info &ot, DistanceType t) : ObjectSpace(d), ObjectRepository(d, ot) {
size_t objectSize = 0;
Expand Down Expand Up @@ -377,11 +377,11 @@ namespace NGT {
case DistanceTypeCosine:
comparator = new ObjectSpaceRepository::ComparatorCosineSimilarity(ObjectSpace::getPaddedDimension(), ObjectRepository::allocator);
break;
case DistanceTypePoincare: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorPoincareDistance(ObjectSpace::getPaddedDimension(), ObjectRepository::allocator);
case DistanceTypePoincare: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorPoincareDistance(ObjectSpace::getPaddedDimension(), ObjectRepository::allocator);
break;
case DistanceTypeLorentz: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorLorentzDistance(ObjectSpace::getPaddedDimension(), ObjectRepository::allocator);
case DistanceTypeLorentz: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorLorentzDistance(ObjectSpace::getPaddedDimension(), ObjectRepository::allocator);
break;
case DistanceTypeNormalizedAngle:
comparator = new ObjectSpaceRepository::ComparatorNormalizedAngleDistance(ObjectSpace::getPaddedDimension(), ObjectRepository::allocator);
Expand Down Expand Up @@ -418,11 +418,11 @@ namespace NGT {
case DistanceTypeCosine:
comparator = new ObjectSpaceRepository::ComparatorCosineSimilarity(ObjectSpace::getPaddedDimension());
break;
case DistanceTypePoincare: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorPoincareDistance(ObjectSpace::getPaddedDimension());
case DistanceTypePoincare: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorPoincareDistance(ObjectSpace::getPaddedDimension());
break;
case DistanceTypeLorentz: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorLorentzDistance(ObjectSpace::getPaddedDimension());
case DistanceTypeLorentz: // added by Nyapicom
comparator = new ObjectSpaceRepository::ComparatorLorentzDistance(ObjectSpace::getPaddedDimension());
break;
case DistanceTypeNormalizedAngle:
comparator = new ObjectSpaceRepository::ComparatorNormalizedAngleDistance(ObjectSpace::getPaddedDimension());
Expand Down
76 changes: 38 additions & 38 deletions lib/NGT/PrimitiveComparator.h
Original file line number Diff line number Diff line change
Expand Up @@ -591,47 +591,47 @@ namespace NGT {
}
}

// added by Nyapicom
inline static double comparePoincareDistance(const unsigned char *a, const unsigned char *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double a2 = 0.0;
double b2 = 0.0;
double c2 = compareL2(a, b, size);
for(size_t i=0; i < size; i++){
a2 += (double)a[i] * (double)a[i];
b2 += (double)b[i] * (double)b[i];
}
// added by Nyapicom
inline static double comparePoincareDistance(const unsigned char *a, const unsigned char *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double a2 = 0.0;
double b2 = 0.0;
double c2 = compareL2(a, b, size);
for(size_t i=0; i < size; i++){
a2 += (double)a[i] * (double)a[i];
b2 += (double)b[i] * (double)b[i];
}
return std::acosh(1 + 2.0 * c2*c2 / (1.0 - a2) / (1.0 - b2));
}
// added by Nyapicom
inline static double comparePoincareDistance(const float *a, const float *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double a2 = 0.0;
double b2 = 0.0;
double c2 = compareL2(a, b, size);
for(size_t i=0; i < size; i++){
a2 += (double)a[i] * (double)a[i];
b2 += (double)b[i] * (double)b[i];
}
// added by Nyapicom
inline static double comparePoincareDistance(const float *a, const float *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double a2 = 0.0;
double b2 = 0.0;
double c2 = compareL2(a, b, size);
for(size_t i=0; i < size; i++){
a2 += (double)a[i] * (double)a[i];
b2 += (double)b[i] * (double)b[i];
}
return std::acosh(1 + 2.0 * c2*c2 / (1.0 - a2) / (1.0 - b2));
}

// added by Nyapicom
inline static double compareLorentzDistance(const unsigned char *a, const unsigned char *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double sum = (double)a[0] * (double)b[0];
for(size_t i=1; i < size; i++){
sum -= (double)a[i] * (double)b[i];
}
// added by Nyapicom
inline static double compareLorentzDistance(const unsigned char *a, const unsigned char *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double sum = (double)a[0] * (double)b[0];
for(size_t i=1; i < size; i++){
sum -= (double)a[i] * (double)b[i];
}
return std::acosh(sum);
}
// added by Nyapicom
inline static double compareLorentzDistance(const float *a, const float *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double sum = (double)a[0] * (double)b[0];
for(size_t i=1; i < size; i++){
sum -= (double)a[i] * (double)b[i];
}
// added by Nyapicom
inline static double compareLorentzDistance(const float *a, const float *b, size_t size) {
// Unlike the other distance functions, this is not optimized...
double sum = (double)a[0] * (double)b[0];
for(size_t i=1; i < size; i++){
sum -= (double)a[i] * (double)b[i];
}
return std::acosh(sum);
}

Expand Down Expand Up @@ -734,16 +734,16 @@ namespace NGT {
}
};

// added by Nyapicom
class poincareFloat {
// added by Nyapicom
class PoincareFloat {
public:
inline static double compare(const void *a, const void *b, size_t size) {
return PrimitiveComparator::comparePoincareDistance((const float*)a, (const float*)b, size);
}
};

// added by Nyapicom
class lorentzFloat {
// added by Nyapicom
class LorentzFloat {
public:
inline static double compare(const void *a, const void *b, size_t size) {
return PrimitiveComparator::compareLorentzDistance((const float*)a, (const float*)b, size);
Expand Down

0 comments on commit ec54799

Please sign in to comment.