1
0
Fork 0
mirror of https://github.com/bolero-MURAKAMI/Sprout synced 2024-11-12 21:09:01 +00:00
Sprout/sprout/complex/operators.hpp

171 lines
5.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2015-01-10 10:13:57 +00:00
Copyright (c) 2011-2015 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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)
=============================================================================*/
2012-08-28 16:16:12 +00:00
#ifndef SPROUT_COMPLEX_OPERATORS_HPP
#define SPROUT_COMPLEX_OPERATORS_HPP
2014-11-28 07:19:07 +00:00
#include <istream>
#include <ostream>
2012-08-28 16:16:12 +00:00
#include <sprout/config.hpp>
#include <sprout/complex/complex.hpp>
namespace sprout {
// 26.4.6, operators:
template<typename T>
2012-10-05 15:58:56 +00:00
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());
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator+(sprout::complex<T> const& lhs, T const& rhs) {
return sprout::complex<T>(lhs.real() + rhs, lhs.imag());
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator+(T const& lhs, sprout::complex<T> const& rhs) {
return sprout::complex<T>(lhs + rhs.real(), rhs.imag());
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
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());
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator-(sprout::complex<T> const& lhs, T const& rhs) {
return sprout::complex<T>(lhs.real() - rhs, lhs.imag());
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator-(T const& lhs, sprout::complex<T> const& rhs) {
return sprout::complex<T>(lhs - rhs.real(), -rhs.imag());
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator*(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
lhs.real() * rhs.real() - lhs.imag() * rhs.imag(),
lhs.real() * rhs.imag() + lhs.imag() * rhs.real()
);
}
template<typename T>
2012-10-05 15:58:56 +00:00
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);
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
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());
2012-08-28 16:16:12 +00:00
}
namespace detail {
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
divides_impl(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs, T const& n) {
2012-08-28 16:16:12 +00:00
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>
2012-10-05 15:58:56 +00:00
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);
2012-08-28 16:16:12 +00:00
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator/(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs));
}
template<typename T>
2012-10-05 15:58:56 +00:00
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);
2012-08-28 16:16:12 +00:00
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator/(T const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return sprout::detail::divides_impl(lhs, rhs, sprout::detail::complex_norm(rhs));
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator+(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return x;
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
operator-(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(-x.real(), -x.imag());
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator==(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return lhs.real() == rhs.real() && lhs.imag() == rhs.imag();
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator==(sprout::complex<T> const& lhs, T const& rhs) {
2012-08-28 16:16:12 +00:00
return lhs.real() == rhs && lhs.imag() == T();
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator==(T const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return lhs == rhs.real() && T() == rhs.imag();
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator!=(sprout::complex<T> const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return !(lhs == rhs);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator!=(sprout::complex<T> const& lhs, T const& rhs) {
2012-08-28 16:16:12 +00:00
return !(lhs == rhs);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR bool
operator!=(T const& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return !(lhs == rhs);
}
template<typename T, typename Char, typename Traits>
2013-11-02 09:28:18 +00:00
inline SPROUT_NON_CONSTEXPR std::basic_istream<Char, Traits>&
2012-10-05 15:58:56 +00:00
operator>>(std::basic_istream<Char, Traits>& lhs, sprout::complex<T>& rhs) {
2012-08-28 16:16:12 +00:00
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>
2013-11-02 09:28:18 +00:00
inline SPROUT_NON_CONSTEXPR std::basic_ostream<Char, Traits>&
2012-10-05 15:58:56 +00:00
operator<<(std::basic_ostream<Char, Traits>& lhs, sprout::complex<T> const& rhs) {
2012-08-28 16:16:12 +00:00
return lhs << '(' << rhs.real() << ',' << rhs.imag() << ')';
}
} // namespace sprout
#endif // #ifndef SPROUT_COMPLEX_OPERATORS_HPP