-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.c
More file actions
54 lines (44 loc) · 746 Bytes
/
vector.c
File metadata and controls
54 lines (44 loc) · 746 Bytes
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
#include <math.h>
#include "vector.h"
const double v_zero[2] = {0, 0};
void v_copy(const double* x, double* y)
{
y[0] = x[0];
y[1] = x[1];
}
void v_axpy(double a, const double* x, double* y)
{
y[0] = a * x[0] + y[0];
y[1] = a * x[1] + y[1];
}
void v_scal(double a, double* x)
{
x[0] *= a;
x[1] *= a;
}
void v_mul(const double* x, double* y)
{
y[0] *= x[0];
y[1] *= x[1];
}
void v_div(const double* x, double* y)
{
y[0] /= x[0];
y[1] /= x[1];
}
double v_dot(const double* x, const double* y)
{
return (x[0] * y[0] + x[1] * y[1]);
}
double v_nrm2(const double* x)
{
return sqrt(v_dot(x, x));
}
double v_normalize(double* x)
{
double l;
l = v_nrm2(x);
l = EPSILON < l ? l : 0;
v_scal((0 < l ? 1.0/l : 0), x);
return l;
}