vocabtree  0.0.1
numerics Namespace Reference

Provides useful wrappers around many numerical functionality, such as dealing with sparse and dense matrix / vector data. More...

Typedefs

typedef std::vector< std::pair
< uint32_t, float > > 
sparse_vector_t
 

Functions

std::vector< std::pair
< uint32_t, float > > 
sparsify (const cv::Mat &dense)
 Converts the input 1D cv::Mat to a sparse format, where each pair in the vector is index, value. More...
 
float cos_sim (const std::vector< std::pair< uint32_t, float > > &weights0, const std::vector< std::pair< uint32_t, float > > &weights1, const std::vector< float > &idfw)
 Converts the cosine similarity between two sparse weight vectors, which are premultiplied by the relevant entries in idfw. More...
 
float min_hist (const std::vector< std::pair< uint32_t, float > > &weights0, const std::vector< std::pair< uint32_t, float > > &weights1, const std::vector< float > &idfw)
 Converts the histogram intersection (min) between two sparse weight vectors, which are premultiplied by the relevant entries in idfw. More...
 

Detailed Description

Provides useful wrappers around many numerical functionality, such as dealing with sparse and dense matrix / vector data.

Typedef Documentation

typedef std::vector< std::pair<uint32_t, float > > numerics::sparse_vector_t

Definition at line 10 of file numerics.hpp.

Function Documentation

float numerics::cos_sim ( const std::vector< std::pair< uint32_t, float > > &  weights0,
const std::vector< std::pair< uint32_t, float > > &  weights1,
const std::vector< float > &  idfw 
)

Converts the cosine similarity between two sparse weight vectors, which are premultiplied by the relevant entries in idfw.

Sample usage would be weights0 and weights1 to represent two BoW vectors, and idfw to represent a vector of inverse document frequencies.

Definition at line 13 of file numerics.cxx.

15  {
16  float ab = 0.f, a2 = 0.f, b2 = 0.f;
17  for(size_t i=0, j=0; i < weights0.size() || j < weights1.size();) {
18  if(i < weights0.size() && j < weights1.size()) {
19  if (weights0[i].first == weights1[j].first) {
20  float a = weights0[i].second * idfw[weights0[i].first];
21  float b = weights1[j].second * idfw[weights1[j].first];
22  ab += a*b;
23  a2 += a*a;
24  b2 += b*b;
25  i++, j++;
26  } else if (weights0[i].first < weights1[j].first) {
27  float a = weights0[i].second * idfw[weights0[i].first];
28  a2 += a*a;
29  i++;
30  } else {
31  float b = weights1[j].second * idfw[weights1[j].first];
32  b2 += b*b;
33  j++;
34  }
35  } else if(i < weights0.size()) {
36  float a = weights0[i].second * idfw[weights0[i].first];
37  a2 += a*a;
38  i++;
39  } else {
40  float b = weights1[j].second * idfw[weights1[j].first];
41  b2 += b*b;
42  j++;
43  }
44  }
45  return ab / (sqrtf(a2)*sqrtf(b2));
46  }
float numerics::min_hist ( const std::vector< std::pair< uint32_t, float > > &  weights0,
const std::vector< std::pair< uint32_t, float > > &  weights1,
const std::vector< float > &  idfw 
)

Converts the histogram intersection (min) between two sparse weight vectors, which are premultiplied by the relevant entries in idfw.

Sample usage would be weights0 and weights1 to represent two BoW vectors, and idfw to represent a vector of inverse document frequencies.

Definition at line 48 of file numerics.cxx.

Referenced by InvertedIndex::search().

50  {
51  float a = 0.f, b = 0.f, ab = 0.f;
52  for(int k=0; k<weights0.size(); k++) {
53  a += weights0[k].second*idfw[weights0[k].first];
54  }
55  for(int k=0; k<weights1.size(); k++) {
56  b += weights1[k].second*idfw[weights1[k].first];
57  }
58  for(size_t i=0, j=0; i < weights0.size() && j < weights1.size();) {
59  if(i < weights0.size() && j < weights1.size()) {
60  if (weights0[i].first == weights1[j].first) {
61  float min_val = weights0[i].second / a;
62  if(min_val > (weights1[j].second / b)) {
63  min_val = weights1[j].second / b;
64  }
65  ab += min_val * idfw[weights0[i].first];
66  i++, j++;
67  } else if (weights0[i].first < weights1[j].first) {
68  i++;
69  } else {
70  j++;
71  }
72  }
73  }
74  return ab;
75  }
std::vector< std::pair< uint32_t, float > > numerics::sparsify ( const cv::Mat &  dense)

Converts the input 1D cv::Mat to a sparse format, where each pair in the vector is index, value.

This is useful for BoW features which are usually zero.

Definition at line 5 of file numerics.cxx.

Referenced by compute_bow(), compute_bow_features(), and main().

5  {
6  std::vector< std::pair<uint32_t, float> > sparse;
7  for(int i=0; i<dense.size().area(); i++) {
8  if( dense.at<float>(i) != 0.f) sparse.push_back( std::pair<uint32_t, float>(i, dense.at<float>(i)) );
9  }
10  return sparse;
11  }