/
vectors.c
90 lines (75 loc) · 1.64 KB
/
vectors.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
87
88
89
90
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
unsigned int dimensions; //const - this is for the benifit of the header generator
short set_dimensions(unsigned int i) {
static int runbefore;
if (!runbefore) {
dimensions = i;
runbefore = 1;
}
else {
printf("Shan't! (you tried to set the dimensions twice, bad)\n");
dimensions = 0;
}
return dimensions;
}
typedef float* vector;
inline vector Vmk(void) {
return calloc(dimensions, sizeof(float));
}
vector Vheap(const vector const a) {
vector v = Vmk();
unsigned short d;
for (d=0; d < dimensions; d++) {
v[d] = a[d];
}
return v;
}
void Vzero(vector const a) {
unsigned short d;
for (d = 0; d < dimensions; d++) {
a[d] = 0.0;
}
}
void Vadd(vector const a, const vector const b, const vector const c) {
unsigned short d;
for (d = 0; d < dimensions; d++)
a[d] = b[d] + c[d];
}
void Vsub(vector const a, const vector const b, const vector const c) {
unsigned short d;
for (d = 0; d < dimensions; d++)
a[d] = b[d] - c[d];
}
void Vtimes_scalar(vector const a, const vector const b, const float c) {
unsigned short d;
for (d = 0; d < dimensions; d++) {
a[d] = b[d]*c;
}
}
float Vdot(const vector const a, const vector const b) {
float sum = 0.0;
unsigned short d;
for (d = 0; d < dimensions; d++) {
sum += a[d]*b[d];
}
return sum;
}
float Vmodulus(const vector const a) {
unsigned short d;
float sum = 0.0;
for (d = 0; d < dimensions; d++) {
sum += pow(a[d], 2.0);
}
return sqrt(sum);
}
/* Useful for testing components
void Vprint(const vector const a) {
unsigned short d;
for (d = 0; d < dimensions; d++) {
printf("%f ", a[d]);
}
printf("\n");
}
*/