| 1 | // boost asinh.hpp header file |
| 2 | |
| 3 | // (C) Copyright Eric Ford 2001 & Hubert Holin. |
| 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_ACOSH_HPP |
| 12 | #define BOOST_ACOSH_HPP |
| 13 | |
| 14 | #ifdef _MSC_VER |
| 15 | #pragma once |
| 16 | #endif |
| 17 | |
| 18 | #include <cmath> |
| 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/constants/constants.hpp> |
| 24 | #include <boost/math/special_functions/fpclassify.hpp> |
| 25 | |
| 26 | // This is the inverse of the hyperbolic cosine function. |
| 27 | |
| 28 | namespace boost |
| 29 | { |
| 30 | namespace math |
| 31 | { |
| 32 | namespace detail |
| 33 | { |
| 34 | template<typename T, typename Policy> |
| 35 | inline T acosh_imp(const T x, const Policy& pol) |
| 36 | { |
| 37 | BOOST_MATH_STD_USING |
| 38 | |
| 39 | if((x < 1) || (boost::math::isnan)(x)) |
| 40 | { |
| 41 | return policies::raise_domain_error<T>("boost::math::acosh<%1%>(%1%)" , "acosh requires x >= 1, but got x = %1%." , x, pol); |
| 42 | } |
| 43 | else if ((x - 1) >= tools::root_epsilon<T>()) |
| 44 | { |
| 45 | if (x > 1 / tools::root_epsilon<T>()) |
| 46 | { |
| 47 | // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/06/01/0001/ |
| 48 | // approximation by laurent series in 1/x at 0+ order from -1 to 0 |
| 49 | return log(x) + constants::ln_two<T>(); |
| 50 | } |
| 51 | else if(x < 1.5f) |
| 52 | { |
| 53 | // This is just a rearrangement of the standard form below |
| 54 | // devised to minimise loss of precision when x ~ 1: |
| 55 | T y = x - 1; |
| 56 | return boost::math::log1p(y + sqrt(y * y + 2 * y), pol); |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | // http://functions.wolfram.com/ElementaryFunctions/ArcCosh/02/ |
| 61 | return( log( x + sqrt(x * x - 1) ) ); |
| 62 | } |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | // see http://functions.wolfram.com/ElementaryFunctions/ArcCosh/06/01/04/01/0001/ |
| 67 | T y = x - 1; |
| 68 | |
| 69 | // approximation by taylor series in y at 0 up to order 2 |
| 70 | T result = sqrt(2 * y) * (1 - y /12 + 3 * y * y / 160); |
| 71 | return result; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | template<typename T, typename Policy> |
| 77 | inline typename tools::promote_args<T>::type acosh(T x, const Policy&) |
| 78 | { |
| 79 | typedef typename tools::promote_args<T>::type result_type; |
| 80 | typedef typename policies::evaluation<result_type, Policy>::type value_type; |
| 81 | typedef typename policies::normalise< |
| 82 | Policy, |
| 83 | policies::promote_float<false>, |
| 84 | policies::promote_double<false>, |
| 85 | policies::discrete_quantile<>, |
| 86 | policies::assert_undefined<> >::type forwarding_policy; |
| 87 | return policies::checked_narrowing_cast<result_type, forwarding_policy>( |
| 88 | detail::acosh_imp(static_cast<value_type>(x), forwarding_policy()), |
| 89 | "boost::math::acosh<%1%>(%1%)" ); |
| 90 | } |
| 91 | template<typename T> |
| 92 | inline typename tools::promote_args<T>::type acosh(T x) |
| 93 | { |
| 94 | return boost::math::acosh(x, policies::policy<>()); |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | #endif /* BOOST_ACOSH_HPP */ |
| 101 | |
| 102 | |
| 103 | |