Blame view

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

8309b07a   David Mayerich   fixed some vec3 e...
8
  

7d1d153a   Pavel Govyadinov   fixed the include...
9
  #include <stim/cuda/cudatools/callable.h>

8309b07a   David Mayerich   fixed some vec3 e...
10
  #include <stim/math/vec3.h>

57729e5b   David Mayerich   changed directory...
11
  

8a86bd56   David Mayerich   changed rts names...
12
  namespace stim

57729e5b   David Mayerich   changed directory...
13
14
  {

  

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

  struct vec : public std::vector<T>

57729e5b   David Mayerich   changed directory...
17
  {

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

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

57729e5b   David Mayerich   changed directory...
24
  

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

  

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

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

a9275be5   David Mayerich   added vector fiel...
29
  	{

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

a9275be5   David Mayerich   added vector fiel...
31
  	}

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

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

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

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

a9275be5   David Mayerich   added vector fiel...
36
  	{

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

  		at(0) = x;

  		at(1) = y;

a9275be5   David Mayerich   added vector fiel...
40
  	}

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

57729e5b   David Mayerich   changed directory...
42
  	{

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

  		at(0) = x;

  		at(1) = y;

  		at(2) = z;

a9275be5   David Mayerich   added vector fiel...
47
  	}

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

a9275be5   David Mayerich   added vector fiel...
49
  	{

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

  		at(0) = x;

  		at(1) = y;

  		at(2) = z;

  		at(3) = w;

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

  

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

  		std::stringstream ss(str);

  

  		T c;

  		while(ss >> c){

  			push_back(c);

  		}

  

  	}

  

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

  	

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

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

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

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

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

8309b07a   David Mayerich   fixed some vec3 e...
74
75
  			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>(){

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

487a9b49   David Mayerich   added the ability...
112
  		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
  	{

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

487a9b49   David Mayerich   added the ability...
124
  

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

          T sum_sq = (T)0;

c8c976a9   David Mayerich   replaced CImg wit...
127
          for(size_t 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
  

  	}

  

c0e09133   Pavel Govyadinov   STABLE: made temp...
135
136
137
138
139
140
141
142
143
144
  	
  	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...
145
  	/// 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...
146
  	vec<T> cart2sph() const

57729e5b   David Mayerich   changed directory...
147
  	{

321ff17a   David Mayerich   Optimized the mat...
148
  

57729e5b   David Mayerich   changed directory...
149
  

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

  		return sph;

  	}

  

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

57729e5b   David Mayerich   changed directory...
164
  	{

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

  		return cart;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
175
  	{

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

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

          //compute and return the unit vector

          vec<T> result;

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

          //compute the vector length

          T l = len();

  

          //normalize

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

57729e5b   David Mayerich   changed directory...
186
          {

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

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

  

          return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
195
  	{

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

  		vec<T> result(3);

57729e5b   David Mayerich   changed directory...
198
199
  

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

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

  		return result;

  	}

  

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

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

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

          T result = (T)0;

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

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

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

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

          return result;

  

      }

  

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

  

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

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

57729e5b   David Mayerich   changed directory...
223
  	{

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

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

  

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

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

57729e5b   David Mayerich   changed directory...
229
  

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

  	}

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

  	/// Arithmetic addition to a scalar

  

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

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

559d0fcb   David Mayerich   wave interactions...
237
  	{

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

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

  		vec<T> result(N);

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

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

559d0fcb   David Mayerich   wave interactions...
243
244
  

  		return result;

57729e5b   David Mayerich   changed directory...
245
  	}

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

  	/// Arithmetic subtraction operator

  

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

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

57729e5b   David Mayerich   changed directory...
251
  	{

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

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

          vec<T> result(N);

  

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

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

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

          return result;

  	}

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

d4721000   Pavel Govyadinov   changes with debu...
267
268
  

  		vec<T> result(N);

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

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

  

  		return result;

  	}

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

  	/// Arithmetic scalar multiplication operator

  

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

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

57729e5b   David Mayerich   changed directory...
279
  	{

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

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

          vec<T> result(N);

  

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

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

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

          return result;

  	}

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

  	/// Arithmetic scalar division operator

  

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

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

57729e5b   David Mayerich   changed directory...
294
  	{

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

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

          vec<T> result(N);

  

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

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

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

          return result;

  	}

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

  	/// Multiplication by a scalar, followed by assignment

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

487a9b49   David Mayerich   added the ability...
307
  

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

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

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

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

  	}

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

  	/// Addition and assignment

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

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[i];

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

  	}

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

  	/// Assign a scalar to all values

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

487a9b49   David Mayerich   added the ability...
324
  

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

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

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

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

  	}

ecfd14df   David Mayerich   implemented D fie...
330
  

308a743c   David Mayerich   fixed class compa...
331
  	/// Cast to a vec3

8309b07a   David Mayerich   fixed some vec3 e...
332
333
  	operator stim::vec3<T>(){

  		stim::vec3<T> r;

308a743c   David Mayerich   fixed class compa...
334
335
336
337
338
339
  		size_t N = std::min<size_t>(size(), 3);

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

  			r[i] = at(i);

  		return r;

  	}

  

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

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

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

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

487a9b49   David Mayerich   added the ability...
344
345
  		resize(N);

  

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

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

ecfd14df   David Mayerich   implemented D fie...
348
349
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
350
351
  

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

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

57729e5b   David Mayerich   changed directory...
353
  

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

321ff17a   David Mayerich   Optimized the mat...
355
356
357
358
  

  		vec<T> r(N);

  

  		//negate the vector

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

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

a9275be5   David Mayerich   added vector fiel...
361
  

559d0fcb   David Mayerich   wave interactions...
362
363
  		return r;

  	}

a9275be5   David Mayerich   added vector fiel...
364
  

57729e5b   David Mayerich   changed directory...
365
  

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

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

57729e5b   David Mayerich   changed directory...
368
369
370
  	{

  		std::stringstream ss;

  

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

487a9b49   David Mayerich   added the ability...
372
  

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

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

57729e5b   David Mayerich   changed directory...
375
  		{

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

57729e5b   David Mayerich   changed directory...
377
378
379
380
381
382
383
384
  			if(i != N-1)

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

57729e5b   David Mayerich   changed directory...
385
386
387
388
389
  };

  

  

  }	//end namespace rts

  

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

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

57729e5b   David Mayerich   changed directory...
392
  {

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

57729e5b   David Mayerich   changed directory...
394
395
396
      return os;

  }

  

57729e5b   David Mayerich   changed directory...
397
  

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

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

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

57729e5b   David Mayerich   changed directory...
401
  {

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

57729e5b   David Mayerich   changed directory...
403
404
405
406
  

      return rhs * lhs;

  }

  

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