Blame view

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

35a7195f   David Mayerich   added median spec...
77
  

4fa3f483   Pavel Govyadinov   Fixed the memory ...
78
79
80
  //	vec( vec3<T>& other){

  //		resize(3);							//resize the current vector to match the copy

  //		for(size_t i=0; i<3; i++){	//copy each element

35a7195f   David Mayerich   added median spec...
81
  //			at(i) = other[i];

4fa3f483   Pavel Govyadinov   Fixed the memory ...
82
83
  //		}

  //	}

57729e5b   David Mayerich   changed directory...
84
  

321ff17a   David Mayerich   Optimized the mat...
85
86
  	//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...
87
88
89
90
91
  	vec<T> push(T x)

  	{

  		push_back(x);

  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
92
  	

5eeaf941   Pavel Govyadinov   changer to the ba...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  	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...
114
115
  

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

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

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

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

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

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

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

ecfd14df   David Mayerich   implemented D fie...
122
123
124
125
  

  		return result;

  	}

  

ecfd14df   David Mayerich   implemented D fie...
126
  

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

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

57729e5b   David Mayerich   changed directory...
129
  	{

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

487a9b49   David Mayerich   added the ability...
131
  

57729e5b   David Mayerich   changed directory...
132
133
          //compute and return the vector length

          T sum_sq = (T)0;

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

57729e5b   David Mayerich   changed directory...
135
          {

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

57729e5b   David Mayerich   changed directory...
137
          }

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

57729e5b   David Mayerich   changed directory...
139
140
141
  

  	}

  

35a7195f   David Mayerich   added median spec...
142
143
144
145
146
147
148
149
150
151
  	

  	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...
152
  	/// 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...
153
  	vec<T> cart2sph() const

57729e5b   David Mayerich   changed directory...
154
  	{

321ff17a   David Mayerich   Optimized the mat...
155
  

57729e5b   David Mayerich   changed directory...
156
  

487a9b49   David Mayerich   added the ability...
157
158
159
160
161
162
163
164
  		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...
165
166
167
168
  

  		return sph;

  	}

  

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

