57729e5b
David Mayerich
changed directory...
|
1
2
3
4
5
6
|
#ifndef RTS_VECTOR_H
#define RTS_VECTOR_H
#include <iostream>
#include <cmath>
#include <sstream>
|
487a9b49
David Mayerich
added the ability...
|
7
|
#include <vector>
|
93de94e6
Laila Saadatifard
fix merge issues
|
8
|
|
7d1d153a
Pavel Govyadinov
fixed the include...
|
9
|
#include <stim/cuda/cudatools/callable.h>
|
57729e5b
David Mayerich
changed directory...
|
10
|
|
8a86bd56
David Mayerich
changed rts names...
|
11
|
namespace stim
|
57729e5b
David Mayerich
changed directory...
|
12
13
14
15
|
{
|
487a9b49
David Mayerich
added the ability...
|
16
17
|
template <class T>
struct vec : public std::vector<T>
|
57729e5b
David Mayerich
changed directory...
|
18
|
{
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
19
20
21
22
|
using std::vector<T>::size;
using std::vector<T>::at;
using std::vector<T>::resize;
using std::vector<T>::push_back;
|
d4721000
Pavel Govyadinov
changes with debu...
|
23
|
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
24
|
vec(){
|
57729e5b
David Mayerich
changed directory...
|
25
|
|
57729e5b
David Mayerich
changed directory...
|
26
27
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
28
29
|
/// Create a vector with a set dimension d
vec(int d)
|
a9275be5
David Mayerich
added vector fiel...
|
30
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
31
|
resize(d,0);
|
a9275be5
David Mayerich
added vector fiel...
|
32
|
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
33
|
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
34
|
|
321ff17a
David Mayerich
Optimized the mat...
|
35
|
// //efficiency constructors, makes construction easier for 1D-4D vectors
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
36
|
vec(T x, T y)
|
a9275be5
David Mayerich
added vector fiel...
|
37
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
38
39
40
|
resize(2, 0);
at(0) = x;
at(1) = y;
|
a9275be5
David Mayerich
added vector fiel...
|
41
|
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
42
|
vec(T x, T y, T z)
|
57729e5b
David Mayerich
changed directory...
|
43
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
44
45
46
47
|
resize(3, 0);
at(0) = x;
at(1) = y;
at(2) = z;
|
a9275be5
David Mayerich
added vector fiel...
|
48
|
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
49
|
vec(T x, T y, T z, T w)
|
a9275be5
David Mayerich
added vector fiel...
|
50
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
51
52
53
54
55
|
resize(4, 0);
at(0) = x;
at(1) = y;
at(2) = z;
at(3) = w;
|
57729e5b
David Mayerich
changed directory...
|
56
57
|
}
|
faef7718
David Mayerich
updates to stim::...
|
58
59
60
61
62
63
64
65
66
67
|
vec(std::string str){
std::stringstream ss(str);
T c;
while(ss >> c){
push_back(c);
}
}
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
68
69
|
|
559d0fcb
David Mayerich
wave interactions...
|
70
|
//copy constructor
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
71
|
vec( const vec<T>& other){
|
487a9b49
David Mayerich
added the ability...
|
72
|
unsigned int N = other.size();
|
eb303393
David Mayerich
updates to the st...
|
73
74
75
|
resize(N); //resize the current vector to match the copy
for(unsigned int i=0; i<N; i++) //copy each element
at(i) = other[i];
|
57729e5b
David Mayerich
changed directory...
|
76
77
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
78
79
|
//I'm not sure what these were doing here.
//Keep them now, we'll worry about it later.
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
80
81
82
83
84
|
vec<T> push(T x)
{
push_back(x);
return *this;
}
|
ecfd14df
David Mayerich
implemented D fie...
|
85
|
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
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;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
107
108
|
/// Casting operator. Creates a new vector with a new type U.
|
ecfd14df
David Mayerich
implemented D fie...
|
109
|
template< typename U >
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
110
|
operator vec<U>(){
|
487a9b49
David Mayerich
added the ability...
|
111
112
|
unsigned int N = size();
vec<U> result;
|
ecfd14df
David Mayerich
implemented D fie...
|
113
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
114
|
result.push_back(at(i));
|
ecfd14df
David Mayerich
implemented D fie...
|
115
116
117
118
|
return result;
}
|
ecfd14df
David Mayerich
implemented D fie...
|
119
|
|
321ff17a
David Mayerich
Optimized the mat...
|
120
|
/// computes the Euclidean length of the vector
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
121
|
T len() const
|
57729e5b
David Mayerich
changed directory...
|
122
|
{
|
487a9b49
David Mayerich
added the ability...
|
123
124
|
unsigned int N = size();
|
57729e5b
David Mayerich
changed directory...
|
125
126
|
//compute and return the vector length
T sum_sq = (T)0;
|
e376212f
David Mayerich
added support for...
|
127
|
for(unsigned int i=0; i<N; i++)
|
57729e5b
David Mayerich
changed directory...
|
128
|
{
|
487a9b49
David Mayerich
added the ability...
|
129
|
sum_sq += pow( at(i), 2 );
|
57729e5b
David Mayerich
changed directory...
|
130
|
}
|
ecfd14df
David Mayerich
implemented D fie...
|
131
|
return sqrt(sum_sq);
|
57729e5b
David Mayerich
changed directory...
|
132
133
134
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
135
|
/// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [0, 2*pi])
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
136
|
vec<T> cart2sph() const
|
57729e5b
David Mayerich
changed directory...
|
137
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
138
|
|
57729e5b
David Mayerich
changed directory...
|
139
|
|
487a9b49
David Mayerich
added the ability...
|
140
141
142
143
144
145
146
147
|
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]));
|
57729e5b
David Mayerich
changed directory...
|
148
149
150
151
|
return sph;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
152
|
/// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi])
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
153
|
vec<T> sph2cart() const
|
57729e5b
David Mayerich
changed directory...
|
154
|
{
|
487a9b49
David Mayerich
added the ability...
|
155
156
157
158
|
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)));
|
57729e5b
David Mayerich
changed directory...
|
159
160
161
162
|
return cart;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
163
|
/// Computes the normalized vector (where each coordinate is divided by the L2 norm)
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
164
|
vec<T> norm() const
|
57729e5b
David Mayerich
changed directory...
|
165
|
{
|
487a9b49
David Mayerich
added the ability...
|
166
167
168
169
|
unsigned int N = size();
//compute and return the unit vector
vec<T> result;
|
57729e5b
David Mayerich
changed directory...
|
170
171
172
173
174
175
176
|
//compute the vector length
T l = len();
//normalize
for(int i=0; i<N; i++)
{
|
487a9b49
David Mayerich
added the ability...
|
177
|
result.push_back(at(i) / l);
|
57729e5b
David Mayerich
changed directory...
|
178
179
180
181
182
|
}
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
183
|
/// Computes the cross product of a 3-dimensional vector
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
184
|
vec<T> cross(const vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
185
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
186
187
|
vec<T> result(3);
|
57729e5b
David Mayerich
changed directory...
|
188
189
|
//compute the cross product (only valid for 3D vectors)
|
321ff17a
David Mayerich
Optimized the mat...
|
190
191
192
|
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]);
|
57729e5b
David Mayerich
changed directory...
|
193
194
195
196
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
197
|
/// Compute the Euclidean inner (dot) product
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
198
|
T dot(vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
199
200
|
{
T result = (T)0;
|
321ff17a
David Mayerich
Optimized the mat...
|
201
|
unsigned int N = size();
|
57729e5b
David Mayerich
changed directory...
|
202
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
203
|
result += at(i) * rhs[i];
|
57729e5b
David Mayerich
changed directory...
|
204
205
206
207
208
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
209
210
211
|
/// Arithmetic addition operator
/// @param rhs is the right-hand-side operator for the addition
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
212
|
vec<T> operator+(vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
213
|
{
|
487a9b49
David Mayerich
added the ability...
|
214
|
unsigned int N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
215
216
|
vec<T> result(N);
|
559d0fcb
David Mayerich
wave interactions...
|
217
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
218
|
result[i] = at(i) + rhs[i];
|
57729e5b
David Mayerich
changed directory...
|
219
|
|
559d0fcb
David Mayerich
wave interactions...
|
220
221
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
222
223
224
225
|
/// Arithmetic addition to a scalar
/// @param rhs is the right-hand-side operator for the addition
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
226
|
vec<T> operator+(T rhs) const
|
559d0fcb
David Mayerich
wave interactions...
|
227
|
{
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
228
|
unsigned int N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
229
230
|
vec<T> result(N);
|
559d0fcb
David Mayerich
wave interactions...
|
231
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
232
|
result[i] = at(i) + rhs;
|
559d0fcb
David Mayerich
wave interactions...
|
233
234
|
return result;
|
57729e5b
David Mayerich
changed directory...
|
235
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
236
237
238
239
|
/// Arithmetic subtraction operator
/// @param rhs is the right-hand-side operator for the subtraction
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
240
|
vec<T> operator-(vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
241
|
{
|
487a9b49
David Mayerich
added the ability...
|
242
|
unsigned int N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
243
244
245
|
vec<T> result(N);
|
e376212f
David Mayerich
added support for...
|
246
|
for(unsigned int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
247
|
result[i] = at(i) - rhs[i];
|
57729e5b
David Mayerich
changed directory...
|
248
249
250
|
return result;
}
|
d4721000
Pavel Govyadinov
changes with debu...
|
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/// 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;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
264
265
266
267
|
/// Arithmetic scalar multiplication operator
/// @param rhs is the right-hand-side operator for the subtraction
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
268
|
vec<T> operator*(T rhs) const
|
57729e5b
David Mayerich
changed directory...
|
269
|
{
|
487a9b49
David Mayerich
added the ability...
|
270
|
unsigned int N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
271
272
273
|
vec<T> result(N);
|
57729e5b
David Mayerich
changed directory...
|
274
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
275
|
result[i] = at(i) * rhs;
|
57729e5b
David Mayerich
changed directory...
|
276
277
278
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
279
280
281
282
|
/// Arithmetic scalar division operator
/// @param rhs is the right-hand-side operator for the subtraction
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
283
|
vec<T> operator/(T rhs) const
|
57729e5b
David Mayerich
changed directory...
|
284
|
{
|
487a9b49
David Mayerich
added the ability...
|
285
|
unsigned int N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
286
287
288
|
vec<T> result(N);
|
57729e5b
David Mayerich
changed directory...
|
289
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
290
|
result[i] = at(i) / rhs;
|
57729e5b
David Mayerich
changed directory...
|
291
292
293
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
294
295
|
/// Multiplication by a scalar, followed by assignment
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
296
|
vec<T> operator*=(T rhs){
|
487a9b49
David Mayerich
added the ability...
|
297
298
|
unsigned int N = size();
|
d609550e
David Mayerich
fixed bug in plan...
|
299
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
300
|
at(i) = at(i) * rhs;
|
d609550e
David Mayerich
fixed bug in plan...
|
301
302
|
return *this;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
303
304
|
/// Addition and assignment
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
305
|
vec<T> operator+=(vec<T> rhs){
|
487a9b49
David Mayerich
added the ability...
|
306
|
unsigned int N = size();
|
d609550e
David Mayerich
fixed bug in plan...
|
307
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
308
|
at(i) += rhs[i];
|
d609550e
David Mayerich
fixed bug in plan...
|
309
310
|
return *this;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
311
312
|
/// Assign a scalar to all values
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
313
|
vec<T> & operator=(T rhs){
|
487a9b49
David Mayerich
added the ability...
|
314
315
|
unsigned int N = size();
|
559d0fcb
David Mayerich
wave interactions...
|
316
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
317
|
at(i) = rhs;
|
559d0fcb
David Mayerich
wave interactions...
|
318
319
|
return *this;
}
|
ecfd14df
David Mayerich
implemented D fie...
|
320
|
|
321ff17a
David Mayerich
Optimized the mat...
|
321
|
/// Casting and assignment
|
ecfd14df
David Mayerich
implemented D fie...
|
322
|
template<typename Y>
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
323
|
vec<T> & operator=(vec<Y> rhs){
|
487a9b49
David Mayerich
added the ability...
|
324
325
326
|
unsigned int N = rhs.size();
resize(N);
|
ecfd14df
David Mayerich
implemented D fie...
|
327
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
328
|
at(i) = rhs[i];
|
ecfd14df
David Mayerich
implemented D fie...
|
329
330
|
return *this;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
331
332
|
/// Unary minus (returns the negative of the vector)
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
333
|
vec<T> operator-() const{
|
57729e5b
David Mayerich
changed directory...
|
334
|
|
487a9b49
David Mayerich
added the ability...
|
335
|
unsigned int N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
336
337
338
339
|
vec<T> r(N);
//negate the vector
|
559d0fcb
David Mayerich
wave interactions...
|
340
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
341
|
r[i] = -at(i);
|
a9275be5
David Mayerich
added vector fiel...
|
342
|
|
559d0fcb
David Mayerich
wave interactions...
|
343
344
|
return r;
}
|
a9275be5
David Mayerich
added vector fiel...
|
345
|
|
57729e5b
David Mayerich
changed directory...
|
346
|
|
321ff17a
David Mayerich
Optimized the mat...
|
347
|
/// Outputs the vector as a string
|
ecfd14df
David Mayerich
implemented D fie...
|
348
|
std::string str() const
|
57729e5b
David Mayerich
changed directory...
|
349
350
351
|
{
std::stringstream ss;
|
487a9b49
David Mayerich
added the ability...
|
352
353
|
unsigned int N = size();
|
57729e5b
David Mayerich
changed directory...
|
354
|
ss<<"[";
|
e376212f
David Mayerich
added support for...
|
355
|
for(unsigned int i=0; i<N; i++)
|
57729e5b
David Mayerich
changed directory...
|
356
|
{
|
487a9b49
David Mayerich
added the ability...
|
357
|
ss<<at(i);
|
57729e5b
David Mayerich
changed directory...
|
358
359
360
361
362
363
364
365
|
if(i != N-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
|
57729e5b
David Mayerich
changed directory...
|
366
367
368
369
370
|
};
} //end namespace rts
|
487a9b49
David Mayerich
added the ability...
|
371
372
|
template <typename T>
std::ostream& operator<<(std::ostream& os, stim::vec<T> v)
|
57729e5b
David Mayerich
changed directory...
|
373
|
{
|
ecfd14df
David Mayerich
implemented D fie...
|
374
|
os<<v.str();
|
57729e5b
David Mayerich
changed directory...
|
375
376
377
|
return os;
}
|
57729e5b
David Mayerich
changed directory...
|
378
|
|
321ff17a
David Mayerich
Optimized the mat...
|
379
|
/// Multiply a vector by a constant when the vector is on the right hand side
|
487a9b49
David Mayerich
added the ability...
|
380
|
template <typename T>
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
381
|
stim::vec<T> operator*(T lhs, stim::vec<T> rhs)
|
57729e5b
David Mayerich
changed directory...
|
382
|
{
|
487a9b49
David Mayerich
added the ability...
|
383
|
stim::vec<T> r;
|
57729e5b
David Mayerich
changed directory...
|
384
385
386
387
|
return rhs * lhs;
}
|
57729e5b
David Mayerich
changed directory...
|
388
|
#endif
|