Blame view

stim/math/vector.h 7.27 KB
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
  

  	}

  

c0e09133   Pavel Govyadinov   STABLE: made temp...
136
137
138
139
140
141
142
143
144
145
  	
  	vec<T> cyl2cart() const
  	{
  		vec<T> cyl;
  		cyl.push_back(at(0)*std::sin(at(1)));
  		cyl.push_back(at(0)*std::cos(at(1)));
  		cyl.push_back(at(2));
  		return(cyl);
  		
  	}
321ff17a   David Mayerich   Optimized the mat...
146
  	/// 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...
147
  	vec<T> cart2sph() const

57729e5b   David Mayerich   changed directory...
148
  	{

321ff17a   David Mayerich   Optimized the mat...
149
  

57729e5b   David Mayerich   changed directory...
150
  

487a9b49   David Mayerich   added the ability...
151
152
153
154
155
156
157
158
  		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...
159
160
161
162
  

  		return sph;

  	}

  

321ff17a   David Mayerich   Optimized the mat...
163
  	/// 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...
164
  	vec<T> sph2cart() const

57729e5b   David Mayerich   changed directory...
165
  	{

487a9b49   David Mayerich   added the ability...
166
167
168
169
  		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...
170
171
172
173
  

  		return cart;

  	}

  

321ff17a   David Mayerich   Optimized the mat...
174
  	/// Computes the normalized vector (where each coordinate is divided by the L2 norm)

5eeaf941   Pavel Govyadinov   changer to the ba...
175
  	vec<T> norm() const

57729e5b   David Mayerich   changed directory...
176
  	{

c8c976a9   David Mayerich   replaced CImg wit...
177
  		size_t N = size();

487a9b49   David Mayerich   added the ability...
178
179
180
  

          //compute and return the unit vector

          vec<T> result;

57729e5b   David Mayerich   changed directory...
181
182
183
184
185
  

          //compute the vector length

          T l = len();

  

          //normalize

c8c976a9   David Mayerich   replaced CImg wit...
186
          for(size_t i=0; i<N; i++)

57729e5b   David Mayerich   changed directory...
187
          {

487a9b49   David Mayerich   added the ability...
188
              result.push_back(at(i) / l);

57729e5b   David Mayerich   changed directory...
189
190
191
192
193
          }

  

          return result;

  	}

  

321ff17a   David Mayerich   Optimized the mat...
194
  	/// Computes the cross product of a 3-dimensional vector

5eeaf941   Pavel Govyadinov   changer to the ba...
195
  	vec<T> cross(const vec<T> rhs) const

57729e5b   David Mayerich   changed directory...
196
  	{

321ff17a   David Mayerich   Optimized the mat...
197
198
  

  		vec<T> result(3);

57729e5b   David Mayerich   changed directory...
199
200
  

  		//compute the cross product (only valid for 3D vectors)

321ff17a   David Mayerich   Optimized the mat...
201
202
203
  		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...
204
205
206
207
  

  		return result;

  	}

  

321ff17a   David Mayerich   Optimized the mat...
208
  	/// Compute the Euclidean inner (dot) product

5eeaf941   Pavel Govyadinov   changer to the ba...
209
      T dot(vec<T> rhs) const

57729e5b   David Mayerich   changed directory...
210
211
      {

          T result = (T)0;

c8c976a9   David Mayerich   replaced CImg wit...
212
          size_t N = size();

57729e5b   David Mayerich   changed directory...
213
          for(int i=0; i<N; i++)

487a9b49   David Mayerich   added the ability...
214
              result += at(i) * rhs[i];

57729e5b   David Mayerich   changed directory...
215
216
217
218
219
  

          return result;

  

      }

  

321ff17a   David Mayerich   Optimized the mat...
220
221
222
  	/// Arithmetic addition operator

  

      /// @param rhs is the right-hand-side operator for the addition

5eeaf941   Pavel Govyadinov   changer to the ba...
223
  	vec<T> operator+(vec<T> rhs) const

