| 1 |
// boost atanh.hpp header file |
| 2 |
|
| 3 |
// (C) Copyright Hubert Holin 2001. |
| 4 |
// (C) Copyright John Maddock 2008. |
| 5 |
// Distributed under the Boost Software License, Version 1.0. (See |
| 6 |
// accompanying file LICENSE_1_0.txt or copy at |
| 7 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 8 |
|
| 9 |
// See http://www.boost.org for updates, documentation, and revision history. |
| 10 |
|
| 11 |
#ifndef BOOST_ATANH_HPP |
| 12 |
#define BOOST_ATANH_HPP |
| 13 |
|
| 14 |
#ifdef _MSC_VER |
| 15 |
#pragma once |
| 16 |
#endif |
| 17 |
|
| 18 |
#include <boost/math/tools/config.hpp> |
| 19 |
#include <boost/math/tools/precision.hpp> |
| 20 |
#include <boost/math/policies/error_handling.hpp> |
| 21 |
#include <boost/math/special_functions/math_fwd.hpp> |
| 22 |
#include <boost/math/special_functions/log1p.hpp> |
| 23 |
#include <boost/math/special_functions/fpclassify.hpp> |
| 24 |
|
| 25 |
// This is the inverse of the hyperbolic tangent function. |
| 26 |
|
| 27 |
namespace boost |
| 28 |
{ |
| 29 |
namespace math |
| 30 |
{ |
| 31 |
namespace detail |
| 32 |
{ |
| 33 |
// This is the main fare |
| 34 |
|
| 35 |
template<typename T, typename Policy> |
| 36 |
BOOST_MATH_GPU_ENABLED inline T atanh_imp(const T x, const Policy& pol) |
| 37 |
{ |
| 38 |
BOOST_MATH_STD_USING |
| 39 |
constexpr auto function = "boost::math::atanh<%1%>(%1%)"; |
| 40 |
|
| 41 |
if(x < -1) |
| 42 |
{ |
| 43 |
return policies::raise_domain_error<T>(function, "atanh requires x >= -1, but got x = %1%.", x, pol); |
| 44 |
} |
| 45 |
else if(x > 1) |
| 46 |
{ |
| 47 |
return policies::raise_domain_error<T>(function, "atanh requires x <= 1, but got x = %1%.", x, pol); |
| 48 |
} |
| 49 |
else if((boost::math::isnan)(x)) |
| 50 |
{ |
| 51 |
return policies::raise_domain_error<T>(function, "atanh requires -1 <= x <= 1, but got x = %1%.", x, pol); |
| 52 |
} |
| 53 |
else if(x < -1 + tools::epsilon<T>()) |
| 54 |
{ |
| 55 |
// -Infinity: |
| 56 |
return -policies::raise_overflow_error<T>(function, nullptr, pol); |
| 57 |
} |
| 58 |
else if(x > 1 - tools::epsilon<T>()) |
| 59 |
{ |
| 60 |
// Infinity: |
| 61 |
return policies::raise_overflow_error<T>(function, nullptr, pol); |
| 62 |
} |
| 63 |
else if(abs(x) >= tools::forth_root_epsilon<T>()) |
| 64 |
{ |
| 65 |
// http://functions.wolfram.com/ElementaryFunctions/ArcTanh/02/ |
| 66 |
if(abs(x) < 0.5f) |
| 67 |
return (boost::math::log1p(x, pol) - boost::math::log1p(-x, pol)) / 2; |
| 68 |
return(log( (1 + x) / (1 - x) ) / 2); |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
// http://functions.wolfram.com/ElementaryFunctions/ArcTanh/06/01/03/01/ |
| 73 |
// approximation by taylor series in x at 0 up to order 2 |
| 74 |
T result = x; |
| 75 |
|
| 76 |
if (abs(x) >= tools::root_epsilon<T>()) |
| 77 |
{ |
| 78 |
T x3 = x*x*x; |
| 79 |
|
| 80 |
// approximation by taylor series in x at 0 up to order 4 |
| 81 |
result += x3/static_cast<T>(3); |
| 82 |
} |
| 83 |
|
| 84 |
return(result); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
template<typename T, typename Policy> |
| 90 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type atanh(T x, const Policy&) |
| 91 |
{ |
| 92 |
typedef typename tools::promote_args<T>::type result_type; |
| 93 |
typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 94 |
typedef typename policies::normalise< |
| 95 |
Policy, |
| 96 |
policies::promote_float<false>, |
| 97 |
policies::promote_double<false>, |
| 98 |
policies::discrete_quantile<>, |
| 99 |
policies::assert_undefined<> >::type forwarding_policy; |
| 100 |
return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 101 |
detail::atanh_imp(static_cast<value_type>(x), forwarding_policy()), |
| 102 |
"boost::math::atanh<%1%>(%1%)"); |
| 103 |
} |
| 104 |
template<typename T> |
| 105 |
BOOST_MATH_GPU_ENABLED inline typename tools::promote_args<T>::type atanh(T x) |
| 106 |
{ |
| 107 |
return boost::math::atanh(x, policies::policy<>()); |
| 108 |
} |
| 109 |
|
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
#endif /* BOOST_ATANH_HPP */ |
| 114 |
|
| 115 |
|
| 116 |
|