Blame view

stim/math/vector.h 7.1 KB
8e4f8364   David Mayerich   started a new opt...
1
2
  #ifndef STIM_VECTOR_H

  #define STIM_VECTOR_H

57729e5b   David Mayerich   changed directory...
3
4
5
6
  

  #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
  {

  

487a9b49   David Mayerich   added the ability...
14
15
  template <class T>

  struct vec : public std::vector<T>

57729e5b   David Mayerich   changed directory...
16
  {

5eeaf941   Pavel Govyadinov   changer to the ba...
17
18
19
20
  	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...
21
  

5eeaf941   Pavel Govyadinov   changer to the ba...
22
  	vec(){

57729e5b   David Mayerich   changed directory...
23
  

57729e5b   David Mayerich   changed directory...
24
25
  	}

  

321ff17a   David Mayerich   Optimized the mat...
26
  	/// Create a vector with a set dimension d

c8c976a9   David Mayerich   replaced CImg wit...
27
  	vec(size_t d)

a9275be5   David Mayerich   added vector fiel...
28
  	{

321ff17a   David Mayerich   Optimized the mat...
29
  		resize(d,0);

a9275be5   David Mayerich   added vector fiel...
30
  	}

5eeaf941   Pavel Govyadinov   changer to the ba...
31
  

5eeaf941   Pavel Govyadinov   changer to the ba...
32
  

321ff17a   David Mayerich   Optimized the mat...
33
  //	//efficiency constructors, makes construction easier for 1D-4D vectors

5eeaf941   Pavel Govyadinov   changer to the ba...
34
  	vec(T x, T y)

a9275be5   David Mayerich   added vector fiel...
35
  	{

321ff17a   David Mayerich   Optimized the mat...
36
37
38
  		resize(2, 0);

  		at(0) = x;

  		at(1) = y;

a9275be5   David Mayerich   added vector fiel...
39
  	}

5eeaf941   Pavel Govyadinov   changer to the ba...
40
  	vec(T x, T y, T z)

57729e5b   David Mayerich   changed directory...
41
  	{

321ff17a   David Mayerich   Optimized the mat...
42
43
44
45
  		resize(3, 0);

  		at(0) = x;

  		at(1) = y;

  		at(2) = z;

a9275be5   David Mayerich   added vector fiel...
46
  	}

5eeaf941   Pavel Govyadinov   changer to the ba...
47
  	vec(T x, T y, T z, T w)

a9275be5   David Mayerich   added vector fiel...
48
  	{

321ff17a   David Mayerich   Optimized the mat...
49
50
51
52
53
  		resize(4, 0);

  		at(0) = x;

  		at(1) = y;

  		at(2) = z;

  		at(3) = w;

57729e5b   David Mayerich   changed directory...
54
55
  	}

  

faef7718   David Mayerich   updates to stim::...
56
57
58
59
60
61
62
63
64
65
  	vec(std::string str){

  		std::stringstream ss(str);

  

  		T c;

  		while(ss >> c){

  			push_back(c);

  		}

  

  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
66
67
  	

  	

559d0fcb   David Mayerich   wave interactions...
68
  	//copy constructor

5eeaf941   Pavel Govyadinov   changer to the ba...
69
  	vec( const vec<T>& other){

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

eb303393   David Mayerich   updates to the st...
71
  		resize(N);							//resize the current vector to match the copy

c8c976a9   David Mayerich   replaced CImg wit...
72
  		for(size_t i=0; i<N; i++){	//copy each element

d03b807e   Pavel Govyadinov   fixed all the bug...
73
74
  			at(i) = other[i];
  		}
57729e5b   David Mayerich   changed directory...
75
76
  	}

  

321ff17a   David Mayerich   Optimized the mat...
77
78
  	//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...
79
80
81
82
83
  	vec<T> push(T x)

  	{

  		push_back(x);

  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
84
  	

5eeaf941   Pavel Govyadinov   changer to the ba...
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
  	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...
106
107
  

  	/// Casting operator. Creates a new vector with a new type U.

ecfd14df   David Mayerich   implemented D fie...
108
  	template< typename U >

5eeaf941   Pavel Govyadinov   changer to the ba...
109
  	operator vec<U>(){

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

487a9b49   David Mayerich   added the ability...
111
  		vec<U> result;

ecfd14df   David Mayerich   implemented D fie...
112
  		for(int i=0; i<N; i++)

487a9b49   David Mayerich   added the ability...
113
  			result.push_back(at(i));

ecfd14df   David Mayerich   implemented D fie...
114
115
116
117
  

  		return result;

  	}

  

ecfd14df   David Mayerich   implemented D fie...
118
  

321ff17a   David Mayerich   Optimized the mat...
119
  	/// computes the Euclidean length of the vector

5eeaf941   Pavel Govyadinov   changer to the ba...
120
  	T len() const

57729e5b   David Mayerich   changed directory...
121
  	{

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

487a9b49   David Mayerich   added the ability...
123
  

57729e5b   David Mayerich   changed directory...
124
125
          //compute and return the vector length

          T sum_sq = (T)0;

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

57729e5b   David Mayerich   changed directory...
127
          {

487a9b49   David Mayerich   added the ability...
128
              sum_sq += pow( at(i), 2 );

57729e5b   David Mayerich   changed directory...
129
          }

ecfd14df   David Mayerich   implemented D fie...
130
          return sqrt(sum_sq);

57729e5b   David Mayerich   changed directory...
131
132
133
  

  	}

  

321ff17a   David Mayerich   Optimized the mat...
134
  	/// 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...
135
  	vec<T> cart2sph() const

57729e5b   David Mayerich   changed directory...
136
  	{

321ff17a   David Mayerich   Optimized the mat...
137
  

57729e5b   David Mayerich   changed directory...
138
  

487a9b49   David Mayerich   added the ability...
139
140
141
142
143
144
145
146
  		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...
147
148
149
150
  

  		return sph;

  	}

  

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

57729e5b   David Mayerich   changed directory...
153
  	{

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

  		return cart;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
164
  	{

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

487a9b49   David Mayerich   added the ability...
166
167
168
  

          //compute and return the unit vector

          vec<T> result;

57729e5b   David Mayerich   changed directory...
169
170
171
172
173
  

          //compute the vector length

          T l = len();

  

          //normalize

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

57729e5b   David Mayerich   changed directory...
175
          {

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

57729e5b   David Mayerich   changed directory...
177
178
179
180
181
          }

  

          return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
184
  	{

321ff17a   David Mayerich   Optimized the mat...
185
186
  

  		vec<T> result(3);

57729e5b   David Mayerich   changed directory...
187
188
  

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

321ff17a   David Mayerich   Optimized the mat...
189
190
191
  		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...
192
193
194
195
  

  		return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
198
199
      {

          T result = (T)0;

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

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

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

57729e5b   David Mayerich   changed directory...
203
204
205
206
207
  

          return result;

  

      }

  

321ff17a   David Mayerich   Optimized the mat...
208
209
210
  	/// Arithmetic addition operator

  

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

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

57729e5b   David Mayerich   changed directory...
212
  	{

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

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

  

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

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

57729e5b   David Mayerich   changed directory...
218
  

559d0fcb   David Mayerich   wave interactions...
219
220
  		return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
221
222
223
224
  

  	/// Arithmetic addition to a scalar

  

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

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

559d0fcb   David Mayerich   wave interactions...
226
  	{

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

321ff17a   David Mayerich   Optimized the mat...
228
229
  

  		vec<T> result(N);

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

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

559d0fcb   David Mayerich   wave interactions...
232
233
  

  		return result;

57729e5b   David Mayerich   changed directory...
234
  	}

321ff17a   David Mayerich   Optimized the mat...
235
236
237
238
  

  	/// Arithmetic subtraction operator

  

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

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

57729e5b   David Mayerich   changed directory...
240
  	{

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

321ff17a   David Mayerich   Optimized the mat...
242
243
244
  

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
247
248
249
  

          return result;

  	}

d4721000   Pavel Govyadinov   changes with debu...
250
251
252
253
254
  	/// 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...
255
  		size_t N = size();

d4721000   Pavel Govyadinov   changes with debu...
256
257
  

  		vec<T> result(N);

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

d4721000   Pavel Govyadinov   changes with debu...
259
260
261
262
  		    result[i] = at(i) - rhs;

  

  		return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
263
264
265
266
  

  	/// Arithmetic scalar multiplication operator

  

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

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

57729e5b   David Mayerich   changed directory...
268
  	{

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

321ff17a   David Mayerich   Optimized the mat...
270
271
272
  

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
275
276
277
  

          return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
278
279
280
281
  

  	/// Arithmetic scalar division operator

  

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

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

57729e5b   David Mayerich   changed directory...
283
  	{

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

321ff17a   David Mayerich   Optimized the mat...
285
286
287
  

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
290
291
292
  

          return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
293
294
  

  	/// Multiplication by a scalar, followed by assignment

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

487a9b49   David Mayerich   added the ability...
296
  

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

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

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

d609550e   David Mayerich   fixed bug in plan...
300
301
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
302
303
  

  	/// Addition and assignment

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

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

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

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

d609550e   David Mayerich   fixed bug in plan...
308
309
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
310
311
  

  	/// Assign a scalar to all values

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

487a9b49   David Mayerich   added the ability...
313
  

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

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

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

559d0fcb   David Mayerich   wave interactions...
317
318
  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
319
  

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

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

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

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

487a9b49   David Mayerich   added the ability...
324
325
  		resize(N);

  

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

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

ecfd14df   David Mayerich   implemented D fie...
328
329
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
330
331
  

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

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

57729e5b   David Mayerich   changed directory...
333
  

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

321ff17a   David Mayerich   Optimized the mat...
335
336
337
338
  

  		vec<T> r(N);

  

  		//negate the vector

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

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

a9275be5   David Mayerich   added vector fiel...
341
  

559d0fcb   David Mayerich   wave interactions...
342
343
  		return r;

  	}

a9275be5   David Mayerich   added vector fiel...
344
  

57729e5b   David Mayerich   changed directory...
345
  

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

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

57729e5b   David Mayerich   changed directory...
348
349
350
  	{

  		std::stringstream ss;

  

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

487a9b49   David Mayerich   added the ability...
352
  

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

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

57729e5b   David Mayerich   changed directory...
355
  		{

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

57729e5b   David Mayerich   changed directory...
357
358
359
360
361
362
363
364
  			if(i != N-1)

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

57729e5b   David Mayerich   changed directory...
365
366
367
368
369
  };

  

  

  }	//end namespace rts

  

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

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

57729e5b   David Mayerich   changed directory...
372
  {

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

57729e5b   David Mayerich   changed directory...
374
375
376
      return os;

  }

  

57729e5b   David Mayerich   changed directory...
377
  

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

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

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

57729e5b   David Mayerich   changed directory...
381
  {

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

57729e5b   David Mayerich   changed directory...
383
384
385
386
  

      return rhs * lhs;

  }

  

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