-
Notifications
You must be signed in to change notification settings - Fork 201
/
v.h
106 lines (84 loc) · 3.13 KB
/
v.h
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
#pragma once
#include <vector>
typedef std::vector<float> Vector;
namespace v {
struct LightVector: public std::pair<float *, float *>
{
using Parent = std::pair<float *, float *>;
template <typename... Arg> LightVector(Arg&& ... arg): Parent(std::forward<Arg>(arg) ...) {}
float *data() const { return first; }
size_t size() const { return std::distance(first, second); }
bool empty() const { return first == second; }
float& operator[](size_t i) { return *(first + i); }
float operator[](size_t i) const { return *(first + i); }
};
template <class Vector1, class Vector2> inline float dot(const Vector1&x, const Vector2& y) {
int m = x.size(); const float *xd = x.data(), *yd = y.data();
float sum = 0.0;
while (--m >= 0) sum += (*xd++) * (*yd++);
return sum;
}
// saxpy: x = x + g * y; x = a * x + g * y
inline void saxpy(Vector& x, float g, const Vector& y) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data();
while (--m >= 0) (*xd++) += g * (*yd++);
}
inline void saxpy(float a, Vector& x, float g, const Vector& y) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data();
while (--m >= 0) { (*xd) = a * (*xd) + g * (*yd); ++xd; ++yd; }
}
inline void saxpy2(Vector& x, float g, const Vector& y, float h, const Vector& z) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data(); const float *zd = z.data();
while (--m >= 0) { (*xd++) += (g * (*yd++) + h * (*zd++)); }
}
inline void scale(Vector& x, float g) {
int m = x.size(); float *xd = x.data();
while (--m >= 0) (*xd++) *= g;
}
#if 0
inline void addsub(Vector& x, const Vector& y, const Vector& z) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data(); const float *zd = z.data();
while (--m >= 0) (*xd++) += ((*yd++) - (*zd++));
}
#endif
inline void unit(Vector& x) {
float len = ::sqrt(dot(x, x));
if (len == 0) return;
int m = x.size(); float *xd = x.data();
while (--m >= 0) (*xd++) /= len;
}
inline void add(Vector& x, const Vector& y) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data();
while (--m >= 0) (*xd++) += (*yd++);
}
inline void sax2(Vector& x, const Vector& y) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data();
while (--m >= 0) { (*xd++) += (*yd) * (*yd); yd++; }
}
inline void multiply(Vector& x, const Vector& y) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data();
while (--m >= 0) { (*xd++) *= (*yd++); }
}
inline bool isfinite(const Vector& x) {
for(auto const& i: x) { if (! std::isfinite(i)) return false; }
return true;
}
#if 0
inline float dot(const Vector&x, const Vector& y) {
int m = x.size(); const float *xd = x.data(), *yd = y.data();
float sum = 0.0;
while (--m >= 0) sum += (*xd++) * (*yd++);
return sum;
}
inline void saxpy(Vector& x, float g, const Vector& y) {
int m = x.size(); float *xd = x.data(); const float *yd = y.data();
while (--m >= 0) (*xd++) += g * (*yd++);
}
inline void unit(Vector& x) {
float len = ::sqrt(dot(x, x));
if (len == 0) return;
int m = x.size(); float *xd = x.data();
while (--m >= 0) (*xd++) /= len;
}
#endif
}