Sprout/sprout/complex/transcendentals.hpp

330 lines
9.9 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
2014-01-08 07:48:12 +00:00
Copyright (c) 2011-2014 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
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_TRANSCENDENTALS_HPP
#define SPROUT_COMPLEX_TRANSCENDENTALS_HPP
#include <sprout/config.hpp>
#include <sprout/complex/complex.hpp>
#include <sprout/complex/operators.hpp>
#include <sprout/complex/values.hpp>
#include <sprout/math/constants.hpp>
#include <sprout/math/abs.hpp>
#include <sprout/math/sin.hpp>
#include <sprout/math/cos.hpp>
#include <sprout/math/atan2.hpp>
#include <sprout/math/sinh.hpp>
#include <sprout/math/cosh.hpp>
#include <sprout/math/exp.hpp>
#include <sprout/math/log.hpp>
#include <sprout/math/pow.hpp>
#include <sprout/math/sqrt.hpp>
namespace sprout {
// 26.4.8, transcendentals:
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
acos(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
asin(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
atan(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
acosh(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
asinh(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
atanh(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
cos(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
cosh(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
exp(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
log(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
log10(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
pow(sprout::complex<T> const& x, T const& y);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
pow(sprout::complex<T> const& x, sprout::complex<T> const& y);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
pow(T const& x, sprout::complex<T> const& y);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
sin(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
sinh(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
sqrt(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
tan(sprout::complex<T> const& x);
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
SPROUT_CONSTEXPR sprout::complex<T>
tanh(sprout::complex<T> const& x);
//
// acos
// asin
// atan
//
2012-08-28 16:16:12 +00:00
namespace detail {
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
acos_impl(sprout::complex<T> const& t) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(sprout::math::half_pi<T>() - t.real(), -t.imag());
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
acos(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::detail::acos_impl(sprout::asin(x));
}
namespace detail {
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
asin_impl(sprout::complex<T> const& t) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(t.imag(), -t.real());
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
asin(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::detail::asin_impl(sprout::asinh(sprout::complex<T>(-x.imag(), x.real())));
}
namespace detail {
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
atan_impl_1(sprout::complex<T> const& x, T const& r2, T const& z, T const& num, T const& den) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
T(0.5) * sprout::atan2(T(2) * x.real(), z),
T(0.25) * sprout::log((r2 + num * num) / (r2 + den * den))
);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
atan_impl(sprout::complex<T> const& x, T const& r2) {
2012-08-28 16:16:12 +00:00
return sprout::detail::atan_impl_1(
2012-10-05 15:58:56 +00:00
x, r2,
2012-08-28 16:16:12 +00:00
T(1) - r2 - x.imag() * x.imag(),
2012-10-05 15:58:56 +00:00
x.imag() + T(1), x.imag() - T(1)
2012-08-28 16:16:12 +00:00
);
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
atan(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::detail::atan_impl(x, x.real() * x.real());
}
2012-10-05 15:58:56 +00:00
//
// acosh
// asinh
// atanh
//
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
acosh(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return T(2) * sprout::log(sprout::sqrt(T(0.5) * (x + T(1))) + sprout::sqrt(T(0.5) * (x - T(1))));
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
asinh(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::log(
sprout::sqrt(
sprout::complex<T>(
(x.real() - x.imag()) * (x.real() + x.imag()) + T(1),
T(2) * x.real() * x.imag()
)
)
+ x
);
}
namespace detail {
template<typename T>
2012-10-05 15:58:56 +00:00
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) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
T(0.25) * (sprout::log(i2 + num * num) - sprout::log(i2 + den * den)),
T(0.5) * sprout::atan2(T(2) * x.imag(), z)
);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
atanh_impl(sprout::complex<T> const& x, T const& i2) {
2012-08-28 16:16:12 +00:00
return sprout::detail::atanh_impl_1(
2012-10-05 15:58:56 +00:00
x, i2,
2012-08-28 16:16:12 +00:00
T(1) - i2 - x.real() * x.real(),
2012-10-05 15:58:56 +00:00
T(1) + x.imag(), T(1) - x.imag()
2012-08-28 16:16:12 +00:00
);
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
atanh(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::detail::atanh_impl(x, x.imag() * x.imag());
}
2012-10-05 15:58:56 +00:00
//
// cos
// cosh
//
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
cos(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
sprout::cos(x.real()) * sprout::cosh(x.imag()),
-(sprout::sin(x.real()) * sprout::sinh(x.imag()))
);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
cosh(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
sprout::cosh(x.real()) * sprout::cos(x.imag()),
sprout::sinh(x.real()) * sprout::sin(x.imag())
);
}
2012-10-05 15:58:56 +00:00
//
// exp
// log
// log10
//
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T>
exp(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::polar(sprout::exp(x.real()), x.imag());
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
log(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(sprout::log(sprout::abs(x)), sprout::arg(x));
}
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T> log10(sprout::complex<T> const& x) {
return sprout::log(x) / sprout::log(T(10));
}
//
// pow
//
2012-08-28 16:16:12 +00:00
namespace detail {
template<typename T>
inline SPROUT_CONSTEXPR sprout::complex<T> pow_impl(sprout::complex<T> const& t, T const& y) {
return sprout::polar(sprout::exp(y * t.real()), y * t.imag());
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
pow(sprout::complex<T> const& x, T const& y) {
2012-08-28 16:16:12 +00:00
return x == T() ? T()
: x.imag() == T() && x.real() > T() ? sprout::pow(x.real(), y)
: sprout::detail::pow_impl(sprout::log(x), y)
;
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
pow(sprout::complex<T> const& x, sprout::complex<T> const& y) {
2012-08-28 16:16:12 +00:00
return x == T() ? T()
: sprout::exp(y * sprout::log(x))
;
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
pow(T const& x, sprout::complex<T> const& y) {
2012-08-28 16:16:12 +00:00
return x > T() ? sprout::polar(sprout::pow(x, y.real()), y.imag() * sprout::log(x))
: sprout::pow(sprout::complex<T>(x), y)
;
}
2012-10-05 15:58:56 +00:00
//
// sin
// sinh
//
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
sin(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
sprout::sin(x.real()) * sprout::cosh(x.imag()),
sprout::cos(x.real()) * sprout::sinh(x.imag())
);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
sinh(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(
sprout::sinh(x.real()) * sprout::cos(x.imag()),
sprout::cosh(x.real()) * sprout::sin(x.imag())
);
}
2012-10-05 15:58:56 +00:00
//
// sqrt
//
2012-08-28 16:16:12 +00:00
namespace detail {
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
sqrt_impl_1(sprout::complex<T> const& x, T const& t) {
2012-08-28 16:16:12 +00:00
return sprout::complex<T>(t, x.imag() < T() ? -t : t);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
sqrt_impl_2_1(sprout::complex<T> const& x, T const& t, T const& u) {
2012-08-28 16:16:12 +00:00
return x.real() > T() ? sprout::complex<T>(u, x.imag() / t)
: sprout::complex<T>(sprout::abs(x.imag()) / t, x.imag() < T() ? -u : u)
;
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
sqrt_impl_2(sprout::complex<T> const& x, T const& t) {
2012-08-28 16:16:12 +00:00
return sprout::detail::sqrt_impl_2_1(x, t, t / 2);
}
} // namespace detail
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
sqrt(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return x.real() == T() ? sprout::detail::sqrt_impl_1(x, sprout::sqrt(abs(x.imag()) / 2))
: sprout::detail::sqrt_impl_2(x, sprout::sqrt(2 * (sprout::abs(x) + sprout::abs(x.real()))))
;
}
//
// tan
// tanh
//
2012-08-28 16:16:12 +00:00
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
tan(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::sin(x) / sprout::cos(x);
}
template<typename T>
2012-10-05 15:58:56 +00:00
inline SPROUT_CONSTEXPR sprout::complex<T>
tanh(sprout::complex<T> const& x) {
2012-08-28 16:16:12 +00:00
return sprout::sinh(x) / sprout::cosh(x);
}
} // namespace sprout
#endif // #ifndef SPROUT_COMPLEX_TRANSCENDENTALS_HPP