Blame view

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

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

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

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

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

57729e5b   David Mayerich   changed directory...
12
  

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

57729e5b   David Mayerich   changed directory...
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

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

  		}

57729e5b   David Mayerich   changed directory...
77
  	}

35a7195f   David Mayerich   added median spec...
78
  

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

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

  //	}

57729e5b   David Mayerich   changed directory...
85
  

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

  	{

  		push_back(x);

  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
93
  	

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

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

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

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

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

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

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

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

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

  		return result;

  	}

  

ecfd14df   David Mayerich   implemented D fie...
127
  

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

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

57729e5b   David Mayerich   changed directory...
130
  	{

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

487a9b49   David Mayerich   added the ability...
132
  

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

          T sum_sq = (T)0;

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

57729e5b   David Mayerich   changed directory...
136
          {

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

57729e5b   David Mayerich   changed directory...
138
          }

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

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

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
155
  	{

321ff17a   David Mayerich   Optimized the mat...
156
  

57729e5b   David Mayerich   changed directory...
157
  

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

  		return sph;

  	}

  

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

57729e5b   David Mayerich   changed directory...
172
  	{

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

  		return cart;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
183
  	{

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

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

          //compute and return the unit vector

          vec<T> result;

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

          //compute the vector length

          T l = len();

  

          //normalize

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

57729e5b   David Mayerich   changed directory...
194
          {

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

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

  

          return result;

  	}

  

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

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

57729e5b   David Mayerich   changed directory...
203
  	{

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

  		vec<T> result(3);

57729e5b   David Mayerich   changed directory...
206
207
  

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

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

  		return result;

  	}

  

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

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

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

          T result = (T)0;

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

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

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

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

          return result;

  

      }

  

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

  

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

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

57729e5b   David Mayerich   changed directory...
231
  	{

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

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

  

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

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

57729e5b   David Mayerich   changed directory...
237
  

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

  	}

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

  	/// Arithmetic addition to a scalar

  

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

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

559d0fcb   David Mayerich   wave interactions...
245
  	{

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

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

  		vec<T> result(N);

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

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

559d0fcb   David Mayerich   wave interactions...
251
252
  

  		return result;

57729e5b   David Mayerich   changed directory...
253
  	}

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

  	/// Arithmetic subtraction operator

  

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

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

57729e5b   David Mayerich   changed directory...
259
  	{

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

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

          vec<T> result(N);

  

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

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

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

          return result;

  	}

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

d4721000   Pavel Govyadinov   changes with debu...
275
276
  

  		vec<T> result(N);

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

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

  

  		return result;

  	}

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

  	/// Arithmetic scalar multiplication operator

  

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

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

57729e5b   David Mayerich   changed directory...
287
  	{

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

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

          vec<T> result(N);

  

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

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

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

          return result;

  	}

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

  	/// Arithmetic scalar division operator

  

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

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

57729e5b   David Mayerich   changed directory...
302
  	{

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

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

          vec<T> result(N);

  

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

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

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

          return result;

  	}

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

  	/// Multiplication by a scalar, followed by assignment

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

487a9b49   David Mayerich   added the ability...
315
  

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

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

  	}

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

  	/// Addition and assignment

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

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

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

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

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

  	}

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

  	/// Assign a scalar to all values

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

487a9b49   David Mayerich   added the ability...
332
  

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

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

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

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

  	}

ecfd14df   David Mayerich   implemented D fie...
338
  

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

  	operator stim::vec3<T>(){

  		stim::vec3<T> r;

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

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

  			r[i] = at(i);

  		return r;

  	}

  

  

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

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

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

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

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

  

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

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

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

  	}

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

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

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

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

57729e5b   David Mayerich   changed directory...
372
  

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

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

  		vec<T> r(N);

  

  		//negate the vector

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

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

a9275be5   David Mayerich   added vector fiel...
380
  

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

  	}

a9275be5   David Mayerich   added vector fiel...
383
  

57729e5b   David Mayerich   changed directory...
384
  

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

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

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

  		std::stringstream ss;

  

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

487a9b49   David Mayerich   added the ability...
391
  

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

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

57729e5b   David Mayerich   changed directory...
394
  		{

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

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

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

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

  

  

  }	//end namespace rts

  

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

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

57729e5b   David Mayerich   changed directory...
411
  {

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

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

  }

  

57729e5b   David Mayerich   changed directory...
416
  

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

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

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

57729e5b   David Mayerich   changed directory...
420
  {

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

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

      return rhs * lhs;

  }

  

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