Skip to content

Commit

Permalink
Merge branch 'Vector_operators' into cgdna
Browse files Browse the repository at this point in the history
  • Loading branch information
fweik committed Oct 29, 2014
2 parents 1a2caf6 + 6480d68 commit 3d32e2f
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions src/core/Vector.hpp
Expand Up @@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cmath>

template<int n, typename Scalar>
class Vector {
Expand All @@ -25,7 +26,7 @@ class Vector {
public:
Vector() : d(new Scalar[n]) {};

Vector(Scalar *a) : d(new Scalar[n]) {
explicit Vector(Scalar *a) : d(new Scalar[n]) {
for (int i = 0; i < n; i++)
d[i] = a[i];
};
Expand All @@ -50,12 +51,43 @@ class Vector {
return d[i];
};

Scalar dot(Vector<n, Scalar> b) {
Scalar sum = 0.0;
inline Scalar dot(const Vector<n, Scalar> &b) const {
Scalar sum = 0;
for(int i = 0; i < n; i++)
sum += d[i]*b[i];
return sum;
};
};

inline Scalar norm2(void) const {
return dot(this);
};

inline Scalar norm(void) const {
return sqrt(norm2());
};

inline void normalize(void) {
Scalar N = norm();
if(norm() > 0) {
for(int i = 0; i < n; i++)
d[i] /= N;
}
};

inline void cross(const Vector<3, Scalar> &a, const Vector<3, Scalar> &b, Vector<3, Scalar> &c) const {
c[0] = a[1]*b[2] - a[2]*b[1];
c[1] = a[2]*b[0] - a[0]*b[2];
c[2] = a[0]*b[1] - a[1]*b[0];
return;
};
inline Vector<3, Scalar> cross(const Vector<3, Scalar> &a, const Vector<3, Scalar> &b) const {
Vector<3, Scalar> c;
cross(a,b,c);
return c;
};
inline Vector<3, Scalar> cross(const Vector<3, Scalar> &a) const {
return cross(this,a);
};
};

typedef Vector<3, double> Vector3d;
Expand Down

0 comments on commit 3d32e2f

Please sign in to comment.