57729e5b   David Mayerich   changed directory...
171
  	{

487a9b49   David Mayerich   added the ability...
172
173
174
175
  		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...
176
177
178
179
  

  		return cart;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
182
  	{

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

487a9b49   David Mayerich   added the ability...
184
185
186
  

          //compute and return the unit vector

          vec<T> result;

57729e5b   David Mayerich   changed directory...
187
188
189
190
191
  

          //compute the vector length

          T l = len();

  

          //normalize

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

57729e5b   David Mayerich   changed directory...
193
          {

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

57729e5b   David Mayerich   changed directory...
195
196
197
198
199
          }

  

          return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
202
  	{

321ff17a   David Mayerich   Optimized the mat...
203
204
  

  		vec<T> result(3);

57729e5b   David Mayerich   changed directory...
205
206
  

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

321ff17a   David Mayerich   Optimized the mat...
207
208
209
  		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...
210
211
212
213
  

  		return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
216
217
      {

          T result = (T)0;

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

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

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

57729e5b   David Mayerich   changed directory...
221
222
223
224
225
  

          return result;

  

      }

  

321ff17a   David Mayerich   Optimized the mat...
226
227
228
  	/// Arithmetic addition operator

  

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

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

57729e5b   David Mayerich   changed directory...
230
  	{

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

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

  

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

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

57729e5b   David Mayerich   changed directory...
236
  

559d0fcb   David Mayerich   wave interactions...
237
238
  		return result;

  	}

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

  	/// Arithmetic addition to a scalar

  

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

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

559d0fcb   David Mayerich   wave interactions...
244
  	{

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

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

  		vec<T> result(N);

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

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

559d0fcb   David Mayerich   wave interactions...
250
251
  

  		return result;

57729e5b   David Mayerich   changed directory...
252
  	}

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

  	/// Arithmetic subtraction operator

  

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

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

57729e5b   David Mayerich   changed directory...
258
  	{

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

321ff17a   David Mayerich   Optimized the mat...
260
261
262
  

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
265
266
267
  

          return result;

  	}

d4721000   Pavel Govyadinov   changes with debu...
268
269
270
271
272
  	/// 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...
273
  		size_t N = size();

d4721000   Pavel Govyadinov   changes with debu...
274
275
  

  		vec<T> result(N);

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

d4721000   Pavel Govyadinov   changes with debu...
277
278
279
280
  		    result[i] = at(i) - rhs;

  

  		return result;

  	}

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

  	/// Arithmetic scalar multiplication operator

  

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

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

57729e5b   David Mayerich   changed directory...
286
  	{

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

321ff17a   David Mayerich   Optimized the mat...
288
289
290
  

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
293
294
295
  

          return result;

  	}

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

  	/// Arithmetic scalar division operator

  

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

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

57729e5b   David Mayerich   changed directory...
301
  	{

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

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

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
308
309
310
  

          return result;

  	}

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

  	/// Multiplication by a scalar, followed by assignment

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

487a9b49   David Mayerich   added the ability...
314
  

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

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

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

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

  	}

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

  	/// Addition and assignment

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

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

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

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

d609550e   David Mayerich   fixed bug in plan...
326
327
  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
328
329
  

  	/// Assign a scalar to all values

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

487a9b49   David Mayerich   added the ability...
331
  

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

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

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

559d0fcb   David Mayerich   wave interactions...
335
336
  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
337
  

35a7195f   David Mayerich   added median spec...
338
339
340
341
342
343
344
345
346
347
  	/// Cast to a vec3

  	operator stim::vec3<T>(){

  		stim::vec3<T> r;

  		size_t N = min(size(), (size_t)3);

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

  			r[i] = at(i);

  		return r;

  	}

  

  

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

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

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

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

487a9b49   David Mayerich   added the ability...
352
353
  		resize(N);

  

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

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

ecfd14df   David Mayerich   implemented D fie...
356
357
  		return *this;

  	}

35a7195f   David Mayerich   added median spec...
358
359
360
361
362
363
364
365
366
367
  

  	/// Assign a vec = vec3

  	template<typename Y>

  	vec<T> & operator=(vec3<Y> rhs)

  	{

  		resize(3);

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

  			at(i) = rhs[i];

  		return *this;

  	}

321ff17a   David Mayerich   Optimized the mat...
368
369
  

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

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

57729e5b   David Mayerich   changed directory...
371
  

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

321ff17a   David Mayerich   Optimized the mat...
373
374
375
376
  

  		vec<T> r(N);

  

  		//negate the vector

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

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

a9275be5   David Mayerich   added vector fiel...
379
  

559d0fcb   David Mayerich   wave interactions...
380
381
  		return r;

  	}

a9275be5   David Mayerich   added vector fiel...
382
  

57729e5b   David Mayerich   changed directory...
383
  

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

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

57729e5b   David Mayerich   changed directory...
386
387
388
  	{

  		std::stringstream ss;

  

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

487a9b49   David Mayerich   added the ability...
390
  

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

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

57729e5b   David Mayerich   changed directory...
393
  		{

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

57729e5b   David Mayerich   changed directory...
395
396
397
398
399
400
401
402
  			if(i != N-1)

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

57729e5b   David Mayerich   changed directory...
403
404
405
406
407
  };

  

  

  }	//end namespace rts

  

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

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

57729e5b   David Mayerich   changed directory...
410
  {

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

57729e5b   David Mayerich   changed directory...
412
413
414
      return os;

  }

  

57729e5b   David Mayerich   changed directory...
415
  

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

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

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

57729e5b   David Mayerich   changed directory...
419
  {

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

57729e5b   David Mayerich   changed directory...
421
422
423
424
  

      return rhs * lhs;

  }

  

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