From 321ff17a4937ffb44195bfd15444c3956ba80f4b Mon Sep 17 00:00:00 2001 From: David Mayerich Date: Wed, 15 Jul 2015 15:27:11 -0500 Subject: [PATCH] Optimized the mathvec.h class to remove excessive push operations --- stim/math/mathvec.h | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------- stim/math/spharmonics.h | 2 +- stim/visualization/gl_spharmonics.h | 4 +++- 3 files changed, 92 insertions(+), 57 deletions(-) diff --git a/stim/math/mathvec.h b/stim/math/mathvec.h index d8523dc..0a59851 100644 --- a/stim/math/mathvec.h +++ b/stim/math/mathvec.h @@ -23,37 +23,34 @@ struct vec : public std::vector } -// //efficiency constructors, makes construction easier for 1D-4D vectors - vec(T rhs) + /// Create a vector with a set dimension d + vec(int d) { - resize(1, rhs); -// cerr << " Created a vector " << endl; -// //for(int i=0; i push_back(other[i]); } + //I'm not sure what these were doing here. + //Keep them now, we'll worry about it later. vec push(T x) { push_back(x); @@ -92,6 +91,8 @@ struct vec : public std::vector push_back(w); return *this; } + + /// Casting operator. Creates a new vector with a new type U. template< typename U > operator vec(){ unsigned int N = size(); @@ -102,9 +103,8 @@ struct vec : public std::vector return result; } - //template - //friend vec::operator vec(); + /// computes the Euclidean length of the vector T len() const { unsigned int N = size(); @@ -119,10 +119,10 @@ struct vec : public std::vector } + /// Convert the vector from cartesian to spherical coordinates (x, y, z -> r, theta, phi where theta = [0, 2*pi]) vec cart2sph() const { - //convert the vector from cartesian to spherical coordinates - //x, y, z -> r, theta, phi (where theta = 0 to 2*pi) + vec sph; 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 return sph; } + /// Convert the vector from cartesian to spherical coordinates (r, theta, phi -> x, y, z where theta = [0, 2*pi]) vec sph2cart() const { - //convert the vector from cartesian to spherical coordinates - //r, theta, phi -> x, y, z (where theta = 0 to 2*pi) - vec 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))); @@ -149,6 +147,7 @@ struct vec : public std::vector return cart; } + /// Computes the normalized vector (where each coordinate is divided by the L2 norm) vec norm() const { unsigned int N = size(); @@ -168,22 +167,27 @@ struct vec : public std::vector return result; } + /// Computes the cross product of a 3-dimensional vector vec cross(const vec rhs) const { - vec result; + if(size() != 3) + std::cout<<"Error in stim::mathvec::cross() - the vector must have 3 components"< result(3); //compute the cross product (only valid for 3D vectors) - 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]); + 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]); return result; } + /// Compute the Euclidean inner (dot) product T dot(vec rhs) const { T result = (T)0; - unsigned int N = size(); + unsigned int N = size(); for(int i=0; i } - //arithmetic + /// Arithmetic addition operator + + /// @param rhs is the right-hand-side operator for the addition vec operator+(vec rhs) const { - vec result; - unsigned int N = size(); + vec result(N); + for(int i=0; i operator+(T rhs) const { - vec result; unsigned int N = size(); + + vec result(N); for(int i=0; i operator-(vec rhs) const { - vec result; - unsigned int N = size(); + + vec result(N); + for(int i=0; i operator*(T rhs) const { - vec result; - unsigned int N = size(); + + vec result(N); + for(int i=0; i operator/(T rhs) const { - vec result; - unsigned int N = size(); + + vec result(N); + for(int i=0; i operator*=(T rhs){ unsigned int N = size(); @@ -248,12 +276,16 @@ struct vec : public std::vector at(i) = at(i) * rhs; return *this; } + + /// Addition and assignment vec operator+=(vec rhs){ unsigned int N = size(); for(int i=0; i & operator=(T rhs){ unsigned int N = size(); @@ -262,6 +294,7 @@ struct vec : public std::vector return *this; } + /// Casting and assignment template vec & operator=(vec rhs){ unsigned int N = rhs.size(); @@ -271,19 +304,23 @@ struct vec : public std::vector at(i) = rhs[i]; return *this; } - //unary minus + + /// Unary minus (returns the negative of the vector) vec operator-() const{ - vec r; - //negate the vector unsigned int N = size(); + + vec r(N); + + //negate the vector for(int i=0; i v) } - +/// Multiply a vector by a constant when the vector is on the right hand side template stim::vec operator*(T lhs, stim::vec rhs) { @@ -324,8 +361,4 @@ stim::vec operator*(T lhs, stim::vec rhs) return rhs * lhs; } -//#if __GNUC__ > 3 && __GNUC_MINOR__ > 7 -//template using rtsVector = rts::vector; -//#endif - #endif diff --git a/stim/math/spharmonics.h b/stim/math/spharmonics.h index ce9626c..137cb0c 100644 --- a/stim/math/spharmonics.h +++ b/stim/math/spharmonics.h @@ -100,7 +100,7 @@ public: /// @param sph_pts is a list of points in spherical coordinates (theta, phi) where theta = [0, 2pi] and phi = [0, pi] /// @param l is the maximum degree of the spherical harmonic function /// @param m is the maximum order - void pdf(std::vector> sph_pts, unsigned int l, int m){ + void pdf(std::vector > sph_pts, unsigned int l, int m){ mcBegin( l, m ); //begin spherical harmonic sampling diff --git a/stim/visualization/gl_spharmonics.h b/stim/visualization/gl_spharmonics.h index b58be1f..1ff0395 100644 --- a/stim/visualization/gl_spharmonics.h +++ b/stim/visualization/gl_spharmonics.h @@ -14,6 +14,8 @@ template class gl_spharmonics : public spharmonics{ protected: + using spharmonics::C; + using spharmonics::SH; T* func; //stores the raw function data (samples at each point) @@ -184,4 +186,4 @@ public: -#endif \ No newline at end of file +#endif -- libgit2 0.21.4