57729e5b   David Mayerich   changed directory...
224
  	{

c8c976a9   David Mayerich   replaced CImg wit...
225
  		size_t N = size();

321ff17a   David Mayerich   Optimized the mat...
226
227
  		vec<T> result(N);

  

559d0fcb   David Mayerich   wave interactions...
228
  		for(int i=0; i<N; i++)

321ff17a   David Mayerich   Optimized the mat...
229
  		    result[i] = at(i) + rhs[i];

57729e5b   David Mayerich   changed directory...
230
  

559d0fcb   David Mayerich   wave interactions...
231
232
  		return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
233
234
235
236
  

  	/// Arithmetic addition to a scalar

  

  	/// @param rhs is the right-hand-side operator for the addition

5eeaf941   Pavel Govyadinov   changer to the ba...
237
  	vec<T> operator+(T rhs) const

559d0fcb   David Mayerich   wave interactions...
238
  	{

c8c976a9   David Mayerich   replaced CImg wit...
239
  		size_t N = size();

321ff17a   David Mayerich   Optimized the mat...
240
241
  

  		vec<T> result(N);

559d0fcb   David Mayerich   wave interactions...
242
  		for(int i=0; i<N; i++)

321ff17a   David Mayerich   Optimized the mat...
243
  		    result[i] = at(i) + rhs;

559d0fcb   David Mayerich   wave interactions...
244
245
  

  		return result;

57729e5b   David Mayerich   changed directory...
246
  	}

321ff17a   David Mayerich   Optimized the mat...
247
248
249
250
  

  	/// Arithmetic subtraction operator

  

  	/// @param rhs is the right-hand-side operator for the subtraction

5eeaf941   Pavel Govyadinov   changer to the ba...
251
  	vec<T> operator-(vec<T> rhs) const

57729e5b   David Mayerich   changed directory...
252
  	{

c8c976a9   David Mayerich   replaced CImg wit...
253
  		size_t N = size();

321ff17a   David Mayerich   Optimized the mat...
254
255
256
  

          vec<T> result(N);

  

c8c976a9   David Mayerich   replaced CImg wit...
257
          for(size_t i=0; i<N; i++)

321ff17a   David Mayerich   Optimized the mat...
258
              result[i] = at(i) - rhs[i];

57729e5b   David Mayerich   changed directory...
259
260
261
  

          return result;

  	}

d4721000   Pavel Govyadinov   changes with debu...
262
263
264
265
266
  	/// 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...
267
  		size_t N = size();

d4721000   Pavel Govyadinov   changes with debu...
268
269
  

  		vec<T> result(N);

c8c976a9   David Mayerich   replaced CImg wit...
270
  		for(size_t i=0; i<N; i++)

d4721000   Pavel Govyadinov   changes with debu...
271
272
273
274
  		    result[i] = at(i) - rhs;

  

  		return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
275
276
277
278
  

  	/// Arithmetic scalar multiplication operator

  

  	/// @param rhs is the right-hand-side operator for the subtraction

5eeaf941   Pavel Govyadinov   changer to the ba...
279
  	vec<T> operator*(T rhs) const

57729e5b   David Mayerich   changed directory...
280
  	{

c8c976a9   David Mayerich   replaced CImg wit...
281
  		size_t N = size();

321ff17a   David Mayerich   Optimized the mat...
282
283
284
  

          vec<T> result(N);

  

c8c976a9   David Mayerich   replaced CImg wit...
285
          for(size_t i=0; i<N; i++)

321ff17a   David Mayerich   Optimized the mat...
286
              result[i] = at(i) * rhs;

57729e5b   David Mayerich   changed directory...
287
288
289
  

          return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
290
291
292
293
  

  	/// Arithmetic scalar division operator

  

  	/// @param rhs is the right-hand-side operator for the subtraction

5eeaf941   Pavel Govyadinov   changer to the ba...
294
  	vec<T> operator/(T rhs) const

57729e5b   David Mayerich   changed directory...
295
  	{

c8c976a9   David Mayerich   replaced CImg wit...
296
  		size_t N = size();

321ff17a   David Mayerich   Optimized the mat...
297
298
299
  

          vec<T> result(N);

  

c8c976a9   David Mayerich   replaced CImg wit...
300
          for(size_t i=0; i<N; i++)

321ff17a   David Mayerich   Optimized the mat...
301
              result[i] = at(i) / rhs;

57729e5b   David Mayerich   changed directory...
302
303
304
  

          return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
305
306
  

  	/// Multiplication by a scalar, followed by assignment

5eeaf941   Pavel Govyadinov   changer to the ba...
307
  	vec<T> operator*=(T rhs){

487a9b49   David Mayerich   added the ability...
308
  

c8c976a9   David Mayerich   replaced CImg wit...
309
310
  		size_t N = size();

  		for(size_t i=0; i<N; i++)

487a9b49   David Mayerich   added the ability...
311
  			at(i) = at(i) * rhs;

d609550e   David Mayerich   fixed bug in plan...
312
313
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
314
315
  

  	/// Addition and assignment

5eeaf941   Pavel Govyadinov   changer to the ba...
316
  	vec<T> operator+=(vec<T> rhs){

c8c976a9   David Mayerich   replaced CImg wit...
317
318
  		size_t N = size();

  		for(size_t i=0; i<N; i++)

487a9b49   David Mayerich   added the ability...
319
  			at(i) += rhs[i];

d609550e   David Mayerich   fixed bug in plan...
320
321
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
322
323
  

  	/// Assign a scalar to all values

5eeaf941   Pavel Govyadinov   changer to the ba...
324
  	vec<T> & operator=(T rhs){

487a9b49   David Mayerich   added the ability...
325
  

c8c976a9   David Mayerich   replaced CImg wit...
326
327
  		size_t N = size();

  		for(size_t i=0; i<N; i++)

487a9b49   David Mayerich   added the ability...
328
  			at(i) = rhs;

559d0fcb   David Mayerich   wave interactions...
329
330
  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
331
  

321ff17a   David Mayerich   Optimized the mat...
332
  	/// Casting and assignment

ecfd14df   David Mayerich   implemented D fie...
333
  	template<typename Y>

5eeaf941   Pavel Govyadinov   changer to the ba...
334
  	vec<T> & operator=(vec<Y> rhs){

c8c976a9   David Mayerich   replaced CImg wit...
335
  		size_t N = rhs.size();

487a9b49   David Mayerich   added the ability...
336
337
  		resize(N);

  

c8c976a9   David Mayerich   replaced CImg wit...
338
  		for(size_t i=0; i<N; i++)

487a9b49   David Mayerich   added the ability...
339
  			at(i) = rhs[i];

ecfd14df   David Mayerich   implemented D fie...
340
341
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
342
343
  

  	/// Unary minus (returns the negative of the vector)

5eeaf941   Pavel Govyadinov   changer to the ba...
344
  	vec<T> operator-() const{

57729e5b   David Mayerich   changed directory...
345
  

c8c976a9   David Mayerich   replaced CImg wit...
346
  		size_t N = size();

321ff17a   David Mayerich   Optimized the mat...
347
348
349
350
  

  		vec<T> r(N);

  

  		//negate the vector

c8c976a9   David Mayerich   replaced CImg wit...
351
  		for(size_t i=0; i<N; i++)

321ff17a   David Mayerich   Optimized the mat...
352
  		    r[i] = -at(i);

a9275be5   David Mayerich   added vector fiel...
353
  

559d0fcb   David Mayerich   wave interactions...
354
355
  		return r;

  	}

a9275be5   David Mayerich   added vector fiel...
356
  

57729e5b   David Mayerich   changed directory...
357
  

321ff17a   David Mayerich   Optimized the mat...
358
  	/// Outputs the vector as a string

ecfd14df   David Mayerich   implemented D fie...
359
  	std::string str() const

57729e5b   David Mayerich   changed directory...
360
361
362
  	{

  		std::stringstream ss;

  

c8c976a9   David Mayerich   replaced CImg wit...
363
  		size_t N = size();

487a9b49   David Mayerich   added the ability...
364
  

57729e5b   David Mayerich   changed directory...
365
  		ss<<"[";

c8c976a9   David Mayerich   replaced CImg wit...
366
  		for(size_t i=0; i<N; i++)

57729e5b   David Mayerich   changed directory...
367
  		{

487a9b49   David Mayerich   added the ability...
368
  			ss<<at(i);

57729e5b   David Mayerich   changed directory...
369
370
371
372
373
374
375
376
  			if(i != N-1)

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

57729e5b   David Mayerich   changed directory...
377
378
379
380
381
  };

  

  

  }	//end namespace rts

  

487a9b49   David Mayerich   added the ability...
382
383
  template <typename T>

  std::ostream& operator<<(std::ostream& os, stim::vec<T> v)

57729e5b   David Mayerich   changed directory...
384
  {

ecfd14df   David Mayerich   implemented D fie...
385
      os<<v.str();

57729e5b   David Mayerich   changed directory...
386
387
388
      return os;

  }

  

57729e5b   David Mayerich   changed directory...
389
  

321ff17a   David Mayerich   Optimized the mat...
390
  /// Multiply a vector by a constant when the vector is on the right hand side

487a9b49   David Mayerich   added the ability...
391
  template <typename T>

5eeaf941   Pavel Govyadinov   changer to the ba...
392
  stim::vec<T> operator*(T lhs, stim::vec<T> rhs)

57729e5b   David Mayerich   changed directory...
393
  {

487a9b49   David Mayerich   added the ability...
394
  	stim::vec<T> r;

57729e5b   David Mayerich   changed directory...
395
396
397
398
  

      return rhs * lhs;

  }

  

57729e5b   David Mayerich   changed directory...
399
  #endif