Blame view

stim/math/vector.h 7.88 KB
b28f312d   David Mayerich   added code for ev...
1
2
  #ifndef TIRA_VECTOR_H

  #define TIRA_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>

fcd2eb7c   David Mayerich   added support for...
8
  #include <algorithm>

14634fca   David Mayerich   edits to update s...
9
  #include <complex>

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

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

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

57729e5b   David Mayerich   changed directory...
13
  

b28f312d   David Mayerich   added code for ev...
14
  namespace tira

57729e5b   David Mayerich   changed directory...
15
16
  {

  

487a9b49   David Mayerich   added the ability...
17
18
  template <class T>

  struct vec : public std::vector<T>

57729e5b   David Mayerich   changed directory...
19
  {

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

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

57729e5b   David Mayerich   changed directory...
26
  

57729e5b   David Mayerich   changed directory...
27
28
  	}

  

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

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

a9275be5   David Mayerich   added vector fiel...
31
  	{

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

a9275be5   David Mayerich   added vector fiel...
33
  	}

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

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

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

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

a9275be5   David Mayerich   added vector fiel...
38
  	{

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

  		at(0) = x;

  		at(1) = y;

a9275be5   David Mayerich   added vector fiel...
42
  	}

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

57729e5b   David Mayerich   changed directory...
44
  	{

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

  		at(0) = x;

  		at(1) = y;

  		at(2) = z;

a9275be5   David Mayerich   added vector fiel...
49
  	}

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

a9275be5   David Mayerich   added vector fiel...
51
  	{

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

  		at(0) = x;

  		at(1) = y;

  		at(2) = z;

  		at(3) = w;

57729e5b   David Mayerich   changed directory...
57
58
  	}

  

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

  		std::stringstream ss(str);

  

  		T c;

  		while(ss >> c){

  			push_back(c);

  		}

  

  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
69
70
  	

  	

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

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

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

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

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

8309b07a   David Mayerich   fixed some vec3 e...
76
77
  			at(i) = other[i];

  		}

57729e5b   David Mayerich   changed directory...
78
  	}

35a7195f   David Mayerich   added median spec...
79
  

4fa3f483   Pavel Govyadinov   Fixed the memory ...
80
81
82
  //	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...
83
  //			at(i) = other[i];

4fa3f483   Pavel Govyadinov   Fixed the memory ...
84
85
  //		}

  //	}

57729e5b   David Mayerich   changed directory...
86
  

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

  	{

  		push_back(x);

  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
94
  	

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

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

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

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

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

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

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

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

ecfd14df   David Mayerich   implemented D fie...
124
125
126
127
  

  		return result;

  	}

  

ecfd14df   David Mayerich   implemented D fie...
128
  

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

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

