Authored by David Mayerich
1 parent 5eeaf941

### Optimized the mathvec.h class to remove excessive push operations

Showing 3 changed files with 92 additions and 57 deletions
stim/math/mathvec.h

 ... ... @@ -23,37 +23,34 @@ struct vec : public std::vector<T> 23 23 24 24 } 25 25 26 -// //efficiency constructors, makes construction easier for 1D-4D vectors 27 - vec(T rhs) 26 + /// Create a vector with a set dimension d 27 + vec(int d) 28 28 { 29 - resize(1, rhs); 30 -// cerr << " Created a vector " << endl; 31 -// //for(int i=0; i push(T x) 69 68 { 70 69 push_back(x); ... ... @@ -92,6 +91,8 @@ struct vec : public std::vector<T> 92 91 push_back(w); 93 92 return *this; 94 93 } 94 + 95 + /// Casting operator. Creates a new vector with a new type U. 95 96 template< typename U > 96 97 operator vec(){ 97 98 unsigned int N = size(); ... ... @@ -102,9 +103,8 @@ struct vec : public std::vector<T> 102 103 return result; 103 104 } 104 105 105 - //template 106 - //friend vec::operator vec(); 107 106 107 + /// computes the Euclidean length of the vector 108 108 T len() const 109 109 { 110 110 unsigned int N = size(); ... ... @@ -119,10 +119,10 @@ struct vec : public std::vector<T> 119 119 120 120 } 121 121 122 + /// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [0, 2*pi]) 122 123 vec cart2sph() const 123 124 { 124 - //convert the vector from cartesian to spherical coordinates 125 - //x, y, z -> r, theta, phi (where theta = 0 to 2*pi) 125 + 126 126 127 127 vec sph; 128 128 sph.push_back(std::sqrt(at(0)*at(0) + at(1)*at(1) + at(2)*at(2))); ... ... @@ -136,11 +136,9 @@ struct vec : public std::vector<T> 136 136 return sph; 137 137 } 138 138 139 + /// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi]) 139 140 vec sph2cart() const 140 141 { 141 - //convert the vector from cartesian to spherical coordinates 142 - //r, theta, phi -> x, y, z (where theta = 0 to 2*pi) 143 - 144 142 vec cart; 145 143 cart.push_back(at(0) * std::cos(at(1)) * std::sin(at(2))); 146 144 cart.push_back(at(0) * std::sin(at(1)) * std::sin(at(2))); ... ... @@ -149,6 +147,7 @@ struct vec : public std::vector<T> 149 147 return cart; 150 148 } 151 149 150 + /// Computes the normalized vector (where each coordinate is divided by the L2 norm) 152 151 vec norm() const 153 152 { 154 153 unsigned int N = size(); ... ... @@ -168,22 +167,27 @@ struct vec : public std::vector<T> 168 167 return result; 169 168 } 170 169 170 + /// Computes the cross product of a 3-dimensional vector 171 171 vec cross(const vec rhs) const 172 172 { 173 - vec result; 173 + if(size() != 3) 174 + std::cout<<"Error in stim::mathvec::cross() - the vector must have 3 components"< result(3); 174 177 175 178 //compute the cross product (only valid for 3D vectors) 176 - result.push_back(at(1) * rhs[2] - at(2) * rhs[1]); 177 - result.push_back(at(2) * rhs[0] - at(0) * rhs[2]); 178 - result.push_back(at(0) * rhs[1] - at(1) * rhs[0]); 179 + result[0] = (at(1) * rhs[2] - at(2) * rhs[1]); 180 + result[1] = (at(2) * rhs[0] - at(0) * rhs[2]); 181 + result[2] = (at(0) * rhs[1] - at(1) * rhs[0]); 179 182 180 183 return result; 181 184 } 182 185 186 + /// Compute the Euclidean inner (dot) product 183 187 T dot(vec rhs) const 184 188 { 185 189 T result = (T)0; 186 - unsigned int N = size(); 190 + unsigned int N = size(); 187 191 for(int i=0; i operator+(vec rhs) const 196 202 { 197 - vec result; 198 - 199 203 unsigned int N = size(); 204 + vec result(N); 205 + 200 206 for(int i=0; i operator+(T rhs) const 206 216 { 207 - vec result; 208 217 unsigned int N = size(); 218 + 219 + vec result(N); 209 220 for(int i=0; i operator-(vec rhs) const 215 230 { 216 - vec result; 217 - 218 231 unsigned int N = size(); 232 + 233 + vec result(N); 234 + 219 235 for(int i=0; i operator*(T rhs) const 225 245 { 226 - vec result; 227 - 228 246 unsigned int N = size(); 247 + 248 + vec result(N); 249 + 229 250 for(int i=0; i operator/(T rhs) const 235 260 { 236 - vec result; 237 - 238 261 unsigned int N = size(); 262 + 263 + vec result(N); 264 + 239 265 for(int i=0; i operator*=(T rhs){ 245 273 246 274 unsigned int N = size(); ... ... @@ -248,12 +276,16 @@ struct vec : public std::vector<T> 248 276 at(i) = at(i) * rhs; 249 277 return *this; 250 278 } 279 + 280 + /// Addition and assignment 251 281 vec operator+=(vec rhs){ 252 282 unsigned int N = size(); 253 283 for(int i=0; i & operator=(T rhs){ 258 290 259 291 unsigned int N = size(); ... ... @@ -262,6 +294,7 @@ struct vec : public std::vector<T> 262 294 return *this; 263 295 } 264 296 297 + /// Casting and assignment 265 298 template 266 299 vec & operator=(vec rhs){ 267 300 unsigned int N = rhs.size(); ... ... @@ -271,19 +304,23 @@ struct vec : public std::vector<T> 271 304 at(i) = rhs[i]; 272 305 return *this; 273 306 } 274 - //unary minus 307 + 308 + /// Unary minus (returns the negative of the vector) 275 309 vec operator-() const{ 276 - vec r; 277 310 278 - //negate the vector 279 311 unsigned int N = size(); 312 + 313 + vec r(N); 314 + 315 + //negate the vector 280 316 for(int i=0; i 320 357 stim::vec operator*(T lhs, stim::vec rhs) 321 358 { ... ... @@ -324,8 +361,4 @@ stim::vec<T> operator*(T lhs, stim::vec<T> rhs) 324 361 return rhs * lhs; 325 362 } 326 363 327 -//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7 328 -//template using rtsVector = rts::vector; 329 -//#endif 330 - 331 364 #endif ... ...
stim/math/spharmonics.h

 ... ... @@ -100,7 +100,7 @@ public: 100 100 /// @param sph_pts is a list of points in spherical coordinates (theta, phi) where theta = [0, 2pi] and phi = [0, pi] 101 101 /// @param l is the maximum degree of the spherical harmonic function 102 102 /// @param m is the maximum order 103 - void pdf(std::vector> sph_pts, unsigned int l, int m){ 103 + void pdf(std::vector > sph_pts, unsigned int l, int m){ 104 104 105 105 mcBegin( l, m ); //begin spherical harmonic sampling 106 106 ... ...
stim/visualization/gl_spharmonics.h

 ... ... @@ -14,6 +14,8 @@ template <typename T> 14 14 class gl_spharmonics : public spharmonics{ 15 15 16 16 protected: 17 + using spharmonics::C; 18 + using spharmonics::SH; 17 19 18 20 T* func; //stores the raw function data (samples at each point) 19 21 ... ... @@ -184,4 +186,4 @@ public: 184 186 185 187 186 188 187 -#endif 188 189 \ No newline at end of file 190 +#endif ... ...