1 // boost quaternion.hpp header file
3 // (C) Copyright Hubert Holin 2001.
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
10 #ifndef BOOST_QUATERNION_HPP
11 #define BOOST_QUATERNION_HPP
15 #include <iosfwd> // for the "<<" and ">>" operators
16 #include <sstream> // for the "<<" operator
18 #include <boost/config.hpp> // for BOOST_NO_STD_LOCALE
19 #include <boost/detail/workaround.hpp>
20 #ifndef BOOST_NO_STD_LOCALE
21 #include <locale> // for the "<<" operator
22 #endif /* BOOST_NO_STD_LOCALE */
28 #include <boost/math/special_functions/sinc.hpp> // for the Sinus cardinal
29 #include <boost/math/special_functions/sinhc.hpp> // for the Hyperbolic Sinus cardinal
37 #define BOOST_QUATERNION_ACCESSOR_GENERATOR(type) \
43 quaternion<type> unreal() const \
45 return(quaternion<type>(static_cast<type>(0),b,c,d)); \
48 type R_component_1() const \
53 type R_component_2() const \
58 type R_component_3() const \
63 type R_component_4() const \
68 ::std::complex<type> C_component_1() const \
70 return(::std::complex<type>(a,b)); \
73 ::std::complex<type> C_component_2() const \
75 return(::std::complex<type>(c,d)); \
79 #define BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(type) \
80 template<typename X> \
81 quaternion<type> & operator = (quaternion<X> const & a_affecter) \
83 a = static_cast<type>(a_affecter.R_component_1()); \
84 b = static_cast<type>(a_affecter.R_component_2()); \
85 c = static_cast<type>(a_affecter.R_component_3()); \
86 d = static_cast<type>(a_affecter.R_component_4()); \
91 quaternion<type> & operator = (quaternion<type> const & a_affecter) \
101 quaternion<type> & operator = (type const & a_affecter) \
105 b = c = d = static_cast<type>(0); \
110 quaternion<type> & operator = (::std::complex<type> const & a_affecter) \
112 a = a_affecter.real(); \
113 b = a_affecter.imag(); \
115 c = d = static_cast<type>(0); \
121 #define BOOST_QUATERNION_MEMBER_DATA_GENERATOR(type) \
133 typedef T value_type;
136 // constructor for H seen as R^4
137 // (also default constructor)
139 explicit quaternion( T const & requested_a = T(),
140 T const & requested_b = T(),
141 T const & requested_c = T(),
142 T const & requested_d = T())
152 // constructor for H seen as C^2
154 explicit quaternion( ::std::complex<T> const & z0,
155 ::std::complex<T> const & z1 = ::std::complex<T>())
165 // UNtemplated copy constructor
166 // (this is taken care of by the compiler itself)
169 // templated copy constructor
172 explicit quaternion(quaternion<X> const & a_recopier)
173 : a(static_cast<T>(a_recopier.R_component_1())),
174 b(static_cast<T>(a_recopier.R_component_2())),
175 c(static_cast<T>(a_recopier.R_component_3())),
176 d(static_cast<T>(a_recopier.R_component_4()))
183 // (this is taken care of by the compiler itself)
188 // Note: Like complex number, quaternions do have a meaningful notion of "real part",
189 // but unlike them there is no meaningful notion of "imaginary part".
190 // Instead there is an "unreal part" which itself is a quaternion, and usually
191 // nothing simpler (as opposed to the complex number case).
192 // However, for practicallity, there are accessors for the other components
193 // (these are necessary for the templated copy constructor, for instance).
195 BOOST_QUATERNION_ACCESSOR_GENERATOR(T)
197 // assignment operators
199 BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(T)
201 // other assignment-related operators
203 // NOTE: Quaternion multiplication is *NOT* commutative;
204 // symbolically, "q *= rhs;" means "q = q * rhs;"
205 // and "q /= rhs;" means "q = q * inverse_of(rhs);"
207 quaternion<T> & operator += (T const & rhs)
209 T at = a + rhs; // exception guard
217 quaternion<T> & operator += (::std::complex<T> const & rhs)
219 T at = a + rhs.real(); // exception guard
220 T bt = b + rhs.imag(); // exception guard
230 quaternion<T> & operator += (quaternion<X> const & rhs)
232 T at = a + static_cast<T>(rhs.R_component_1()); // exception guard
233 T bt = b + static_cast<T>(rhs.R_component_2()); // exception guard
234 T ct = c + static_cast<T>(rhs.R_component_3()); // exception guard
235 T dt = d + static_cast<T>(rhs.R_component_4()); // exception guard
247 quaternion<T> & operator -= (T const & rhs)
249 T at = a - rhs; // exception guard
257 quaternion<T> & operator -= (::std::complex<T> const & rhs)
259 T at = a - rhs.real(); // exception guard
260 T bt = b - rhs.imag(); // exception guard
270 quaternion<T> & operator -= (quaternion<X> const & rhs)
272 T at = a - static_cast<T>(rhs.R_component_1()); // exception guard
273 T bt = b - static_cast<T>(rhs.R_component_2()); // exception guard
274 T ct = c - static_cast<T>(rhs.R_component_3()); // exception guard
275 T dt = d - static_cast<T>(rhs.R_component_4()); // exception guard
286 quaternion<T> & operator *= (T const & rhs)
288 T at = a * rhs; // exception guard
289 T bt = b * rhs; // exception guard
290 T ct = c * rhs; // exception guard
291 T dt = d * rhs; // exception guard
302 quaternion<T> & operator *= (::std::complex<T> const & rhs)
322 quaternion<T> & operator *= (quaternion<X> const & rhs)
324 T ar = static_cast<T>(rhs.R_component_1());
325 T br = static_cast<T>(rhs.R_component_2());
326 T cr = static_cast<T>(rhs.R_component_3());
327 T dr = static_cast<T>(rhs.R_component_4());
329 T at = +a*ar-b*br-c*cr-d*dr;
330 T bt = +a*br+b*ar+c*dr-d*cr; //(a*br+ar*b)+(c*dr-cr*d);
331 T ct = +a*cr-b*dr+c*ar+d*br; //(a*cr+ar*c)+(d*br-dr*b);
332 T dt = +a*dr+b*cr-c*br+d*ar; //(a*dr+ar*d)+(b*cr-br*c);
344 quaternion<T> & operator /= (T const & rhs)
346 T at = a / rhs; // exception guard
347 T bt = b / rhs; // exception guard
348 T ct = c / rhs; // exception guard
349 T dt = d / rhs; // exception guard
360 quaternion<T> & operator /= (::std::complex<T> const & rhs)
365 T denominator = ar*ar+br*br;
367 T at = (+a*ar+b*br)/denominator; //(a*ar+b*br)/denominator;
368 T bt = (-a*br+b*ar)/denominator; //(ar*b-a*br)/denominator;
369 T ct = (+c*ar-d*br)/denominator; //(ar*c-d*br)/denominator;
370 T dt = (+c*br+d*ar)/denominator; //(ar*d+br*c)/denominator;
382 quaternion<T> & operator /= (quaternion<X> const & rhs)
384 T ar = static_cast<T>(rhs.R_component_1());
385 T br = static_cast<T>(rhs.R_component_2());
386 T cr = static_cast<T>(rhs.R_component_3());
387 T dr = static_cast<T>(rhs.R_component_4());
389 T denominator = ar*ar+br*br+cr*cr+dr*dr;
391 T at = (+a*ar+b*br+c*cr+d*dr)/denominator; //(a*ar+b*br+c*cr+d*dr)/denominator;
392 T bt = (-a*br+b*ar-c*dr+d*cr)/denominator; //((ar*b-a*br)+(cr*d-c*dr))/denominator;
393 T ct = (-a*cr+b*dr+c*ar-d*br)/denominator; //((ar*c-a*cr)+(dr*b-d*br))/denominator;
394 T dt = (-a*dr-b*cr+c*br+d*ar)/denominator; //((ar*d-a*dr)+(br*c-b*cr))/denominator;
407 BOOST_QUATERNION_MEMBER_DATA_GENERATOR(T)
415 // declaration of quaternion specialization
417 template<> class quaternion<float>;
418 template<> class quaternion<double>;
419 template<> class quaternion<long double>;
422 // helper templates for converting copy constructors (declaration)
427 template< typename T,
430 quaternion<T> quaternion_type_converter(quaternion<U> const & rhs);
434 // implementation of quaternion specialization
437 #define BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(type) \
438 explicit quaternion( type const & requested_a = static_cast<type>(0), \
439 type const & requested_b = static_cast<type>(0), \
440 type const & requested_c = static_cast<type>(0), \
441 type const & requested_d = static_cast<type>(0)) \
449 explicit quaternion( ::std::complex<type> const & z0, \
450 ::std::complex<type> const & z1 = ::std::complex<type>()) \
459 #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1(type) \
460 quaternion<type> & operator += (type const & rhs) \
467 #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2(type) \
468 quaternion<type> & operator += (::std::complex<type> const & rhs) \
476 #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR_3(type) \
477 template<typename X> \
478 quaternion<type> & operator += (quaternion<X> const & rhs) \
480 a += static_cast<type>(rhs.R_component_1()); \
481 b += static_cast<type>(rhs.R_component_2()); \
482 c += static_cast<type>(rhs.R_component_3()); \
483 d += static_cast<type>(rhs.R_component_4()); \
488 #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR_1(type) \
489 quaternion<type> & operator -= (type const & rhs) \
496 #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR_2(type) \
497 quaternion<type> & operator -= (::std::complex<type> const & rhs) \
505 #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR_3(type) \
506 template<typename X> \
507 quaternion<type> & operator -= (quaternion<X> const & rhs) \
509 a -= static_cast<type>(rhs.R_component_1()); \
510 b -= static_cast<type>(rhs.R_component_2()); \
511 c -= static_cast<type>(rhs.R_component_3()); \
512 d -= static_cast<type>(rhs.R_component_4()); \
517 #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR_1(type) \
518 quaternion<type> & operator *= (type const & rhs) \
528 #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR_2(type) \
529 quaternion<type> & operator *= (::std::complex<type> const & rhs) \
531 type ar = rhs.real(); \
532 type br = rhs.imag(); \
534 type at = +a*ar-b*br; \
535 type bt = +a*br+b*ar; \
536 type ct = +c*ar+d*br; \
537 type dt = -c*br+d*ar; \
547 #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR_3(type) \
548 template<typename X> \
549 quaternion<type> & operator *= (quaternion<X> const & rhs) \
551 type ar = static_cast<type>(rhs.R_component_1()); \
552 type br = static_cast<type>(rhs.R_component_2()); \
553 type cr = static_cast<type>(rhs.R_component_3()); \
554 type dr = static_cast<type>(rhs.R_component_4()); \
556 type at = +a*ar-b*br-c*cr-d*dr; \
557 type bt = +a*br+b*ar+c*dr-d*cr; \
558 type ct = +a*cr-b*dr+c*ar+d*br; \
559 type dt = +a*dr+b*cr-c*br+d*ar; \
569 // There is quite a lot of repetition in the code below. This is intentional.
570 // The last conditional block is the normal form, and the others merely
571 // consist of workarounds for various compiler deficiencies. Hopefuly, when
572 // more compilers are conformant and we can retire support for those that are
573 // not, we will be able to remove the clutter. This is makes the situation
574 // (painfully) explicit.
576 #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_1(type) \
577 quaternion<type> & operator /= (type const & rhs) \
587 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
588 #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \
589 quaternion<type> & operator /= (::std::complex<type> const & rhs) \
591 using ::std::valarray; \
594 valarray<type> tr(2); \
596 tr[0] = rhs.real(); \
597 tr[1] = rhs.imag(); \
599 type mixam = static_cast<type>(1)/(abs(tr).max)(); \
603 valarray<type> tt(4); \
605 tt[0] = +a*tr[0]+b*tr[1]; \
606 tt[1] = -a*tr[1]+b*tr[0]; \
607 tt[2] = +c*tr[0]-d*tr[1]; \
608 tt[3] = +c*tr[1]+d*tr[0]; \
612 tt *= (mixam/tr.sum()); \
622 #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \
623 quaternion<type> & operator /= (::std::complex<type> const & rhs) \
625 using ::std::valarray; \
627 valarray<type> tr(2); \
629 tr[0] = rhs.real(); \
630 tr[1] = rhs.imag(); \
632 type mixam = static_cast<type>(1)/(abs(tr).max)(); \
636 valarray<type> tt(4); \
638 tt[0] = +a*tr[0]+b*tr[1]; \
639 tt[1] = -a*tr[1]+b*tr[0]; \
640 tt[2] = +c*tr[0]-d*tr[1]; \
641 tt[3] = +c*tr[1]+d*tr[0]; \
645 tt *= (mixam/tr.sum()); \
654 #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
656 #if defined(BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP)
657 #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type) \
658 template<typename X> \
659 quaternion<type> & operator /= (quaternion<X> const & rhs) \
661 using ::std::valarray; \
664 valarray<type> tr(4); \
666 tr[0] = static_cast<type>(rhs.R_component_1()); \
667 tr[1] = static_cast<type>(rhs.R_component_2()); \
668 tr[2] = static_cast<type>(rhs.R_component_3()); \
669 tr[3] = static_cast<type>(rhs.R_component_4()); \
671 type mixam = static_cast<type>(1)/(abs(tr).max)(); \
675 valarray<type> tt(4); \
677 tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \
678 tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \
679 tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \
680 tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \
684 tt *= (mixam/tr.sum()); \
694 #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type) \
695 template<typename X> \
696 quaternion<type> & operator /= (quaternion<X> const & rhs) \
698 using ::std::valarray; \
700 valarray<type> tr(4); \
702 tr[0] = static_cast<type>(rhs.R_component_1()); \
703 tr[1] = static_cast<type>(rhs.R_component_2()); \
704 tr[2] = static_cast<type>(rhs.R_component_3()); \
705 tr[3] = static_cast<type>(rhs.R_component_4()); \
707 type mixam = static_cast<type>(1)/(abs(tr).max)(); \
711 valarray<type> tt(4); \
713 tt[0] = +a*tr[0]+b*tr[1]+c*tr[2]+d*tr[3]; \
714 tt[1] = -a*tr[1]+b*tr[0]-c*tr[3]+d*tr[2]; \
715 tt[2] = -a*tr[2]+b*tr[3]+c*tr[0]-d*tr[1]; \
716 tt[3] = -a*tr[3]-b*tr[2]+c*tr[1]+d*tr[0]; \
720 tt *= (mixam/tr.sum()); \
729 #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
731 #define BOOST_QUATERNION_MEMBER_ADD_GENERATOR(type) \
732 BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1(type) \
733 BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2(type) \
734 BOOST_QUATERNION_MEMBER_ADD_GENERATOR_3(type)
736 #define BOOST_QUATERNION_MEMBER_SUB_GENERATOR(type) \
737 BOOST_QUATERNION_MEMBER_SUB_GENERATOR_1(type) \
738 BOOST_QUATERNION_MEMBER_SUB_GENERATOR_2(type) \
739 BOOST_QUATERNION_MEMBER_SUB_GENERATOR_3(type)
741 #define BOOST_QUATERNION_MEMBER_MUL_GENERATOR(type) \
742 BOOST_QUATERNION_MEMBER_MUL_GENERATOR_1(type) \
743 BOOST_QUATERNION_MEMBER_MUL_GENERATOR_2(type) \
744 BOOST_QUATERNION_MEMBER_MUL_GENERATOR_3(type)
746 #define BOOST_QUATERNION_MEMBER_DIV_GENERATOR(type) \
747 BOOST_QUATERNION_MEMBER_DIV_GENERATOR_1(type) \
748 BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2(type) \
749 BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3(type)
751 #define BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(type) \
752 BOOST_QUATERNION_MEMBER_ADD_GENERATOR(type) \
753 BOOST_QUATERNION_MEMBER_SUB_GENERATOR(type) \
754 BOOST_QUATERNION_MEMBER_MUL_GENERATOR(type) \
755 BOOST_QUATERNION_MEMBER_DIV_GENERATOR(type)
759 class quaternion<float>
763 typedef float value_type;
765 BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(float)
767 // UNtemplated copy constructor
768 // (this is taken care of by the compiler itself)
770 // explicit copy constructors (precision-loosing converters)
772 explicit quaternion(quaternion<double> const & a_recopier)
774 *this = detail::quaternion_type_converter<float, double>(a_recopier);
777 explicit quaternion(quaternion<long double> const & a_recopier)
779 *this = detail::quaternion_type_converter<float, long double>(a_recopier);
783 // (this is taken care of by the compiler itself)
787 // Note: Like complex number, quaternions do have a meaningful notion of "real part",
788 // but unlike them there is no meaningful notion of "imaginary part".
789 // Instead there is an "unreal part" which itself is a quaternion, and usually
790 // nothing simpler (as opposed to the complex number case).
791 // However, for practicallity, there are accessors for the other components
792 // (these are necessary for the templated copy constructor, for instance).
794 BOOST_QUATERNION_ACCESSOR_GENERATOR(float)
796 // assignment operators
798 BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(float)
800 // other assignment-related operators
802 // NOTE: Quaternion multiplication is *NOT* commutative;
803 // symbolically, "q *= rhs;" means "q = q * rhs;"
804 // and "q /= rhs;" means "q = q * inverse_of(rhs);"
806 BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(float)
811 BOOST_QUATERNION_MEMBER_DATA_GENERATOR(float)
820 class quaternion<double>
824 typedef double value_type;
826 BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(double)
828 // UNtemplated copy constructor
829 // (this is taken care of by the compiler itself)
831 // converting copy constructor
833 explicit quaternion(quaternion<float> const & a_recopier)
835 *this = detail::quaternion_type_converter<double, float>(a_recopier);
838 // explicit copy constructors (precision-loosing converters)
840 explicit quaternion(quaternion<long double> const & a_recopier)
842 *this = detail::quaternion_type_converter<double, long double>(a_recopier);
846 // (this is taken care of by the compiler itself)
850 // Note: Like complex number, quaternions do have a meaningful notion of "real part",
851 // but unlike them there is no meaningful notion of "imaginary part".
852 // Instead there is an "unreal part" which itself is a quaternion, and usually
853 // nothing simpler (as opposed to the complex number case).
854 // However, for practicallity, there are accessors for the other components
855 // (these are necessary for the templated copy constructor, for instance).
857 BOOST_QUATERNION_ACCESSOR_GENERATOR(double)
859 // assignment operators
861 BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(double)
863 // other assignment-related operators
865 // NOTE: Quaternion multiplication is *NOT* commutative;
866 // symbolically, "q *= rhs;" means "q = q * rhs;"
867 // and "q /= rhs;" means "q = q * inverse_of(rhs);"
869 BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(double)
874 BOOST_QUATERNION_MEMBER_DATA_GENERATOR(double)
883 class quaternion<long double>
887 typedef long double value_type;
889 BOOST_QUATERNION_CONSTRUCTOR_GENERATOR(long double)
891 // UNtemplated copy constructor
892 // (this is taken care of by the compiler itself)
894 // converting copy constructors
896 explicit quaternion(quaternion<float> const & a_recopier)
898 *this = detail::quaternion_type_converter<long double, float>(a_recopier);
901 explicit quaternion(quaternion<double> const & a_recopier)
903 *this = detail::quaternion_type_converter<long double, double>(a_recopier);
907 // (this is taken care of by the compiler itself)
911 // Note: Like complex number, quaternions do have a meaningful notion of "real part",
912 // but unlike them there is no meaningful notion of "imaginary part".
913 // Instead there is an "unreal part" which itself is a quaternion, and usually
914 // nothing simpler (as opposed to the complex number case).
915 // However, for practicallity, there are accessors for the other components
916 // (these are necessary for the templated copy constructor, for instance).
918 BOOST_QUATERNION_ACCESSOR_GENERATOR(long double)
920 // assignment operators
922 BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR(long double)
924 // other assignment-related operators
926 // NOTE: Quaternion multiplication is *NOT* commutative;
927 // symbolically, "q *= rhs;" means "q = q * rhs;"
928 // and "q /= rhs;" means "q = q * inverse_of(rhs);"
930 BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR(long double)
935 BOOST_QUATERNION_MEMBER_DATA_GENERATOR(long double)
943 #undef BOOST_QUATERNION_MEMBER_ALGEBRAIC_GENERATOR
944 #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR
945 #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR
946 #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR
947 #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR
948 #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR_1
949 #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR_2
950 #undef BOOST_QUATERNION_MEMBER_ADD_GENERATOR_3
951 #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR_1
952 #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR_2
953 #undef BOOST_QUATERNION_MEMBER_SUB_GENERATOR_3
954 #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR_1
955 #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR_2
956 #undef BOOST_QUATERNION_MEMBER_MUL_GENERATOR_3
957 #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR_1
958 #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR_2
959 #undef BOOST_QUATERNION_MEMBER_DIV_GENERATOR_3
961 #undef BOOST_QUATERNION_CONSTRUCTOR_GENERATOR
964 #undef BOOST_QUATERNION_MEMBER_ASSIGNMENT_GENERATOR
966 #undef BOOST_QUATERNION_MEMBER_DATA_GENERATOR
968 #undef BOOST_QUATERNION_ACCESSOR_GENERATOR
973 #define BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op) \
975 quaternion<T> res(lhs); \
980 #define BOOST_QUATERNION_OPERATOR_GENERATOR_1_L(op) \
981 template<typename T> \
982 inline quaternion<T> operator op (T const & lhs, quaternion<T> const & rhs) \
983 BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op)
985 #define BOOST_QUATERNION_OPERATOR_GENERATOR_1_R(op) \
986 template<typename T> \
987 inline quaternion<T> operator op (quaternion<T> const & lhs, T const & rhs) \
988 BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op)
990 #define BOOST_QUATERNION_OPERATOR_GENERATOR_2_L(op) \
991 template<typename T> \
992 inline quaternion<T> operator op (::std::complex<T> const & lhs, quaternion<T> const & rhs) \
993 BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op)
995 #define BOOST_QUATERNION_OPERATOR_GENERATOR_2_R(op) \
996 template<typename T> \
997 inline quaternion<T> operator op (quaternion<T> const & lhs, ::std::complex<T> const & rhs) \
998 BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op)
1000 #define BOOST_QUATERNION_OPERATOR_GENERATOR_3(op) \
1001 template<typename T> \
1002 inline quaternion<T> operator op (quaternion<T> const & lhs, quaternion<T> const & rhs) \
1003 BOOST_QUATERNION_OPERATOR_GENERATOR_BODY(op)
1005 #define BOOST_QUATERNION_OPERATOR_GENERATOR(op) \
1006 BOOST_QUATERNION_OPERATOR_GENERATOR_1_L(op) \
1007 BOOST_QUATERNION_OPERATOR_GENERATOR_1_R(op) \
1008 BOOST_QUATERNION_OPERATOR_GENERATOR_2_L(op) \
1009 BOOST_QUATERNION_OPERATOR_GENERATOR_2_R(op) \
1010 BOOST_QUATERNION_OPERATOR_GENERATOR_3(op)
1013 BOOST_QUATERNION_OPERATOR_GENERATOR(+)
1014 BOOST_QUATERNION_OPERATOR_GENERATOR(-)
1015 BOOST_QUATERNION_OPERATOR_GENERATOR(*)
1016 BOOST_QUATERNION_OPERATOR_GENERATOR(/)
1019 #undef BOOST_QUATERNION_OPERATOR_GENERATOR
1021 #undef BOOST_QUATERNION_OPERATOR_GENERATOR_1_L
1022 #undef BOOST_QUATERNION_OPERATOR_GENERATOR_1_R
1023 #undef BOOST_QUATERNION_OPERATOR_GENERATOR_2_L
1024 #undef BOOST_QUATERNION_OPERATOR_GENERATOR_2_R
1025 #undef BOOST_QUATERNION_OPERATOR_GENERATOR_3
1027 #undef BOOST_QUATERNION_OPERATOR_GENERATOR_BODY
1030 template<typename T>
1031 inline quaternion<T> operator + (quaternion<T> const & q)
1037 template<typename T>
1038 inline quaternion<T> operator - (quaternion<T> const & q)
1040 return(quaternion<T>(-q.R_component_1(),-q.R_component_2(),-q.R_component_3(),-q.R_component_4()));
1044 template<typename T>
1045 inline bool operator == (T const & lhs, quaternion<T> const & rhs)
1048 (rhs.R_component_1() == lhs)&&
1049 (rhs.R_component_2() == static_cast<T>(0))&&
1050 (rhs.R_component_3() == static_cast<T>(0))&&
1051 (rhs.R_component_4() == static_cast<T>(0))
1056 template<typename T>
1057 inline bool operator == (quaternion<T> const & lhs, T const & rhs)
1060 (lhs.R_component_1() == rhs)&&
1061 (lhs.R_component_2() == static_cast<T>(0))&&
1062 (lhs.R_component_3() == static_cast<T>(0))&&
1063 (lhs.R_component_4() == static_cast<T>(0))
1068 template<typename T>
1069 inline bool operator == (::std::complex<T> const & lhs, quaternion<T> const & rhs)
1072 (rhs.R_component_1() == lhs.real())&&
1073 (rhs.R_component_2() == lhs.imag())&&
1074 (rhs.R_component_3() == static_cast<T>(0))&&
1075 (rhs.R_component_4() == static_cast<T>(0))
1080 template<typename T>
1081 inline bool operator == (quaternion<T> const & lhs, ::std::complex<T> const & rhs)
1084 (lhs.R_component_1() == rhs.real())&&
1085 (lhs.R_component_2() == rhs.imag())&&
1086 (lhs.R_component_3() == static_cast<T>(0))&&
1087 (lhs.R_component_4() == static_cast<T>(0))
1092 template<typename T>
1093 inline bool operator == (quaternion<T> const & lhs, quaternion<T> const & rhs)
1096 (rhs.R_component_1() == lhs.R_component_1())&&
1097 (rhs.R_component_2() == lhs.R_component_2())&&
1098 (rhs.R_component_3() == lhs.R_component_3())&&
1099 (rhs.R_component_4() == lhs.R_component_4())
1104 #define BOOST_QUATERNION_NOT_EQUAL_GENERATOR \
1106 return(!(lhs == rhs)); \
1109 template<typename T>
1110 inline bool operator != (T const & lhs, quaternion<T> const & rhs)
1111 BOOST_QUATERNION_NOT_EQUAL_GENERATOR
1113 template<typename T>
1114 inline bool operator != (quaternion<T> const & lhs, T const & rhs)
1115 BOOST_QUATERNION_NOT_EQUAL_GENERATOR
1117 template<typename T>
1118 inline bool operator != (::std::complex<T> const & lhs, quaternion<T> const & rhs)
1119 BOOST_QUATERNION_NOT_EQUAL_GENERATOR
1121 template<typename T>
1122 inline bool operator != (quaternion<T> const & lhs, ::std::complex<T> const & rhs)
1123 BOOST_QUATERNION_NOT_EQUAL_GENERATOR
1125 template<typename T>
1126 inline bool operator != (quaternion<T> const & lhs, quaternion<T> const & rhs)
1127 BOOST_QUATERNION_NOT_EQUAL_GENERATOR
1129 #undef BOOST_QUATERNION_NOT_EQUAL_GENERATOR
1132 // Note: we allow the following formats, whith a, b, c, and d reals
1134 // (a), (a,b), (a,b,c), (a,b,c,d)
1135 // (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))
1136 template<typename T, typename charT, class traits>
1137 ::std::basic_istream<charT,traits> & operator >> ( ::std::basic_istream<charT,traits> & is,
1141 #ifdef BOOST_NO_STD_LOCALE
1143 const ::std::ctype<charT> & ct = ::std::use_facet< ::std::ctype<charT> >(is.getloc());
1144 #endif /* BOOST_NO_STD_LOCALE */
1151 ::std::complex<T> u = ::std::complex<T>();
1152 ::std::complex<T> v = ::std::complex<T>();
1157 is >> ch; // get the first lexeme
1159 if (!is.good()) goto finish;
1161 #ifdef BOOST_NO_STD_LOCALE
1164 cc = ct.narrow(ch, char());
1165 #endif /* BOOST_NO_STD_LOCALE */
1167 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,))
1169 is >> ch; // get the second lexeme
1171 if (!is.good()) goto finish;
1173 #ifdef BOOST_NO_STD_LOCALE
1176 cc = ct.narrow(ch, char());
1177 #endif /* BOOST_NO_STD_LOCALE */
1179 if (cc == '(') // read "((", possible: ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b,),(c,d,))
1183 is >> u; // we extract the first and second components
1187 if (!is.good()) goto finish;
1189 is >> ch; // get the next lexeme
1191 if (!is.good()) goto finish;
1193 #ifdef BOOST_NO_STD_LOCALE
1196 cc = ct.narrow(ch, char());
1197 #endif /* BOOST_NO_STD_LOCALE */
1199 if (cc == ')') // format: ((a)) or ((a,b))
1201 q = quaternion<T>(a,b);
1203 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,))
1205 is >> v; // we extract the third and fourth components
1209 if (!is.good()) goto finish;
1211 is >> ch; // get the last lexeme
1213 if (!is.good()) goto finish;
1215 #ifdef BOOST_NO_STD_LOCALE
1218 cc = ct.narrow(ch, char());
1219 #endif /* BOOST_NO_STD_LOCALE */
1221 if (cc == ')') // format: ((a),c), ((a),(c)), ((a),(c,d)), ((a,b),c), ((a,b),(c)) or ((a,b,),(c,d,))
1223 q = quaternion<T>(a,b,c,d);
1227 is.setstate(::std::ios_base::failbit);
1232 is.setstate(::std::ios_base::failbit);
1235 else // read "(a", possible: (a), (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d))
1239 is >> a; // we extract the first component
1241 if (!is.good()) goto finish;
1243 is >> ch; // get the third lexeme
1245 if (!is.good()) goto finish;
1247 #ifdef BOOST_NO_STD_LOCALE
1250 cc = ct.narrow(ch, char());
1251 #endif /* BOOST_NO_STD_LOCALE */
1253 if (cc == ')') // format: (a)
1255 q = quaternion<T>(a);
1257 else if (cc == ',') // read "(a,", possible: (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d))
1259 is >> ch; // get the fourth lexeme
1261 if (!is.good()) goto finish;
1263 #ifdef BOOST_NO_STD_LOCALE
1266 cc = ct.narrow(ch, char());
1267 #endif /* BOOST_NO_STD_LOCALE */
1269 if (cc == '(') // read "(a,(", possible: (a,(c)), (a,(c,d))
1273 is >> v; // we extract the third and fourth component
1278 if (!is.good()) goto finish;
1280 is >> ch; // get the ninth lexeme
1282 if (!is.good()) goto finish;
1284 #ifdef BOOST_NO_STD_LOCALE
1287 cc = ct.narrow(ch, char());
1288 #endif /* BOOST_NO_STD_LOCALE */
1290 if (cc == ')') // format: (a,(c)) or (a,(c,d))
1292 q = quaternion<T>(a,b,c,d);
1296 is.setstate(::std::ios_base::failbit);
1299 else // read "(a,b", possible: (a,b), (a,b,c), (a,b,c,d)
1303 is >> b; // we extract the second component
1305 if (!is.good()) goto finish;
1307 is >> ch; // get the fifth lexeme
1309 if (!is.good()) goto finish;
1311 #ifdef BOOST_NO_STD_LOCALE
1314 cc = ct.narrow(ch, char());
1315 #endif /* BOOST_NO_STD_LOCALE */
1317 if (cc == ')') // format: (a,b)
1319 q = quaternion<T>(a,b);
1321 else if (cc == ',') // read "(a,b,", possible: (a,b,c), (a,b,c,d)
1323 is >> c; // we extract the third component
1325 if (!is.good()) goto finish;
1327 is >> ch; // get the seventh lexeme
1329 if (!is.good()) goto finish;
1331 #ifdef BOOST_NO_STD_LOCALE
1334 cc = ct.narrow(ch, char());
1335 #endif /* BOOST_NO_STD_LOCALE */
1337 if (cc == ')') // format: (a,b,c)
1339 q = quaternion<T>(a,b,c);
1341 else if (cc == ',') // read "(a,b,c,", possible: (a,b,c,d)
1343 is >> d; // we extract the fourth component
1345 if (!is.good()) goto finish;
1347 is >> ch; // get the ninth lexeme
1349 if (!is.good()) goto finish;
1351 #ifdef BOOST_NO_STD_LOCALE
1354 cc = ct.narrow(ch, char());
1355 #endif /* BOOST_NO_STD_LOCALE */
1357 if (cc == ')') // format: (a,b,c,d)
1359 q = quaternion<T>(a,b,c,d);
1363 is.setstate(::std::ios_base::failbit);
1368 is.setstate(::std::ios_base::failbit);
1373 is.setstate(::std::ios_base::failbit);
1379 is.setstate(::std::ios_base::failbit);
1387 is >> a; // we extract the first component
1389 if (!is.good()) goto finish;
1391 q = quaternion<T>(a);
1399 template<typename T, typename charT, class traits>
1400 ::std::basic_ostream<charT,traits> & operator << ( ::std::basic_ostream<charT,traits> & os,
1401 quaternion<T> const & q)
1403 ::std::basic_ostringstream<charT,traits> s;
1405 s.flags(os.flags());
1406 #ifdef BOOST_NO_STD_LOCALE
1408 s.imbue(os.getloc());
1409 #endif /* BOOST_NO_STD_LOCALE */
1410 s.precision(os.precision());
1412 s << '(' << q.R_component_1() << ','
1413 << q.R_component_2() << ','
1414 << q.R_component_3() << ','
1415 << q.R_component_4() << ')';
1417 return os << s.str();
1423 template<typename T>
1424 inline T real(quaternion<T> const & q)
1430 template<typename T>
1431 inline quaternion<T> unreal(quaternion<T> const & q)
1437 #define BOOST_QUATERNION_VALARRAY_LOADER \
1438 using ::std::valarray; \
1440 valarray<T> temp(4); \
1442 temp[0] = q.R_component_1(); \
1443 temp[1] = q.R_component_2(); \
1444 temp[2] = q.R_component_3(); \
1445 temp[3] = q.R_component_4();
1448 template<typename T>
1449 inline T sup(quaternion<T> const & q)
1451 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
1453 #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
1455 BOOST_QUATERNION_VALARRAY_LOADER
1457 return((abs(temp).max)());
1461 template<typename T>
1462 inline T l1(quaternion<T> const & q)
1464 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
1466 #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
1468 BOOST_QUATERNION_VALARRAY_LOADER
1470 return(abs(temp).sum());
1474 template<typename T>
1475 inline T abs(quaternion<T> const & q)
1477 #ifdef BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP
1479 #endif /* BOOST_NO_ARGUMENT_DEPENDENT_LOOKUP */
1483 BOOST_QUATERNION_VALARRAY_LOADER
1485 T maxim = (abs(temp).max)(); // overflow protection
1487 if (maxim == static_cast<T>(0))
1493 T mixam = static_cast<T>(1)/maxim; // prefer multiplications over divisions
1499 return(maxim*sqrt(temp.sum()));
1502 //return(sqrt(norm(q)));
1506 #undef BOOST_QUATERNION_VALARRAY_LOADER
1509 // Note: This is the Cayley norm, not the Euclidian norm...
1511 template<typename T>
1512 inline T norm(quaternion<T>const & q)
1514 return(real(q*conj(q)));
1518 template<typename T>
1519 inline quaternion<T> conj(quaternion<T> const & q)
1521 return(quaternion<T>( +q.R_component_1(),
1524 -q.R_component_4()));
1528 template<typename T>
1529 inline quaternion<T> spherical( T const & rho,
1537 //T a = cos(theta)*cos(phi1)*cos(phi2);
1538 //T b = sin(theta)*cos(phi1)*cos(phi2);
1539 //T c = sin(phi1)*cos(phi2);
1542 T courrant = static_cast<T>(1);
1546 courrant *= cos(phi2);
1548 T c = sin(phi1)*courrant;
1550 courrant *= cos(phi1);
1552 T b = sin(theta)*courrant;
1553 T a = cos(theta)*courrant;
1555 return(rho*quaternion<T>(a,b,c,d));
1559 template<typename T>
1560 inline quaternion<T> semipolar( T const & rho,
1568 T a = cos(alpha)*cos(theta1);
1569 T b = cos(alpha)*sin(theta1);
1570 T c = sin(alpha)*cos(theta2);
1571 T d = sin(alpha)*sin(theta2);
1573 return(rho*quaternion<T>(a,b,c,d));
1577 template<typename T>
1578 inline quaternion<T> multipolar( T const & rho1,
1586 T a = rho1*cos(theta1);
1587 T b = rho1*sin(theta1);
1588 T c = rho2*cos(theta2);
1589 T d = rho2*sin(theta2);
1591 return(quaternion<T>(a,b,c,d));
1595 template<typename T>
1596 inline quaternion<T> cylindrospherical( T const & t,
1598 T const & longitude,
1606 T b = radius*cos(longitude)*cos(latitude);
1607 T c = radius*sin(longitude)*cos(latitude);
1608 T d = radius*sin(latitude);
1610 return(quaternion<T>(t,b,c,d));
1614 template<typename T>
1615 inline quaternion<T> cylindrical(T const & r,
1626 return(quaternion<T>(a,b,h1,h2));
1631 // (please see the documentation)
1634 template<typename T>
1635 inline quaternion<T> exp(quaternion<T> const & q)
1640 using ::boost::math::sinc_pi;
1644 T z = abs(unreal(q));
1648 return(u*quaternion<T>(cos(z),
1649 w*q.R_component_2(), w*q.R_component_3(),
1650 w*q.R_component_4()));
1654 template<typename T>
1655 inline quaternion<T> cos(quaternion<T> const & q)
1661 using ::boost::math::sinhc_pi;
1663 T z = abs(unreal(q));
1665 T w = -sin(q.real())*sinhc_pi(z);
1667 return(quaternion<T>(cos(q.real())*cosh(z),
1668 w*q.R_component_2(), w*q.R_component_3(),
1669 w*q.R_component_4()));
1673 template<typename T>
1674 inline quaternion<T> sin(quaternion<T> const & q)
1680 using ::boost::math::sinhc_pi;
1682 T z = abs(unreal(q));
1684 T w = +cos(q.real())*sinhc_pi(z);
1686 return(quaternion<T>(sin(q.real())*cosh(z),
1687 w*q.R_component_2(), w*q.R_component_3(),
1688 w*q.R_component_4()));
1692 template<typename T>
1693 inline quaternion<T> tan(quaternion<T> const & q)
1695 return(sin(q)/cos(q));
1699 template<typename T>
1700 inline quaternion<T> cosh(quaternion<T> const & q)
1702 return((exp(+q)+exp(-q))/static_cast<T>(2));
1706 template<typename T>
1707 inline quaternion<T> sinh(quaternion<T> const & q)
1709 return((exp(+q)-exp(-q))/static_cast<T>(2));
1713 template<typename T>
1714 inline quaternion<T> tanh(quaternion<T> const & q)
1716 return(sinh(q)/cosh(q));
1720 template<typename T>
1721 quaternion<T> pow(quaternion<T> const & q,
1728 quaternion<T> result = pow(q, m);
1734 result *= q; // n odd
1745 return(quaternion<T>(static_cast<T>(1)));
1749 return(pow(quaternion<T>(static_cast<T>(1))/q,-n));
1754 // helper templates for converting copy constructors (definition)
1759 template< typename T,
1762 quaternion<T> quaternion_type_converter(quaternion<U> const & rhs)
1764 return(quaternion<T>( static_cast<T>(rhs.R_component_1()),
1765 static_cast<T>(rhs.R_component_2()),
1766 static_cast<T>(rhs.R_component_3()),
1767 static_cast<T>(rhs.R_component_4())));
1773 #endif /* BOOST_QUATERNION_HPP */