2013-08-08 09:54:33 +00:00
|
|
|
/*=============================================================================
|
2014-01-08 07:48:12 +00:00
|
|
|
Copyright (c) 2011-2014 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_COMPLEX_HPP
|
|
|
|
#define SPROUT_COMPLEX_COMPLEX_HPP
|
|
|
|
|
|
|
|
#include <sprout/config.hpp>
|
|
|
|
|
|
|
|
namespace sprout {
|
|
|
|
template<typename T>
|
|
|
|
class complex;
|
|
|
|
|
|
|
|
namespace detail {
|
2014-04-18 10:33:48 +00:00
|
|
|
template<typename Complex>
|
|
|
|
inline SPROUT_CONSTEXPR typename Complex::value_type
|
|
|
|
complex_norm(Complex const& x) {
|
2012-08-28 16:16:12 +00:00
|
|
|
return x.real() * x.real() + x.imag() * x.imag();
|
|
|
|
}
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
//
|
|
|
|
// complex
|
|
|
|
//
|
|
|
|
template<typename T>
|
|
|
|
class complex {
|
|
|
|
public:
|
|
|
|
typedef T value_type;
|
|
|
|
private:
|
|
|
|
T re_;
|
|
|
|
T im_;
|
|
|
|
public:
|
2012-11-18 14:32:36 +00:00
|
|
|
SPROUT_CONSTEXPR complex(T const& re = T(), T const& im = T()) SPROUT_NOEXCEPT
|
2013-02-26 01:43:27 +00:00
|
|
|
: re_(re), im_(im)
|
2012-08-28 16:16:12 +00:00
|
|
|
{}
|
2013-10-29 10:15:52 +00:00
|
|
|
complex(complex const&) = default;
|
2012-08-28 16:16:12 +00:00
|
|
|
template<typename X>
|
2012-11-18 14:32:36 +00:00
|
|
|
SPROUT_CONSTEXPR complex(complex<X> const& other) SPROUT_NOEXCEPT
|
2013-02-26 01:43:27 +00:00
|
|
|
: re_(other.real()), im_(other.imag())
|
2012-08-28 16:16:12 +00:00
|
|
|
{}
|
2014-04-18 02:34:28 +00:00
|
|
|
SPROUT_CONSTEXPR T const& real() const SPROUT_NOEXCEPT {
|
|
|
|
return re_;
|
|
|
|
}
|
|
|
|
SPROUT_CXX14_CONSTEXPR T& real() SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
return re_;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void real(T re) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ = re;
|
|
|
|
}
|
2014-04-18 02:34:28 +00:00
|
|
|
SPROUT_CONSTEXPR T const& imag() const SPROUT_NOEXCEPT {
|
|
|
|
return im_;
|
|
|
|
}
|
|
|
|
SPROUT_CXX14_CONSTEXPR T& imag() SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
return im_;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR void imag(T im) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
im_ = im;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator=(T const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ = rhs;
|
|
|
|
im_ = T();
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator+=(T const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ += rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator-=(T const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ -= rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator*=(T const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ *= rhs;
|
|
|
|
im_ *= rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator/=(T const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ /= rhs;
|
|
|
|
im_ /= rhs;
|
|
|
|
return *this;
|
|
|
|
}
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator=(complex const& rhs) SPROUT_NOEXCEPT {
|
|
|
|
re_ = rhs.real();
|
|
|
|
im_ = rhs.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
2012-08-28 16:16:12 +00:00
|
|
|
template<typename X>
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator=(complex<X> const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ = rhs.real();
|
|
|
|
im_ = rhs.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<typename X>
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator+=(complex<X> const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ += rhs.real();
|
|
|
|
im_ += rhs.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<typename X>
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator-=(complex<X> const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ -= rhs.real();
|
|
|
|
im_ -= rhs.imag();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
template<typename X>
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator*=(complex<X> const& rhs) SPROUT_NOEXCEPT {
|
2012-10-05 15:58:56 +00:00
|
|
|
return *this = complex(
|
2012-08-28 16:16:12 +00:00
|
|
|
re_ * rhs.real() - im_ * rhs.imag(),
|
|
|
|
re_ * rhs.imag() + im_ * rhs.real()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
template<typename X>
|
2013-10-29 10:15:52 +00:00
|
|
|
SPROUT_CXX14_CONSTEXPR complex& operator/=(complex<X> const& rhs) SPROUT_NOEXCEPT {
|
2012-08-28 16:16:12 +00:00
|
|
|
T n = sprout::detail::complex_norm(rhs);
|
2012-10-05 15:58:56 +00:00
|
|
|
return *this = complex(
|
2012-08-28 16:16:12 +00:00
|
|
|
(re_ * rhs.real() + im_ * rhs.imag()) / n,
|
|
|
|
(im_ * rhs.real() - re_ * rhs.imag()) / n
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace sprout
|
|
|
|
|
|
|
|
#endif // #ifndef SPROUT_COMPLEX_COMPLEX_HPP
|