From c2ea313c6c5399578ff17b37cb035e92f86cf0f8 Mon Sep 17 00:00:00 2001 From: bolero-MURAKAMI Date: Fri, 1 Aug 2014 17:21:44 +0900 Subject: [PATCH] fix complex norm --- sprout/complex/atanh.hpp | 23 +---------------------- sprout/complex/norm.hpp | 15 ++++++++++++--- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/sprout/complex/atanh.hpp b/sprout/complex/atanh.hpp index 3dcbbc47..669cd067 100644 --- a/sprout/complex/atanh.hpp +++ b/sprout/complex/atanh.hpp @@ -17,11 +17,10 @@ #include #include #include +#include #include #include -#include - namespace sprout { // // atanh @@ -40,25 +39,6 @@ namespace sprout { // catanh(NaN + i) returns }0 + ip /2 (where the sign of the real part of the result is unspecified). // catanh(NaN + iNaN) returns NaN + iNaN. // - namespace detail { - template - inline SPROUT_CONSTEXPR sprout::complex - atanh_impl_1(sprout::complex const& x, T const& i2, T const& z, T const& num, T const& den) { - return sprout::complex( - sprout::math::quarter() * (sprout::math::log(i2 + num * num) - sprout::math::log(i2 + den * den)), - sprout::math::half() * sprout::math::atan2(T(2) * x.imag(), z) - ); - } - template - inline SPROUT_CONSTEXPR sprout::complex - atanh_impl(sprout::complex const& x, T const& i2) { - return sprout::detail::atanh_impl_1( - x, i2, - T(1) - i2 - x.real() * x.real(), - T(1) + x.imag(), T(1) - x.imag() - ); - } - } // namespace detail template inline SPROUT_CONSTEXPR sprout::complex atanh(sprout::complex const& x) { @@ -75,7 +55,6 @@ namespace sprout { ? type(sprout::math::copysign(T(0), x.real()), sprout::math::copysign(sprout::math::half_pi(), x.imag())) : x.real() == 0 && x.imag() == 0 ? x : (x.real() == 1 || x.real() == -1) && x.imag() == 0 ? type(sprout::math::copysign(sprout::numeric_limits::infinity(), x.real()), x.imag()) -// : sprout::detail::atanh_impl(x, x.imag() * x.imag()) : (sprout::log(T(1) + x) - sprout::log(T(1) - x)) / T(2) ; } diff --git a/sprout/complex/norm.hpp b/sprout/complex/norm.hpp index f9801f40..51cb6aa6 100644 --- a/sprout/complex/norm.hpp +++ b/sprout/complex/norm.hpp @@ -10,9 +10,11 @@ #include #include -#include #include #include +#include +#include +#include namespace sprout { // @@ -21,7 +23,11 @@ namespace sprout { template inline SPROUT_CONSTEXPR T norm(sprout::complex const& x) { - return x.real() * x.real() + x.imag() * x.imag(); + return sprout::math::isinf(x.imag()) || sprout::math::isinf(x.real()) ? sprout::numeric_limits::infinity() + : sprout::math::isnan(x.real()) ? x.real() + : sprout::math::isnan(x.imag()) ? x.imag() + : x.real() * x.real() + x.imag() * x.imag() + ; } template< typename ArithmeticType, @@ -30,7 +36,10 @@ namespace sprout { inline SPROUT_CONSTEXPR typename sprout::float_promote::type norm(ArithmeticType x) { typedef typename sprout::float_promote::type type; - return type(x) * type(x); + return sprout::math::isinf(x) ? sprout::numeric_limits::infinity() + : sprout::math::isnan(x) ? type(x) + : type(x) * type(x) + ; } } // namespace sprout