-
Notifications
You must be signed in to change notification settings - Fork 1
/
svec.c
77 lines (69 loc) · 1.61 KB
/
svec.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
#include <stdio.h>
#include <math.h>
#include "svec.h"
#include "rng.h"
void svec_print(svec x) {
for (int i = 0; i < x->size; i++) {
if (i > 0) printf("\t");
printf("%.6f", svec_get(x, i));
}
}
void svec_mul_print(svec x, double k) {
for (int i = 0; i < x->size; i++) {
if (i > 0) printf("\t");
printf("%.6f", k * svec_get(x, i));
}
}
float svec_sqdist(svec x, svec y) {
float sqdist = 0;
for (int i = x->size - 1; i >= 0; i--) {
float xi = svec_get(x, i);
float yi = svec_get(y, i);
float xy = xi - yi;
sqdist += xy * xy;
}
return sqdist;
}
void svec_randomize(svec x) {
for (int i = x->size - 1; i >= 0; i--) {
svec_set(x, i, -1 + 2 * gsl_rng_uniform(rng_R));
}
svec_normalize(x);
}
void svec_zero(svec x){
for (int i = x->size - 1; i >= 0; i--) {
svec_set(x, i, 0);
}
}
float svec_pull(svec x, svec y, float d) {
float sumsq = 0;
for (int i = x->size - 1; i >= 0; i--) {
float xi = svec_get(x, i);
float yi = svec_get(y, i);
float move = d * (yi - xi);
svec_set(x, i, xi + move);
sumsq += move * move;
}
svec_normalize(x);
return sumsq;
}
float svec_push(svec x, svec y, float d) {
float sumsq = 0;
for (int i = x->size - 1; i >= 0; i--) {
float xi = svec_get(x, i);
float yi = svec_get(y, i);
float move = d * (xi - yi);
svec_set(x, i, xi + move);
sumsq += move * move;
}
svec_normalize(x);
return sumsq;
}
void svec_normalize(svec x) {
float sumsq = 0;
for (int i = x->size - 1; i >= 0; i--) {
float xi = svec_get(x, i);
sumsq += xi * xi;
}
svec_scale(x, 1.0 / sqrt(sumsq));
}