57729e5b   David Mayerich   changed directory...
131
  	{

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

487a9b49   David Mayerich   added the ability...
133
  

57729e5b   David Mayerich   changed directory...
134
135
          //compute and return the vector length

          T sum_sq = (T)0;

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

57729e5b   David Mayerich   changed directory...
137
          {

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

57729e5b   David Mayerich   changed directory...
139
          }

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

57729e5b   David Mayerich   changed directory...
141
142
143
  

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
156
  	{

321ff17a   David Mayerich   Optimized the mat...
157
  

57729e5b   David Mayerich   changed directory...
158
  

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

  		return sph;

  	}

  

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

57729e5b   David Mayerich   changed directory...
173
  	{

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

  		return cart;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
184
  	{

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

487a9b49   David Mayerich   added the ability...
186
187
188
  

          //compute and return the unit vector

          vec<T> result;

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

          //compute the vector length

          T l = len();

  

          //normalize

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

57729e5b   David Mayerich   changed directory...
195
          {

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

57729e5b   David Mayerich   changed directory...
197
198
199
200
201
          }

  

          return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
204
  	{

321ff17a   David Mayerich   Optimized the mat...
205
206
  

  		vec<T> result(3);

57729e5b   David Mayerich   changed directory...
207
208
  

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

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

  		return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
218
219
      {

          T result = (T)0;

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

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

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

57729e5b   David Mayerich   changed directory...
223
224
225
226
227
  

          return result;

  

      }

  

321ff17a   David Mayerich   Optimized the mat...
228
229
230
  	/// Arithmetic addition operator

  

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

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

57729e5b   David Mayerich   changed directory...
232
  	{

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

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

  

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

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

57729e5b   David Mayerich   changed directory...
238
  

559d0fcb   David Mayerich   wave interactions...
239
240
  		return result;

  	}

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

  	/// Arithmetic addition to a scalar

  

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

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

559d0fcb   David Mayerich   wave interactions...
246
  	{

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

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

  		vec<T> result(N);

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

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

559d0fcb   David Mayerich   wave interactions...
252
253
  

  		return result;

57729e5b   David Mayerich   changed directory...
254
  	}

321ff17a   David Mayerich   Optimized the mat...
255
256
257
258
  

  	/// Arithmetic subtraction operator

  

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

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

57729e5b   David Mayerich   changed directory...
260
  	{

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

321ff17a   David Mayerich   Optimized the mat...
262
263
264
  

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
267
268
269
  

          return result;

  	}

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

d4721000   Pavel Govyadinov   changes with debu...
276
277
  

  		vec<T> result(N);

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

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

  

  		return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
283
284
285
286
  

  	/// Arithmetic scalar multiplication operator

  

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

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

57729e5b   David Mayerich   changed directory...
288
  	{

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

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

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
295
296
297
  

          return result;

  	}

321ff17a   David Mayerich   Optimized the mat...
298
299
300
301
  

  	/// Arithmetic scalar division operator

  

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

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

57729e5b   David Mayerich   changed directory...
303
  	{

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

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

          vec<T> result(N);

  

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

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

57729e5b   David Mayerich   changed directory...
310
311
312
  

          return result;

  	}

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

  	/// Multiplication by a scalar, followed by assignment

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

487a9b49   David Mayerich   added the ability...
316
  

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) = at(i) * rhs;

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

  	}

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

  	/// Addition and assignment

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

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

d609550e   David Mayerich   fixed bug in plan...
328
329
  		return *this;

  	}

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

  	/// Assign a scalar to all values

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

487a9b49   David Mayerich   added the ability...
333
  

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

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

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

559d0fcb   David Mayerich   wave interactions...
337
338
  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
339
  

35a7195f   David Mayerich   added median spec...
340
  	/// Cast to a vec3

b28f312d   David Mayerich   added code for ev...
341
342
  	operator tira::vec3<T>(){

  		tira::vec3<T> r;

87d0a1d2   David Mayerich   minor changes for...
343
  		size_t N = std::min(size(), (size_t)3);

35a7195f   David Mayerich   added median spec...
344
345
346
347
348
349
  		for(size_t i = 0; i < N; i++)

  			r[i] = at(i);

  		return r;

  	}

  

  

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

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

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

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

487a9b49   David Mayerich   added the ability...
354
355
  		resize(N);

  

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

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

ecfd14df   David Mayerich   implemented D fie...
358
359
  		return *this;

  	}

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

  	/// 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...
370
371
  

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

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

57729e5b   David Mayerich   changed directory...
373
  

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

321ff17a   David Mayerich   Optimized the mat...
375
376
377
378
  

  		vec<T> r(N);

  

  		//negate the vector

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

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

a9275be5   David Mayerich   added vector fiel...
381
  

559d0fcb   David Mayerich   wave interactions...
382
383
  		return r;

  	}

a9275be5   David Mayerich   added vector fiel...
384
  

57729e5b   David Mayerich   changed directory...
385
  

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

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

57729e5b   David Mayerich   changed directory...
388
389
390
  	{

  		std::stringstream ss;

  

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

487a9b49   David Mayerich   added the ability...
392
  

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

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

57729e5b   David Mayerich   changed directory...
395
  		{

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

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

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

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

  

  

b28f312d   David Mayerich   added code for ev...
408
  }	//end namespace tira

57729e5b   David Mayerich   changed directory...
409
  

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

b28f312d   David Mayerich   added code for ev...
411
  std::ostream& operator<<(std::ostream& os, tira::vec<T> v)

57729e5b   David Mayerich   changed directory...
412
  {

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

57729e5b   David Mayerich   changed directory...
414
415
416
      return os;

  }

  

57729e5b   David Mayerich   changed directory...
417
  

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

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

b28f312d   David Mayerich   added code for ev...
420
  tira::vec<T> operator*(T lhs, tira::vec<T> rhs)

57729e5b   David Mayerich   changed directory...
421
  {

b28f312d   David Mayerich   added code for ev...
422
  	tira::vec<T> r;

57729e5b   David Mayerich   changed directory...
423
424
425
426
  

      return rhs * lhs;

  }

  

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