1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2025-08-03 12:49:50 +00:00

Split complex and quaternion arithmetic and stream operators

This commit is contained in:
Nick Matteo 2016-05-25 11:37:03 -04:00
parent 42133b805e
commit 69590e2ad2
20 changed files with 801 additions and 734 deletions

View 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

View file

@ -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>

View file

@ -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>

View file

@ -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>

View file

@ -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>

View file

@ -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 {

View file

@ -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 {

View file

@ -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

View file

@ -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>

View file

@ -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>

View file

@ -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>

View 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

View file

@ -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>

View file

@ -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>