-
Notifications
You must be signed in to change notification settings - Fork 5
/
tiny_vector_x.h
219 lines (190 loc) · 6.24 KB
/
tiny_vector_x.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TINY_VECTOR_X_H
#define TINY_VECTOR_X_H
#include <stdio.h>
#include <vector>
/**
* Represents a vector with arbitrary number of dimensions.
*/
template <typename TinyScalar, typename TinyConstants>
class TinyVectorX {
TinyScalar* m_data{nullptr};
public:
int m_size{0};
TinyVectorX() = default;
explicit TinyVectorX(int size) : m_size(size) {
m_data = new TinyScalar[m_size];
set_zero();
}
TinyVectorX(int size, TinyScalar* data) : m_size(size), m_data(data) {}
TinyVectorX(const TinyVectorX& v) : m_size(v.m_size) {
m_data = new TinyScalar[m_size];
for (int i = 0; i < m_size; ++i) m_data[i] = v.m_data[i];
}
TinyVectorX(const std::vector<TinyScalar>& v) : m_size(v.size()) {
m_data = new TinyScalar[m_size];
for (int i = 0; i < m_size; ++i) {
m_data[i] = v.at(i);
}
}
inline TinyScalar* data() { return m_data; }
inline const TinyScalar* data() const { return m_data; }
TinyVectorX& operator=(const TinyVectorX& v) {
delete[] m_data;
m_size = v.m_size;
m_data = new TinyScalar[m_size];
for (int i = 0; i < m_size; ++i) m_data[i] = v.m_data[i];
return *this;
}
virtual ~TinyVectorX() { delete[] m_data; }
void set_zero() {
for (int i = 0; i < m_size; ++i) m_data[i] = TinyConstants::zero();
}
void resize(int n) {
if (m_size != n) {
m_size = n;
delete[] m_data;
m_data = new TinyScalar[m_size];
}
set_zero();
}
std::vector<TinyScalar> std() const {
std::vector<TinyScalar> v(m_data, m_data + m_size);
return v;
}
template <template <typename, typename> typename VectorType>
inline TinyScalar dot(
const VectorType<TinyScalar, TinyConstants>& other) const {
assert(m_size == other.m_size);
TinyScalar res = TinyConstants::zero();
for (int i = 0; i < m_size; ++i) res = res + m_data[i] * other[i];
return res;
}
inline TinyScalar sqnorm() const {
TinyScalar res = TinyConstants::zero();
for (int i = 0; i < m_size; ++i) res += m_data[i] * m_data[i];
return res;
}
inline TinyScalar length() const {
TinyScalar res = sqnorm();
res = TinyConstants::sqrt1(res);
return res;
}
inline int size() const {
return m_size;
}
template <template <typename, typename> typename VectorType>
inline TinyVectorX& operator+=(
const VectorType<TinyScalar, TinyConstants>& v) {
assert(m_size == v.m_size);
for (int i = 0; i < m_size; ++i) m_data[i] = m_data[i] + v[i];
return *this;
}
template <template <typename, typename> typename VectorType>
inline TinyVectorX& operator-=(
const VectorType<TinyScalar, TinyConstants>& v) {
assert(m_size == v.m_size);
for (int i = 0; i < m_size; ++i) m_data[i] -= v[i];
return *this;
}
inline TinyVectorX operator-() const {
TinyVectorX v(m_size);
for (int i = 0; i < m_size; ++i) v.m_data[i] = -m_data[i];
return v;
}
inline TinyScalar& operator[](int i) {
TinyConstants::FullAssert(0 <= i && i < m_size);
return m_data[i];
}
inline const TinyScalar& operator[](int i) const {
TinyConstants::FullAssert(0 <= i && i < m_size);
return m_data[i];
}
inline friend TinyVectorX operator+(const TinyVectorX& a, TinyScalar s) {
TinyVectorX v(a.m_size);
for (int i = 0; i < a.m_size; ++i) v.m_data[i] = a.m_data[i] + s;
return v;
}
inline friend TinyVectorX operator*(const TinyVectorX& a, TinyScalar s) {
TinyVectorX v(a.m_size);
for (int i = 0; i < a.m_size; ++i) v.m_data[i] = a.m_data[i] * s;
return v;
}
inline friend TinyVectorX operator/(const TinyVectorX& a, TinyScalar s) {
TinyVectorX v(a.m_size);
for (int i = 0; i < a.m_size; ++i) v.m_data[i] = a.m_data[i] / s;
return v;
}
inline friend TinyVectorX operator+(TinyScalar s, const TinyVectorX& a) {
return a + s;
}
inline friend TinyVectorX operator*(TinyScalar s, const TinyVectorX& a) {
return a * s;
}
inline friend TinyVectorX operator/(TinyScalar s, const TinyVectorX& a) {
return a / s;
}
template <template <typename, typename> typename VectorType>
inline friend TinyVectorX operator+(
const TinyVectorX& a, const VectorType<TinyScalar, TinyConstants>& b) {
assert(a.m_size == b.m_size);
TinyVectorX v(a.m_size);
for (int i = 0; i < a.m_size; ++i) v.m_data[i] = a.m_data[i] + b[i];
return v;
}
template <template <typename, typename> typename VectorType>
inline friend TinyVectorX operator-(
const TinyVectorX& a, const VectorType<TinyScalar, TinyConstants>& b) {
assert(a.m_size == b.m_size);
TinyVectorX v(a.m_size);
for (int i = 0; i < a.m_size; ++i) v.m_data[i] = a.m_data[i] - b[i];
return v;
}
template <template <typename, typename> typename VectorType>
inline friend TinyVectorX operator*(
const TinyVectorX& a, const VectorType<TinyScalar, TinyConstants>& b) {
assert(a.m_size == b.m_size);
TinyVectorX v(a.m_size);
for (int i = 0; i < a.m_size; ++i) v.m_data[i] = a.m_data[i] * b[i];
return v;
}
void print(const char* txt) const {
printf("%s\n", txt);
for (int i = 0; i < m_size; ++i) {
double v = TinyConstants::getDouble(m_data[i]);
printf("%f, ", v);
}
printf("\n");
}
TinyVectorX segment(int start, int length) const {
assert(start >= 0);
assert(start + length <= m_size);
TinyVectorX v(length);
for (int i = 0; i < length; ++i) {
v[i] = m_data[i + start];
}
return v;
}
void assign_vector_add(int start, const TinyVectorX& v) {
TinyConstants::FullAssert(0 <= start);
TinyConstants::FullAssert(start + v.m_size <= m_size);
for (int j = 0; j < v.m_size; ++j) {
m_data[start + j] += v[j];
}
}
};
#endif // TINY_VECTOR_X_H