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

121 lines
3.4 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2013 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)
=============================================================================*/
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 {
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR T
complex_norm(sprout::complex<T> 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
{}
2012-11-18 14:32:36 +00:00
SPROUT_CONSTEXPR T real() const 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;
}
2012-11-18 14:32:36 +00:00
SPROUT_CONSTEXPR T imag() const 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