forked from rackingroll/mcmc_lsh
-
Notifications
You must be signed in to change notification settings - Fork 1
/
L2LSH.cpp
120 lines (99 loc) · 2.52 KB
/
L2LSH.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "L2LSH.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <time.h>
#include <math.h>
#define PI 3.14159265
//#pragma once
using namespace std;
L2LSH::L2LSH()
{
}
void L2LSH::Initialize(int dimention,int numOfHashes)
{
_dim = dimention;
_numhashes = numOfHashes;
std::random_device rd;
std::mt19937 gen(rd());
// values near the mean are the most likely
// standard deviation affects the dispersion of generated values from the mean
std::normal_distribution<> d(0,1);
_rand_vec = new double*[_numhashes];
for (size_t i = 0; i < _numhashes; i++ )
{
_rand_vec[i] = new double[_dim];
double sum = 0.0;
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] = d(gen);
sum += _rand_vec[i][j];
}
// Normalize
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] /= sum;
//printf("%f \n", _rand_vec[i][j]);
}
}
}
L2LSH::L2LSH(int dimention,int numOfHashes)
{
_dim = dimention;
_numhashes = numOfHashes;
std::random_device rd;
std::mt19937 gen(rd());
// values near the mean are the most likely
// standard deviation affects the dispersion of generated values from the mean
std::normal_distribution<> d(0,1);
_rand_vec = new double*[_numhashes];
for (size_t i = 0; i < _numhashes; i++ )
{
_rand_vec[i] = new double[_dim];
double sum = 0.0;
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] = d(gen);
sum += _rand_vec[i][j];
}
// Normalize
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] /= sum;
}
}
}
/*
* Calculate the collision probability under sign random projection
*/
double L2LSH::getProb(double * q, double * vector, int length)
{
return 0.0;
}
int * L2LSH::getHash(double * vector, int length)
{
int * hashes = new int[_numhashes];
for (size_t i = 0; i < _numhashes; i++)
{
double inner_product = 0.0;
hashes[i] = 0;
double _b = (double) (rand() % int(_w));
for (size_t j=0;j<length;j++)
{
inner_product += vector[j]* _rand_vec[i][j];
}
//printf("%f ", inner_product);
hashes[i] = floor((inner_product+_b) / _w);
printf ("%d ", hashes[i]);
}
printf ("\n");
return hashes;
}
L2LSH::~L2LSH()
{
for (size_t i = 0; i < _numhashes; i++)
{
delete[] _rand_vec[i];
}
delete[] _rand_vec;
}