From 69590e2ad27340b89338223237185e9c1a48edbc Mon Sep 17 00:00:00 2001 From: Nick Matteo Date: Wed, 25 May 2016 11:37:03 -0400 Subject: [PATCH] Split complex and quaternion arithmetic and stream operators --- sprout/complex.hpp | 3 +- sprout/complex/arithmetic_operators.hpp | 136 +++++ sprout/complex/asin.hpp | 2 +- sprout/complex/atan.hpp | 2 +- sprout/complex/atanh.hpp | 2 +- sprout/complex/cosh.hpp | 2 +- sprout/complex/log10.hpp | 2 +- sprout/complex/log2.hpp | 2 +- sprout/complex/operators.hpp | 185 +----- sprout/complex/pow.hpp | 2 +- sprout/complex/sin.hpp | 2 +- sprout/complex/sinh.hpp | 2 +- sprout/complex/stream_operators.hpp | 51 ++ sprout/complex/tan.hpp | 2 +- sprout/complex/tanh.hpp | 2 +- sprout/math/quaternion.hpp | 3 +- .../math/quaternion/arithmetic_operators.hpp | 381 ++++++++++++ sprout/math/quaternion/norm.hpp | 2 +- sprout/math/quaternion/operators.hpp | 552 +----------------- sprout/math/quaternion/stream_operators.hpp | 200 +++++++ 20 files changed, 801 insertions(+), 734 deletions(-) create mode 100644 sprout/complex/arithmetic_operators.hpp create mode 100644 sprout/complex/stream_operators.hpp create mode 100644 sprout/math/quaternion/arithmetic_operators.hpp create mode 100644 sprout/math/quaternion/stream_operators.hpp diff --git a/sprout/complex.hpp b/sprout/complex.hpp index b4567304..a6630d96 100644 --- a/sprout/complex.hpp +++ b/sprout/complex.hpp @@ -10,7 +10,8 @@ #include #include -#include +#include +#include #include #include #include diff --git a/sprout/complex/arithmetic_operators.hpp b/sprout/complex/arithmetic_operators.hpp new file mode 100644 index 00000000..08acc52c --- /dev/null +++ b/sprout/complex/arithmetic_operators.hpp @@ -0,0 +1,136 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_COMPLEX_ARITHMETIC_OPERATORS_HPP +#define SPROUT_COMPLEX_ARITHMETIC_OPERATORS_HPP + +#include +#include + +namespace sprout { + // 26.4.6, operators: + template + inline SPROUT_CONSTEXPR sprout::complex + operator+(sprout::complex const& lhs, sprout::complex const& rhs) { + return sprout::complex(lhs.real() + rhs.real(), lhs.imag() + rhs.imag()); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator+(sprout::complex const& lhs, T const& rhs) { + return sprout::complex(lhs.real() + rhs, lhs.imag()); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator+(T const& lhs, sprout::complex const& rhs) { + return sprout::complex(lhs + rhs.real(), rhs.imag()); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator-(sprout::complex const& lhs, sprout::complex const& rhs) { + return sprout::complex(lhs.real() - rhs.real(), lhs.imag() - rhs.imag()); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator-(sprout::complex const& lhs, T const& rhs) { + return sprout::complex(lhs.real() - rhs, lhs.imag()); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator-(T const& lhs, sprout::complex const& rhs) { + return sprout::complex(lhs - rhs.real(), -rhs.imag()); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator*(sprout::complex const& lhs, sprout::complex const& rhs) { + return sprout::complex( + lhs.real() * rhs.real() - lhs.imag() * rhs.imag(), + lhs.real() * rhs.imag() + lhs.imag() * rhs.real() + ); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator*(sprout::complex const& lhs, T const& rhs) { + return sprout::complex(lhs.real() * rhs, lhs.imag() * rhs); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator*(T const& lhs, sprout::complex const& rhs) { + return sprout::complex(lhs * rhs.real(), lhs * rhs.imag()); + } + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::complex + divides_impl(sprout::complex const& lhs, sprout::complex const& rhs, T const& n) { + return sprout::complex( + (lhs.real() * rhs.real() + lhs.imag() * rhs.imag()) / n, + (lhs.imag() * rhs.real() - lhs.real() * rhs.imag()) / n + ); + } + template + inline SPROUT_CONSTEXPR sprout::complex + divides_impl(T const& lhs, sprout::complex const& rhs, T const& n) { + return sprout::complex(lhs * rhs.real() / n, -lhs * rhs.imag() / n); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR sprout::complex + operator/(sprout::complex const& lhs, sprout::complex const& rhs) { + return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator/(sprout::complex const& lhs, T const& rhs) { + return sprout::complex(lhs.real() / rhs, lhs.imag() / rhs); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator/(T const& lhs, sprout::complex const& rhs) { + return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs)); + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator+(sprout::complex const& x) { + return x; + } + template + inline SPROUT_CONSTEXPR sprout::complex + operator-(sprout::complex const& x) { + return sprout::complex(-x.real(), -x.imag()); + } + template + inline SPROUT_CONSTEXPR bool + operator==(sprout::complex const& lhs, sprout::complex const& rhs) { + return lhs.real() == rhs.real() && lhs.imag() == rhs.imag(); + } + template + inline SPROUT_CONSTEXPR bool + operator==(sprout::complex const& lhs, T const& rhs) { + return lhs.real() == rhs && lhs.imag() == T(); + } + template + inline SPROUT_CONSTEXPR bool + operator==(T const& lhs, sprout::complex const& rhs) { + return lhs == rhs.real() && T() == rhs.imag(); + } + template + inline SPROUT_CONSTEXPR bool + operator!=(sprout::complex const& lhs, sprout::complex const& rhs) { + return !(lhs == rhs); + } + template + inline SPROUT_CONSTEXPR bool + operator!=(sprout::complex const& lhs, T const& rhs) { + return !(lhs == rhs); + } + template + inline SPROUT_CONSTEXPR bool + operator!=(T const& lhs, sprout::complex const& rhs) { + return !(lhs == rhs); + } +} // namespace sprout + +#endif // #ifndef SPROUT_COMPLEX_ARITHMETIC_OPERATORS_HPP diff --git a/sprout/complex/asin.hpp b/sprout/complex/asin.hpp index ce2f7dfc..83beb68d 100644 --- a/sprout/complex/asin.hpp +++ b/sprout/complex/asin.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/sprout/complex/atan.hpp b/sprout/complex/atan.hpp index f5ed6033..79505dcb 100644 --- a/sprout/complex/atan.hpp +++ b/sprout/complex/atan.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/sprout/complex/atanh.hpp b/sprout/complex/atanh.hpp index c3931883..6a1f7742 100644 --- a/sprout/complex/atanh.hpp +++ b/sprout/complex/atanh.hpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/complex/cosh.hpp b/sprout/complex/cosh.hpp index 934b998f..0e73d8dc 100644 --- a/sprout/complex/cosh.hpp +++ b/sprout/complex/cosh.hpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/complex/log10.hpp b/sprout/complex/log10.hpp index 6d713d76..5f66a13a 100644 --- a/sprout/complex/log10.hpp +++ b/sprout/complex/log10.hpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include namespace sprout { diff --git a/sprout/complex/log2.hpp b/sprout/complex/log2.hpp index 2e60b29e..a1a1dbb5 100644 --- a/sprout/complex/log2.hpp +++ b/sprout/complex/log2.hpp @@ -12,7 +12,7 @@ #include #include #include -#include +#include #include namespace sprout { diff --git a/sprout/complex/operators.hpp b/sprout/complex/operators.hpp index e7a8d489..b65b7f1b 100644 --- a/sprout/complex/operators.hpp +++ b/sprout/complex/operators.hpp @@ -1,170 +1,15 @@ -/*============================================================================= - Copyright (c) 2011-2016 Bolero MURAKAMI - https://github.com/bolero-MURAKAMI/Sprout - - Distributed under the Boost Software License, Version 1.0. (See accompanying - file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -=============================================================================*/ -#ifndef SPROUT_COMPLEX_OPERATORS_HPP -#define SPROUT_COMPLEX_OPERATORS_HPP - -#include -#include -#include -#include - -namespace sprout { - // 26.4.6, operators: - template - inline SPROUT_CONSTEXPR sprout::complex - operator+(sprout::complex const& lhs, sprout::complex const& rhs) { - return sprout::complex(lhs.real() + rhs.real(), lhs.imag() + rhs.imag()); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator+(sprout::complex const& lhs, T const& rhs) { - return sprout::complex(lhs.real() + rhs, lhs.imag()); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator+(T const& lhs, sprout::complex const& rhs) { - return sprout::complex(lhs + rhs.real(), rhs.imag()); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator-(sprout::complex const& lhs, sprout::complex const& rhs) { - return sprout::complex(lhs.real() - rhs.real(), lhs.imag() - rhs.imag()); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator-(sprout::complex const& lhs, T const& rhs) { - return sprout::complex(lhs.real() - rhs, lhs.imag()); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator-(T const& lhs, sprout::complex const& rhs) { - return sprout::complex(lhs - rhs.real(), -rhs.imag()); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator*(sprout::complex const& lhs, sprout::complex const& rhs) { - return sprout::complex( - lhs.real() * rhs.real() - lhs.imag() * rhs.imag(), - lhs.real() * rhs.imag() + lhs.imag() * rhs.real() - ); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator*(sprout::complex const& lhs, T const& rhs) { - return sprout::complex(lhs.real() * rhs, lhs.imag() * rhs); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator*(T const& lhs, sprout::complex const& rhs) { - return sprout::complex(lhs * rhs.real(), lhs * rhs.imag()); - } - namespace detail { - template - inline SPROUT_CONSTEXPR sprout::complex - divides_impl(sprout::complex const& lhs, sprout::complex const& rhs, T const& n) { - return sprout::complex( - (lhs.real() * rhs.real() + lhs.imag() * rhs.imag()) / n, - (lhs.imag() * rhs.real() - lhs.real() * rhs.imag()) / n - ); - } - template - inline SPROUT_CONSTEXPR sprout::complex - divides_impl(T const& lhs, sprout::complex const& rhs, T const& n) { - return sprout::complex(lhs * rhs.real() / n, -lhs * rhs.imag() / n); - } - } // namespace detail - template - inline SPROUT_CONSTEXPR sprout::complex - operator/(sprout::complex const& lhs, sprout::complex const& rhs) { - return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs)); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator/(sprout::complex const& lhs, T const& rhs) { - return sprout::complex(lhs.real() / rhs, lhs.imag() / rhs); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator/(T const& lhs, sprout::complex const& rhs) { - return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs)); - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator+(sprout::complex const& x) { - return x; - } - template - inline SPROUT_CONSTEXPR sprout::complex - operator-(sprout::complex const& x) { - return sprout::complex(-x.real(), -x.imag()); - } - template - inline SPROUT_CONSTEXPR bool - operator==(sprout::complex const& lhs, sprout::complex const& rhs) { - return lhs.real() == rhs.real() && lhs.imag() == rhs.imag(); - } - template - inline SPROUT_CONSTEXPR bool - operator==(sprout::complex const& lhs, T const& rhs) { - return lhs.real() == rhs && lhs.imag() == T(); - } - template - inline SPROUT_CONSTEXPR bool - operator==(T const& lhs, sprout::complex const& rhs) { - return lhs == rhs.real() && T() == rhs.imag(); - } - template - inline SPROUT_CONSTEXPR bool - operator!=(sprout::complex const& lhs, sprout::complex const& rhs) { - return !(lhs == rhs); - } - template - inline SPROUT_CONSTEXPR bool - operator!=(sprout::complex const& lhs, T const& rhs) { - return !(lhs == rhs); - } - template - inline SPROUT_CONSTEXPR bool - operator!=(T const& lhs, sprout::complex const& rhs) { - return !(lhs == rhs); - } - template - inline SPROUT_NON_CONSTEXPR std::basic_istream& - operator>>(std::basic_istream& lhs, sprout::complex& rhs) { - T re, im; - Char ch; - lhs >> ch; - if (ch == '(') { - lhs >> re >> ch; - if (ch == ',') { - lhs >> im >> ch; - if (ch == ')') { - rhs = sprout::complex(re, im); - } else { - lhs.setstate(std::ios_base::failbit); - } - } else if (ch == ')') { - rhs = re; - } else { - lhs.setstate(std::ios_base::failbit); - } - } else { - lhs.putback(ch); - lhs >> re; - rhs = re; - } - return lhs; - } - template - inline SPROUT_NON_CONSTEXPR std::basic_ostream& - operator<<(std::basic_ostream& lhs, sprout::complex const& rhs) { - return lhs << '(' << rhs.real() << ',' << rhs.imag() << ')'; - } -} // namespace sprout - -#endif // #ifndef SPROUT_COMPLEX_OPERATORS_HPP +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_COMPLEX_OPERATORS_HPP +#define SPROUT_COMPLEX_OPERATORS_HPP + +#include +#include + +#endif // #ifndef SPROUT_COMPLEX_OPERATORS_HPP + diff --git a/sprout/complex/pow.hpp b/sprout/complex/pow.hpp index 34b1b189..709f0317 100644 --- a/sprout/complex/pow.hpp +++ b/sprout/complex/pow.hpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/complex/sin.hpp b/sprout/complex/sin.hpp index 37de93c8..2d689a65 100644 --- a/sprout/complex/sin.hpp +++ b/sprout/complex/sin.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/sprout/complex/sinh.hpp b/sprout/complex/sinh.hpp index db871d44..bd9c14f2 100644 --- a/sprout/complex/sinh.hpp +++ b/sprout/complex/sinh.hpp @@ -17,7 +17,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/complex/stream_operators.hpp b/sprout/complex/stream_operators.hpp new file mode 100644 index 00000000..c51da3cd --- /dev/null +++ b/sprout/complex/stream_operators.hpp @@ -0,0 +1,51 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_COMPLEX_STREAM_OPERATORS_HPP +#define SPROUT_COMPLEX_STREAM_OPERATORS_HPP + +#include +#include +#include +#include + +namespace sprout { + template + inline SPROUT_NON_CONSTEXPR std::basic_istream& + operator>>(std::basic_istream& lhs, sprout::complex& rhs) { + T re, im; + Char ch; + lhs >> ch; + if (ch == '(') { + lhs >> re >> ch; + if (ch == ',') { + lhs >> im >> ch; + if (ch == ')') { + rhs = sprout::complex(re, im); + } else { + lhs.setstate(std::ios_base::failbit); + } + } else if (ch == ')') { + rhs = re; + } else { + lhs.setstate(std::ios_base::failbit); + } + } else { + lhs.putback(ch); + lhs >> re; + rhs = re; + } + return lhs; + } + template + inline SPROUT_NON_CONSTEXPR std::basic_ostream& + operator<<(std::basic_ostream& lhs, sprout::complex const& rhs) { + return lhs << '(' << rhs.real() << ',' << rhs.imag() << ')'; + } +} // namespace sprout + +#endif // #ifndef SPROUT_COMPLEX_STREAM_OPERATORS_HPP diff --git a/sprout/complex/tan.hpp b/sprout/complex/tan.hpp index 5d98aa1c..9c308da3 100644 --- a/sprout/complex/tan.hpp +++ b/sprout/complex/tan.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/sprout/complex/tanh.hpp b/sprout/complex/tanh.hpp index 99abef61..313a5e86 100644 --- a/sprout/complex/tanh.hpp +++ b/sprout/complex/tanh.hpp @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include diff --git a/sprout/math/quaternion.hpp b/sprout/math/quaternion.hpp index 499ad207..afbd527b 100644 --- a/sprout/math/quaternion.hpp +++ b/sprout/math/quaternion.hpp @@ -10,7 +10,8 @@ #include #include -#include +#include +#include #include #include #include diff --git a/sprout/math/quaternion/arithmetic_operators.hpp b/sprout/math/quaternion/arithmetic_operators.hpp new file mode 100644 index 00000000..41930e51 --- /dev/null +++ b/sprout/math/quaternion/arithmetic_operators.hpp @@ -0,0 +1,381 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_MATH_QUATERNION_ARITHMETIC_OPERATORS_HPP +#define SPROUT_MATH_QUATERNION_ARITHMETIC_OPERATORS_HPP + +#include +#include + +namespace sprout { + namespace math { + // + // operator+ + // + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator+(T const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs + rhs.R_component_1(), + rhs.R_component_2(), + rhs.R_component_3(), + rhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator+(sprout::math::quaternion const& lhs, T const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() + rhs, + lhs.R_component_2(), + lhs.R_component_3(), + lhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator+(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs.real() + rhs.R_component_1(), + lhs.imag() + rhs.R_component_2(), + rhs.R_component_3(), + rhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator+(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() + rhs.real(), + lhs.R_component_2() + rhs.imag(), + lhs.R_component_3(), + lhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator+(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() + rhs.R_component_1(), + lhs.R_component_2() + rhs.R_component_2(), + lhs.R_component_3() + rhs.R_component_3(), + lhs.R_component_4() + rhs.R_component_4() + ); + } + // + // operator- + // + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator-(T const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs - rhs.R_component_1(), + rhs.R_component_2(), + rhs.R_component_3(), + rhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator-(sprout::math::quaternion const& lhs, T const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() - rhs, + lhs.R_component_2(), + lhs.R_component_3(), + lhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator-(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs.real() - rhs.R_component_1(), + lhs.imag() - rhs.R_component_2(), + rhs.R_component_3(), + rhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator-(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() - rhs.real(), + lhs.R_component_2() - rhs.imag(), + lhs.R_component_3(), + lhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator-(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() - rhs.R_component_1(), + lhs.R_component_2() - rhs.R_component_2(), + lhs.R_component_3() - rhs.R_component_3(), + lhs.R_component_4() - rhs.R_component_4() + ); + } + // + // operator* + // + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator*(T const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs * rhs.R_component_1(), + lhs * rhs.R_component_2(), + lhs * rhs.R_component_3(), + lhs * rhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator*(sprout::math::quaternion const& lhs, T const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() * rhs, + lhs.R_component_2() * rhs, + lhs.R_component_3() * rhs, + lhs.R_component_4() * rhs + ); + } + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + mul_qc_impl(T const& a, T const& b, T const& c, T const& d, T const& ar, T const& br) { + return sprout::math::quaternion( + +a * ar - b * br, + +a * br + b * ar, + +c * ar + d * br, + -c * br + d * ar + ); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator*(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::detail::mul_qc_impl( + lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), + rhs.real(), rhs.imag() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator*(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { + return sprout::math::detail::mul_qc_impl( + rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4(), + lhs.real(), lhs.imag() + ); + } + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + mul_qq_impl(T const& a, T const& b, T const& c, T const& d, T const& ar, T const& br, T const& cr, T const& dr) { + return sprout::math::quaternion( + +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 + ); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator*(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::detail::mul_qq_impl( + lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), + rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4() + ); + } + // + // operator/ + // + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator/(T const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::quaternion( + lhs / rhs.R_component_1(), + lhs / rhs.R_component_2(), + lhs / rhs.R_component_3(), + lhs / rhs.R_component_4() + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator/(sprout::math::quaternion const& lhs, T const& rhs) { + return sprout::math::quaternion( + lhs.R_component_1() / rhs, + lhs.R_component_2() / rhs, + lhs.R_component_3() / rhs, + lhs.R_component_4() / rhs + ); + } + namespace detail { + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + div_qq_impl_4(sprout::array const& tt) { + return sprout::math::quaternion( + tt[0], tt[1], tt[2], tt[3] + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + div_qq_impl_3(sprout::array const& tr, T const& mixam, sprout::array const& tt) { + return sprout::math::detail::div_qq_impl_3( + sprout::math::detail::mul(tt, mixam / sprout::math::detail::sum(tr)) + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + div_qq_impl_2(T const& a, T const& b, T const& c, T const& d, sprout::array const& tr, T const& mixam) { + return sprout::math::detail::div_qq_impl_3( + sprout::math::detail::mul(tr, tr), mixam, + sprout::array{{ + +a * tr[0] + b * tr[1] + c * tr[2] + d * tr[3], + -a * tr[1] + b * tr[0] - c * tr[3] + d * tr[2], + -a * tr[2] + b * tr[3] + c * tr[0] - d * tr[1], + -a * tr[3] - b * tr[2] + c * tr[1] + d * tr[0] + }} + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + div_qq_impl_1(T const& a, T const& b, T const& c, T const& d, sprout::array const& tr, T const& mixam) { + return sprout::math::detail::div_qq_impl_2( + a, b, c, d, sprout::math::detail::mul(tr, mixam), mixam + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + div_qq_impl(T const& a, T const& b, T const& c, T const& d, sprout::array const& tr) { + return sprout::math::detail::div_qq_impl_1( + a, b, c, d, tr, + static_cast(1) / sprout::math::detail::abs_max(tr) + ); + } + } // namespace detail + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator/(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::detail::div_qq_impl( + lhs.real(), lhs.imag(), static_cast(0), static_cast(0), + sprout::array{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}} + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator/(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { + return sprout::math::detail::div_qq_impl( + lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), + sprout::array{{rhs.real(), rhs.imag(), static_cast(0), static_cast(0)}} + ); + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator/(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { + return sprout::math::detail::div_qq_impl( + lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), + sprout::array{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}} + ); + } + + // + // operator+ + // operator- + // + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator+(sprout::math::quaternion const& q) { + return q; + } + template + inline SPROUT_CONSTEXPR sprout::math::quaternion + operator-(sprout::math::quaternion const& q) { + return sprout::math::quaternion(-q.R_component_1(), -q.R_component_2(), -q.R_component_3(), -q.R_component_4()); + } + + // + // operator== + // operator!= + // + template + inline SPROUT_CONSTEXPR bool + operator==(T const& lhs, sprout::math::quaternion const& rhs) { + return rhs.R_component_1() == lhs + && rhs.R_component_2() == static_cast(0) + && rhs.R_component_3() == static_cast(0) + && rhs.R_component_4() == static_cast(0) + ; + } + template + inline SPROUT_CONSTEXPR bool + operator==(sprout::math::quaternion const& lhs, T const& rhs) { + return lhs.R_component_1() == rhs + && lhs.R_component_2() == static_cast(0) + && lhs.R_component_3() == static_cast(0) + && lhs.R_component_4() == static_cast(0) + ; + } + template + inline SPROUT_CONSTEXPR bool + operator==(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { + return rhs.R_component_1() == lhs.real() + && rhs.R_component_2() == lhs.imag() + && rhs.R_component_3() == static_cast(0) + && rhs.R_component_4() == static_cast(0) + ; + } + template + inline SPROUT_CONSTEXPR bool + operator==(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { + return lhs.R_component_1() == rhs.real() + && lhs.R_component_2() == rhs.imag() + && lhs.R_component_3() == static_cast(0) + && lhs.R_component_4() == static_cast(0) + ; + } + template + inline SPROUT_CONSTEXPR bool + operator==(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { + return rhs.R_component_1() == lhs.R_component_1() + && rhs.R_component_2() == lhs.R_component_2() + && rhs.R_component_3() == lhs.R_component_3() + && rhs.R_component_4() == lhs.R_component_4() + ; + } + +#define SPROUT_QUATERNION_NOT_EQUAL_GENERATOR \ + { \ + return !(lhs == rhs); \ + } + + template + inline SPROUT_CONSTEXPR bool + operator!=(T const& lhs, sprout::math::quaternion const& rhs) + SPROUT_QUATERNION_NOT_EQUAL_GENERATOR + template + inline SPROUT_CONSTEXPR bool + operator!=(sprout::math::quaternion const& lhs, T const& rhs) + SPROUT_QUATERNION_NOT_EQUAL_GENERATOR + template + inline SPROUT_CONSTEXPR bool + operator!=(sprout::complex const& lhs, sprout::math::quaternion const& rhs) + SPROUT_QUATERNION_NOT_EQUAL_GENERATOR + template + inline SPROUT_CONSTEXPR bool + operator!=(sprout::math::quaternion const& lhs, sprout::complex const& rhs) + SPROUT_QUATERNION_NOT_EQUAL_GENERATOR + template + inline SPROUT_CONSTEXPR bool + operator!=(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) + SPROUT_QUATERNION_NOT_EQUAL_GENERATOR + +#undef SPROUT_QUATERNION_NOT_EQUAL_GENERATOR + + } // namespace math +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_QUATERNION_ARITHMETIC_OPERATORS_HPP diff --git a/sprout/math/quaternion/norm.hpp b/sprout/math/quaternion/norm.hpp index 391dbbb7..947246de 100644 --- a/sprout/math/quaternion/norm.hpp +++ b/sprout/math/quaternion/norm.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include #include #include diff --git a/sprout/math/quaternion/operators.hpp b/sprout/math/quaternion/operators.hpp index 5f20baaf..57958b6d 100644 --- a/sprout/math/quaternion/operators.hpp +++ b/sprout/math/quaternion/operators.hpp @@ -8,555 +8,7 @@ #ifndef SPROUT_MATH_QUATERNION_OPERATORS_HPP #define SPROUT_MATH_QUATERNION_OPERATORS_HPP -#include -#include -#include -#include -#include - -namespace sprout { - namespace math { - // - // operator+ - // - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator+(T const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs + rhs.R_component_1(), - rhs.R_component_2(), - rhs.R_component_3(), - rhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator+(sprout::math::quaternion const& lhs, T const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() + rhs, - lhs.R_component_2(), - lhs.R_component_3(), - lhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator+(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs.real() + rhs.R_component_1(), - lhs.imag() + rhs.R_component_2(), - rhs.R_component_3(), - rhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator+(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() + rhs.real(), - lhs.R_component_2() + rhs.imag(), - lhs.R_component_3(), - lhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator+(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() + rhs.R_component_1(), - lhs.R_component_2() + rhs.R_component_2(), - lhs.R_component_3() + rhs.R_component_3(), - lhs.R_component_4() + rhs.R_component_4() - ); - } - // - // operator- - // - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator-(T const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs - rhs.R_component_1(), - rhs.R_component_2(), - rhs.R_component_3(), - rhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator-(sprout::math::quaternion const& lhs, T const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() - rhs, - lhs.R_component_2(), - lhs.R_component_3(), - lhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator-(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs.real() - rhs.R_component_1(), - lhs.imag() - rhs.R_component_2(), - rhs.R_component_3(), - rhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator-(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() - rhs.real(), - lhs.R_component_2() - rhs.imag(), - lhs.R_component_3(), - lhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator-(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() - rhs.R_component_1(), - lhs.R_component_2() - rhs.R_component_2(), - lhs.R_component_3() - rhs.R_component_3(), - lhs.R_component_4() - rhs.R_component_4() - ); - } - // - // operator* - // - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator*(T const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs * rhs.R_component_1(), - lhs * rhs.R_component_2(), - lhs * rhs.R_component_3(), - lhs * rhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator*(sprout::math::quaternion const& lhs, T const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() * rhs, - lhs.R_component_2() * rhs, - lhs.R_component_3() * rhs, - lhs.R_component_4() * rhs - ); - } - namespace detail { - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - mul_qc_impl(T const& a, T const& b, T const& c, T const& d, T const& ar, T const& br) { - return sprout::math::quaternion( - +a * ar - b * br, - +a * br + b * ar, - +c * ar + d * br, - -c * br + d * ar - ); - } - } // namespace detail - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator*(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::detail::mul_qc_impl( - lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), - rhs.real(), rhs.imag() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator*(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { - return sprout::math::detail::mul_qc_impl( - rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4(), - lhs.real(), lhs.imag() - ); - } - namespace detail { - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - mul_qq_impl(T const& a, T const& b, T const& c, T const& d, T const& ar, T const& br, T const& cr, T const& dr) { - return sprout::math::quaternion( - +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 - ); - } - } // namespace detail - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator*(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::detail::mul_qq_impl( - lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), - rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4() - ); - } - // - // operator/ - // - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator/(T const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::quaternion( - lhs / rhs.R_component_1(), - lhs / rhs.R_component_2(), - lhs / rhs.R_component_3(), - lhs / rhs.R_component_4() - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator/(sprout::math::quaternion const& lhs, T const& rhs) { - return sprout::math::quaternion( - lhs.R_component_1() / rhs, - lhs.R_component_2() / rhs, - lhs.R_component_3() / rhs, - lhs.R_component_4() / rhs - ); - } - namespace detail { - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - div_qq_impl_4(sprout::array const& tt) { - return sprout::math::quaternion( - tt[0], tt[1], tt[2], tt[3] - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - div_qq_impl_3(sprout::array const& tr, T const& mixam, sprout::array const& tt) { - return sprout::math::detail::div_qq_impl_3( - sprout::math::detail::mul(tt, mixam / sprout::math::detail::sum(tr)) - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - div_qq_impl_2(T const& a, T const& b, T const& c, T const& d, sprout::array const& tr, T const& mixam) { - return sprout::math::detail::div_qq_impl_3( - sprout::math::detail::mul(tr, tr), mixam, - sprout::array{{ - +a * tr[0] + b * tr[1] + c * tr[2] + d * tr[3], - -a * tr[1] + b * tr[0] - c * tr[3] + d * tr[2], - -a * tr[2] + b * tr[3] + c * tr[0] - d * tr[1], - -a * tr[3] - b * tr[2] + c * tr[1] + d * tr[0] - }} - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - div_qq_impl_1(T const& a, T const& b, T const& c, T const& d, sprout::array const& tr, T const& mixam) { - return sprout::math::detail::div_qq_impl_2( - a, b, c, d, sprout::math::detail::mul(tr, mixam), mixam - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - div_qq_impl(T const& a, T const& b, T const& c, T const& d, sprout::array const& tr) { - return sprout::math::detail::div_qq_impl_1( - a, b, c, d, tr, - static_cast(1) / sprout::math::detail::abs_max(tr) - ); - } - } // namespace detail - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator/(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::detail::div_qq_impl( - lhs.real(), lhs.imag(), static_cast(0), static_cast(0), - sprout::array{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}} - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator/(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { - return sprout::math::detail::div_qq_impl( - lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), - sprout::array{{rhs.real(), rhs.imag(), static_cast(0), static_cast(0)}} - ); - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator/(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { - return sprout::math::detail::div_qq_impl( - lhs.R_component_1(), lhs.R_component_2(), lhs.R_component_3(), lhs.R_component_4(), - sprout::array{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}} - ); - } - - // - // operator+ - // operator- - // - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator+(sprout::math::quaternion const& q) { - return q; - } - template - inline SPROUT_CONSTEXPR sprout::math::quaternion - operator-(sprout::math::quaternion const& q) { - return sprout::math::quaternion(-q.R_component_1(), -q.R_component_2(), -q.R_component_3(), -q.R_component_4()); - } - - // - // operator== - // operator!= - // - template - inline SPROUT_CONSTEXPR bool - operator==(T const& lhs, sprout::math::quaternion const& rhs) { - return rhs.R_component_1() == lhs - && rhs.R_component_2() == static_cast(0) - && rhs.R_component_3() == static_cast(0) - && rhs.R_component_4() == static_cast(0) - ; - } - template - inline SPROUT_CONSTEXPR bool - operator==(sprout::math::quaternion const& lhs, T const& rhs) { - return lhs.R_component_1() == rhs - && lhs.R_component_2() == static_cast(0) - && lhs.R_component_3() == static_cast(0) - && lhs.R_component_4() == static_cast(0) - ; - } - template - inline SPROUT_CONSTEXPR bool - operator==(sprout::complex const& lhs, sprout::math::quaternion const& rhs) { - return rhs.R_component_1() == lhs.real() - && rhs.R_component_2() == lhs.imag() - && rhs.R_component_3() == static_cast(0) - && rhs.R_component_4() == static_cast(0) - ; - } - template - inline SPROUT_CONSTEXPR bool - operator==(sprout::math::quaternion const& lhs, sprout::complex const& rhs) { - return lhs.R_component_1() == rhs.real() - && lhs.R_component_2() == rhs.imag() - && lhs.R_component_3() == static_cast(0) - && lhs.R_component_4() == static_cast(0) - ; - } - template - inline SPROUT_CONSTEXPR bool - operator==(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) { - return rhs.R_component_1() == lhs.R_component_1() - && rhs.R_component_2() == lhs.R_component_2() - && rhs.R_component_3() == lhs.R_component_3() - && rhs.R_component_4() == lhs.R_component_4() - ; - } - -#define SPROUT_QUATERNION_NOT_EQUAL_GENERATOR \ - { \ - return !(lhs == rhs); \ - } - - template - inline SPROUT_CONSTEXPR bool - operator!=(T const& lhs, sprout::math::quaternion const& rhs) - SPROUT_QUATERNION_NOT_EQUAL_GENERATOR - template - inline SPROUT_CONSTEXPR bool - operator!=(sprout::math::quaternion const& lhs, T const& rhs) - SPROUT_QUATERNION_NOT_EQUAL_GENERATOR - template - inline SPROUT_CONSTEXPR bool - operator!=(sprout::complex const& lhs, sprout::math::quaternion const& rhs) - SPROUT_QUATERNION_NOT_EQUAL_GENERATOR - template - inline SPROUT_CONSTEXPR bool - operator!=(sprout::math::quaternion const& lhs, sprout::complex const& rhs) - SPROUT_QUATERNION_NOT_EQUAL_GENERATOR - template - inline SPROUT_CONSTEXPR bool - operator!=(sprout::math::quaternion const& lhs, sprout::math::quaternion const& rhs) - SPROUT_QUATERNION_NOT_EQUAL_GENERATOR - -#undef SPROUT_QUATERNION_NOT_EQUAL_GENERATOR - - // - // operator<< - // operator>> - // - template - inline SPROUT_NON_CONSTEXPR std::basic_ostream& - operator<<( std::basic_ostream& lhs, sprout::math::quaternion const& rhs) { - std::basic_ostringstream s; - s.flags(lhs.flags()); - s.imbue(lhs.getloc()); - s.precision(lhs.precision()); - s - << '(' - << rhs.R_component_1() << ',' - << rhs.R_component_2() << ',' - << rhs.R_component_3() << ',' - << rhs.R_component_4() - << ')' - ; - return lhs << s.str(); - } - template - inline SPROUT_NON_CONSTEXPR std::basic_istream& - operator>>(std::basic_istream& lhs, sprout::math::quaternion& rhs) { - std::ctype const& ct = std::use_facet >(lhs.getloc()); - T a = T(); - T b = T(); - T c = T(); - T d = T(); - sprout::complex u = sprout::complex(); - sprout::complex v = sprout::complex(); - Elem ch = Elem(); - char cc; - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == '(') { - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == '(') { - lhs.putback(ch); - lhs >> u; - a = u.real(); - b = u.imag(); - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a, b); - } else if (cc == ',') { - lhs >> v; - c = v.real(); - d = v.imag(); - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a, b, c, d); - } else { - lhs.setstate(std::ios_base::failbit); - } - } else { - lhs.setstate(std::ios_base::failbit); - } - } else { - lhs.putback(ch); - lhs >> a; - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a); - } else if (cc == ',') { - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == '(') { - lhs.putback(ch); - lhs >> v; - c = v.real(); - d = v.imag(); - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a, b, c, d); - } else { - lhs.setstate(std::ios_base::failbit); - } - } else { - lhs.putback(ch); - lhs >> b; - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a, b); - } else if (cc == ',') { - lhs >> c; - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a, b, c); - } else if (cc == ',') { - lhs >> d; - if (!lhs.good()) { - goto finish; - } - lhs >> ch; - if (!lhs.good()) { - goto finish; - } - cc = ct.narrow(ch, char()); - if (cc == ')') { - rhs = sprout::math::quaternion(a, b, c, d); - } else { - lhs.setstate(std::ios_base::failbit); - } - } else { - lhs.setstate(std::ios_base::failbit); - } - } else { - lhs.setstate(std::ios_base::failbit); - } - } - } else { - lhs.setstate(std::ios_base::failbit); - } - } - } else { - lhs.putback(ch); - lhs >> a; - if (!lhs.good()) { - goto finish; - } - rhs = sprout::math::quaternion(a); - } - finish: - return lhs; - } - } // namespace math -} // namespace sprout +#include +#include #endif // #ifndef SPROUT_MATH_QUATERNION_OPERATORS_HPP diff --git a/sprout/math/quaternion/stream_operators.hpp b/sprout/math/quaternion/stream_operators.hpp new file mode 100644 index 00000000..bd42f5e6 --- /dev/null +++ b/sprout/math/quaternion/stream_operators.hpp @@ -0,0 +1,200 @@ +/*============================================================================= + Copyright (c) 2011-2016 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_MATH_QUATERNION_STREAM_OPERATORS_HPP +#define SPROUT_MATH_QUATERNION_STREAM_OPERATORS_HPP + +#include +#include +#include +#include +#include + +namespace sprout { + namespace math { + // + // operator<< + // operator>> + // + template + inline SPROUT_NON_CONSTEXPR std::basic_ostream& + operator<<( std::basic_ostream& lhs, sprout::math::quaternion const& rhs) { + std::basic_ostringstream s; + s.flags(lhs.flags()); + s.imbue(lhs.getloc()); + s.precision(lhs.precision()); + s + << '(' + << rhs.R_component_1() << ',' + << rhs.R_component_2() << ',' + << rhs.R_component_3() << ',' + << rhs.R_component_4() + << ')' + ; + return lhs << s.str(); + } + template + inline SPROUT_NON_CONSTEXPR std::basic_istream& + operator>>(std::basic_istream& lhs, sprout::math::quaternion& rhs) { + std::ctype const& ct = std::use_facet >(lhs.getloc()); + T a = T(); + T b = T(); + T c = T(); + T d = T(); + sprout::complex u = sprout::complex(); + sprout::complex v = sprout::complex(); + Elem ch = Elem(); + char cc; + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == '(') { + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == '(') { + lhs.putback(ch); + lhs >> u; + a = u.real(); + b = u.imag(); + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a, b); + } else if (cc == ',') { + lhs >> v; + c = v.real(); + d = v.imag(); + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a, b, c, d); + } else { + lhs.setstate(std::ios_base::failbit); + } + } else { + lhs.setstate(std::ios_base::failbit); + } + } else { + lhs.putback(ch); + lhs >> a; + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a); + } else if (cc == ',') { + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == '(') { + lhs.putback(ch); + lhs >> v; + c = v.real(); + d = v.imag(); + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a, b, c, d); + } else { + lhs.setstate(std::ios_base::failbit); + } + } else { + lhs.putback(ch); + lhs >> b; + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a, b); + } else if (cc == ',') { + lhs >> c; + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a, b, c); + } else if (cc == ',') { + lhs >> d; + if (!lhs.good()) { + goto finish; + } + lhs >> ch; + if (!lhs.good()) { + goto finish; + } + cc = ct.narrow(ch, char()); + if (cc == ')') { + rhs = sprout::math::quaternion(a, b, c, d); + } else { + lhs.setstate(std::ios_base::failbit); + } + } else { + lhs.setstate(std::ios_base::failbit); + } + } else { + lhs.setstate(std::ios_base::failbit); + } + } + } else { + lhs.setstate(std::ios_base::failbit); + } + } + } else { + lhs.putback(ch); + lhs >> a; + if (!lhs.good()) { + goto finish; + } + rhs = sprout::math::quaternion(a); + } + finish: + return lhs; + } + } // namespace math +} // namespace sprout + +#endif // #ifndef SPROUT_MATH_QUATERNION_STREAM_OPERATORS_HPP