vector.h
6.99 KB
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#ifndef RTS_VECTOR_H
#define RTS_VECTOR_H
#include <iostream>
#include <cmath>
#include <sstream>
#include <vector>
#include "../cuda/callable.h"
namespace stim
{
template <class T>
struct vec : public std::vector<T>
{
using std::vector<T>::size;
using std::vector<T>::at;
using std::vector<T>::resize;
using std::vector<T>::push_back;
vec(){
}
/// Create a vector with a set dimension d
vec(int d)
{
resize(d,0);
}
// //efficiency constructors, makes construction easier for 1D-4D vectors
vec(T x, T y)
{
resize(2, 0);
at(0) = x;
at(1) = y;
}
vec(T x, T y, T z)
{
resize(3, 0);
at(0) = x;
at(1) = y;
at(2) = z;
}
vec(T x, T y, T z, T w)
{
resize(4, 0);
at(0) = x;
at(1) = y;
at(2) = z;
at(3) = w;
}
//copy constructor
vec( const vec<T>& other){
unsigned int N = other.size();
for(unsigned int i=0; i<N; i++)
push_back(other[i]);
}
//I'm not sure what these were doing here.
//Keep them now, we'll worry about it later.
vec<T> push(T x)
{
push_back(x);
return *this;
}
vec<T> push(T x, T y)
{
push_back(x);
push_back(y);
return *this;
}
vec<T> push(T x, T y, T z)
{
push_back(x);
push_back(y);
push_back(z);
return *this;
}
vec<T> push(T x, T y, T z, T w)
{
push_back(x);
push_back(y);
push_back(z);
push_back(w);
return *this;
}
/// Casting operator. Creates a new vector with a new type U.
template< typename U >
operator vec<U>(){
unsigned int N = size();
vec<U> result;
for(int i=0; i<N; i++)
result.push_back(at(i));
return result;
}
/// computes the Euclidean length of the vector
T len() const
{
unsigned int N = size();
//compute and return the vector length
T sum_sq = (T)0;
for(unsigned int i=0; i<N; i++)
{
sum_sq += pow( at(i), 2 );
}
return sqrt(sum_sq);
}
/// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [0, 2*pi])
vec<T> cart2sph() const
{
vec<T> sph;
sph.push_back(std::sqrt(at(0)*at(0) + at(1)*at(1) + at(2)*at(2)));
sph.push_back(std::atan2(at(1), at(0)));
if(sph[0] == 0)
sph.push_back(0);
else
sph.push_back(std::acos(at(2) / sph[0]));
return sph;
}
/// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi])
vec<T> sph2cart() const
{
vec<T> cart;
cart.push_back(at(0) * std::cos(at(1)) * std::sin(at(2)));
cart.push_back(at(0) * std::sin(at(1)) * std::sin(at(2)));
cart.push_back(at(0) * std::cos(at(2)));
return cart;
}
/// Computes the normalized vector (where each coordinate is divided by the L2 norm)
vec<T> norm() const
{
unsigned int N = size();
//compute and return the unit vector
vec<T> result;
//compute the vector length
T l = len();
//normalize
for(int i=0; i<N; i++)
{
result.push_back(at(i) / l);
}
return result;
}
/// Computes the cross product of a 3-dimensional vector
vec<T> cross(const vec<T> rhs) const
{
vec<T> result(3);
//compute the cross product (only valid for 3D vectors)
result[0] = (at(1) * rhs[2] - at(2) * rhs[1]);
result[1] = (at(2) * rhs[0] - at(0) * rhs[2]);
result[2] = (at(0) * rhs[1] - at(1) * rhs[0]);
return result;
}
/// Compute the Euclidean inner (dot) product
T dot(vec<T> rhs) const
{
T result = (T)0;
unsigned int N = size();
for(int i=0; i<N; i++)
result += at(i) * rhs[i];
return result;
}
/// Arithmetic addition operator
/// @param rhs is the right-hand-side operator for the addition
vec<T> operator+(vec<T> rhs) const
{
unsigned int N = size();
vec<T> result(N);
for(int i=0; i<N; i++)
result[i] = at(i) + rhs[i];
return result;
}
/// Arithmetic addition to a scalar
/// @param rhs is the right-hand-side operator for the addition
vec<T> operator+(T rhs) const
{
unsigned int N = size();
vec<T> result(N);
for(int i=0; i<N; i++)
result[i] = at(i) + rhs;
return result;
}
/// Arithmetic subtraction operator
/// @param rhs is the right-hand-side operator for the subtraction
vec<T> operator-(vec<T> rhs) const
{
unsigned int N = size();
vec<T> result(N);
for(unsigned int i=0; i<N; i++)
result[i] = at(i) - rhs[i];
return result;
}
/// Arithmetic subtraction to a scalar
/// @param rhs is the right-hand-side operator for the addition
vec<T> operator-(T rhs) const
{
unsigned int N = size();
vec<T> result(N);
for(int i=0; i<N; i++)
result[i] = at(i) - rhs;
return result;
}
/// Arithmetic scalar multiplication operator
/// @param rhs is the right-hand-side operator for the subtraction
vec<T> operator*(T rhs) const
{
unsigned int N = size();
vec<T> result(N);
for(int i=0; i<N; i++)
result[i] = at(i) * rhs;
return result;
}
/// Arithmetic scalar division operator
/// @param rhs is the right-hand-side operator for the subtraction
vec<T> operator/(T rhs) const
{
unsigned int N = size();
vec<T> result(N);
for(int i=0; i<N; i++)
result[i] = at(i) / rhs;
return result;
}
/// Multiplication by a scalar, followed by assignment
vec<T> operator*=(T rhs){
unsigned int N = size();
for(int i=0; i<N; i++)
at(i) = at(i) * rhs;
return *this;
}
/// Addition and assignment
vec<T> operator+=(vec<T> rhs){
unsigned int N = size();
for(int i=0; i<N; i++)
at(i) += rhs[i];
return *this;
}
/// Assign a scalar to all values
vec<T> & operator=(T rhs){
unsigned int N = size();
for(int i=0; i<N; i++)
at(i) = rhs;
return *this;
}
/// Casting and assignment
template<typename Y>
vec<T> & operator=(vec<Y> rhs){
unsigned int N = rhs.size();
resize(N);
for(int i=0; i<N; i++)
at(i) = rhs[i];
return *this;
}
/// Unary minus (returns the negative of the vector)
vec<T> operator-() const{
unsigned int N = size();
vec<T> r(N);
//negate the vector
for(int i=0; i<N; i++)
r[i] = -at(i);
return r;
}
/// Outputs the vector as a string
std::string str() const
{
std::stringstream ss;
unsigned int N = size();
ss<<"[";
for(unsigned int i=0; i<N; i++)
{
ss<<at(i);
if(i != N-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
};
} //end namespace rts
template <typename T>
std::ostream& operator<<(std::ostream& os, stim::vec<T> v)
{
os<<v.str();
return os;
}
/// Multiply a vector by a constant when the vector is on the right hand side
template <typename T>
stim::vec<T> operator*(T lhs, stim::vec<T> rhs)
{
stim::vec<T> r;
return rhs * lhs;
}
#endif