diff --git a/README.rst b/README.rst index 241679cc..3597a13a 100644 --- a/README.rst +++ b/README.rst @@ -35,8 +35,8 @@ Supported Compilers Linux: -* GCC, C++11/14 mode: 4.7.0, 4.7.1, 4.7.2, 4.7.3, 4.8.0, 4.8.1, 4.8.2, 4.9.0 -* Clang, C++11/14 mode: 3.2, 3.3, 3.4 +* GCC, C++11/14 mode: 4.7.0, 4.7.1, 4.7.2, 4.7.3, 4.7.4, 4.8.0, 4.8.1, 4.8.2, 4.8.3, 4.9.0 +* Clang, C++11/14 mode: 3.2, 3.3, 3.4, 3.4.1 ******************************************************************************* Author diff --git a/sprout/complex/values.hpp b/sprout/complex/values.hpp index ef8f3eb4..0725f169 100644 --- a/sprout/complex/values.hpp +++ b/sprout/complex/values.hpp @@ -8,7 +8,9 @@ #ifndef SPROUT_COMPLEX_VALUES_HPP #define SPROUT_COMPLEX_VALUES_HPP +#include #include +#include #include #include #include @@ -16,25 +18,49 @@ #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) { @@ -45,37 +71,112 @@ namespace sprout { 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::isinf(x.real()) || sprout::isinf(x.imag()) - ? sprout::complex(sprout::numeric_limits::infinity(), sprout::copysign(T(), x.imag())) - : 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 = 0) { - return sprout::complex(rho * sprout::cos(theta), rho * sprout::sin(theta)); + 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 diff --git a/sprout/math/pow.hpp b/sprout/math/pow.hpp index 6b6923ea..32c6fc56 100644 --- a/sprout/math/pow.hpp +++ b/sprout/math/pow.hpp @@ -44,7 +44,11 @@ namespace sprout { template inline SPROUT_CONSTEXPR T pow_impl(T x, T y) { - return sprout::math::exp(y * sprout::math::log(x)); + return x < 0 + ? is_odd(y) ? -sprout::math::exp(y * sprout::math::log(-x)) + : sprout::math::exp(y * sprout::math::log(-x)) + : sprout::math::exp(y * sprout::math::log(x)) + ; } } // namespace detail // diff --git a/sprout/type_traits.hpp b/sprout/type_traits.hpp index 2a990288..911c012a 100644 --- a/sprout/type_traits.hpp +++ b/sprout/type_traits.hpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include diff --git a/sprout/type_traits/complex_promote.hpp b/sprout/type_traits/complex_promote.hpp new file mode 100644 index 00000000..c495715d --- /dev/null +++ b/sprout/type_traits/complex_promote.hpp @@ -0,0 +1,71 @@ +/*============================================================================= + 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_TYPE_TRAITS_COMPLEX_PROMOTE_HPP +#define SPROUT_TYPE_TRAITS_COMPLEX_PROMOTE_HPP + +#include +#include +#include +#include +#include +#include +#include + +namespace sprout { + namespace detail { + template::value> + struct complex_promote1 + : public sprout::identity + {}; + template + struct complex_promote1 + : public sprout::identity::type> > + {}; + + template + struct complex_promote2 + : public sprout::identity::type::value_type, + typename sprout::detail::complex_promote1::type::value_type + >::type + >> + {}; + + template + struct complex_promote_impl; + template + struct complex_promote_impl + : public sprout::detail::complex_promote_impl< + typename sprout::detail::complex_promote2::type, + Tail... + > + {}; + template + struct complex_promote_impl + : public sprout::detail::complex_promote1 + {}; + } // namespace detail + + // + // complex_promote + // + template + struct complex_promote + : public sprout::detail::complex_promote_impl< + typename std::remove_cv::type... + > + {}; + +#if SPROUT_USE_TEMPLATE_ALIASES + template + using complex_promote_t = typename sprout::complex_promote::type; +#endif // #if SPROUT_USE_TEMPLATE_ALIASES +} // namespace sprout + +#endif // #ifndef SPROUT_TYPE_TRAITS_COMPLEX_PROMOTE_HPP