-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojection.c
87 lines (66 loc) · 1.56 KB
/
projection.c
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
#include "projection.h"
#include "standard_normal.h"
#include <math.h>
#include <stdlib.h>
static int initialized = 0;
projection_t*
init_random_projection(size_t dim, unsigned int seed, unsigned int bin_width)
{
normal_generator_t gen = init_normal_distribution(seed);
size_t i;
projection_t* proj = malloc(sizeof(projection_t));
if (!proj)
return NULL;
if (!initialized)
{
srand(seed);
initialized = 1;
}
proj->dimension = dim;
proj->vector = malloc(sizeof(double) * dim);
proj->bin_width = bin_width;
proj->bias = rand() % (bin_width + 1);
if (!proj->vector)
return proj;
for (i = 0; i < dim; i++)
proj->vector[i] = next_gaussian(&gen);
return proj;
}
projection_t*
init_random_projection_rng(size_t dim, unsigned int seed,
unsigned int bin_width, normal_generator_t* gen)
{
size_t i;
projection_t* proj = malloc(sizeof(projection_t));
if (!proj)
return NULL;
if (!initialized)
{
srand(seed);
initialized = 1;
}
proj->dimension = dim;
proj->vector = malloc(sizeof(double) * dim);
proj->bin_width = bin_width;
proj->bias = rand() % (bin_width + 1);
if (!proj->vector)
return proj;
for (i = 0; i < dim; i++)
proj->vector[i] = next_gaussian(gen);
return proj;
}
int
project_data(projection_t* proj, double* data)
{
size_t i;
double dot_product_r = 0;
for (i = 0; i < proj->dimension; i++)
dot_product_r += proj->vector[i] * data[i];
return (int) floor((dot_product_r + proj->bias) / (double) proj->bin_width);
}
void
free_projection(projection_t* proj)
{
free(proj->vector);
free(proj);
}