Blame view

stim/math/mathvec.h 5.71 KB
57729e5b   David Mayerich   changed directory...
1
2
3
4
5
6
  #ifndef RTS_VECTOR_H

  #define RTS_VECTOR_H

  

  #include <iostream>

  #include <cmath>

  #include <sstream>

487a9b49   David Mayerich   added the ability...
7
  #include <vector>

87da9adc   David Mayerich   fixed issues with...
8
  #include "../cuda/callable.h"

57729e5b   David Mayerich   changed directory...
9
  

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

57729e5b   David Mayerich   changed directory...
11
12
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
22
  	using std::vector<T>::size;

  	using std::vector<T>::at;

  	using std::vector<T>::resize;

  	using std::vector<T>::push_back;

  	vec(){

57729e5b   David Mayerich   changed directory...
23
  

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

  

5eeaf941   Pavel Govyadinov   changer to the ba...
26
27
  //	//efficiency constructors, makes construction easier for 1D-4D vectors

  	vec(T rhs)

a9275be5   David Mayerich   added vector fiel...
28
  	{

487a9b49   David Mayerich   added the ability...
29
  		resize(1, rhs);

5eeaf941   Pavel Govyadinov   changer to the ba...
30
31
32
  //		cerr << " Created a vector " << endl;

  //		//for(int i=0; i<N; i++)

  //		//	v[i] = rhs;

a9275be5   David Mayerich   added vector fiel...
33
  	}

5eeaf941   Pavel Govyadinov   changer to the ba...
34
35
36
37
38
39
40
  

  //	vec(int s)

  //	{

  //		resize(s,0);

  //	}	

  

  	vec(T x, T y)

a9275be5   David Mayerich   added vector fiel...
41
  	{

487a9b49   David Mayerich   added the ability...
42
43
  		push_back(x);

  		push_back(y);

a9275be5   David Mayerich   added vector fiel...
44
  	}

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

57729e5b   David Mayerich   changed directory...
46
  	{

487a9b49   David Mayerich   added the ability...
47
48
49
  		push_back(x);

  		push_back(y);

  		push_back(z);

a9275be5   David Mayerich   added vector fiel...
50
  	}

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

a9275be5   David Mayerich   added vector fiel...
52
  	{

487a9b49   David Mayerich   added the ability...
53
54
55
56
  		push_back(x);

  		push_back(y);

  		push_back(z);

  		push_back(w);

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

  

5eeaf941   Pavel Govyadinov   changer to the ba...
59
60
  	

  	

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

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

487a9b49   David Mayerich   added the ability...
63
  		unsigned int N = other.size();

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

487a9b49   David Mayerich   added the ability...
65
  			push_back(other[i]);

57729e5b   David Mayerich   changed directory...
66
67
  	}

  

5eeaf941   Pavel Govyadinov   changer to the ba...
68
69
70
71
72
  	vec<T> push(T x)

  	{

  		push_back(x);

  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
73
  	

5eeaf941   Pavel Govyadinov   changer to the ba...
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  	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;

  	}

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

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

487a9b49   David Mayerich   added the ability...
97
98
  		unsigned int N = size();

  		vec<U> result;

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

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

ecfd14df   David Mayerich   implemented D fie...
101
102
103
104
105
106
107
  

  		return result;

  	}

  

  	//template<class U>

  	//friend vec<U, N>::operator vec<T, N>();

  

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

57729e5b   David Mayerich   changed directory...
109
  	{

487a9b49   David Mayerich   added the ability...
110
111
  		unsigned int N = size();

  

57729e5b   David Mayerich   changed directory...
112
113
114
115
          //compute and return the vector length

          T sum_sq = (T)0;

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

          {

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

57729e5b   David Mayerich   changed directory...
117
          }

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

57729e5b   David Mayerich   changed directory...
119
120
121
  

  	}

  

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

57729e5b   David Mayerich   changed directory...
123
124
125
126
  	{

  		//convert the vector from cartesian to spherical coordinates

  		//x, y, z -> r, theta, phi (where theta = 0 to 2*pi)

  

487a9b49   David Mayerich   added the ability...
127
128
129
130
131
132
133
134
  		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...
135
136
137
138
  

  		return sph;

  	}

  

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

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

  		//convert the vector from cartesian to spherical coordinates

  		//r, theta, phi -> x, y, z (where theta = 0 to 2*pi)

  

487a9b49   David Mayerich   added the ability...
144
145
146
147
  		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...
148
149
150
151
  

  		return cart;

  	}

  

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

57729e5b   David Mayerich   changed directory...
153
  	{

487a9b49   David Mayerich   added the ability...
154
155
156
157
  		unsigned int N = size();

  

          //compute and return the unit vector

          vec<T> result;

57729e5b   David Mayerich   changed directory...
158
159
160
161
162
163
164
  

          //compute the vector length

          T l = len();

  

          //normalize

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

          {

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

57729e5b   David Mayerich   changed directory...
166
167
168
169
170
          }

  

          return result;

  	}

  

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

57729e5b   David Mayerich   changed directory...
172
  	{

487a9b49   David Mayerich   added the ability...
173
  		vec<T> result;

57729e5b   David Mayerich   changed directory...
174
175
  

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

487a9b49   David Mayerich   added the ability...
176
177
178
  		result.push_back(at(1) * rhs[2] - at(2) * rhs[1]);

  		result.push_back(at(2) * rhs[0] - at(0) * rhs[2]);

  		result.push_back(at(0) * rhs[1] - at(1) * rhs[0]);

57729e5b   David Mayerich   changed directory...
179
180
181
182
  

  		return result;

  	}

  

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

