/*============================================================================= Copyright (c) 2011-2014 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_VALUES_HPP #define SPROUT_COMPLEX_VALUES_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace sprout { template SPROUT_CONSTEXPR T norm(sprout::complex const& x); // // 26.4.7, values: // template inline SPROUT_CONSTEXPR T const& real(sprout::complex const& x) { return x.real(); } template< typename ArithmeticType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::float_promote::type real(ArithmeticType x) { return x; } template inline SPROUT_CONSTEXPR T const& imag(sprout::complex const& x) { return x.imag(); } template< typename ArithmeticType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::float_promote::type imag(ArithmeticType) { return 0; } template inline SPROUT_CONSTEXPR T& real(sprout::complex& x) { return x.real(); } template inline SPROUT_CONSTEXPR T& imag(sprout::complex& x) { return x.imag(); } template inline SPROUT_CONSTEXPR T abs(sprout::complex const& x) { return sprout::sqrt(sprout::norm(x)); } template inline SPROUT_CONSTEXPR T arg(sprout::complex const& x) { return sprout::atan2(x.imag(), x.real()); } template< typename ArithmeticType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::float_promote::type arg(ArithmeticType x) { return sprout::atan2(ArithmeticType(0), x); } template inline SPROUT_CONSTEXPR T norm(sprout::complex const& x) { return x.real() * x.real() + x.imag() * x.imag(); } template< typename ArithmeticType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::float_promote::type norm(ArithmeticType x) { typedef typename sprout::complex_promote::type type; return type(x) * type(x); } template inline SPROUT_CONSTEXPR sprout::complex conj(sprout::complex const& x) { return sprout::complex(x.real(), -x.imag()); } template< typename ArithmeticType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::complex_promote::type conj(ArithmeticType x) { typedef typename sprout::complex_promote::type type; return type(x); } template inline SPROUT_CONSTEXPR sprout::complex proj(sprout::complex const& x) { return sprout::math::isinf(x.real()) || sprout::math::isinf(x.imag()) ? sprout::complex(sprout::numeric_limits::infinity(), sprout::math::copysign(T(), x.imag())) : x ; } template< typename ArithmeticType, typename sprout::enabler_if::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::complex_promote::type proj(ArithmeticType x) { typedef typename sprout::complex_promote::type type; return sprout::math::isinf(x) ? type(sprout::numeric_limits::infinity()) : type(x) ; } namespace detail { template inline SPROUT_CONSTEXPR sprout::complex polar_impl(T const& x, T const& y) { typedef sprout::complex type; return type( sprout::math::isnan(x) ? T() : x, sprout::math::isnan(y) ? T() : y ); } } // namespace detail template inline SPROUT_CONSTEXPR sprout::complex polar(T const& rho, T const& theta = T()) { typedef sprout::complex type; return sprout::math::isnan(rho) || sprout::math::signbit(rho) ? type(sprout::numeric_limits::quiet_NaN(), sprout::numeric_limits::quiet_NaN()) : sprout::math::isnan(theta) ? sprout::math::isinf(rho) ? type(rho, theta) : type(theta, theta) : sprout::math::isinf(theta) ? sprout::math::isinf(rho) ? type(rho, sprout::numeric_limits::quiet_NaN()) : type(sprout::numeric_limits::quiet_NaN(), sprout::numeric_limits::quiet_NaN()) : sprout::detail::polar_impl(rho * sprout::cos(theta), rho * sprout::sin(theta)) ; } template< typename ArithmeticType1, typename ArithmeticType2, typename sprout::enabler_if::value && std::is_arithmetic::value>::type = sprout::enabler > inline SPROUT_CONSTEXPR typename sprout::complex_promote::type polar(ArithmeticType1 const& rho, ArithmeticType2 const& theta) { typedef typename sprout::complex_promote::type::value_type value_type; return sprout::polar(rho, theta); } } // namespace sprout #endif // #ifndef SPROUT_COMPLEX_VALUES_HPP