sl@0: // boost quaternion.hpp header file sl@0: sl@0: // (C) Copyright Hubert Holin 2001. sl@0: // Distributed under the Boost Software License, Version 1.0. (See sl@0: // accompanying file LICENSE_1_0.txt or copy at sl@0: // http://www.boost.org/LICENSE_1_0.txt) sl@0: sl@0: // See http://www.boost.org for updates, documentation, and revision history. sl@0: sl@0: #ifndef BOOST_QUATERNION_HPP sl@0: #define BOOST_QUATERNION_HPP sl@0: sl@0: sl@0: #include sl@0: #include // for the "<<" and ">>" operators sl@0: #include // for the "<<" operator sl@0: sl@0: #include // for BOOST_NO_STD_LOCALE sl@0: #include sl@0: #ifndef BOOST_NO_STD_LOCALE sl@0: #include // for the "<<" operator sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: #include sl@0: sl@0: sl@0: sl@0: #include // for the Sinus cardinal sl@0: #include // for the Hyperbolic Sinus cardinal sl@0: sl@0: sl@0: namespace boost sl@0: { sl@0: namespace math sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: // gcc 2.95.x uses expression templates for valarray calculations, but sl@0: // the result is not conforming. We need BOOST_GET_VALARRAY to get an sl@0: // actual valarray result when we need to call a member function sl@0: #define BOOST_GET_VALARRAY(T,x) ::std::valarray(x) sl@0: // gcc 2.95.x has an "std::ios" class that is similar to sl@0: // "std::ios_base", so we just use a #define sl@0: #define BOOST_IOS_BASE ::std::ios sl@0: // gcc 2.x ignores function scope using declarations, sl@0: // put them in the scope of the enclosing namespace instead: sl@0: using ::std::valarray; sl@0: using ::std::sqrt; sl@0: using ::std::cos; sl@0: using ::std::sin; sl@0: using ::std::exp; sl@0: using ::std::cosh; sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: sl@0: #define BOOST_QUATERNION_ACCESSOR_GENERATOR(type) \ sl@0: type real() const \ sl@0: { \ sl@0: return(a); \ sl@0: } \ sl@0: \ sl@0: quaternion unreal() const \ sl@0: { \ sl@0: return(quaternion(static_cast(0),b,c,d)); \ sl@0: } \ sl@0: \ sl@0: type R_component_1() const \ sl@0: { \ sl@0: return(a); \ sl@0: } \ sl@0: \ sl@0: type R_component_2() const \ sl@0: { \ sl@0: return(b); \ sl@0: } \ sl@0: \ sl@0: type R_component_3() const \ sl@0: { \ sl@0: return(c); \ sl@0: } \ sl@0: \ sl@0: type R_component_4() const \ sl@0: { \ sl@0: return(d); \ sl@0: } \ sl@0: \ sl@0: ::std::complex C_component_1() const \ sl@0: { \ sl@0: return(::std::complex(a,b)); \ sl@0: } \ sl@0: \ sl@0: ::std::complex C_component_2() const \ sl@0: { \ sl@0: return(::std::complex(c,d)); \ sl@0: } sl@0: sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(type) \ sl@0: template \ sl@0: quaternion & operator = (quaternion const & a_affecter) \ sl@0: { \ sl@0: a = static_cast(a_affecter.R_component_1()); \ sl@0: b = static_cast(a_affecter.R_component_2()); \ sl@0: c = static_cast(a_affecter.R_component_3()); \ sl@0: d = static_cast(a_affecter.R_component_4()); \ sl@0: \ sl@0: return(*this); \ sl@0: } \ sl@0: \ sl@0: quaternion & operator = (quaternion const & a_affecter) \ sl@0: { \ sl@0: a = a_affecter.a; \ sl@0: b = a_affecter.b; \ sl@0: c = a_affecter.c; \ sl@0: d = a_affecter.d; \ sl@0: \ sl@0: return(*this); \ sl@0: } \ sl@0: \ sl@0: quaternion & operator = (type const & a_affecter) \ sl@0: { \ sl@0: a = a_affecter; \ sl@0: \ sl@0: b = c = d = static_cast(0); \ sl@0: \ sl@0: return(*this); \ sl@0: } \ sl@0: \ sl@0: quaternion & operator = (::std::complex const & a_affecter) \ sl@0: { \ sl@0: a = a_affecter.real(); \ sl@0: b = a_affecter.imag(); \ sl@0: \ sl@0: c = d = static_cast(0); \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_DATA_GENERATOR(type) \ sl@0: type a; \ sl@0: type b; \ sl@0: type c; \ sl@0: type d; sl@0: sl@0: sl@0: template sl@0: class quaternion sl@0: { sl@0: public: sl@0: sl@0: typedef T value_type; sl@0: sl@0: sl@0: // constructor for H seen as R^4 sl@0: // (also default constructor) sl@0: sl@0: explicit quaternion( T const & requested_a = T(), sl@0: T const & requested_b = T(), sl@0: T const & requested_c = T(), sl@0: T const & requested_d = T()) sl@0: : a(requested_a), sl@0: b(requested_b), sl@0: c(requested_c), sl@0: d(requested_d) sl@0: { sl@0: // nothing to do! sl@0: } sl@0: sl@0: sl@0: // constructor for H seen as C^2 sl@0: sl@0: explicit quaternion( ::std::complex const & z0, sl@0: ::std::complex const & z1 = ::std::complex()) sl@0: : a(z0.real()), sl@0: b(z0.imag()), sl@0: c(z1.real()), sl@0: d(z1.imag()) sl@0: { sl@0: // nothing to do! sl@0: } sl@0: sl@0: sl@0: // UNtemplated copy constructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: sl@0: // templated copy constructor sl@0: sl@0: template sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: : a(static_cast(a_recopier.R_component_1())), sl@0: b(static_cast(a_recopier.R_component_2())), sl@0: c(static_cast(a_recopier.R_component_3())), sl@0: d(static_cast(a_recopier.R_component_4())) sl@0: { sl@0: // nothing to do! sl@0: } sl@0: sl@0: sl@0: // destructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: sl@0: // accessors sl@0: // sl@0: // Note: Like complex number, quaternions do have a meaningful notion of "real part", sl@0: // but unlike them there is no meaningful notion of "imaginary part". sl@0: // Instead there is an "unreal part" which itself is a quaternion, and usually sl@0: // nothing simpler (as opposed to the complex number case). sl@0: // However, for practicallity, there are accessors for the other components sl@0: // (these are necessary for the templated copy constructor, for instance). sl@0: sl@0: BOOST_QUATERNION_ACCESSOR_GENERATOR(T) sl@0: sl@0: // assignment operators sl@0: sl@0: BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(T) sl@0: sl@0: // other assignment-related operators sl@0: // sl@0: // NOTE: Quaternion multiplication is *NOT* commutative; sl@0: // symbolically, "q *= rhs;" means "q = q * rhs;" sl@0: // and "q /= rhs;" means "q = q * inverse_of(rhs);" sl@0: sl@0: quaternion & operator += (T const & rhs) sl@0: { sl@0: T at = a + rhs; // exception guard sl@0: sl@0: a = at; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: quaternion & operator += (::std::complex const & rhs) sl@0: { sl@0: T at = a + rhs.real(); // exception guard sl@0: T bt = b + rhs.imag(); // exception guard sl@0: sl@0: a = at; sl@0: b = bt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: template sl@0: quaternion & operator += (quaternion const & rhs) sl@0: { sl@0: T at = a + static_cast(rhs.R_component_1()); // exception guard sl@0: T bt = b + static_cast(rhs.R_component_2()); // exception guard sl@0: T ct = c + static_cast(rhs.R_component_3()); // exception guard sl@0: T dt = d + static_cast(rhs.R_component_4()); // exception guard sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: sl@0: quaternion & operator -= (T const & rhs) sl@0: { sl@0: T at = a - rhs; // exception guard sl@0: sl@0: a = at; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: quaternion & operator -= (::std::complex const & rhs) sl@0: { sl@0: T at = a - rhs.real(); // exception guard sl@0: T bt = b - rhs.imag(); // exception guard sl@0: sl@0: a = at; sl@0: b = bt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: template sl@0: quaternion & operator -= (quaternion const & rhs) sl@0: { sl@0: T at = a - static_cast(rhs.R_component_1()); // exception guard sl@0: T bt = b - static_cast(rhs.R_component_2()); // exception guard sl@0: T ct = c - static_cast(rhs.R_component_3()); // exception guard sl@0: T dt = d - static_cast(rhs.R_component_4()); // exception guard sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: quaternion & operator *= (T const & rhs) sl@0: { sl@0: T at = a * rhs; // exception guard sl@0: T bt = b * rhs; // exception guard sl@0: T ct = c * rhs; // exception guard sl@0: T dt = d * rhs; // exception guard sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: quaternion & operator *= (::std::complex const & rhs) sl@0: { sl@0: T ar = rhs.real(); sl@0: T br = rhs.imag(); sl@0: sl@0: T at = +a*ar-b*br; sl@0: T bt = +a*br+b*ar; sl@0: T ct = +c*ar+d*br; sl@0: T dt = -c*br+d*ar; sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: template sl@0: quaternion & operator *= (quaternion const & rhs) sl@0: { sl@0: T ar = static_cast(rhs.R_component_1()); sl@0: T br = static_cast(rhs.R_component_2()); sl@0: T cr = static_cast(rhs.R_component_3()); sl@0: T dr = static_cast(rhs.R_component_4()); sl@0: sl@0: T at = +a*ar-b*br-c*cr-d*dr; sl@0: T bt = +a*br+b*ar+c*dr-d*cr; //(a*br+ar*b)+(c*dr-cr*d); sl@0: T ct = +a*cr-b*dr+c*ar+d*br; //(a*cr+ar*c)+(d*br-dr*b); sl@0: T dt = +a*dr+b*cr-c*br+d*ar; //(a*dr+ar*d)+(b*cr-br*c); sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: sl@0: quaternion & operator /= (T const & rhs) sl@0: { sl@0: T at = a / rhs; // exception guard sl@0: T bt = b / rhs; // exception guard sl@0: T ct = c / rhs; // exception guard sl@0: T dt = d / rhs; // exception guard sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: quaternion & operator /= (::std::complex const & rhs) sl@0: { sl@0: T ar = rhs.real(); sl@0: T br = rhs.imag(); sl@0: sl@0: T denominator = ar*ar+br*br; sl@0: sl@0: T at = (+a*ar+b*br)/denominator; //(a*ar+b*br)/denominator; sl@0: T bt = (-a*br+b*ar)/denominator; //(ar*b-a*br)/denominator; sl@0: T ct = (+c*ar-d*br)/denominator; //(ar*c-d*br)/denominator; sl@0: T dt = (+c*br+d*ar)/denominator; //(ar*d+br*c)/denominator; sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: template sl@0: quaternion & operator /= (quaternion const & rhs) sl@0: { sl@0: T ar = static_cast(rhs.R_component_1()); sl@0: T br = static_cast(rhs.R_component_2()); sl@0: T cr = static_cast(rhs.R_component_3()); sl@0: T dr = static_cast(rhs.R_component_4()); sl@0: sl@0: T denominator = ar*ar+br*br+cr*cr+dr*dr; sl@0: sl@0: T at = (+a*ar+b*br+c*cr+d*dr)/denominator; //(a*ar+b*br+c*cr+d*dr)/denominator; sl@0: T bt = (-a*br+b*ar-c*dr+d*cr)/denominator; //((ar*b-a*br)+(cr*d-c*dr))/denominator; sl@0: T ct = (-a*cr+b*dr+c*ar-d*br)/denominator; //((ar*c-a*cr)+(dr*b-d*br))/denominator; sl@0: T dt = (-a*dr-b*cr+c*br+d*ar)/denominator; //((ar*d-a*dr)+(br*c-b*cr))/denominator; sl@0: sl@0: a = at; sl@0: b = bt; sl@0: c = ct; sl@0: d = dt; sl@0: sl@0: return(*this); sl@0: } sl@0: sl@0: sl@0: protected: sl@0: sl@0: BOOST_QUATERNION_MEMBER_DATA_GENERATOR(T) sl@0: sl@0: sl@0: private: sl@0: sl@0: }; sl@0: sl@0: sl@0: // declaration of quaternion specialization sl@0: sl@0: template<> class quaternion; sl@0: template<> class quaternion; sl@0: template<> class quaternion; sl@0: sl@0: sl@0: // helper templates for converting copy constructors (declaration) sl@0: sl@0: namespace detail sl@0: { sl@0: sl@0: template< typename T, sl@0: typename U sl@0: > sl@0: quaternion quaternion_type_converter(quaternion const & rhs); sl@0: } sl@0: sl@0: sl@0: // implementation of quaternion specialization sl@0: sl@0: sl@0: #define BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(type) \ sl@0: explicit quaternion( type const & requested_a = static_cast(0), \ sl@0: type const & requested_b = static_cast(0), \ sl@0: type const & requested_c = static_cast(0), \ sl@0: type const & requested_d = static_cast(0)) \ sl@0: : a(requested_a), \ sl@0: b(requested_b), \ sl@0: c(requested_c), \ sl@0: d(requested_d) \ sl@0: { \ sl@0: } \ sl@0: \ sl@0: explicit quaternion( ::std::complex const & z0, \ sl@0: ::std::complex const & z1 = ::std::complex()) \ sl@0: : a(z0.real()), \ sl@0: b(z0.imag()), \ sl@0: c(z1.real()), \ sl@0: d(z1.imag()) \ sl@0: { \ sl@0: } sl@0: sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1(type) \ sl@0: quaternion & operator += (type const & rhs) \ sl@0: { \ sl@0: a += rhs; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2(type) \ sl@0: quaternion & operator += (::std::complex const & rhs) \ sl@0: { \ sl@0: a += rhs.real(); \ sl@0: b += rhs.imag(); \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_3(type) \ sl@0: template \ sl@0: quaternion & operator += (quaternion const & rhs) \ sl@0: { \ sl@0: a += static_cast(rhs.R_component_1()); \ sl@0: b += static_cast(rhs.R_component_2()); \ sl@0: c += static_cast(rhs.R_component_3()); \ sl@0: d += static_cast(rhs.R_component_4()); \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR_1(type) \ sl@0: quaternion & operator -= (type const & rhs) \ sl@0: { \ sl@0: a -= rhs; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR_2(type) \ sl@0: quaternion & operator -= (::std::complex const & rhs) \ sl@0: { \ sl@0: a -= rhs.real(); \ sl@0: b -= rhs.imag(); \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR_3(type) \ sl@0: template \ sl@0: quaternion & operator -= (quaternion const & rhs) \ sl@0: { \ sl@0: a -= static_cast(rhs.R_component_1()); \ sl@0: b -= static_cast(rhs.R_component_2()); \ sl@0: c -= static_cast(rhs.R_component_3()); \ sl@0: d -= static_cast(rhs.R_component_4()); \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR_1(type) \ sl@0: quaternion & operator *= (type const & rhs) \ sl@0: { \ sl@0: a *= rhs; \ sl@0: b *= rhs; \ sl@0: c *= rhs; \ sl@0: d *= rhs; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR_2(type) \ sl@0: quaternion & operator *= (::std::complex const & rhs) \ sl@0: { \ sl@0: type ar = rhs.real(); \ sl@0: type br = rhs.imag(); \ sl@0: \ sl@0: type at = +a*ar-b*br; \ sl@0: type bt = +a*br+b*ar; \ sl@0: type ct = +c*ar+d*br; \ sl@0: type dt = -c*br+d*ar; \ sl@0: \ sl@0: a = at; \ sl@0: b = bt; \ sl@0: c = ct; \ sl@0: d = dt; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR_3(type) \ sl@0: template \ sl@0: quaternion & operator *= (quaternion const & rhs) \ sl@0: { \ sl@0: type ar = static_cast(rhs.R_component_1()); \ sl@0: type br = static_cast(rhs.R_component_2()); \ sl@0: type cr = static_cast(rhs.R_component_3()); \ sl@0: type dr = static_cast(rhs.R_component_4()); \ sl@0: \ sl@0: type at = +a*ar-b*br-c*cr-d*dr; \ sl@0: type bt = +a*br+b*ar+c*dr-d*cr; \ sl@0: type ct = +a*cr-b*dr+c*ar+d*br; \ sl@0: type dt = +a*dr+b*cr-c*br+d*ar; \ sl@0: \ sl@0: a = at; \ sl@0: b = bt; \ sl@0: c = ct; \ sl@0: d = dt; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: // There is quite a lot of repetition in the code below. This is intentional. sl@0: // The last conditional block is the normal form, and the others merely sl@0: // consist of workarounds for various compiler deficiencies. Hopefuly, when sl@0: // more compilers are conformant and we can retire support for those that are sl@0: // not, we will be able to remove the clutter. This is makes the situation sl@0: // (painfully) explicit. sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_1(type) \ sl@0: quaternion & operator /= (type const & rhs) \ sl@0: { \ sl@0: a /= rhs; \ sl@0: b /= rhs; \ sl@0: c /= rhs; \ sl@0: d /= rhs; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: sl@0: #if defined(__GNUC__) && (__GNUC__ < 3) sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \ sl@0: quaternion & operator /= (::std::complex const & rhs) \ sl@0: { \ sl@0: using ::std::valarray; \ sl@0: \ sl@0: valarray tr(2); \ sl@0: \ sl@0: tr[0] = rhs.real(); \ sl@0: tr[1] = rhs.imag(); \ sl@0: \ sl@0: type mixam = (BOOST_GET_VALARRAY(type,static_cast(1)/abs(tr)).max)(); \ sl@0: \ sl@0: tr *= mixam; \ sl@0: \ sl@0: valarray tt(4); \ sl@0: \ sl@0: tt[0] = +a*tr[0]+b*tr[1]; \ sl@0: tt[1] = -a*tr[1]+b*tr[0]; \ sl@0: tt[2] = +c*tr[0]-d*tr[1]; \ sl@0: tt[3] = +c*tr[1]+d*tr[0]; \ sl@0: \ sl@0: tr *= tr; \ sl@0: \ sl@0: tt *= (mixam/tr.sum()); \ sl@0: \ sl@0: a = tt[0]; \ sl@0: b = tt[1]; \ sl@0: c = tt[2]; \ sl@0: d = tt[3]; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: #elif defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \ sl@0: quaternion & operator /= (::std::complex const & rhs) \ sl@0: { \ sl@0: using ::std::valarray; \ sl@0: using ::std::abs; \ sl@0: \ sl@0: valarray tr(2); \ sl@0: \ sl@0: tr[0] = rhs.real(); \ sl@0: tr[1] = rhs.imag(); \ sl@0: \ sl@0: type mixam = static_cast(1)/(abs(tr).max)(); \ sl@0: \ sl@0: tr *= mixam; \ sl@0: \ sl@0: valarray tt(4); \ sl@0: \ sl@0: tt[0] = +a*tr[0]+b*tr[1]; \ sl@0: tt[1] = -a*tr[1]+b*tr[0]; \ sl@0: tt[2] = +c*tr[0]-d*tr[1]; \ sl@0: tt[3] = +c*tr[1]+d*tr[0]; \ sl@0: \ sl@0: tr *= tr; \ sl@0: \ sl@0: tt *= (mixam/tr.sum()); \ sl@0: \ sl@0: a = tt[0]; \ sl@0: b = tt[1]; \ sl@0: c = tt[2]; \ sl@0: d = tt[3]; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: #else sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \ sl@0: quaternion & operator /= (::std::complex const & rhs) \ sl@0: { \ sl@0: using ::std::valarray; \ sl@0: \ sl@0: valarray tr(2); \ sl@0: \ sl@0: tr[0] = rhs.real(); \ sl@0: tr[1] = rhs.imag(); \ sl@0: \ sl@0: type mixam = static_cast(1)/(abs(tr).max)(); \ sl@0: \ sl@0: tr *= mixam; \ sl@0: \ sl@0: valarray tt(4); \ sl@0: \ sl@0: tt[0] = +a*tr[0]+b*tr[1]; \ sl@0: tt[1] = -a*tr[1]+b*tr[0]; \ sl@0: tt[2] = +c*tr[0]-d*tr[1]; \ sl@0: tt[3] = +c*tr[1]+d*tr[0]; \ sl@0: \ sl@0: tr *= tr; \ sl@0: \ sl@0: tt *= (mixam/tr.sum()); \ sl@0: \ sl@0: a = tt[0]; \ sl@0: b = tt[1]; \ sl@0: c = tt[2]; \ sl@0: d = tt[3]; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: #endif /* defined(__GNUC__) && (__GNUC__ < 3) */ /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */ sl@0: sl@0: #if defined(__GNUC__) && (__GNUC__ < 3) sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type) \ sl@0: template \ sl@0: quaternion & operator /= (quaternion const & rhs) \ sl@0: { \ sl@0: using ::std::valarray; \ sl@0: \ sl@0: valarray tr(4); \ sl@0: \ sl@0: tr[0] = static_cast(rhs.R_component_1()); \ sl@0: tr[1] = static_cast(rhs.R_component_2()); \ sl@0: tr[2] = static_cast(rhs.R_component_3()); \ sl@0: tr[3] = static_cast(rhs.R_component_4()); \ sl@0: \ sl@0: type mixam = (BOOST_GET_VALARRAY(type,static_cast(1)/abs(tr)).max)(); \ sl@0: \ sl@0: tr *= mixam; \ sl@0: \ sl@0: valarray tt(4); \ sl@0: \ sl@0: tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \ sl@0: tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \ sl@0: tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \ sl@0: tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \ sl@0: \ sl@0: tr *= tr; \ sl@0: \ sl@0: tt *= (mixam/tr.sum()); \ sl@0: \ sl@0: a = tt[0]; \ sl@0: b = tt[1]; \ sl@0: c = tt[2]; \ sl@0: d = tt[3]; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: #elif defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP) sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type) \ sl@0: template \ sl@0: quaternion & operator /= (quaternion const & rhs) \ sl@0: { \ sl@0: using ::std::valarray; \ sl@0: using ::std::abs; \ sl@0: \ sl@0: valarray tr(4); \ sl@0: \ sl@0: tr[0] = static_cast(rhs.R_component_1()); \ sl@0: tr[1] = static_cast(rhs.R_component_2()); \ sl@0: tr[2] = static_cast(rhs.R_component_3()); \ sl@0: tr[3] = static_cast(rhs.R_component_4()); \ sl@0: \ sl@0: type mixam = static_cast(1)/(abs(tr).max)(); \ sl@0: \ sl@0: tr *= mixam; \ sl@0: \ sl@0: valarray tt(4); \ sl@0: \ sl@0: tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \ sl@0: tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \ sl@0: tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \ sl@0: tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \ sl@0: \ sl@0: tr *= tr; \ sl@0: \ sl@0: tt *= (mixam/tr.sum()); \ sl@0: \ sl@0: a = tt[0]; \ sl@0: b = tt[1]; \ sl@0: c = tt[2]; \ sl@0: d = tt[3]; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: #else sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type) \ sl@0: template \ sl@0: quaternion & operator /= (quaternion const & rhs) \ sl@0: { \ sl@0: using ::std::valarray; \ sl@0: \ sl@0: valarray tr(4); \ sl@0: \ sl@0: tr[0] = static_cast(rhs.R_component_1()); \ sl@0: tr[1] = static_cast(rhs.R_component_2()); \ sl@0: tr[2] = static_cast(rhs.R_component_3()); \ sl@0: tr[3] = static_cast(rhs.R_component_4()); \ sl@0: \ sl@0: type mixam = static_cast(1)/(abs(tr).max)(); \ sl@0: \ sl@0: tr *= mixam; \ sl@0: \ sl@0: valarray tt(4); \ sl@0: \ sl@0: tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \ sl@0: tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \ sl@0: tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \ sl@0: tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \ sl@0: \ sl@0: tr *= tr; \ sl@0: \ sl@0: tt *= (mixam/tr.sum()); \ sl@0: \ sl@0: a = tt[0]; \ sl@0: b = tt[1]; \ sl@0: c = tt[2]; \ sl@0: d = tt[3]; \ sl@0: \ sl@0: return(*this); \ sl@0: } sl@0: #endif /* defined(__GNUC__) && (__GNUC__ < 3) */ /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */ sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1(type) \ sl@0: BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2(type) \ sl@0: BOOST_QUATERNION_MEMBER_ADD_GENERATOR_3(type) sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_SUB_GENERATOR_1(type) \ sl@0: BOOST_QUATERNION_MEMBER_SUB_GENERATOR_2(type) \ sl@0: BOOST_QUATERNION_MEMBER_SUB_GENERATOR_3(type) sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_MUL_GENERATOR_1(type) \ sl@0: BOOST_QUATERNION_MEMBER_MUL_GENERATOR_2(type) \ sl@0: BOOST_QUATERNION_MEMBER_MUL_GENERATOR_3(type) sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_DIV_GENERATOR_1(type) \ sl@0: BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \ sl@0: BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type) sl@0: sl@0: #define BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_ADD_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_SUB_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_MUL_GENERATOR(type) \ sl@0: BOOST_QUATERNION_MEMBER_DIV_GENERATOR(type) sl@0: sl@0: sl@0: template<> sl@0: class quaternion sl@0: { sl@0: public: sl@0: sl@0: typedef float value_type; sl@0: sl@0: BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(float) sl@0: sl@0: // UNtemplated copy constructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: // explicit copy constructors (precision-loosing converters) sl@0: sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: { sl@0: *this = detail::quaternion_type_converter(a_recopier); sl@0: } sl@0: sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: { sl@0: *this = detail::quaternion_type_converter(a_recopier); sl@0: } sl@0: sl@0: // destructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: // accessors sl@0: // sl@0: // Note: Like complex number, quaternions do have a meaningful notion of "real part", sl@0: // but unlike them there is no meaningful notion of "imaginary part". sl@0: // Instead there is an "unreal part" which itself is a quaternion, and usually sl@0: // nothing simpler (as opposed to the complex number case). sl@0: // However, for practicallity, there are accessors for the other components sl@0: // (these are necessary for the templated copy constructor, for instance). sl@0: sl@0: BOOST_QUATERNION_ACCESSOR_GENERATOR(float) sl@0: sl@0: // assignment operators sl@0: sl@0: BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(float) sl@0: sl@0: // other assignment-related operators sl@0: // sl@0: // NOTE: Quaternion multiplication is *NOT* commutative; sl@0: // symbolically, "q *= rhs;" means "q = q * rhs;" sl@0: // and "q /= rhs;" means "q = q * inverse_of(rhs);" sl@0: sl@0: BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(float) sl@0: sl@0: sl@0: protected: sl@0: sl@0: BOOST_QUATERNION_MEMBER_DATA_GENERATOR(float) sl@0: sl@0: sl@0: private: sl@0: sl@0: }; sl@0: sl@0: sl@0: template<> sl@0: class quaternion sl@0: { sl@0: public: sl@0: sl@0: typedef double value_type; sl@0: sl@0: BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(double) sl@0: sl@0: // UNtemplated copy constructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: // converting copy constructor sl@0: sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: { sl@0: *this = detail::quaternion_type_converter(a_recopier); sl@0: } sl@0: sl@0: // explicit copy constructors (precision-loosing converters) sl@0: sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: { sl@0: *this = detail::quaternion_type_converter(a_recopier); sl@0: } sl@0: sl@0: // destructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: // accessors sl@0: // sl@0: // Note: Like complex number, quaternions do have a meaningful notion of "real part", sl@0: // but unlike them there is no meaningful notion of "imaginary part". sl@0: // Instead there is an "unreal part" which itself is a quaternion, and usually sl@0: // nothing simpler (as opposed to the complex number case). sl@0: // However, for practicallity, there are accessors for the other components sl@0: // (these are necessary for the templated copy constructor, for instance). sl@0: sl@0: BOOST_QUATERNION_ACCESSOR_GENERATOR(double) sl@0: sl@0: // assignment operators sl@0: sl@0: BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(double) sl@0: sl@0: // other assignment-related operators sl@0: // sl@0: // NOTE: Quaternion multiplication is *NOT* commutative; sl@0: // symbolically, "q *= rhs;" means "q = q * rhs;" sl@0: // and "q /= rhs;" means "q = q * inverse_of(rhs);" sl@0: sl@0: BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(double) sl@0: sl@0: sl@0: protected: sl@0: sl@0: BOOST_QUATERNION_MEMBER_DATA_GENERATOR(double) sl@0: sl@0: sl@0: private: sl@0: sl@0: }; sl@0: sl@0: sl@0: template<> sl@0: class quaternion sl@0: { sl@0: public: sl@0: sl@0: typedef long double value_type; sl@0: sl@0: BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(long double) sl@0: sl@0: // UNtemplated copy constructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: // converting copy constructors sl@0: sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: { sl@0: *this = detail::quaternion_type_converter(a_recopier); sl@0: } sl@0: sl@0: explicit quaternion(quaternion const & a_recopier) sl@0: { sl@0: *this = detail::quaternion_type_converter(a_recopier); sl@0: } sl@0: sl@0: // destructor sl@0: // (this is taken care of by the compiler itself) sl@0: sl@0: // accessors sl@0: // sl@0: // Note: Like complex number, quaternions do have a meaningful notion of "real part", sl@0: // but unlike them there is no meaningful notion of "imaginary part". sl@0: // Instead there is an "unreal part" which itself is a quaternion, and usually sl@0: // nothing simpler (as opposed to the complex number case). sl@0: // However, for practicallity, there are accessors for the other components sl@0: // (these are necessary for the templated copy constructor, for instance). sl@0: sl@0: BOOST_QUATERNION_ACCESSOR_GENERATOR(long double) sl@0: sl@0: // assignment operators sl@0: sl@0: BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(long double) sl@0: sl@0: // other assignment-related operators sl@0: // sl@0: // NOTE: Quaternion multiplication is *NOT* commutative; sl@0: // symbolically, "q *= rhs;" means "q = q * rhs;" sl@0: // and "q /= rhs;" means "q = q * inverse_of(rhs);" sl@0: sl@0: BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(long double) sl@0: sl@0: sl@0: protected: sl@0: sl@0: BOOST_QUATERNION_MEMBER_DATA_GENERATOR(long double) sl@0: sl@0: sl@0: private: sl@0: sl@0: }; sl@0: sl@0: sl@0: #undef BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR sl@0: #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR sl@0: #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR sl@0: #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR sl@0: #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR sl@0: #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1 sl@0: #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2 sl@0: #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR_3 sl@0: #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR_1 sl@0: #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR_2 sl@0: #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR_3 sl@0: #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR_1 sl@0: #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR_2 sl@0: #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR_3 sl@0: #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR_1 sl@0: #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2 sl@0: #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3 sl@0: sl@0: #undef BOOST_QUATERNION_CONSTRUCTOR_GENERATOR sl@0: sl@0: sl@0: #undef BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR sl@0: sl@0: #undef BOOST_QUATERNION_MEMBER_DATA_GENERATOR sl@0: sl@0: #undef BOOST_QUATERNION_ACCESSOR_GENERATOR sl@0: sl@0: sl@0: // operators sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) \ sl@0: { \ sl@0: quaternion res(lhs); \ sl@0: res op##= rhs; \ sl@0: return(res); \ sl@0: } sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR_1_L(op) \ sl@0: template \ sl@0: inline quaternion operator op (T const & lhs, quaternion const & rhs) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR_1_R(op) \ sl@0: template \ sl@0: inline quaternion operator op (quaternion const & lhs, T const & rhs) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR_2_L(op) \ sl@0: template \ sl@0: inline quaternion operator op (::std::complex const & lhs, quaternion const & rhs) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR_2_R(op) \ sl@0: template \ sl@0: inline quaternion operator op (quaternion const & lhs, ::std::complex const & rhs) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR_3(op) \ sl@0: template \ sl@0: inline quaternion operator op (quaternion const & lhs, quaternion const & rhs) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) sl@0: sl@0: #define BOOST_QUATERNION_OPERATOR_GENERATOR(op) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_1_L(op) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_1_R(op) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_2_L(op) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_2_R(op) \ sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR_3(op) sl@0: sl@0: sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR(+) sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR(-) sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR(*) sl@0: BOOST_QUATERNION_OPERATOR_GENERATOR(/) sl@0: sl@0: sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR sl@0: sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR_1_L sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR_1_R sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR_2_L sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR_2_R sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR_3 sl@0: sl@0: #undef BOOST_QUATERNION_OPERATOR_GENERATOR_BODY sl@0: sl@0: sl@0: template sl@0: inline quaternion operator + (quaternion const & q) sl@0: { sl@0: return(q); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion operator - (quaternion const & q) sl@0: { sl@0: return(quaternion(-q.R_component_1(),-q.R_component_2(),-q.R_component_3(),-q.R_component_4())); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline bool operator == (T const & lhs, quaternion const & rhs) sl@0: { sl@0: return ( sl@0: (rhs.R_component_1() == lhs)&& sl@0: (rhs.R_component_2() == static_cast(0))&& sl@0: (rhs.R_component_3() == static_cast(0))&& sl@0: (rhs.R_component_4() == static_cast(0)) sl@0: ); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline bool operator == (quaternion const & lhs, T const & rhs) sl@0: { sl@0: return ( sl@0: (lhs.R_component_1() == rhs)&& sl@0: (lhs.R_component_2() == static_cast(0))&& sl@0: (lhs.R_component_3() == static_cast(0))&& sl@0: (lhs.R_component_4() == static_cast(0)) sl@0: ); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline bool operator == (::std::complex const & lhs, quaternion const & rhs) sl@0: { sl@0: return ( sl@0: (rhs.R_component_1() == lhs.real())&& sl@0: (rhs.R_component_2() == lhs.imag())&& sl@0: (rhs.R_component_3() == static_cast(0))&& sl@0: (rhs.R_component_4() == static_cast(0)) sl@0: ); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline bool operator == (quaternion const & lhs, ::std::complex const & rhs) sl@0: { sl@0: return ( sl@0: (lhs.R_component_1() == rhs.real())&& sl@0: (lhs.R_component_2() == rhs.imag())&& sl@0: (lhs.R_component_3() == static_cast(0))&& sl@0: (lhs.R_component_4() == static_cast(0)) sl@0: ); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline bool operator == (quaternion const & lhs, quaternion const & rhs) sl@0: { sl@0: return ( sl@0: (rhs.R_component_1() == lhs.R_component_1())&& sl@0: (rhs.R_component_2() == lhs.R_component_2())&& sl@0: (rhs.R_component_3() == lhs.R_component_3())&& sl@0: (rhs.R_component_4() == lhs.R_component_4()) sl@0: ); sl@0: } sl@0: sl@0: sl@0: #define BOOST_QUATERNION_NOT_EQUAL_GENERATOR \ sl@0: { \ sl@0: return(!(lhs == rhs)); \ sl@0: } sl@0: sl@0: template sl@0: inline bool operator != (T const & lhs, quaternion const & rhs) sl@0: BOOST_QUATERNION_NOT_EQUAL_GENERATOR sl@0: sl@0: template sl@0: inline bool operator != (quaternion const & lhs, T const & rhs) sl@0: BOOST_QUATERNION_NOT_EQUAL_GENERATOR sl@0: sl@0: template sl@0: inline bool operator != (::std::complex const & lhs, quaternion const & rhs) sl@0: BOOST_QUATERNION_NOT_EQUAL_GENERATOR sl@0: sl@0: template sl@0: inline bool operator != (quaternion const & lhs, ::std::complex const & rhs) sl@0: BOOST_QUATERNION_NOT_EQUAL_GENERATOR sl@0: sl@0: template sl@0: inline bool operator != (quaternion const & lhs, quaternion const & rhs) sl@0: BOOST_QUATERNION_NOT_EQUAL_GENERATOR sl@0: sl@0: #undef BOOST_QUATERNION_NOT_EQUAL_GENERATOR sl@0: sl@0: sl@0: // Note: we allow the following formats, whith a, b, c, and d reals sl@0: // a sl@0: // (a), (a,b), (a,b,c), (a,b,c,d) sl@0: // (a,(c)), (a,(c,d)), ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b),(c,d)) sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: template sl@0: std::istream & operator >> ( ::std::istream & is, sl@0: quaternion & q) sl@0: #else sl@0: template sl@0: ::std::basic_istream & operator >> ( ::std::basic_istream & is, sl@0: quaternion & q) sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: typedef char charT; sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: #else sl@0: const ::std::ctype & ct = ::std::use_facet< ::std::ctype >(is.getloc()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: T a = T(); sl@0: T b = T(); sl@0: T c = T(); sl@0: T d = T(); sl@0: sl@0: ::std::complex u = ::std::complex(); sl@0: ::std::complex v = ::std::complex(); sl@0: sl@0: charT ch = charT(); sl@0: char cc; sl@0: sl@0: is >> ch; // get the first lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == '(') // read "(", possible: (a), (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d)), ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b,),(c,d,)) sl@0: { sl@0: is >> ch; // get the second lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == '(') // read "((", possible: ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b,),(c,d,)) sl@0: { sl@0: is.putback(ch); sl@0: sl@0: is >> u; // we extract the first and second components sl@0: a = u.real(); sl@0: b = u.imag(); sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the next lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: ((a)) or ((a,b)) sl@0: { sl@0: q = quaternion(a,b); sl@0: } sl@0: else if (cc == ',') // read "((a)," or "((a,b),", possible: ((a),c), ((a),(c)), ((a),(c,d)), ((a,b),c), ((a,b),(c)), ((a,b,),(c,d,)) sl@0: { sl@0: is >> v; // we extract the third and fourth components sl@0: c = v.real(); sl@0: d = v.imag(); sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the last lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: ((a),c), ((a),(c)), ((a),(c,d)), ((a,b),c), ((a,b),(c)) or ((a,b,),(c,d,)) sl@0: { sl@0: q = quaternion(a,b,c,d); sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: else // read "(a", possible: (a), (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d)) sl@0: { sl@0: is.putback(ch); sl@0: sl@0: is >> a; // we extract the first component sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the third lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: (a) sl@0: { sl@0: q = quaternion(a); sl@0: } sl@0: else if (cc == ',') // read "(a,", possible: (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d)) sl@0: { sl@0: is >> ch; // get the fourth lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == '(') // read "(a,(", possible: (a,(c)), (a,(c,d)) sl@0: { sl@0: is.putback(ch); sl@0: sl@0: is >> v; // we extract the third and fourth component sl@0: sl@0: c = v.real(); sl@0: d = v.imag(); sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the ninth lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: (a,(c)) or (a,(c,d)) sl@0: { sl@0: q = quaternion(a,b,c,d); sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: else // read "(a,b", possible: (a,b), (a,b,c), (a,b,c,d) sl@0: { sl@0: is.putback(ch); sl@0: sl@0: is >> b; // we extract the second component sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the fifth lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: (a,b) sl@0: { sl@0: q = quaternion(a,b); sl@0: } sl@0: else if (cc == ',') // read "(a,b,", possible: (a,b,c), (a,b,c,d) sl@0: { sl@0: is >> c; // we extract the third component sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the seventh lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: (a,b,c) sl@0: { sl@0: q = quaternion(a,b,c); sl@0: } sl@0: else if (cc == ',') // read "(a,b,c,", possible: (a,b,c,d) sl@0: { sl@0: is >> d; // we extract the fourth component sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: is >> ch; // get the ninth lexeme sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: cc = ch; sl@0: #else sl@0: cc = ct.narrow(ch, char()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: sl@0: if (cc == ')') // format: (a,b,c,d) sl@0: { sl@0: q = quaternion(a,b,c,d); sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: } sl@0: else // error sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: is.setstate(::std::ios::failbit); sl@0: #else sl@0: is.setstate(::std::ios_base::failbit); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: } sl@0: } sl@0: else // format: a sl@0: { sl@0: is.putback(ch); sl@0: sl@0: is >> a; // we extract the first component sl@0: sl@0: if (!is.good()) goto finish; sl@0: sl@0: q = quaternion(a); sl@0: } sl@0: sl@0: finish: sl@0: return(is); sl@0: } sl@0: sl@0: sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: template sl@0: ::std::ostream & operator << ( ::std::ostream & os, sl@0: quaternion const & q) sl@0: #else sl@0: template sl@0: ::std::basic_ostream & operator << ( ::std::basic_ostream & os, sl@0: quaternion const & q) sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: { sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: ::std::ostringstream s; sl@0: #else sl@0: ::std::basic_ostringstream s; sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: sl@0: s.flags(os.flags()); sl@0: #ifdef BOOST_NO_STD_LOCALE sl@0: #else sl@0: s.imbue(os.getloc()); sl@0: #endif /* BOOST_NO_STD_LOCALE */ sl@0: s.precision(os.precision()); sl@0: sl@0: s << '(' << q.R_component_1() << ',' sl@0: << q.R_component_2() << ',' sl@0: << q.R_component_3() << ',' sl@0: << q.R_component_4() << ')'; sl@0: sl@0: return os << s.str(); sl@0: } sl@0: sl@0: sl@0: // values sl@0: sl@0: template sl@0: inline T real(quaternion const & q) sl@0: { sl@0: return(q.real()); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion unreal(quaternion const & q) sl@0: { sl@0: return(q.unreal()); sl@0: } sl@0: sl@0: sl@0: #define BOOST_QUATERNION_VALARRAY_LOADER \ sl@0: using ::std::valarray; \ sl@0: \ sl@0: valarray temp(4); \ sl@0: \ sl@0: temp[0] = q.R_component_1(); \ sl@0: temp[1] = q.R_component_2(); \ sl@0: temp[2] = q.R_component_3(); \ sl@0: temp[3] = q.R_component_4(); sl@0: sl@0: sl@0: template sl@0: inline T sup(quaternion const & q) sl@0: { sl@0: #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP sl@0: using ::std::abs; sl@0: #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */ sl@0: sl@0: BOOST_QUATERNION_VALARRAY_LOADER sl@0: sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: return((BOOST_GET_VALARRAY(T, abs(temp)).max)()); sl@0: #else sl@0: return((abs(temp).max)()); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: sl@0: sl@0: template sl@0: inline T l1(quaternion const & q) sl@0: { sl@0: #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP sl@0: using ::std::abs; sl@0: #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */ sl@0: sl@0: BOOST_QUATERNION_VALARRAY_LOADER sl@0: sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: return(BOOST_GET_VALARRAY(T, abs(temp)).sum()); sl@0: #else sl@0: return(abs(temp).sum()); sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: } sl@0: sl@0: sl@0: template sl@0: inline T abs(quaternion const & q) sl@0: { sl@0: #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP sl@0: using ::std::abs; sl@0: #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */ sl@0: sl@0: using ::std::sqrt; sl@0: sl@0: BOOST_QUATERNION_VALARRAY_LOADER sl@0: sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: T maxim = (BOOST_GET_VALARRAY(T, abs(temp)).max)(); // overflow protection sl@0: #else sl@0: T maxim = (abs(temp).max)(); // overflow protection sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: sl@0: if (maxim == static_cast(0)) sl@0: { sl@0: return(maxim); sl@0: } sl@0: else sl@0: { sl@0: T mixam = static_cast(1)/maxim; // prefer multiplications over divisions sl@0: sl@0: temp *= mixam; sl@0: sl@0: temp *= temp; sl@0: sl@0: return(maxim*sqrt(temp.sum())); sl@0: } sl@0: sl@0: //return(sqrt(norm(q))); sl@0: } sl@0: sl@0: sl@0: #undef BOOST_QUATERNION_VALARRAY_LOADER sl@0: sl@0: sl@0: // Note: This is the Cayley norm, not the Euclidian norm... sl@0: sl@0: template sl@0: inline T norm(quaternionconst & q) sl@0: { sl@0: return(real(q*conj(q))); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion conj(quaternion const & q) sl@0: { sl@0: return(quaternion( +q.R_component_1(), sl@0: -q.R_component_2(), sl@0: -q.R_component_3(), sl@0: -q.R_component_4())); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion spherical( T const & rho, sl@0: T const & theta, sl@0: T const & phi1, sl@0: T const & phi2) sl@0: { sl@0: using ::std::cos; sl@0: using ::std::sin; sl@0: sl@0: //T a = cos(theta)*cos(phi1)*cos(phi2); sl@0: //T b = sin(theta)*cos(phi1)*cos(phi2); sl@0: //T c = sin(phi1)*cos(phi2); sl@0: //T d = sin(phi2); sl@0: sl@0: T courrant = static_cast(1); sl@0: sl@0: T d = sin(phi2); sl@0: sl@0: courrant *= cos(phi2); sl@0: sl@0: T c = sin(phi1)*courrant; sl@0: sl@0: courrant *= cos(phi1); sl@0: sl@0: T b = sin(theta)*courrant; sl@0: T a = cos(theta)*courrant; sl@0: sl@0: return(rho*quaternion(a,b,c,d)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion semipolar( T const & rho, sl@0: T const & alpha, sl@0: T const & theta1, sl@0: T const & theta2) sl@0: { sl@0: using ::std::cos; sl@0: using ::std::sin; sl@0: sl@0: T a = cos(alpha)*cos(theta1); sl@0: T b = cos(alpha)*sin(theta1); sl@0: T c = sin(alpha)*cos(theta2); sl@0: T d = sin(alpha)*sin(theta2); sl@0: sl@0: return(rho*quaternion(a,b,c,d)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion multipolar( T const & rho1, sl@0: T const & theta1, sl@0: T const & rho2, sl@0: T const & theta2) sl@0: { sl@0: using ::std::cos; sl@0: using ::std::sin; sl@0: sl@0: T a = rho1*cos(theta1); sl@0: T b = rho1*sin(theta1); sl@0: T c = rho2*cos(theta2); sl@0: T d = rho2*sin(theta2); sl@0: sl@0: return(quaternion(a,b,c,d)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion cylindrospherical( T const & t, sl@0: T const & radius, sl@0: T const & longitude, sl@0: T const & latitude) sl@0: { sl@0: using ::std::cos; sl@0: using ::std::sin; sl@0: sl@0: sl@0: sl@0: T b = radius*cos(longitude)*cos(latitude); sl@0: T c = radius*sin(longitude)*cos(latitude); sl@0: T d = radius*sin(latitude); sl@0: sl@0: return(quaternion(t,b,c,d)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion cylindrical(T const & r, sl@0: T const & angle, sl@0: T const & h1, sl@0: T const & h2) sl@0: { sl@0: using ::std::cos; sl@0: using ::std::sin; sl@0: sl@0: T a = r*cos(angle); sl@0: T b = r*sin(angle); sl@0: sl@0: return(quaternion(a,b,h1,h2)); sl@0: } sl@0: sl@0: sl@0: // transcendentals sl@0: // (please see the documentation) sl@0: sl@0: sl@0: template sl@0: inline quaternion exp(quaternion const & q) sl@0: { sl@0: using ::std::exp; sl@0: using ::std::cos; sl@0: sl@0: using ::boost::math::sinc_pi; sl@0: sl@0: T u = exp(real(q)); sl@0: sl@0: T z = abs(unreal(q)); sl@0: sl@0: T w = sinc_pi(z); sl@0: sl@0: return(u*quaternion(cos(z), sl@0: w*q.R_component_2(), w*q.R_component_3(), sl@0: w*q.R_component_4())); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion cos(quaternion const & q) sl@0: { sl@0: using ::std::sin; sl@0: using ::std::cos; sl@0: using ::std::cosh; sl@0: sl@0: using ::boost::math::sinhc_pi; sl@0: sl@0: T z = abs(unreal(q)); sl@0: sl@0: T w = -sin(q.real())*sinhc_pi(z); sl@0: sl@0: return(quaternion(cos(q.real())*cosh(z), sl@0: w*q.R_component_2(), w*q.R_component_3(), sl@0: w*q.R_component_4())); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion sin(quaternion const & q) sl@0: { sl@0: using ::std::sin; sl@0: using ::std::cos; sl@0: using ::std::cosh; sl@0: sl@0: using ::boost::math::sinhc_pi; sl@0: sl@0: T z = abs(unreal(q)); sl@0: sl@0: T w = +cos(q.real())*sinhc_pi(z); sl@0: sl@0: return(quaternion(sin(q.real())*cosh(z), sl@0: w*q.R_component_2(), w*q.R_component_3(), sl@0: w*q.R_component_4())); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion tan(quaternion const & q) sl@0: { sl@0: return(sin(q)/cos(q)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion cosh(quaternion const & q) sl@0: { sl@0: return((exp(+q)+exp(-q))/static_cast(2)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion sinh(quaternion const & q) sl@0: { sl@0: return((exp(+q)-exp(-q))/static_cast(2)); sl@0: } sl@0: sl@0: sl@0: template sl@0: inline quaternion tanh(quaternion const & q) sl@0: { sl@0: return(sinh(q)/cosh(q)); sl@0: } sl@0: sl@0: sl@0: template sl@0: quaternion pow(quaternion const & q, sl@0: int n) sl@0: { sl@0: if (n > 1) sl@0: { sl@0: int m = n>>1; sl@0: sl@0: quaternion result = pow(q, m); sl@0: sl@0: result *= result; sl@0: sl@0: if (n != (m<<1)) sl@0: { sl@0: result *= q; // n odd sl@0: } sl@0: sl@0: return(result); sl@0: } sl@0: else if (n == 1) sl@0: { sl@0: return(q); sl@0: } sl@0: else if (n == 0) sl@0: { sl@0: return(quaternion(1)); sl@0: } sl@0: else /* n < 0 */ sl@0: { sl@0: return(pow(quaternion(1)/q,-n)); sl@0: } sl@0: } sl@0: sl@0: sl@0: // helper templates for converting copy constructors (definition) sl@0: sl@0: namespace detail sl@0: { sl@0: sl@0: template< typename T, sl@0: typename U sl@0: > sl@0: quaternion quaternion_type_converter(quaternion const & rhs) sl@0: { sl@0: return(quaternion( static_cast(rhs.R_component_1()), sl@0: static_cast(rhs.R_component_2()), sl@0: static_cast(rhs.R_component_3()), sl@0: static_cast(rhs.R_component_4()))); sl@0: } sl@0: } sl@0: } sl@0: } sl@0: sl@0: sl@0: #if BOOST_WORKAROUND(__GNUC__, < 3) sl@0: #undef BOOST_GET_VALARRAY sl@0: #endif /* BOOST_WORKAROUND(__GNUC__, < 3) */ sl@0: sl@0: sl@0: #endif /* BOOST_QUATERNION_HPP */