57729e5b   David Mayerich   changed directory...
184
185
      {

          T result = (T)0;

5eeaf941   Pavel Govyadinov   changer to the ba...
186
  	unsigned int N = size();

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

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

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

          return result;

  

      }

  

  	//arithmetic

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

57729e5b   David Mayerich   changed directory...
196
  	{

487a9b49   David Mayerich   added the ability...
197
  		vec<T> result;

57729e5b   David Mayerich   changed directory...
198
  

487a9b49   David Mayerich   added the ability...
199
  		unsigned int N = size();

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

487a9b49   David Mayerich   added the ability...
201
  		    result.push_back(at(i) + rhs[i]);

57729e5b   David Mayerich   changed directory...
202
  

559d0fcb   David Mayerich   wave interactions...
203
204
  		return result;

  	}

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

559d0fcb   David Mayerich   wave interactions...
206
  	{

487a9b49   David Mayerich   added the ability...
207
  		vec<T> result;

5eeaf941   Pavel Govyadinov   changer to the ba...
208
  		unsigned int N = size();

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

487a9b49   David Mayerich   added the ability...
210
  		    result.push_back(at(i) + rhs);

559d0fcb   David Mayerich   wave interactions...
211
212
  

  		return result;

57729e5b   David Mayerich   changed directory...
213
  	}

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

57729e5b   David Mayerich   changed directory...
215
  	{

487a9b49   David Mayerich   added the ability...
216
          vec<T> result;

57729e5b   David Mayerich   changed directory...
217
  

487a9b49   David Mayerich   added the ability...
218
  		unsigned int N = size();

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

487a9b49   David Mayerich   added the ability...
220
              result.push_back(at(i) - rhs[i]);

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

          return result;

  	}

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

57729e5b   David Mayerich   changed directory...
225
  	{

487a9b49   David Mayerich   added the ability...
226
          vec<T> result;

57729e5b   David Mayerich   changed directory...
227
  

487a9b49   David Mayerich   added the ability...
228
  		unsigned int N = size();

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

487a9b49   David Mayerich   added the ability...
230
              result.push_back(at(i) * rhs);

57729e5b   David Mayerich   changed directory...
231
232
233
  

          return result;

  	}

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

57729e5b   David Mayerich   changed directory...
235
  	{

487a9b49   David Mayerich   added the ability...
236
          vec<T> result;

57729e5b   David Mayerich   changed directory...
237
  

487a9b49   David Mayerich   added the ability...
238
  		unsigned int N = size();

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

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

57729e5b   David Mayerich   changed directory...
241
242
243
  

          return result;

  	}

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

487a9b49   David Mayerich   added the ability...
245
246
247
248
249
250
  

  		unsigned int N = size();

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

  			at(i) = at(i) * rhs;

  		return *this;

  	}

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

487a9b49   David Mayerich   added the ability...
252
  		unsigned int N = size();

d609550e   David Mayerich   fixed bug in plan...
253
  		for(int i=0; i<N; i++)

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

d609550e   David Mayerich   fixed bug in plan...
255
256
  		return *this;

  	}

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

487a9b49   David Mayerich   added the ability...
258
259
  

  		unsigned int N = size();

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

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

559d0fcb   David Mayerich   wave interactions...
262
263
  		return *this;

  	}

ecfd14df   David Mayerich   implemented D fie...
264
265
  

  	template<typename Y>

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

487a9b49   David Mayerich   added the ability...
267
268
269
  		unsigned int N = rhs.size();

  		resize(N);

  

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

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

ecfd14df   David Mayerich   implemented D fie...
272
273
  		return *this;

  	}

559d0fcb   David Mayerich   wave interactions...
274
  	//unary minus

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

487a9b49   David Mayerich   added the ability...
276
  		vec<T> r;

57729e5b   David Mayerich   changed directory...
277
  

559d0fcb   David Mayerich   wave interactions...
278
  		//negate the vector

487a9b49   David Mayerich   added the ability...
279
  		unsigned int N = size();

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

487a9b49   David Mayerich   added the ability...
281
  		    r.push_back(-at(i));

a9275be5   David Mayerich   added vector fiel...
282
  

559d0fcb   David Mayerich   wave interactions...
283
284
  		return r;

  	}

a9275be5   David Mayerich   added vector fiel...
285
  

57729e5b   David Mayerich   changed directory...
286
  

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

57729e5b   David Mayerich   changed directory...
288
289
290
  	{

  		std::stringstream ss;

  

487a9b49   David Mayerich   added the ability...
291
292
  		unsigned int N = size();

  

57729e5b   David Mayerich   changed directory...
293
294
295
  		ss<<"[";

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

  		{

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

57729e5b   David Mayerich   changed directory...
297
298
299
300
301
302
303
304
  			if(i != N-1)

  				ss<<", ";

  		}

  		ss<<"]";

  

  		return ss.str();

  	}

  

57729e5b   David Mayerich   changed directory...
305
306
307
308
309
  };

  

  

  }	//end namespace rts

  

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

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

57729e5b   David Mayerich   changed directory...
312
  {

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

57729e5b   David Mayerich   changed directory...
314
315
316
      return os;

  }

  

57729e5b   David Mayerich   changed directory...
317
  

57729e5b   David Mayerich   changed directory...
318
  

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

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

57729e5b   David Mayerich   changed directory...
321
  {

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

57729e5b   David Mayerich   changed directory...
323
324
325
326
327
328
329
330
331
  

      return rhs * lhs;

  }

  

  //#if __GNUC__ > 3 && __GNUC_MINOR__ > 7

  //template<class T, int N> using rtsVector = rts::vector<T, N>;

  //#endif

  

  #endif