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
|
/// Create a vector with a set dimension d
|
c8c976a9
David Mayerich
replaced CImg wit...
|
29
|
vec(size_t 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){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
72
|
size_t N = other.size();
|
eb303393
David Mayerich
updates to the st...
|
73
|
resize(N); //resize the current vector to match the copy
|
c8c976a9
David Mayerich
replaced CImg wit...
|
74
|
for(size_t i=0; i<N; i++){ //copy each element
|
d03b807e
Pavel Govyadinov
fixed all the bug...
|
75
76
|
at(i) = other[i];
}
|
57729e5b
David Mayerich
changed directory...
|
77
78
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
79
80
|
//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...
|
81
82
83
84
85
|
vec<T> push(T x)
{
push_back(x);
return *this;
}
|
ecfd14df
David Mayerich
implemented D fie...
|
86
|
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
|
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...
|
108
109
|
/// Casting operator. Creates a new vector with a new type U.
|
ecfd14df
David Mayerich
implemented D fie...
|
110
|
template< typename U >
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
111
|
operator vec<U>(){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
112
|
size_t N = size();
|
487a9b49
David Mayerich
added the ability...
|
113
|
vec<U> result;
|
ecfd14df
David Mayerich
implemented D fie...
|
114
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
115
|
result.push_back(at(i));
|
ecfd14df
David Mayerich
implemented D fie...
|
116
117
118
119
|
return result;
}
|
ecfd14df
David Mayerich
implemented D fie...
|
120
|
|
321ff17a
David Mayerich
Optimized the mat...
|
121
|
/// computes the Euclidean length of the vector
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
122
|
T len() const
|
57729e5b
David Mayerich
changed directory...
|
123
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
124
|
size_t N = size();
|
487a9b49
David Mayerich
added the ability...
|
125
|
|
57729e5b
David Mayerich
changed directory...
|
126
127
|
//compute and return the vector length
T sum_sq = (T)0;
|
c8c976a9
David Mayerich
replaced CImg wit...
|
128
|
for(size_t i=0; i<N; i++)
|
57729e5b
David Mayerich
changed directory...
|
129
|
{
|
487a9b49
David Mayerich
added the ability...
|
130
|
sum_sq += pow( at(i), 2 );
|
57729e5b
David Mayerich
changed directory...
|
131
|
}
|
ecfd14df
David Mayerich
implemented D fie...
|
132
|
return sqrt(sum_sq);
|
57729e5b
David Mayerich
changed directory...
|
133
134
135
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
136
|
/// 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...
|
137
|
vec<T> cart2sph() const
|
57729e5b
David Mayerich
changed directory...
|
138
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
139
|
|
57729e5b
David Mayerich
changed directory...
|
140
|
|
487a9b49
David Mayerich
added the ability...
|
141
142
143
144
145
146
147
148
|
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...
|
149
150
151
152
|
return sph;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
153
|
/// 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...
|
154
|
vec<T> sph2cart() const
|
57729e5b
David Mayerich
changed directory...
|
155
|
{
|
487a9b49
David Mayerich
added the ability...
|
156
157
158
159
|
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...
|
160
161
162
163
|
return cart;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
164
|
/// Computes the normalized vector (where each coordinate is divided by the L2 norm)
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
165
|
vec<T> norm() const
|
57729e5b
David Mayerich
changed directory...
|
166
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
167
|
size_t N = size();
|
487a9b49
David Mayerich
added the ability...
|
168
169
170
|
//compute and return the unit vector
vec<T> result;
|
57729e5b
David Mayerich
changed directory...
|
171
172
173
174
175
|
//compute the vector length
T l = len();
//normalize
|
c8c976a9
David Mayerich
replaced CImg wit...
|
176
|
for(size_t i=0; i<N; i++)
|
57729e5b
David Mayerich
changed directory...
|
177
|
{
|
487a9b49
David Mayerich
added the ability...
|
178
|
result.push_back(at(i) / l);
|
57729e5b
David Mayerich
changed directory...
|
179
180
181
182
183
|
}
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
184
|
/// Computes the cross product of a 3-dimensional vector
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
185
|
vec<T> cross(const vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
186
|
{
|
321ff17a
David Mayerich
Optimized the mat...
|
187
188
|
vec<T> result(3);
|
57729e5b
David Mayerich
changed directory...
|
189
190
|
//compute the cross product (only valid for 3D vectors)
|
321ff17a
David Mayerich
Optimized the mat...
|
191
192
193
|
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...
|
194
195
196
197
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
198
|
/// Compute the Euclidean inner (dot) product
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
199
|
T dot(vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
200
201
|
{
T result = (T)0;
|
c8c976a9
David Mayerich
replaced CImg wit...
|
202
|
size_t N = size();
|
57729e5b
David Mayerich
changed directory...
|
203
|
for(int i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
204
|
result += at(i) * rhs[i];
|
57729e5b
David Mayerich
changed directory...
|
205
206
207
208
209
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
210
211
212
|
/// Arithmetic addition operator
/// @param rhs is the right-hand-side operator for the addition
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
213
|
vec<T> operator+(vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
214
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
215
|
size_t N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
216
217
|
vec<T> result(N);
|
559d0fcb
David Mayerich
wave interactions...
|
218
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
219
|
result[i] = at(i) + rhs[i];
|
57729e5b
David Mayerich
changed directory...
|
220
|
|
559d0fcb
David Mayerich
wave interactions...
|
221
222
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
223
224
225
226
|
/// Arithmetic addition to a scalar
/// @param rhs is the right-hand-side operator for the addition
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
227
|
vec<T> operator+(T rhs) const
|
559d0fcb
David Mayerich
wave interactions...
|
228
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
229
|
size_t N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
230
231
|
vec<T> result(N);
|
559d0fcb
David Mayerich
wave interactions...
|
232
|
for(int i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
233
|
result[i] = at(i) + rhs;
|
559d0fcb
David Mayerich
wave interactions...
|
234
235
|
return result;
|
57729e5b
David Mayerich
changed directory...
|
236
|
}
|
321ff17a
David Mayerich
Optimized the mat...
|
237
238
239
240
|
/// Arithmetic subtraction operator
/// @param rhs is the right-hand-side operator for the subtraction
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
241
|
vec<T> operator-(vec<T> rhs) const
|
57729e5b
David Mayerich
changed directory...
|
242
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
243
|
size_t N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
244
245
246
|
vec<T> result(N);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
247
|
for(size_t i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
248
|
result[i] = at(i) - rhs[i];
|
57729e5b
David Mayerich
changed directory...
|
249
250
251
|
return result;
}
|
d4721000
Pavel Govyadinov
changes with debu...
|
252
253
254
255
256
|
/// Arithmetic subtraction to a scalar
/// @param rhs is the right-hand-side operator for the addition
vec<T> operator-(T rhs) const
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
257
|
size_t N = size();
|
d4721000
Pavel Govyadinov
changes with debu...
|
258
259
|
vec<T> result(N);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
260
|
for(size_t i=0; i<N; i++)
|
d4721000
Pavel Govyadinov
changes with debu...
|
261
262
263
264
|
result[i] = at(i) - rhs;
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
265
266
267
268
|
/// Arithmetic scalar multiplication operator
/// @param rhs is the right-hand-side operator for the subtraction
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
269
|
vec<T> operator*(T rhs) const
|
57729e5b
David Mayerich
changed directory...
|
270
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
271
|
size_t N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
272
273
274
|
vec<T> result(N);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
275
|
for(size_t i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
276
|
result[i] = at(i) * rhs;
|
57729e5b
David Mayerich
changed directory...
|
277
278
279
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
280
281
282
283
|
/// Arithmetic scalar division operator
/// @param rhs is the right-hand-side operator for the subtraction
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
284
|
vec<T> operator/(T rhs) const
|
57729e5b
David Mayerich
changed directory...
|
285
|
{
|
c8c976a9
David Mayerich
replaced CImg wit...
|
286
|
size_t N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
287
288
289
|
vec<T> result(N);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
290
|
for(size_t i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
291
|
result[i] = at(i) / rhs;
|
57729e5b
David Mayerich
changed directory...
|
292
293
294
|
return result;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
295
296
|
/// Multiplication by a scalar, followed by assignment
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
297
|
vec<T> operator*=(T rhs){
|
487a9b49
David Mayerich
added the ability...
|
298
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
299
300
|
size_t N = size();
for(size_t i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
301
|
at(i) = at(i) * rhs;
|
d609550e
David Mayerich
fixed bug in plan...
|
302
303
|
return *this;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
304
305
|
/// Addition and assignment
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
306
|
vec<T> operator+=(vec<T> rhs){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
307
308
|
size_t N = size();
for(size_t i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
309
|
at(i) += rhs[i];
|
d609550e
David Mayerich
fixed bug in plan...
|
310
311
|
return *this;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
312
313
|
/// Assign a scalar to all values
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
314
|
vec<T> & operator=(T rhs){
|
487a9b49
David Mayerich
added the ability...
|
315
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
316
317
|
size_t N = size();
for(size_t i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
318
|
at(i) = rhs;
|
559d0fcb
David Mayerich
wave interactions...
|
319
320
|
return *this;
}
|
ecfd14df
David Mayerich
implemented D fie...
|
321
|
|
321ff17a
David Mayerich
Optimized the mat...
|
322
|
/// Casting and assignment
|
ecfd14df
David Mayerich
implemented D fie...
|
323
|
template<typename Y>
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
324
|
vec<T> & operator=(vec<Y> rhs){
|
c8c976a9
David Mayerich
replaced CImg wit...
|
325
|
size_t N = rhs.size();
|
487a9b49
David Mayerich
added the ability...
|
326
327
|
resize(N);
|
c8c976a9
David Mayerich
replaced CImg wit...
|
328
|
for(size_t i=0; i<N; i++)
|
487a9b49
David Mayerich
added the ability...
|
329
|
at(i) = rhs[i];
|
ecfd14df
David Mayerich
implemented D fie...
|
330
331
|
return *this;
}
|
321ff17a
David Mayerich
Optimized the mat...
|
332
333
|
/// Unary minus (returns the negative of the vector)
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
334
|
vec<T> operator-() const{
|
57729e5b
David Mayerich
changed directory...
|
335
|
|
c8c976a9
David Mayerich
replaced CImg wit...
|
336
|
size_t N = size();
|
321ff17a
David Mayerich
Optimized the mat...
|
337
338
339
340
|
vec<T> r(N);
//negate the vector
|
c8c976a9
David Mayerich
replaced CImg wit...
|
341
|
for(size_t i=0; i<N; i++)
|
321ff17a
David Mayerich
Optimized the mat...
|
342
|
r[i] = -at(i);
|
a9275be5
David Mayerich
added vector fiel...
|
343
|
|
559d0fcb
David Mayerich
wave interactions...
|
344
345
|
return r;
}
|
a9275be5
David Mayerich
added vector fiel...
|
346
|
|
57729e5b
David Mayerich
changed directory...
|
347
|
|
321ff17a
David Mayerich
Optimized the mat...
|
348
|
/// Outputs the vector as a string
|
ecfd14df
David Mayerich
implemented D fie...
|
349
|
std::string str() const
|
57729e5b
David Mayerich
changed directory...
|
350
351
352
|
{
std::stringstream ss;
|
c8c976a9
David Mayerich
replaced CImg wit...
|
353
|
size_t N = size();
|
487a9b49
David Mayerich
added the ability...
|
354
|
|
57729e5b
David Mayerich
changed directory...
|
355
|
ss<<"[";
|
c8c976a9
David Mayerich
replaced CImg wit...
|
356
|
for(size_t i=0; i<N; i++)
|
57729e5b
David Mayerich
changed directory...
|
357
|
{
|
487a9b49
David Mayerich
added the ability...
|
358
|
ss<<at(i);
|
57729e5b
David Mayerich
changed directory...
|
359
360
361
362
363
364
365
366
|
if(i != N-1)
ss<<", ";
}
ss<<"]";
return ss.str();
}
|
57729e5b
David Mayerich
changed directory...
|
367
368
369
370
371
|
};
} //end namespace rts
|
487a9b49
David Mayerich
added the ability...
|
372
373
|
template <typename T>
std::ostream& operator<<(std::ostream& os, stim::vec<T> v)
|
57729e5b
David Mayerich
changed directory...
|
374
|
{
|
ecfd14df
David Mayerich
implemented D fie...
|
375
|
os<<v.str();
|
57729e5b
David Mayerich
changed directory...
|
376
377
378
|
return os;
}
|
57729e5b
David Mayerich
changed directory...
|
379
|
|
321ff17a
David Mayerich
Optimized the mat...
|
380
|
/// Multiply a vector by a constant when the vector is on the right hand side
|
487a9b49
David Mayerich
added the ability...
|
381
|
template <typename T>
|
5eeaf941
Pavel Govyadinov
changer to the ba...
|
382
|
stim::vec<T> operator*(T lhs, stim::vec<T> rhs)
|
57729e5b
David Mayerich
changed directory...
|
383
|
{
|
487a9b49
David Mayerich
added the ability...
|
384
|
stim::vec<T> r;
|
57729e5b
David Mayerich
changed directory...
|
385
386
387
388
|
return rhs * lhs;
}
|
57729e5b
David Mayerich
changed directory...
|
389
|
#endif
|