diff --git a/quaternion.cc b/quaternion.cc index caa92e0..1f70de8 100644 --- a/quaternion.cc +++ b/quaternion.cc @@ -17,5 +17,19 @@ */ #include "quaternion.h" +#include + +template<> +double Quaternion::norm(void) const +{ +return sqrt(this->normsqr()); +} + +template<> +float Quaternion::norm(void) const +{ +return sqrtf(this->normsqr()); +} + diff --git a/quaternion.h b/quaternion.h index 12a0af6..280bece 100644 --- a/quaternion.h +++ b/quaternion.h @@ -19,6 +19,7 @@ #define _QUATERNION_H_ #include +#include template class Quaternion @@ -31,6 +32,10 @@ public: Quaternion(const T x, const T u=0, const T v=0, const T w=0) {q[0]=x; q[1]=u; q[2]=v; q[3]=w;}; //quaternion from real //compiler generates default copy constructor and assignment operator + + //formal indexing + const T operator[](const int i) const {return this->q[i];}; + T operator[](const int i) {return this->q[i];}; //operations of quaternions with scalars Quaternion& operator=(const T x) {q[0]=x; memset(&q[1],0,3*sizeof(T)); return *this;}; //quaternion from real @@ -58,7 +63,14 @@ public: this->q[0]*rhs.q[3]+this->q[3]*rhs.q[0]+this->q[1]*rhs.q[2]-this->q[2]*rhs.q[1] ); }; + Quaternion& conjugateme(void) {q[1] = -q[1]; q[2] = -q[2]; q[3] = -q[3]; return *this;} + Quaternion conjugate(void) const {return Quaternion(*this).conjugateme();} + T normsqr(void) const {return q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3];}; + T norm(void) const; + Quaternion inverse(void) const {return Quaternion(*this).conjugateme()/this->normsqr();}; + const Quaternion operator/(const Quaternion &rhs) const {return *this * rhs.inverse();}; }; + //stream I/O template