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/detail/workaround.hpp>
15 #include <boost/type_traits/is_convertible.hpp>
16 #include <boost/utility/enable_if.hpp>
17 #ifndef BOOST_NO_STD_LOCALE
18 # include <locale> // for the "<<" operator
19 #endif /* BOOST_NO_STD_LOCALE */
22 #include <iosfwd> // for the "<<" and ">>" operators
23 #include <sstream> // for the "<<" operator
25 #include <boost/math/special_functions/sinc.hpp> // for the Sinus cardinal
26 #include <boost/math/special_functions/sinhc.hpp> // for the Hyperbolic Sinus cardinal
28 #if defined(BOOST_NO_CXX11_NOEXCEPT) || defined(BOOST_NO_CXX11_RVALUE_REFERENCES) || defined(BOOST_NO_SFINAE_EXPR)
29 #include <boost/type_traits/is_pod.hpp>
39 #if !defined(BOOST_NO_CXX11_NOEXCEPT) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) && !defined(BOOST_NO_SFINAE_EXPR)
42 struct is_trivial_arithmetic_type_imp
45 noexcept(std::declval<T&>() += std::declval<T>())
46 && noexcept(std::declval<T&>() -= std::declval<T>())
47 && noexcept(std::declval<T&>() *= std::declval<T>())
48 && noexcept(std::declval<T&>() /= std::declval<T>())
53 struct is_trivial_arithmetic_type : public is_trivial_arithmetic_type_imp<T>::type {};
57 struct is_trivial_arithmetic_type : public boost::is_pod<T> {};
63 #ifndef BOOST_NO_CXX14_CONSTEXPR
64 namespace constexpr_detail
67 constexpr void swap(T& a, T& b)
84 // constructor for H seen as R^4
85 // (also default constructor)
87 BOOST_CONSTEXPR explicit quaternion( T const & requested_a = T(),
88 T const & requested_b = T(),
89 T const & requested_c = T(),
90 T const & requested_d = T())
100 // constructor for H seen as C^2
102 BOOST_CONSTEXPR explicit quaternion( ::std::complex<T> const & z0,
103 ::std::complex<T> const & z1 = ::std::complex<T>())
113 // UNtemplated copy constructor
114 BOOST_CONSTEXPR quaternion(quaternion const & a_recopier)
115 : a(a_recopier.R_component_1()),
116 b(a_recopier.R_component_2()),
117 c(a_recopier.R_component_3()),
118 d(a_recopier.R_component_4()) {}
119 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
120 BOOST_CONSTEXPR quaternion(quaternion && a_recopier)
121 : a(std::move(a_recopier.R_component_1())),
122 b(std::move(a_recopier.R_component_2())),
123 c(std::move(a_recopier.R_component_3())),
124 d(std::move(a_recopier.R_component_4())) {}
127 // templated copy constructor
130 BOOST_CONSTEXPR explicit quaternion(quaternion<X> const & a_recopier)
131 : a(static_cast<T>(a_recopier.R_component_1())),
132 b(static_cast<T>(a_recopier.R_component_2())),
133 c(static_cast<T>(a_recopier.R_component_3())),
134 d(static_cast<T>(a_recopier.R_component_4()))
141 // (this is taken care of by the compiler itself)
146 // Note: Like complex number, quaternions do have a meaningful notion of "real part",
147 // but unlike them there is no meaningful notion of "imaginary part".
148 // Instead there is an "unreal part" which itself is a quaternion, and usually
149 // nothing simpler (as opposed to the complex number case).
150 // However, for practicallity, there are accessors for the other components
151 // (these are necessary for the templated copy constructor, for instance).
153 BOOST_CONSTEXPR T real() const
158 BOOST_CONSTEXPR quaternion<T> unreal() const
160 return(quaternion<T>(static_cast<T>(0), b, c, d));
163 BOOST_CONSTEXPR T R_component_1() const
168 BOOST_CONSTEXPR T R_component_2() const
173 BOOST_CONSTEXPR T R_component_3() const
178 BOOST_CONSTEXPR T R_component_4() const
183 BOOST_CONSTEXPR ::std::complex<T> C_component_1() const
185 return(::std::complex<T>(a, b));
188 BOOST_CONSTEXPR ::std::complex<T> C_component_2() const
190 return(::std::complex<T>(c, d));
193 BOOST_CXX14_CONSTEXPR void swap(quaternion& o)
195 #ifndef BOOST_NO_CXX14_CONSTEXPR
196 using constexpr_detail::swap;
206 // assignment operators
209 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (quaternion<X> const & a_affecter)
211 a = static_cast<T>(a_affecter.R_component_1());
212 b = static_cast<T>(a_affecter.R_component_2());
213 c = static_cast<T>(a_affecter.R_component_3());
214 d = static_cast<T>(a_affecter.R_component_4());
219 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (quaternion<T> const & a_affecter)
228 #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES
229 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (quaternion<T> && a_affecter)
231 a = std::move(a_affecter.a);
232 b = std::move(a_affecter.b);
233 c = std::move(a_affecter.c);
234 d = std::move(a_affecter.d);
239 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (T const & a_affecter)
243 b = c = d = static_cast<T>(0);
248 BOOST_CXX14_CONSTEXPR quaternion<T> & operator = (::std::complex<T> const & a_affecter)
250 a = a_affecter.real();
251 b = a_affecter.imag();
253 c = d = static_cast<T>(0);
258 // other assignment-related operators
260 // NOTE: Quaternion multiplication is *NOT* commutative;
261 // symbolically, "q *= rhs;" means "q = q * rhs;"
262 // and "q /= rhs;" means "q = q * inverse_of(rhs);"
264 // Note2: Each operator comes in 2 forms - one for the simple case where
265 // type T throws no exceptions, and one exception-safe version
266 // for the case where it might.
268 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(T const & rhs, const mpl::true_&)
273 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(T const & rhs, const mpl::false_&)
275 quaternion<T> result(a + rhs, b, c, d); // exception guard
279 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(std::complex<T> const & rhs, const mpl::true_&)
285 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(std::complex<T> const & rhs, const mpl::false_&)
287 quaternion<T> result(a + std::real(rhs), b + std::imag(rhs), c, d); // exception guard
292 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(quaternion<X> const & rhs, const mpl::true_&)
294 a += rhs.R_component_1();
295 b += rhs.R_component_2();
296 c += rhs.R_component_3();
297 d += rhs.R_component_4();
301 BOOST_CXX14_CONSTEXPR quaternion<T> & do_add(quaternion<X> const & rhs, const mpl::false_&)
303 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
308 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(T const & rhs, const mpl::true_&)
313 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(T const & rhs, const mpl::false_&)
315 quaternion<T> result(a - rhs, b, c, d); // exception guard
319 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(std::complex<T> const & rhs, const mpl::true_&)
325 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(std::complex<T> const & rhs, const mpl::false_&)
327 quaternion<T> result(a - std::real(rhs), b - std::imag(rhs), c, d); // exception guard
332 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(quaternion<X> const & rhs, const mpl::true_&)
334 a -= rhs.R_component_1();
335 b -= rhs.R_component_2();
336 c -= rhs.R_component_3();
337 d -= rhs.R_component_4();
341 BOOST_CXX14_CONSTEXPR quaternion<T> & do_subtract(quaternion<X> const & rhs, const mpl::false_&)
343 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
348 BOOST_CXX14_CONSTEXPR quaternion<T> & do_multiply(T const & rhs, const mpl::true_&)
356 BOOST_CXX14_CONSTEXPR quaternion<T> & do_multiply(T const & rhs, const mpl::false_&)
358 quaternion<T> result(a * rhs, b * rhs, c * rhs, d * rhs); // exception guard
363 BOOST_CXX14_CONSTEXPR quaternion<T> & do_divide(T const & rhs, const mpl::true_&)
371 BOOST_CXX14_CONSTEXPR quaternion<T> & do_divide(T const & rhs, const mpl::false_&)
373 quaternion<T> result(a / rhs, b / rhs, c / rhs, d / rhs); // exception guard
379 BOOST_CXX14_CONSTEXPR quaternion<T> & operator += (T const & rhs) { return do_add(rhs, detail::is_trivial_arithmetic_type<T>()); }
380 BOOST_CXX14_CONSTEXPR quaternion<T> & operator += (::std::complex<T> const & rhs) { return do_add(rhs, detail::is_trivial_arithmetic_type<T>()); }
381 template<typename X> BOOST_CXX14_CONSTEXPR quaternion<T> & operator += (quaternion<X> const & rhs) { return do_add(rhs, detail::is_trivial_arithmetic_type<T>()); }
383 BOOST_CXX14_CONSTEXPR quaternion<T> & operator -= (T const & rhs) { return do_subtract(rhs, detail::is_trivial_arithmetic_type<T>()); }
384 BOOST_CXX14_CONSTEXPR quaternion<T> & operator -= (::std::complex<T> const & rhs) { return do_subtract(rhs, detail::is_trivial_arithmetic_type<T>()); }
385 template<typename X> BOOST_CXX14_CONSTEXPR quaternion<T> & operator -= (quaternion<X> const & rhs) { return do_subtract(rhs, detail::is_trivial_arithmetic_type<T>()); }
387 BOOST_CXX14_CONSTEXPR quaternion<T> & operator *= (T const & rhs) { return do_multiply(rhs, detail::is_trivial_arithmetic_type<T>()); }
389 BOOST_CXX14_CONSTEXPR quaternion<T> & operator *= (::std::complex<T> const & rhs)
393 quaternion<T> result(a*ar - b*br, a*br + b*ar, c*ar + d*br, -c*br+d*ar);
399 BOOST_CXX14_CONSTEXPR quaternion<T> & operator *= (quaternion<X> const & rhs)
401 T ar = static_cast<T>(rhs.R_component_1());
402 T br = static_cast<T>(rhs.R_component_2());
403 T cr = static_cast<T>(rhs.R_component_3());
404 T dr = static_cast<T>(rhs.R_component_4());
406 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);
411 BOOST_CXX14_CONSTEXPR quaternion<T> & operator /= (T const & rhs) { return do_divide(rhs, detail::is_trivial_arithmetic_type<T>()); }
413 BOOST_CXX14_CONSTEXPR quaternion<T> & operator /= (::std::complex<T> const & rhs)
417 T denominator = ar*ar+br*br;
418 quaternion<T> result((+a*ar + b*br) / denominator, (-a*br + b*ar) / denominator, (+c*ar - d*br) / denominator, (+c*br + d*ar) / denominator);
424 BOOST_CXX14_CONSTEXPR quaternion<T> & operator /= (quaternion<X> const & rhs)
426 T ar = static_cast<T>(rhs.R_component_1());
427 T br = static_cast<T>(rhs.R_component_2());
428 T cr = static_cast<T>(rhs.R_component_3());
429 T dr = static_cast<T>(rhs.R_component_4());
431 T denominator = ar*ar+br*br+cr*cr+dr*dr;
432 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);
443 BOOST_CXX14_CONSTEXPR void swap(quaternion<T>& a, quaternion<T>& b) { a.swap(b); }
446 template <class T1, class T2>
447 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
448 operator + (const quaternion<T1>& a, const T2& b)
450 return quaternion<T1>(static_cast<T1>(a.R_component_1() + b), a.R_component_2(), a.R_component_3(), a.R_component_4());
452 template <class T1, class T2>
453 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
454 operator + (const T1& a, const quaternion<T2>& b)
456 return quaternion<T2>(static_cast<T2>(b.R_component_1() + a), b.R_component_2(), b.R_component_3(), b.R_component_4());
458 template <class T1, class T2>
459 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
460 operator + (const quaternion<T1>& a, const std::complex<T2>& b)
462 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());
464 template <class T1, class T2>
465 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
466 operator + (const std::complex<T1>& a, const quaternion<T2>& b)
468 return quaternion<T1>(b.R_component_1() + real(a), b.R_component_2() + imag(a), b.R_component_3(), b.R_component_4());
471 inline BOOST_CONSTEXPR quaternion<T> operator + (const quaternion<T>& a, const quaternion<T>& b)
473 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());
476 template <class T1, class T2>
477 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
478 operator - (const quaternion<T1>& a, const T2& b)
480 return quaternion<T1>(static_cast<T1>(a.R_component_1() - b), a.R_component_2(), a.R_component_3(), a.R_component_4());
482 template <class T1, class T2>
483 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
484 operator - (const T1& a, const quaternion<T2>& b)
486 return quaternion<T2>(static_cast<T2>(a - b.R_component_1()), -b.R_component_2(), -b.R_component_3(), -b.R_component_4());
488 template <class T1, class T2>
489 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
490 operator - (const quaternion<T1>& a, const std::complex<T2>& b)
492 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());
494 template <class T1, class T2>
495 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
496 operator - (const std::complex<T1>& a, const quaternion<T2>& b)
498 return quaternion<T1>(real(a) - b.R_component_1(), imag(a) - b.R_component_2(), -b.R_component_3(), -b.R_component_4());
501 inline BOOST_CONSTEXPR quaternion<T> operator - (const quaternion<T>& a, const quaternion<T>& b)
503 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());
507 template <class T1, class T2>
508 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
509 operator * (const quaternion<T1>& a, const T2& b)
511 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);
513 template <class T1, class T2>
514 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
515 operator * (const T1& a, const quaternion<T2>& b)
517 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());
519 template <class T1, class T2>
520 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
521 operator * (const quaternion<T1>& a, const std::complex<T2>& b)
523 quaternion<T1> result(a);
527 template <class T1, class T2>
528 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
529 operator * (const std::complex<T1>& a, const quaternion<T2>& b)
531 quaternion<T1> result(a);
536 inline BOOST_CXX14_CONSTEXPR quaternion<T> operator * (const quaternion<T>& a, const quaternion<T>& b)
538 quaternion<T> result(a);
544 template <class T1, class T2>
545 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
546 operator / (const quaternion<T1>& a, const T2& b)
548 return quaternion<T1>(a.R_component_1() / b, a.R_component_2() / b, a.R_component_3() / b, a.R_component_4() / b);
550 template <class T1, class T2>
551 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
552 operator / (const T1& a, const quaternion<T2>& b)
554 quaternion<T2> result(a);
558 template <class T1, class T2>
559 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T2, T1>::value, quaternion<T1> >::type
560 operator / (const quaternion<T1>& a, const std::complex<T2>& b)
562 quaternion<T1> result(a);
566 template <class T1, class T2>
567 inline BOOST_CXX14_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<T1, T2>::value, quaternion<T2> >::type
568 operator / (const std::complex<T1>& a, const quaternion<T2>& b)
570 quaternion<T2> result(a);
575 inline BOOST_CXX14_CONSTEXPR quaternion<T> operator / (const quaternion<T>& a, const quaternion<T>& b)
577 quaternion<T> result(a);
584 inline BOOST_CONSTEXPR const quaternion<T>& operator + (quaternion<T> const & q)
591 inline BOOST_CONSTEXPR quaternion<T> operator - (quaternion<T> const & q)
593 return(quaternion<T>(-q.R_component_1(),-q.R_component_2(),-q.R_component_3(),-q.R_component_4()));
597 template<typename R, typename T>
598 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<R, T>::value, bool>::type operator == (R const & lhs, quaternion<T> const & rhs)
601 (rhs.R_component_1() == lhs)&&
602 (rhs.R_component_2() == static_cast<T>(0))&&
603 (rhs.R_component_3() == static_cast<T>(0))&&
604 (rhs.R_component_4() == static_cast<T>(0))
609 template<typename T, typename R>
610 inline BOOST_CONSTEXPR typename boost::enable_if_c<boost::is_convertible<R, T>::value, bool>::type operator == (quaternion<T> const & lhs, R const & rhs)
617 inline BOOST_CONSTEXPR bool operator == (::std::complex<T> const & lhs, quaternion<T> const & rhs)
620 (rhs.R_component_1() == lhs.real())&&
621 (rhs.R_component_2() == lhs.imag())&&
622 (rhs.R_component_3() == static_cast<T>(0))&&
623 (rhs.R_component_4() == static_cast<T>(0))
629 inline BOOST_CONSTEXPR bool operator == (quaternion<T> const & lhs, ::std::complex<T> const & rhs)
636 inline BOOST_CONSTEXPR bool operator == (quaternion<T> const & lhs, quaternion<T> const & rhs)
639 (rhs.R_component_1() == lhs.R_component_1())&&
640 (rhs.R_component_2() == lhs.R_component_2())&&
641 (rhs.R_component_3() == lhs.R_component_3())&&
642 (rhs.R_component_4() == lhs.R_component_4())
646 template<typename R, typename T> inline BOOST_CONSTEXPR bool operator != (R const & lhs, quaternion<T> const & rhs) { return !(lhs == rhs); }
647 template<typename T, typename R> inline BOOST_CONSTEXPR bool operator != (quaternion<T> const & lhs, R const & rhs) { return !(lhs == rhs); }
648 template<typename T> inline BOOST_CONSTEXPR bool operator != (::std::complex<T> const & lhs, quaternion<T> const & rhs) { return !(lhs == rhs); }
649 template<typename T> inline BOOST_CONSTEXPR bool operator != (quaternion<T> const & lhs, ::std::complex<T> const & rhs) { return !(lhs == rhs); }
650 template<typename T> inline BOOST_CONSTEXPR bool operator != (quaternion<T> const & lhs, quaternion<T> const & rhs) { return !(lhs == rhs); }
653 // Note: we allow the following formats, whith a, b, c, and d reals
655 // (a), (a,b), (a,b,c), (a,b,c,d)
656 // (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))
657 template<typename T, typename charT, class traits>
658 ::std::basic_istream<charT,traits> & operator >> ( ::std::basic_istream<charT,traits> & is,
662 #ifdef BOOST_NO_STD_LOCALE
664 const ::std::ctype<charT> & ct = ::std::use_facet< ::std::ctype<charT> >(is.getloc());
665 #endif /* BOOST_NO_STD_LOCALE */
672 ::std::complex<T> u = ::std::complex<T>();
673 ::std::complex<T> v = ::std::complex<T>();
678 is >> ch; // get the first lexeme
680 if (!is.good()) goto finish;
682 #ifdef BOOST_NO_STD_LOCALE
685 cc = ct.narrow(ch, char());
686 #endif /* BOOST_NO_STD_LOCALE */
688 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,))
690 is >> ch; // get the second lexeme
692 if (!is.good()) goto finish;
694 #ifdef BOOST_NO_STD_LOCALE
697 cc = ct.narrow(ch, char());
698 #endif /* BOOST_NO_STD_LOCALE */
700 if (cc == '(') // read "((", possible: ((a)), ((a),c), ((a),(c)), ((a),(c,d)), ((a,b)), ((a,b),c), ((a,b),(c)), ((a,b,),(c,d,))
704 is >> u; // we extract the first and second components
708 if (!is.good()) goto finish;
710 is >> ch; // get the next lexeme
712 if (!is.good()) goto finish;
714 #ifdef BOOST_NO_STD_LOCALE
717 cc = ct.narrow(ch, char());
718 #endif /* BOOST_NO_STD_LOCALE */
720 if (cc == ')') // format: ((a)) or ((a,b))
722 q = quaternion<T>(a,b);
724 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,))
726 is >> v; // we extract the third and fourth components
730 if (!is.good()) goto finish;
732 is >> ch; // get the last lexeme
734 if (!is.good()) goto finish;
736 #ifdef BOOST_NO_STD_LOCALE
739 cc = ct.narrow(ch, char());
740 #endif /* BOOST_NO_STD_LOCALE */
742 if (cc == ')') // format: ((a),c), ((a),(c)), ((a),(c,d)), ((a,b),c), ((a,b),(c)) or ((a,b,),(c,d,))
744 q = quaternion<T>(a,b,c,d);
748 is.setstate(::std::ios_base::failbit);
753 is.setstate(::std::ios_base::failbit);
756 else // read "(a", possible: (a), (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d))
760 is >> a; // we extract the first component
762 if (!is.good()) goto finish;
764 is >> ch; // get the third lexeme
766 if (!is.good()) goto finish;
768 #ifdef BOOST_NO_STD_LOCALE
771 cc = ct.narrow(ch, char());
772 #endif /* BOOST_NO_STD_LOCALE */
774 if (cc == ')') // format: (a)
776 q = quaternion<T>(a);
778 else if (cc == ',') // read "(a,", possible: (a,b), (a,b,c), (a,b,c,d), (a,(c)), (a,(c,d))
780 is >> ch; // get the fourth lexeme
782 if (!is.good()) goto finish;
784 #ifdef BOOST_NO_STD_LOCALE
787 cc = ct.narrow(ch, char());
788 #endif /* BOOST_NO_STD_LOCALE */
790 if (cc == '(') // read "(a,(", possible: (a,(c)), (a,(c,d))
794 is >> v; // we extract the third and fourth component
799 if (!is.good()) goto finish;
801 is >> ch; // get the ninth lexeme
803 if (!is.good()) goto finish;
805 #ifdef BOOST_NO_STD_LOCALE
808 cc = ct.narrow(ch, char());
809 #endif /* BOOST_NO_STD_LOCALE */
811 if (cc == ')') // format: (a,(c)) or (a,(c,d))
813 q = quaternion<T>(a,b,c,d);
817 is.setstate(::std::ios_base::failbit);
820 else // read "(a,b", possible: (a,b), (a,b,c), (a,b,c,d)
824 is >> b; // we extract the second component
826 if (!is.good()) goto finish;
828 is >> ch; // get the fifth lexeme
830 if (!is.good()) goto finish;
832 #ifdef BOOST_NO_STD_LOCALE
835 cc = ct.narrow(ch, char());
836 #endif /* BOOST_NO_STD_LOCALE */
838 if (cc == ')') // format: (a,b)
840 q = quaternion<T>(a,b);
842 else if (cc == ',') // read "(a,b,", possible: (a,b,c), (a,b,c,d)
844 is >> c; // we extract the third component
846 if (!is.good()) goto finish;
848 is >> ch; // get the seventh lexeme
850 if (!is.good()) goto finish;
852 #ifdef BOOST_NO_STD_LOCALE
855 cc = ct.narrow(ch, char());
856 #endif /* BOOST_NO_STD_LOCALE */
858 if (cc == ')') // format: (a,b,c)
860 q = quaternion<T>(a,b,c);
862 else if (cc == ',') // read "(a,b,c,", possible: (a,b,c,d)
864 is >> d; // we extract the fourth component
866 if (!is.good()) goto finish;
868 is >> ch; // get the ninth lexeme
870 if (!is.good()) goto finish;
872 #ifdef BOOST_NO_STD_LOCALE
875 cc = ct.narrow(ch, char());
876 #endif /* BOOST_NO_STD_LOCALE */
878 if (cc == ')') // format: (a,b,c,d)
880 q = quaternion<T>(a,b,c,d);
884 is.setstate(::std::ios_base::failbit);
889 is.setstate(::std::ios_base::failbit);
894 is.setstate(::std::ios_base::failbit);
900 is.setstate(::std::ios_base::failbit);
908 is >> a; // we extract the first component
910 if (!is.good()) goto finish;
912 q = quaternion<T>(a);
920 template<typename T, typename charT, class traits>
921 ::std::basic_ostream<charT,traits> & operator << ( ::std::basic_ostream<charT,traits> & os,
922 quaternion<T> const & q)
924 ::std::basic_ostringstream<charT,traits> s;
927 #ifdef BOOST_NO_STD_LOCALE
929 s.imbue(os.getloc());
930 #endif /* BOOST_NO_STD_LOCALE */
931 s.precision(os.precision());
933 s << '(' << q.R_component_1() << ','
934 << q.R_component_2() << ','
935 << q.R_component_3() << ','
936 << q.R_component_4() << ')';
938 return os << s.str();
945 inline BOOST_CONSTEXPR T real(quaternion<T> const & q)
952 inline BOOST_CONSTEXPR quaternion<T> unreal(quaternion<T> const & q)
958 inline T sup(quaternion<T> const & q)
961 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())));
966 inline T l1(quaternion<T> const & q)
969 return abs(q.R_component_1()) + abs(q.R_component_2()) + abs(q.R_component_3()) + abs(q.R_component_4());
974 inline T abs(quaternion<T> const & q)
979 T maxim = sup(q); // overflow protection
981 if (maxim == static_cast<T>(0))
987 T mixam = static_cast<T>(1)/maxim; // prefer multiplications over divisions
989 T a = q.R_component_1() * mixam;
990 T b = q.R_component_2() * mixam;
991 T c = q.R_component_3() * mixam;
992 T d = q.R_component_4() * mixam;
999 return(maxim * sqrt(a + b + c + d));
1002 //return(sqrt(norm(q)));
1006 // Note: This is the Cayley norm, not the Euclidian norm...
1008 template<typename T>
1009 inline BOOST_CXX14_CONSTEXPR T norm(quaternion<T>const & q)
1011 return(real(q*conj(q)));
1015 template<typename T>
1016 inline BOOST_CONSTEXPR quaternion<T> conj(quaternion<T> const & q)
1018 return(quaternion<T>( +q.R_component_1(),
1021 -q.R_component_4()));
1025 template<typename T>
1026 inline quaternion<T> spherical( T const & rho,
1034 //T a = cos(theta)*cos(phi1)*cos(phi2);
1035 //T b = sin(theta)*cos(phi1)*cos(phi2);
1036 //T c = sin(phi1)*cos(phi2);
1039 T courrant = static_cast<T>(1);
1043 courrant *= cos(phi2);
1045 T c = sin(phi1)*courrant;
1047 courrant *= cos(phi1);
1049 T b = sin(theta)*courrant;
1050 T a = cos(theta)*courrant;
1052 return(rho*quaternion<T>(a,b,c,d));
1056 template<typename T>
1057 inline quaternion<T> semipolar( T const & rho,
1065 T a = cos(alpha)*cos(theta1);
1066 T b = cos(alpha)*sin(theta1);
1067 T c = sin(alpha)*cos(theta2);
1068 T d = sin(alpha)*sin(theta2);
1070 return(rho*quaternion<T>(a,b,c,d));
1074 template<typename T>
1075 inline quaternion<T> multipolar( T const & rho1,
1083 T a = rho1*cos(theta1);
1084 T b = rho1*sin(theta1);
1085 T c = rho2*cos(theta2);
1086 T d = rho2*sin(theta2);
1088 return(quaternion<T>(a,b,c,d));
1092 template<typename T>
1093 inline quaternion<T> cylindrospherical( T const & t,
1095 T const & longitude,
1103 T b = radius*cos(longitude)*cos(latitude);
1104 T c = radius*sin(longitude)*cos(latitude);
1105 T d = radius*sin(latitude);
1107 return(quaternion<T>(t,b,c,d));
1111 template<typename T>
1112 inline quaternion<T> cylindrical(T const & r,
1123 return(quaternion<T>(a,b,h1,h2));
1128 // (please see the documentation)
1131 template<typename T>
1132 inline quaternion<T> exp(quaternion<T> const & q)
1137 using ::boost::math::sinc_pi;
1141 T z = abs(unreal(q));
1145 return(u*quaternion<T>(cos(z),
1146 w*q.R_component_2(), w*q.R_component_3(),
1147 w*q.R_component_4()));
1151 template<typename T>
1152 inline quaternion<T> cos(quaternion<T> const & q)
1158 using ::boost::math::sinhc_pi;
1160 T z = abs(unreal(q));
1162 T w = -sin(q.real())*sinhc_pi(z);
1164 return(quaternion<T>(cos(q.real())*cosh(z),
1165 w*q.R_component_2(), w*q.R_component_3(),
1166 w*q.R_component_4()));
1170 template<typename T>
1171 inline quaternion<T> sin(quaternion<T> const & q)
1177 using ::boost::math::sinhc_pi;
1179 T z = abs(unreal(q));
1181 T w = +cos(q.real())*sinhc_pi(z);
1183 return(quaternion<T>(sin(q.real())*cosh(z),
1184 w*q.R_component_2(), w*q.R_component_3(),
1185 w*q.R_component_4()));
1189 template<typename T>
1190 inline quaternion<T> tan(quaternion<T> const & q)
1192 return(sin(q)/cos(q));
1196 template<typename T>
1197 inline quaternion<T> cosh(quaternion<T> const & q)
1199 return((exp(+q)+exp(-q))/static_cast<T>(2));
1203 template<typename T>
1204 inline quaternion<T> sinh(quaternion<T> const & q)
1206 return((exp(+q)-exp(-q))/static_cast<T>(2));
1210 template<typename T>
1211 inline quaternion<T> tanh(quaternion<T> const & q)
1213 return(sinh(q)/cosh(q));
1217 template<typename T>
1218 quaternion<T> pow(quaternion<T> const & q,
1225 quaternion<T> result = pow(q, m);
1231 result *= q; // n odd
1242 return(quaternion<T>(static_cast<T>(1)));
1246 return(pow(quaternion<T>(static_cast<T>(1))/q,-n));
1252 #endif /* BOOST_QUATERNION_HPP */