fix complex norm

This commit is contained in:
bolero-MURAKAMI 2014-08-01 17:21:44 +09:00
parent d0d2b677bc
commit c2ea313c6c
2 changed files with 13 additions and 25 deletions

View file

@ -17,11 +17,10 @@
#include <sprout/math/log.hpp>
#include <sprout/math/atan2.hpp>
#include <sprout/complex/complex.hpp>
#include <sprout/complex/operators.hpp>
#include <sprout/complex/log.hpp>
#include <sprout/complex/sqrt.hpp>
#include <sprout/complex/operators.hpp>
namespace sprout {
//
// atanh
@ -40,25 +39,6 @@ namespace sprout {
// catanh(NaN + i<>‡) returns <20>}0 + ip /2 (where the sign of the real part of the result is unspecified).
// catanh(NaN + iNaN) returns NaN + iNaN.
//
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
atanh_impl_1(sprout::complex<T> const& x, T const& i2, T const& z, T const& num, T const& den) {
return sprout::complex<T>(
sprout::math::quarter<T>() * (sprout::math::log(i2 + num * num) - sprout::math::log(i2 + den * den)),
sprout::math::half<T>() * sprout::math::atan2(T(2) * x.imag(), z)
);
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
atanh_impl(sprout::complex<T> 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<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
atanh(sprout::complex<T> const& x) {
@ -75,7 +55,6 @@ namespace sprout {
? type(sprout::math::copysign(T(0), x.real()), sprout::math::copysign(sprout::math::half_pi<T>(), 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<T>::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)
;
}

View file

@ -10,9 +10,11 @@
#include <type_traits>
#include <sprout/config.hpp>
#include <sprout/complex/complex.hpp>
#include <sprout/type_traits/float_promote.hpp>
#include <sprout/type_traits/enabler_if.hpp>
#include <sprout/math/isnan.hpp>
#include <sprout/math/isinf.hpp>
#include <sprout/complex/complex.hpp>
namespace sprout {
//
@ -21,7 +23,11 @@ namespace sprout {
template<typename T>
inline SPROUT_CONSTEXPR T
norm(sprout::complex<T> 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<T>::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<ArithmeticType>::type
norm(ArithmeticType x) {
typedef typename sprout::float_promote<ArithmeticType>::type type;
return type(x) * type(x);
return sprout::math::isinf(x) ? sprout::numeric_limits<type>::infinity()
: sprout::math::isnan(x) ? type(x)
: type(x) * type(x)
;
}
} // namespace sprout