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
13 #include <boost/config.hpp> // for BOOST_NO_STD_LOCALE
14 #include <boost/math_fwd.hpp>
15 #include <boost/detail/workaround.hpp>
16 #include <boost/type_traits/is_convertible.hpp>
17 #include <boost/utility/enable_if.hpp>
18 #ifndef BOOST_NO_STD_LOCALE
19 # include <locale> // for the "<<" operator
20 #endif /* BOOST_NO_STD_LOCALE */
23 #include <iosfwd> // for the "<<" and ">>" operators
24 #include <sstream> // for the "<<" operator
26 #include <boost/math/special_functions/sinc.hpp> // for the Sinus cardinal
27 #include <boost/math/special_functions/sinhc.hpp> // for the Hyperbolic Sinus cardinal
29 #if defined(BOOST_NO_CXX11_NOEXCEPT) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_NO_SFINAE_EXPR)
30 #include <boost/type_traits/is_pod.hpp>
40 #if !defined(BOOST_NO_CXX11_NOEXCEPT) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_SFINAE_EXPR)
43 struct is_trivial_arithmetic_type_imp
46 noexcept(std::declval<T&>() += std::declval<T>())
47 && noexcept(std::declval<T&>() -= std::declval<T>())
48 && noexcept(std::declval<T&>() *= std::declval<T>())
49 && noexcept(std::declval<T&>() /= std::declval<T>())
54 struct is_trivial_arithmetic_type : public is_trivial_arithmetic_type_imp<T>::type {};
58 struct is_trivial_arithmetic_type : public boost::is_pod<T> {};
64 #ifndef BOOST_NO_CXX14_CONSTEXPR
65 namespace constexpr_detail
68 constexpr void swap(T& a, T& b)
85 // constructor for H seen as R^4
86 // (also default constructor)
88 BOOST_CONSTEXPR explicit quaternion( T const & requested_a = T(),
89 T const & requested_b = T(),
90 T const & requested_c = T(),
91 T const & requested_d = T())
101 // constructor for H seen as C^2
103 BOOST_CONSTEXPR explicit quaternion( ::std::complex<T> const & z0,
104 ::std::complex<T> const & z1 = ::std::complex<T>())
114 // UNtemplated copy constructor
115 BOOST_CONSTEXPR quaternion(quaternion const & a_recopier)
116 : a(a_recopier.R_component_1()),
117 b(a_recopier.R_component_2()),
118 c(a_recopier.R_component_3()),
119 d(a_recopier.R_component_4()) {}
120 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
121 BOOST_CONSTEXPR quaternion(quaternion && a_recopier)
122 : a(std::move(a_recopier.R_component_1())),
123 b(std::move(a_recopier.R_component_2())),
124 c(std::move(a_recopier.R_component_3())),
125 d(std::move(a_recopier.R_component_4())) {}
128 // templated copy constructor
131 BOOST_CONSTEXPR explicit quaternion(quaternion<X> const & a_recopier)
132 : a(static_cast<T>(a_recopier.R_component_1())),
133 b(static_cast<T>(a_recopier.R_component_2())),
134 c(static_cast<T>(a_recopier.R_component_3())),
135 d(static_cast<T>(a_recopier.R_component_4()))
142 // (this is taken care of by the compiler itself)
147 // Note: Like complex number, quaternions do have a meaningful notion of "real part",
148 // but unlike them there is no meaningful notion of "imaginary part".
149 // Instead there is an "unreal part" which itself is a quaternion, and usually
150 // nothing simpler (as opposed to the complex number case).
151 // However, for practicallity, there are accessors for the other components
152 // (these are necessary for the templated copy constructor, for instance).
154 BOOST_CONSTEXPR T real() const
159 BOOST_CONSTEXPR quaternion<T> unreal() const
161 return(quaternion<T>(static_cast<T>(0), b, c, d));
164 BOOST_CONSTEXPR T R_component_1() const
169 BOOST_CONSTEXPR T R_component_2() const
174 BOOST_CONSTEXPR T R_component_3() const
179 BOOST_CONSTEXPR T R_component_4() const
184 BOOST_CONSTEXPR ::std::complex<T> C_component_1() const
186 return(::std::complex<T>(a, b));
189 BOOST_CONSTEXPR ::std::complex<T> C_component_2() const
191 return(::std::complex<T>(c, d));
194 BOOST_CXX14_CONSTEXPR void swap(quaternion& o)
196 #ifndef BOOST_NO_CXX14_CONSTEXPR
197 using constexpr_detail::swap;
207 // assignment operators
210 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (quaternion<X> const & a_affecter)
212 a = static_cast<T>(a_affecter.R_component_1());
213 b = static_cast<T>(a_affecter.R_component_2());
214 c = static_cast<T>(a_affecter.R_component_3());
215 d = static_cast<T>(a_affecter.R_component_4());
220 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (quaternion<T> const & a_affecter)
229 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
230 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (quaternion<T> && a_affecter)
232 a = std::move(a_affecter.a);
233 b = std::move(a_affecter.b);
234 c = std::move(a_affecter.c);
235 d = std::move(a_affecter.d);
240 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (T const & a_affecter)
244 b = c = d = static_cast<T>(0);
249 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (::std::complex<T> const & a_affecter)
251 a = a_affecter.real();
252 b = a_affecter.imag();
254 c = d = static_cast<T>(0);
259 // other assignment-related operators
261 // NOTE: Quaternion multiplication is *NOT* commutative;
262 // symbolically, "q *= rhs;" means "q = q * rhs;"
263 // and "q /= rhs;" means "q = q * inverse_of(rhs);"
265 // Note2: Each operator comes in 2 forms - one for the simple case where
266 // type T throws no exceptions, and one exception-safe version
267 // for the case where it might.
269 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(T const & rhs, const mpl::true_&)
274 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(T const & rhs, const mpl::false_&)
276 quaternion<T> result(a + rhs, b, c, d); // exception guard
280 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(std::complex<T> const & rhs, const mpl::true_&)
286 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(std::complex<T> const & rhs, const mpl::false_&)
288 quaternion<T> result(a + std::real(rhs), b + std::imag(rhs), c, d); // exception guard
293 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(quaternion<X> const & rhs, const mpl::true_&)
295 a += rhs.R_component_1();
296 b += rhs.R_component_2();
297 c += rhs.R_component_3();
298 d += rhs.R_component_4();
302 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(quaternion<X> const & rhs, const mpl::false_&)
304 quaternion<T> result(a + rhs.R_component_1(), b + rhs.R_component_2(), c + rhs.R_component_3(), d + rhs.R_component_4()); // exception guard
309 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(T const & rhs, const mpl::true_&)
314 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(T const & rhs, const mpl::false_&)
316 quaternion<T> result(a - rhs, b, c, d); // exception guard
320 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(std::complex<T> const & rhs, const mpl::true_&)
326 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(std::complex<T> const & rhs, const mpl::false_&)
328 quaternion<T> result(a - std::real(rhs), b - std::imag(rhs), c, d); // exception guard
333 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(quaternion<X> const & rhs, const mpl::true_&)
335 a -= rhs.R_component_1();
336 b -= rhs.R_component_2();
337 c -= rhs.R_component_3();
338 d -= rhs.R_component_4();
342 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(quaternion<X> const & rhs, const mpl::false_&)
344 quaternion<T> result(a - rhs.R_component_1(), b - rhs.R_component_2(), c - rhs.R_component_3(), d - rhs.R_component_4()); // exception guard
349 BOOST_CXX14_CONSTEXPR quaternion<T> & do_multiply(T const & rhs, const mpl::true_&)
357 BOOST_CXX14_CONSTEXPR quaternion<T> & do_multiply(T const & rhs, const mpl::false_&)
359 quaternion<T> result(a * rhs, b * rhs, c * rhs, d * rhs); // exception guard
364 BOOST_CXX14_CONSTEXPR quaternion<T> & do_divide(T const & rhs, const mpl::true_&)
372 BOOST_CXX14_CONSTEXPR quaternion<T> & do_divide(T const & rhs, const mpl::false_&)
374 quaternion<T> result(a / rhs, b / rhs, c / rhs, d / rhs); // exception guard
380 BOOST_CXX14_CONSTEXPR quaternion<T> & operator += (T const & rhs) { return do_add(rhs, detail::is_trivial_arithmetic_type<T>()); }
381 BOOST_CXX14_CONSTEXPR quaternion<T> & operator += (::std::complex<T> const & rhs) { return do_add(rhs, detail::is_trivial_arithmetic_type<T>()); }
382 template<typename X> BOOST_CXX14_CONSTEXPR quaternion<T> & operator += (quaternion<X> const & rhs) { return do_add(rhs, detail::is_trivial_arithmetic_type<T>()); }
384 BOOST_CXX14_CONSTEXPR quaternion<T> & operator -= (T const & rhs) { return do_subtract(rhs, detail::is_trivial_arithmetic_type<T>()); }
385 BOOST_CXX14_CONSTEXPR quaternion<T> & operator -= (::std::complex<T> const & rhs) { return do_subtract(rhs, detail::is_trivial_arithmetic_type<T>()); }
386 template<typename X> BOOST_CXX14_CONSTEXPR quaternion<T> & operator -= (quaternion<X> const & rhs) { return do_subtract(rhs, detail::is_trivial_arithmetic_type<T>()); }
388 BOOST_CXX14_CONSTEXPR quaternion<T> & operator *= (T const & rhs) { return do_multiply(rhs, detail::is_trivial_arithmetic_type<T>()); }
390 BOOST_CXX14_CONSTEXPR quaternion<T> & operator *= (::std::complex<T> const & rhs)
394 quaternion<T> result(a*ar - b*br, a*br + b*ar, c*ar + d*br, -c*br+d*ar);
400 BOOST_CXX14_CONSTEXPR quaternion<T> & operator *= (quaternion<X> const & rhs)
402 T ar = static_cast<T>(rhs.R_component_1());
403 T br = static_cast<T>(rhs.R_component_2());
404 T cr = static_cast<T>(rhs.R_component_3());
405 T dr = static_cast<T>(rhs.R_component_4());
407 quaternion<T> result(a*ar - b*br - c*cr - d*dr, a*br + b*ar + c*dr - d*cr, a*cr - b*dr + c*ar + d*br, a*dr + b*cr - c*br + d*ar);
412 BOOST_CXX14_CONSTEXPR quaternion<T> & operator /= (T const & rhs) { return do_divide(rhs, detail::is_trivial_arithmetic_type<T>()); }
414 BOOST_CXX14_CONSTEXPR quaternion<T> & operator /= (::std::complex<T> const & rhs)
418 T denominator = ar*ar+br*br;
419 quaternion<T> result((+a*ar + b*br) / denominator, (-a*br + b*ar) / denominator, (+c*ar - d*br) / denominator, (+c*br + d*ar) / denominator);
425 BOOST_CXX14_CONSTEXPR quaternion<T> & operator /= (quaternion<X> const & rhs)
427 T ar = static_cast<T>(rhs.R_component_1());
428 T br = static_cast<T>(rhs.R_component_2());
429 T cr = static_cast<T>(rhs.R_component_3());
430 T dr = static_cast<T>(rhs.R_component_4());
432 T denominator = ar*ar+br*br+cr*cr+dr*dr;
433 quaternion<T> result((+a*ar+b*br+c*cr+d*dr)/denominator, (-a*br+b*ar-c*dr+d*cr)/denominator, (-a*cr+b*dr+c*ar-d*br)/denominator, (-a*dr-b*cr+c*br+d*ar)/denominator);
444 BOOST_CXX14_CONSTEXPR void swap(quaternion<T>& a, quaternion<T>& b) { a.swap(b); }
447 template <class T1, class T2>
448 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
449 operator + (const quaternion<T1>& a, const T2& b)
451 return quaternion<T1>(static_cast<T1>(a.R_component_1() + b), a.R_component_2(), a.R_component_3(), a.R_component_4());
453 template <class T1, class T2>
454 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
455 operator + (const T1& a, const quaternion<T2>& b)
457 return quaternion<T2>(static_cast<T2>(b.R_component_1() + a), b.R_component_2(), b.R_component_3(), b.R_component_4());
459 template <class T1, class T2>
460 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
461 operator + (const quaternion<T1>& a, const std::complex<T2>& b)
463 return quaternion<T1>(a.R_component_1() + std::real(b), a.R_component_2() + std::imag(b), a.R_component_3(), a.R_component_4());
465 template <class T1, class T2>
466 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
467 operator + (const std::complex<T1>& a, const quaternion<T2>& b)
469 return quaternion<T1>(b.R_component_1() + real(a), b.R_component_2() + imag(a), b.R_component_3(), b.R_component_4());
472 inline BOOST_CONSTEXPR quaternion<T> operator + (const quaternion<T>& a, const quaternion<T>& b)
474 return quaternion<T>(a.R_component_1() + b.R_component_1(), a.R_component_2() + b.R_component_2(), a.R_component_3() + b.R_component_3(), a.R_component_4() + b.R_component_4());
477 template <class T1, class T2>
478 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
479 operator - (const quaternion<T1>& a, const T2& b)
481 return quaternion<T1>(static_cast<T1>(a.R_component_1() - b), a.R_component_2(), a.R_component_3(), a.R_component_4());
483 template <class T1, class T2>
484 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
485 operator - (const T1& a, const quaternion<T2>& b)
487 return quaternion<T2>(static_cast<T2>(a - b.R_component_1()), -b.R_component_2(), -b.R_component_3(), -b.R_component_4());
489 template <class T1, class T2>
490 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
491 operator - (const quaternion<T1>& a, const std::complex<T2>& b)
493 return quaternion<T1>(a.R_component_1() - std::real(b), a.R_component_2() - std::imag(b), a.R_component_3(), a.R_component_4());
495 template <class T1, class T2>
496 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
497 operator - (const std::complex<T1>& a, const quaternion<T2>& b)
499 return quaternion<T1>(real(a) - b.R_component_1(), imag(a) - b.R_component_2(), -b.R_component_3(), -b.R_component_4());
502 inline BOOST_CONSTEXPR quaternion<T> operator - (const quaternion<T>& a, const quaternion<T>& b)
504 return quaternion<T>(a.R_component_1() - b.R_component_1(), a.R_component_2() - b.R_component_2(), a.R_component_3() - b.R_component_3(), a.R_component_4() - b.R_component_4());
508 template <class T1, class T2>
509 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
510 operator * (const quaternion<T1>& a, const T2& b)
512 return quaternion<T1>(static_cast<T1>(a.R_component_1() * b), a.R_component_2() * b, a.R_component_3() * b, a.R_component_4() * b);
514 template <class T1, class T2>
515 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
516 operator * (const T1& a, const quaternion<T2>& b)
518 return quaternion<T2>(static_cast<T2>(a * b.R_component_1()), a * b.R_component_2(), a * b.R_component_3(), a * b.R_component_4());
520 template <class T1, class T2>
521 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
522 operator * (const quaternion<T1>& a, const std::complex<T2>& b)
524 quaternion<T1> result(a);
528 template <class T1, class T2>
529 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
530 operator * (const std::complex<T1>& a, const quaternion<T2>& b)
532 quaternion<T1> result(a);
537 inline BOOST_CXX14_CONSTEXPR quaternion<T> operator * (const quaternion<T>& a, const quaternion<T>& b)
539 quaternion<T> result(a);
545 template <class T1, class T2>
546 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
547 operator / (const quaternion<T1>& a, const T2& b)
549 return quaternion<T1>(a.R_component_1() / b, a.R_component_2() / b, a.R_component_3() / b, a.R_component_4() / b);
551 template <class T1, class T2>
552 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
553 operator / (const T1& a, const quaternion<T2>& b)
555 quaternion<T2> result(a);
559 template <class T1, class T2>
560 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
561 operator / (const quaternion<T1>& a, const std::complex<T2>& b)
563 quaternion<T1> result(a);
567 template <class T1, class T2>
568 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
569 operator / (const std::complex<T1>& a, const quaternion<T2>& b)
571 quaternion<T2> result(a);
576 inline BOOST_CXX14_CONSTEXPR quaternion<T> operator / (const quaternion<T>& a, const quaternion<T>& b)
578 quaternion<T> result(a);
585 inline BOOST_CONSTEXPR const quaternion<T>& operator + (quaternion<T> const & q)
592 inline BOOST_CONSTEXPR quaternion<T> operator - (quaternion<T> const & q)
594 return(quaternion<T>(-q.R_component_1(),-q.R_component_2(),-q.R_component_3(),-q.R_component_4()));
598 template<typename R, typename T>
599 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<R, T>::value, bool>::type operator == (R const & lhs, quaternion<T> const & rhs)
602 (rhs.R_component_1() == lhs)&&
603 (rhs.R_component_2() == static_cast<T>(0))&&
604 (rhs.R_component_3() == static_cast<T>(0))&&
605 (rhs.R_component_4() == static_cast<T>(0))
610 template<typename T, typename R>
611 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<R, T>::value, bool>::type operator == (quaternion<T> const & lhs, R const & rhs)
618 inline BOOST_CONSTEXPR bool operator == (::std::complex<T> const & lhs, quaternion<T> const & rhs)
621 (rhs.R_component_1() == lhs.real())&&
622 (rhs.R_component_2() == lhs.imag())&&
623 (rhs.R_component_3() == static_cast<T>(0))&&
624 (rhs.R_component_4() == static_cast<T>(0))
630 inline BOOST_CONSTEXPR bool operator == (quaternion<T> const & lhs, ::std::complex<T> const & rhs)
637 inline BOOST_CONSTEXPR bool operator == (quaternion<T> const & lhs, quaternion<T> const & rhs)
640 (rhs.R_component_1() == lhs.R_component_1())&&
641 (rhs.R_component_2() == lhs.R_component_2())&&
642 (rhs.R_component_3() == lhs.R_component_3())&&
643 (rhs.R_component_4() == lhs.R_component_4())
647 template<typename R, typename T> inline BOOST_CONSTEXPR bool operator != (R const & lhs, quaternion<T> const & rhs) { return !(lhs == rhs); }
648 template<typename T, typename R> inline BOOST_CONSTEXPR bool operator != (quaternion<T> const & lhs, R const & rhs) { return !(lhs == rhs); }
649 template<typename T> inline BOOST_CONSTEXPR bool operator != (::std::complex<T> const & lhs, quaternion<T> const & rhs) { return !(lhs == rhs); }
650 template<typename T> inline BOOST_CONSTEXPR bool operator != (quaternion<T> const & lhs, ::std::complex<T> const & rhs) { return !(lhs == rhs); }
651 template<typename T> inline BOOST_CONSTEXPR bool operator != (quaternion<T> const & lhs, quaternion<T> const & rhs) { return !(lhs == rhs); }
654 // Note: we allow the following formats, whith a, b, c, and d reals
656 // (a), (a,b), (a,b,c), (a,b,c,d)
657 // (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))
658 template<typename T, typename charT, class traits>
659 ::std::basic_istream<charT,traits> & operator >> ( ::std::basic_istream<charT,traits> & is,
663 #ifdef BOOST_NO_STD_LOCALE
665 const ::std::ctype<charT> & ct = ::std::use_facet< ::std::ctype<charT> >(is.getloc());
666 #endif /* BOOST_NO_STD_LOCALE */
673 ::std::complex<T> u = ::std::complex<T>();
674 ::std::complex<T> v = ::std::complex<T>();
679 is >> ch; // get the first lexeme
681 if (!is.good()) goto finish;
683 #ifdef BOOST_NO_STD_LOCALE
686 cc = ct.narrow(ch, char());
687 #endif /* BOOST_NO_STD_LOCALE */
689 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,))
691 is >> ch; // get the second lexeme
693 if (!is.good()) goto finish;
695 #ifdef BOOST_NO_STD_LOCALE
698 cc = ct.narrow(ch, char());
699 #endif /* BOOST_NO_STD_LOCALE */
701 if (cc == '(') // read "((", possible: ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b,),(c,d,))
705 is >> u; // we extract the first and second components
709 if (!is.good()) goto finish;
711 is >> ch; // get the next lexeme
713 if (!is.good()) goto finish;
715 #ifdef BOOST_NO_STD_LOCALE
718 cc = ct.narrow(ch, char());
719 #endif /* BOOST_NO_STD_LOCALE */
721 if (cc == ')') // format: ((a)) or ((a,b))
723 q = quaternion<T>(a,b);
725 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,))
727 is >> v; // we extract the third and fourth components
731 if (!is.good()) goto finish;
733 is >> ch; // get the last lexeme
735 if (!is.good()) goto finish;
737 #ifdef BOOST_NO_STD_LOCALE
740 cc = ct.narrow(ch, char());
741 #endif /* BOOST_NO_STD_LOCALE */
743 if (cc == ')') // format: ((a),c), ((a),(c)), ((a),(c,d)), ((a,b),c), ((a,b),(c)) or ((a,b,),(c,d,))
745 q = quaternion<T>(a,b,c,d);
749 is.setstate(::std::ios_base::failbit);
754 is.setstate(::std::ios_base::failbit);
757 else // read "(a", possible: (a), (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d))
761 is >> a; // we extract the first component
763 if (!is.good()) goto finish;
765 is >> ch; // get the third lexeme
767 if (!is.good()) goto finish;
769 #ifdef BOOST_NO_STD_LOCALE
772 cc = ct.narrow(ch, char());
773 #endif /* BOOST_NO_STD_LOCALE */
775 if (cc == ')') // format: (a)
777 q = quaternion<T>(a);
779 else if (cc == ',') // read "(a,", possible: (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d))
781 is >> ch; // get the fourth lexeme
783 if (!is.good()) goto finish;
785 #ifdef BOOST_NO_STD_LOCALE
788 cc = ct.narrow(ch, char());
789 #endif /* BOOST_NO_STD_LOCALE */
791 if (cc == '(') // read "(a,(", possible: (a,(c)), (a,(c,d))
795 is >> v; // we extract the third and fourth component
800 if (!is.good()) goto finish;
802 is >> ch; // get the ninth lexeme
804 if (!is.good()) goto finish;
806 #ifdef BOOST_NO_STD_LOCALE
809 cc = ct.narrow(ch, char());
810 #endif /* BOOST_NO_STD_LOCALE */
812 if (cc == ')') // format: (a,(c)) or (a,(c,d))
814 q = quaternion<T>(a,b,c,d);
818 is.setstate(::std::ios_base::failbit);
821 else // read "(a,b", possible: (a,b), (a,b,c), (a,b,c,d)
825 is >> b; // we extract the second component
827 if (!is.good()) goto finish;
829 is >> ch; // get the fifth lexeme
831 if (!is.good()) goto finish;
833 #ifdef BOOST_NO_STD_LOCALE
836 cc = ct.narrow(ch, char());
837 #endif /* BOOST_NO_STD_LOCALE */
839 if (cc == ')') // format: (a,b)
841 q = quaternion<T>(a,b);
843 else if (cc == ',') // read "(a,b,", possible: (a,b,c), (a,b,c,d)
845 is >> c; // we extract the third component
847 if (!is.good()) goto finish;
849 is >> ch; // get the seventh lexeme
851 if (!is.good()) goto finish;
853 #ifdef BOOST_NO_STD_LOCALE
856 cc = ct.narrow(ch, char());
857 #endif /* BOOST_NO_STD_LOCALE */
859 if (cc == ')') // format: (a,b,c)
861 q = quaternion<T>(a,b,c);
863 else if (cc == ',') // read "(a,b,c,", possible: (a,b,c,d)
865 is >> d; // we extract the fourth component
867 if (!is.good()) goto finish;
869 is >> ch; // get the ninth lexeme
871 if (!is.good()) goto finish;
873 #ifdef BOOST_NO_STD_LOCALE
876 cc = ct.narrow(ch, char());
877 #endif /* BOOST_NO_STD_LOCALE */
879 if (cc == ')') // format: (a,b,c,d)
881 q = quaternion<T>(a,b,c,d);
885 is.setstate(::std::ios_base::failbit);
890 is.setstate(::std::ios_base::failbit);
895 is.setstate(::std::ios_base::failbit);
901 is.setstate(::std::ios_base::failbit);
909 is >> a; // we extract the first component
911 if (!is.good()) goto finish;
913 q = quaternion<T>(a);
921 template<typename T, typename charT, class traits>
922 ::std::basic_ostream<charT,traits> & operator << ( ::std::basic_ostream<charT,traits> & os,
923 quaternion<T> const & q)
925 ::std::basic_ostringstream<charT,traits> s;
928 #ifdef BOOST_NO_STD_LOCALE
930 s.imbue(os.getloc());
931 #endif /* BOOST_NO_STD_LOCALE */
932 s.precision(os.precision());
934 s << '(' << q.R_component_1() << ','
935 << q.R_component_2() << ','
936 << q.R_component_3() << ','
937 << q.R_component_4() << ')';
939 return os << s.str();
946 inline BOOST_CONSTEXPR T real(quaternion<T> const & q)
953 inline BOOST_CONSTEXPR quaternion<T> unreal(quaternion<T> const & q)
959 inline T sup(quaternion<T> const & q)
962 return (std::max)((std::max)(abs(q.R_component_1()), abs(q.R_component_2())), (std::max)(abs(q.R_component_3()), abs(q.R_component_4())));
967 inline T l1(quaternion<T> const & q)
970 return abs(q.R_component_1()) + abs(q.R_component_2()) + abs(q.R_component_3()) + abs(q.R_component_4());
975 inline T abs(quaternion<T> const & q)
980 T maxim = sup(q); // overflow protection
982 if (maxim == static_cast<T>(0))
988 T mixam = static_cast<T>(1)/maxim; // prefer multiplications over divisions
990 T a = q.R_component_1() * mixam;
991 T b = q.R_component_2() * mixam;
992 T c = q.R_component_3() * mixam;
993 T d = q.R_component_4() * mixam;
1000 return(maxim * sqrt(a + b + c + d));
1003 //return(sqrt(norm(q)));
1007 // Note: This is the Cayley norm, not the Euclidian norm...
1009 template<typename T>
1010 inline BOOST_CXX14_CONSTEXPR T norm(quaternion<T>const & q)
1012 return(real(q*conj(q)));
1016 template<typename T>
1017 inline BOOST_CONSTEXPR quaternion<T> conj(quaternion<T> const & q)
1019 return(quaternion<T>( +q.R_component_1(),
1022 -q.R_component_4()));
1026 template<typename T>
1027 inline quaternion<T> spherical( T const & rho,
1035 //T a = cos(theta)*cos(phi1)*cos(phi2);
1036 //T b = sin(theta)*cos(phi1)*cos(phi2);
1037 //T c = sin(phi1)*cos(phi2);
1040 T courrant = static_cast<T>(1);
1044 courrant *= cos(phi2);
1046 T c = sin(phi1)*courrant;
1048 courrant *= cos(phi1);
1050 T b = sin(theta)*courrant;
1051 T a = cos(theta)*courrant;
1053 return(rho*quaternion<T>(a,b,c,d));
1057 template<typename T>
1058 inline quaternion<T> semipolar( T const & rho,
1066 T a = cos(alpha)*cos(theta1);
1067 T b = cos(alpha)*sin(theta1);
1068 T c = sin(alpha)*cos(theta2);
1069 T d = sin(alpha)*sin(theta2);
1071 return(rho*quaternion<T>(a,b,c,d));
1075 template<typename T>
1076 inline quaternion<T> multipolar( T const & rho1,
1084 T a = rho1*cos(theta1);
1085 T b = rho1*sin(theta1);
1086 T c = rho2*cos(theta2);
1087 T d = rho2*sin(theta2);
1089 return(quaternion<T>(a,b,c,d));
1093 template<typename T>
1094 inline quaternion<T> cylindrospherical( T const & t,
1096 T const & longitude,
1104 T b = radius*cos(longitude)*cos(latitude);
1105 T c = radius*sin(longitude)*cos(latitude);
1106 T d = radius*sin(latitude);
1108 return(quaternion<T>(t,b,c,d));
1112 template<typename T>
1113 inline quaternion<T> cylindrical(T const & r,
1124 return(quaternion<T>(a,b,h1,h2));
1129 // (please see the documentation)
1132 template<typename T>
1133 inline quaternion<T> exp(quaternion<T> const & q)
1138 using ::boost::math::sinc_pi;
1142 T z = abs(unreal(q));
1146 return(u*quaternion<T>(cos(z),
1147 w*q.R_component_2(), w*q.R_component_3(),
1148 w*q.R_component_4()));
1152 template<typename T>
1153 inline quaternion<T> cos(quaternion<T> const & q)
1159 using ::boost::math::sinhc_pi;
1161 T z = abs(unreal(q));
1163 T w = -sin(q.real())*sinhc_pi(z);
1165 return(quaternion<T>(cos(q.real())*cosh(z),
1166 w*q.R_component_2(), w*q.R_component_3(),
1167 w*q.R_component_4()));
1171 template<typename T>
1172 inline quaternion<T> sin(quaternion<T> const & q)
1178 using ::boost::math::sinhc_pi;
1180 T z = abs(unreal(q));
1182 T w = +cos(q.real())*sinhc_pi(z);
1184 return(quaternion<T>(sin(q.real())*cosh(z),
1185 w*q.R_component_2(), w*q.R_component_3(),
1186 w*q.R_component_4()));
1190 template<typename T>
1191 inline quaternion<T> tan(quaternion<T> const & q)
1193 return(sin(q)/cos(q));
1197 template<typename T>
1198 inline quaternion<T> cosh(quaternion<T> const & q)
1200 return((exp(+q)+exp(-q))/static_cast<T>(2));
1204 template<typename T>
1205 inline quaternion<T> sinh(quaternion<T> const & q)
1207 return((exp(+q)-exp(-q))/static_cast<T>(2));
1211 template<typename T>
1212 inline quaternion<T> tanh(quaternion<T> const & q)
1214 return(sinh(q)/cosh(q));
1218 template<typename T>
1219 quaternion<T> pow(quaternion<T> const & q,
1226 quaternion<T> result = pow(q, m);
1232 result *= q; // n odd
1243 return(quaternion<T>(static_cast<T>(1)));
1247 return(pow(quaternion<T>(static_cast<T>(1))/q,-n));
1253 #endif /* BOOST_QUATERNION_HPP */