mirror of
https://github.com/bolero-MURAKAMI/Sprout.git
synced 2024-12-23 21:25:49 +00:00
Split complex and quaternion arithmetic and stream operators
This commit is contained in:
parent
42133b805e
commit
69590e2ad2
20 changed files with 801 additions and 734 deletions
|
@ -10,7 +10,8 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/stream_operators.hpp>
|
||||
#include <sprout/complex/values.hpp>
|
||||
#include <sprout/complex/transcendentals.hpp>
|
||||
#include <sprout/complex/hash.hpp>
|
||||
|
|
136
sprout/complex/arithmetic_operators.hpp
Normal file
136
sprout/complex/arithmetic_operators.hpp
Normal file
|
@ -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 <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
|
||||
namespace sprout {
|
||||
// 26.4.6, operators:
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() + rhs.real(), lhs.imag() + rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() + rhs, lhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs + rhs.real(), rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() - rhs.real(), lhs.imag() - rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() - rhs, lhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs - rhs.real(), -rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator*(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(
|
||||
lhs.real() * rhs.real() - lhs.imag() * rhs.imag(),
|
||||
lhs.real() * rhs.imag() + lhs.imag() * rhs.real()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator*(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() * rhs, lhs.imag() * rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator*(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs * rhs.real(), lhs * rhs.imag());
|
||||
}
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
divides_impl(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs, T const& n) {
|
||||
return sprout::complex<T>(
|
||||
(lhs.real() * rhs.real() + lhs.imag() * rhs.imag()) / n,
|
||||
(lhs.imag() * rhs.real() - lhs.real() * rhs.imag()) / n
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
divides_impl(T const& lhs, sprout::complex<T> const& rhs, T const& n) {
|
||||
return sprout::complex<T>(lhs * rhs.real() / n, -lhs * rhs.imag() / n);
|
||||
}
|
||||
} // namespace detail
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator/(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs));
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator/(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() / rhs, lhs.imag() / rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator/(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs));
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(sprout::complex<T> const& x) {
|
||||
return x;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(sprout::complex<T> const& x) {
|
||||
return sprout::complex<T>(-x.real(), -x.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs.real() == rhs.real() && lhs.imag() == rhs.imag();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return lhs.real() == rhs && lhs.imag() == T();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs == rhs.real() && T() == rhs.imag();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_COMPLEX_ARITHMETIC_OPERATORS_HPP
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/perp.hpp>
|
||||
#include <sprout/complex/asinh.hpp>
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/perp.hpp>
|
||||
#include <sprout/complex/atanh.hpp>
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include <sprout/math/log.hpp>
|
||||
#include <sprout/math/atan2.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/log.hpp>
|
||||
#include <sprout/complex/sqrt.hpp>
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@
|
|||
#include <sprout/math/cosh.hpp>
|
||||
#include <sprout/math/sinh.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/euler.hpp>
|
||||
#include <sprout/complex/detail/copysign_mul.hpp>
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include <sprout/math/constants.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/log.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
#include <sprout/math/constants.hpp>
|
||||
#include <sprout/math/isnan.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/log.hpp>
|
||||
|
||||
namespace sprout {
|
||||
|
|
|
@ -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 <istream>
|
||||
#include <ostream>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
|
||||
namespace sprout {
|
||||
// 26.4.6, operators:
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() + rhs.real(), lhs.imag() + rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() + rhs, lhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs + rhs.real(), rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() - rhs.real(), lhs.imag() - rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() - rhs, lhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs - rhs.real(), -rhs.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator*(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(
|
||||
lhs.real() * rhs.real() - lhs.imag() * rhs.imag(),
|
||||
lhs.real() * rhs.imag() + lhs.imag() * rhs.real()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator*(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() * rhs, lhs.imag() * rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator*(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::complex<T>(lhs * rhs.real(), lhs * rhs.imag());
|
||||
}
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
divides_impl(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs, T const& n) {
|
||||
return sprout::complex<T>(
|
||||
(lhs.real() * rhs.real() + lhs.imag() * rhs.imag()) / n,
|
||||
(lhs.imag() * rhs.real() - lhs.real() * rhs.imag()) / n
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
divides_impl(T const& lhs, sprout::complex<T> const& rhs, T const& n) {
|
||||
return sprout::complex<T>(lhs * rhs.real() / n, -lhs * rhs.imag() / n);
|
||||
}
|
||||
} // namespace detail
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator/(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs));
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator/(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return sprout::complex<T>(lhs.real() / rhs, lhs.imag() / rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator/(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs));
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator+(sprout::complex<T> const& x) {
|
||||
return x;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::complex<T>
|
||||
operator-(sprout::complex<T> const& x) {
|
||||
return sprout::complex<T>(-x.real(), -x.imag());
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs.real() == rhs.real() && lhs.imag() == rhs.imag();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return lhs.real() == rhs && lhs.imag() == T();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs == rhs.real() && T() == rhs.imag();
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::complex<T> const& lhs, T const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(T const& lhs, sprout::complex<T> const& rhs) {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
template<typename T, typename Char, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_istream<Char, Traits>&
|
||||
operator>>(std::basic_istream<Char, Traits>& lhs, sprout::complex<T>& rhs) {
|
||||
T re, im;
|
||||
Char ch;
|
||||
lhs >> ch;
|
||||
if (ch == '(') {
|
||||
lhs >> re >> ch;
|
||||
if (ch == ',') {
|
||||
lhs >> im >> ch;
|
||||
if (ch == ')') {
|
||||
rhs = sprout::complex<T>(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<typename T, typename Char, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Char, Traits>&
|
||||
operator<<(std::basic_ostream<Char, Traits>& lhs, sprout::complex<T> 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 <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/stream_operators.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_COMPLEX_OPERATORS_HPP
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/pow.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/exp.hpp>
|
||||
#include <sprout/complex/log.hpp>
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/perp.hpp>
|
||||
#include <sprout/complex/sinh.hpp>
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include <sprout/math/cosh.hpp>
|
||||
#include <sprout/math/sinh.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/euler.hpp>
|
||||
#include <sprout/complex/detail/copysign_mul.hpp>
|
||||
|
||||
|
|
51
sprout/complex/stream_operators.hpp
Normal file
51
sprout/complex/stream_operators.hpp
Normal file
|
@ -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 <istream>
|
||||
#include <ostream>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
|
||||
namespace sprout {
|
||||
template<typename T, typename Char, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_istream<Char, Traits>&
|
||||
operator>>(std::basic_istream<Char, Traits>& lhs, sprout::complex<T>& rhs) {
|
||||
T re, im;
|
||||
Char ch;
|
||||
lhs >> ch;
|
||||
if (ch == '(') {
|
||||
lhs >> re >> ch;
|
||||
if (ch == ',') {
|
||||
lhs >> im >> ch;
|
||||
if (ch == ')') {
|
||||
rhs = sprout::complex<T>(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<typename T, typename Char, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Char, Traits>&
|
||||
operator<<(std::basic_ostream<Char, Traits>& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs << '(' << rhs.real() << ',' << rhs.imag() << ')';
|
||||
}
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_COMPLEX_STREAM_OPERATORS_HPP
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/perp.hpp>
|
||||
#include <sprout/complex/tanh.hpp>
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include <sprout/math/sin.hpp>
|
||||
#include <sprout/math/copysign.hpp>
|
||||
#include <sprout/complex/complex.hpp>
|
||||
#include <sprout/complex/operators.hpp>
|
||||
#include <sprout/complex/arithmetic_operators.hpp>
|
||||
#include <sprout/complex/cosh.hpp>
|
||||
#include <sprout/complex/sinh.hpp>
|
||||
|
||||
|
|
|
@ -10,7 +10,8 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/quaternion/quaternion.hpp>
|
||||
#include <sprout/math/quaternion/operators.hpp>
|
||||
#include <sprout/math/quaternion/arithmetic_operators.hpp>
|
||||
#include <sprout/math/quaternion/stream_operators.hpp>
|
||||
#include <sprout/math/quaternion/values.hpp>
|
||||
#include <sprout/math/quaternion/creations.hpp>
|
||||
#include <sprout/math/quaternion/transcendentals.hpp>
|
||||
|
|
381
sprout/math/quaternion/arithmetic_operators.hpp
Normal file
381
sprout/math/quaternion/arithmetic_operators.hpp
Normal file
|
@ -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 <sprout/config.hpp>
|
||||
#include <sprout/math/quaternion/quaternion.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
//
|
||||
// operator+
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs + rhs.R_component_1(),
|
||||
rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() + rhs,
|
||||
lhs.R_component_2(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.real() + rhs.R_component_1(),
|
||||
lhs.imag() + rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() + rhs.real(),
|
||||
lhs.R_component_2() + rhs.imag(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs - rhs.R_component_1(),
|
||||
rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() - rhs,
|
||||
lhs.R_component_2(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.real() - rhs.R_component_1(),
|
||||
lhs.imag() - rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() - rhs.real(),
|
||||
lhs.R_component_2() - rhs.imag(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs * rhs.R_component_1(),
|
||||
lhs * rhs.R_component_2(),
|
||||
lhs * rhs.R_component_3(),
|
||||
lhs * rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() * rhs,
|
||||
lhs.R_component_2() * rhs,
|
||||
lhs.R_component_3() * rhs,
|
||||
lhs.R_component_4() * rhs
|
||||
);
|
||||
}
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
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<T>(
|
||||
+a * ar - b * br,
|
||||
+a * br + b * ar,
|
||||
+c * ar + d * br,
|
||||
-c * br + d * ar
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::complex<T> const& lhs, sprout::math::quaternion<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::math::quaternion<T> const& lhs, sprout::complex<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
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<T>(
|
||||
+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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs / rhs.R_component_1(),
|
||||
lhs / rhs.R_component_2(),
|
||||
lhs / rhs.R_component_3(),
|
||||
lhs / rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() / rhs,
|
||||
lhs.R_component_2() / rhs,
|
||||
lhs.R_component_3() / rhs,
|
||||
lhs.R_component_4() / rhs
|
||||
);
|
||||
}
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_4(sprout::array<T, 4> const& tt) {
|
||||
return sprout::math::quaternion<T>(
|
||||
tt[0], tt[1], tt[2], tt[3]
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_3(sprout::array<T, 4> const& tr, T const& mixam, sprout::array<T, 4> const& tt) {
|
||||
return sprout::math::detail::div_qq_impl_3(
|
||||
sprout::math::detail::mul(tt, mixam / sprout::math::detail::sum(tr))
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_2(T const& a, T const& b, T const& c, T const& d, sprout::array<T, 4> const& tr, T const& mixam) {
|
||||
return sprout::math::detail::div_qq_impl_3(
|
||||
sprout::math::detail::mul(tr, tr), mixam,
|
||||
sprout::array<T, 4>{{
|
||||
+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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_1(T const& a, T const& b, T const& c, T const& d, sprout::array<T, 4> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl(T const& a, T const& b, T const& c, T const& d, sprout::array<T, 4> const& tr) {
|
||||
return sprout::math::detail::div_qq_impl_1(
|
||||
a, b, c, d, tr,
|
||||
static_cast<T>(1) / sprout::math::detail::abs_max(tr)
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::detail::div_qq_impl(
|
||||
lhs.real(), lhs.imag(), static_cast<T>(0), static_cast<T>(0),
|
||||
sprout::array<T, 4>{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}}
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::math::quaternion<T> const& lhs, sprout::complex<T> 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<T, 4>{{rhs.real(), rhs.imag(), static_cast<T>(0), static_cast<T>(0)}}
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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<T, 4>{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}}
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// operator+
|
||||
// operator-
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& q) {
|
||||
return q;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& q) {
|
||||
return sprout::math::quaternion<T>(-q.R_component_1(), -q.R_component_2(), -q.R_component_3(), -q.R_component_4());
|
||||
}
|
||||
|
||||
//
|
||||
// operator==
|
||||
// operator!=
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return rhs.R_component_1() == lhs
|
||||
&& rhs.R_component_2() == static_cast<T>(0)
|
||||
&& rhs.R_component_3() == static_cast<T>(0)
|
||||
&& rhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return lhs.R_component_1() == rhs
|
||||
&& lhs.R_component_2() == static_cast<T>(0)
|
||||
&& lhs.R_component_3() == static_cast<T>(0)
|
||||
&& lhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return rhs.R_component_1() == lhs.real()
|
||||
&& rhs.R_component_2() == lhs.imag()
|
||||
&& rhs.R_component_3() == static_cast<T>(0)
|
||||
&& rhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs.R_component_1() == rhs.real()
|
||||
&& lhs.R_component_2() == rhs.imag()
|
||||
&& lhs.R_component_3() == static_cast<T>(0)
|
||||
&& lhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(T const& lhs, sprout::math::quaternion<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::math::quaternion<T> const& lhs, T const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/quaternion/quaternion.hpp>
|
||||
#include <sprout/math/quaternion/operators.hpp>
|
||||
#include <sprout/math/quaternion/arithmetic_operators.hpp>
|
||||
#include <sprout/math/quaternion/real.hpp>
|
||||
#include <sprout/math/quaternion/conj.hpp>
|
||||
|
||||
|
|
|
@ -8,555 +8,7 @@
|
|||
#ifndef SPROUT_MATH_QUATERNION_OPERATORS_HPP
|
||||
#define SPROUT_MATH_QUATERNION_OPERATORS_HPP
|
||||
|
||||
#include <iosfwd>
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/quaternion/quaternion.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
//
|
||||
// operator+
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs + rhs.R_component_1(),
|
||||
rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() + rhs,
|
||||
lhs.R_component_2(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.real() + rhs.R_component_1(),
|
||||
lhs.imag() + rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() + rhs.real(),
|
||||
lhs.R_component_2() + rhs.imag(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs - rhs.R_component_1(),
|
||||
rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() - rhs,
|
||||
lhs.R_component_2(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.real() - rhs.R_component_1(),
|
||||
lhs.imag() - rhs.R_component_2(),
|
||||
rhs.R_component_3(),
|
||||
rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() - rhs.real(),
|
||||
lhs.R_component_2() - rhs.imag(),
|
||||
lhs.R_component_3(),
|
||||
lhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs * rhs.R_component_1(),
|
||||
lhs * rhs.R_component_2(),
|
||||
lhs * rhs.R_component_3(),
|
||||
lhs * rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() * rhs,
|
||||
lhs.R_component_2() * rhs,
|
||||
lhs.R_component_3() * rhs,
|
||||
lhs.R_component_4() * rhs
|
||||
);
|
||||
}
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
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<T>(
|
||||
+a * ar - b * br,
|
||||
+a * br + b * ar,
|
||||
+c * ar + d * br,
|
||||
-c * br + d * ar
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::complex<T> const& lhs, sprout::math::quaternion<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::math::quaternion<T> const& lhs, sprout::complex<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
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<T>(
|
||||
+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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator*(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs / rhs.R_component_1(),
|
||||
lhs / rhs.R_component_2(),
|
||||
lhs / rhs.R_component_3(),
|
||||
lhs / rhs.R_component_4()
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return sprout::math::quaternion<T>(
|
||||
lhs.R_component_1() / rhs,
|
||||
lhs.R_component_2() / rhs,
|
||||
lhs.R_component_3() / rhs,
|
||||
lhs.R_component_4() / rhs
|
||||
);
|
||||
}
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_4(sprout::array<T, 4> const& tt) {
|
||||
return sprout::math::quaternion<T>(
|
||||
tt[0], tt[1], tt[2], tt[3]
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_3(sprout::array<T, 4> const& tr, T const& mixam, sprout::array<T, 4> const& tt) {
|
||||
return sprout::math::detail::div_qq_impl_3(
|
||||
sprout::math::detail::mul(tt, mixam / sprout::math::detail::sum(tr))
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_2(T const& a, T const& b, T const& c, T const& d, sprout::array<T, 4> const& tr, T const& mixam) {
|
||||
return sprout::math::detail::div_qq_impl_3(
|
||||
sprout::math::detail::mul(tr, tr), mixam,
|
||||
sprout::array<T, 4>{{
|
||||
+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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl_1(T const& a, T const& b, T const& c, T const& d, sprout::array<T, 4> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
div_qq_impl(T const& a, T const& b, T const& c, T const& d, sprout::array<T, 4> const& tr) {
|
||||
return sprout::math::detail::div_qq_impl_1(
|
||||
a, b, c, d, tr,
|
||||
static_cast<T>(1) / sprout::math::detail::abs_max(tr)
|
||||
);
|
||||
}
|
||||
} // namespace detail
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return sprout::math::detail::div_qq_impl(
|
||||
lhs.real(), lhs.imag(), static_cast<T>(0), static_cast<T>(0),
|
||||
sprout::array<T, 4>{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}}
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::math::quaternion<T> const& lhs, sprout::complex<T> 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<T, 4>{{rhs.real(), rhs.imag(), static_cast<T>(0), static_cast<T>(0)}}
|
||||
);
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator/(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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<T, 4>{{rhs.R_component_1(), rhs.R_component_2(), rhs.R_component_3(), rhs.R_component_4()}}
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// operator+
|
||||
// operator-
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator+(sprout::math::quaternion<T> const& q) {
|
||||
return q;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR sprout::math::quaternion<T>
|
||||
operator-(sprout::math::quaternion<T> const& q) {
|
||||
return sprout::math::quaternion<T>(-q.R_component_1(), -q.R_component_2(), -q.R_component_3(), -q.R_component_4());
|
||||
}
|
||||
|
||||
//
|
||||
// operator==
|
||||
// operator!=
|
||||
//
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(T const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return rhs.R_component_1() == lhs
|
||||
&& rhs.R_component_2() == static_cast<T>(0)
|
||||
&& rhs.R_component_3() == static_cast<T>(0)
|
||||
&& rhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::math::quaternion<T> const& lhs, T const& rhs) {
|
||||
return lhs.R_component_1() == rhs
|
||||
&& lhs.R_component_2() == static_cast<T>(0)
|
||||
&& lhs.R_component_3() == static_cast<T>(0)
|
||||
&& lhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
return rhs.R_component_1() == lhs.real()
|
||||
&& rhs.R_component_2() == lhs.imag()
|
||||
&& rhs.R_component_3() == static_cast<T>(0)
|
||||
&& rhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs) {
|
||||
return lhs.R_component_1() == rhs.real()
|
||||
&& lhs.R_component_2() == rhs.imag()
|
||||
&& lhs.R_component_3() == static_cast<T>(0)
|
||||
&& lhs.R_component_4() == static_cast<T>(0)
|
||||
;
|
||||
}
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator==(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> 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<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(T const& lhs, sprout::math::quaternion<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::math::quaternion<T> const& lhs, T const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::complex<T> const& lhs, sprout::math::quaternion<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::math::quaternion<T> const& lhs, sprout::complex<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
template<typename T>
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
operator!=(sprout::math::quaternion<T> const& lhs, sprout::math::quaternion<T> const& rhs)
|
||||
SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
|
||||
#undef SPROUT_QUATERNION_NOT_EQUAL_GENERATOR
|
||||
|
||||
//
|
||||
// operator<<
|
||||
// operator>>
|
||||
//
|
||||
template<typename T, typename Elem, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
|
||||
operator<<( std::basic_ostream<Elem, Traits>& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
std::basic_ostringstream<Elem, Traits> 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<typename T, typename Elem, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>&
|
||||
operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::math::quaternion<T>& rhs) {
|
||||
std::ctype<Elem> const& ct = std::use_facet<std::ctype<Elem> >(lhs.getloc());
|
||||
T a = T();
|
||||
T b = T();
|
||||
T c = T();
|
||||
T d = T();
|
||||
sprout::complex<T> u = sprout::complex<T>();
|
||||
sprout::complex<T> v = sprout::complex<T>();
|
||||
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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(a);
|
||||
}
|
||||
finish:
|
||||
return lhs;
|
||||
}
|
||||
} // namespace math
|
||||
} // namespace sprout
|
||||
#include <sprout/math/quaternion/arithmetic_operators.hpp>
|
||||
#include <sprout/math/quaternion/stream_operators.hpp>
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_QUATERNION_OPERATORS_HPP
|
||||
|
|
200
sprout/math/quaternion/stream_operators.hpp
Normal file
200
sprout/math/quaternion/stream_operators.hpp
Normal file
|
@ -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 <iosfwd>
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
#include <sprout/config.hpp>
|
||||
#include <sprout/math/quaternion/quaternion.hpp>
|
||||
|
||||
namespace sprout {
|
||||
namespace math {
|
||||
//
|
||||
// operator<<
|
||||
// operator>>
|
||||
//
|
||||
template<typename T, typename Elem, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Elem, Traits>&
|
||||
operator<<( std::basic_ostream<Elem, Traits>& lhs, sprout::math::quaternion<T> const& rhs) {
|
||||
std::basic_ostringstream<Elem, Traits> 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<typename T, typename Elem, typename Traits>
|
||||
inline SPROUT_NON_CONSTEXPR std::basic_istream<Elem, Traits>&
|
||||
operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::math::quaternion<T>& rhs) {
|
||||
std::ctype<Elem> const& ct = std::use_facet<std::ctype<Elem> >(lhs.getloc());
|
||||
T a = T();
|
||||
T b = T();
|
||||
T c = T();
|
||||
T d = T();
|
||||
sprout::complex<T> u = sprout::complex<T>();
|
||||
sprout::complex<T> v = sprout::complex<T>();
|
||||
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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(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<T>(a);
|
||||
}
|
||||
finish:
|
||||
return lhs;
|
||||
}
|
||||
} // namespace math
|
||||
} // namespace sprout
|
||||
|
||||
#endif // #ifndef SPROUT_MATH_QUATERNION_STREAM_OPERATORS_HPP
|
Loading…
Reference in a new issue