Path: blob/main/contrib/llvm-project/libcxx/include/__math/special_functions.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MATH_SPECIAL_FUNCTIONS_H10#define _LIBCPP___MATH_SPECIAL_FUNCTIONS_H1112#include <__config>13#include <__math/copysign.h>14#include <__math/traits.h>15#include <__type_traits/enable_if.h>16#include <__type_traits/is_integral.h>17#include <limits>1819#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif2223_LIBCPP_BEGIN_NAMESPACE_STD2425#if _LIBCPP_STD_VER >= 172627template <class _Real>28_LIBCPP_HIDE_FROM_ABI _Real __hermite(unsigned __n, _Real __x) {29// The Hermite polynomial H_n(x).30// The implementation is based on the recurrence formula: H_{n+1}(x) = 2x H_n(x) - 2n H_{n-1}.31// Press, William H., et al. Numerical recipes 3rd edition: The art of scientific computing.32// Cambridge university press, 2007, p. 183.3334// NOLINTBEGIN(readability-identifier-naming)35if (__math::isnan(__x))36return __x;3738_Real __H_0{1};39if (__n == 0)40return __H_0;4142_Real __H_n_prev = __H_0;43_Real __H_n = 2 * __x;44for (unsigned __i = 1; __i < __n; ++__i) {45_Real __H_n_next = 2 * (__x * __H_n - __i * __H_n_prev);46__H_n_prev = __H_n;47__H_n = __H_n_next;48}4950if (!__math::isfinite(__H_n)) {51// Overflow occured. Two possible cases:52// n is odd: return infinity of the same sign as x.53// n is even: return +Inf54_Real __inf = std::numeric_limits<_Real>::infinity();55return (__n & 1) ? __math::copysign(__inf, __x) : __inf;56}57return __H_n;58// NOLINTEND(readability-identifier-naming)59}6061inline _LIBCPP_HIDE_FROM_ABI double hermite(unsigned __n, double __x) { return std::__hermite(__n, __x); }6263inline _LIBCPP_HIDE_FROM_ABI float hermite(unsigned __n, float __x) {64// use double internally -- float is too prone to overflow!65return static_cast<float>(std::hermite(__n, static_cast<double>(__x)));66}6768inline _LIBCPP_HIDE_FROM_ABI long double hermite(unsigned __n, long double __x) { return std::__hermite(__n, __x); }6970inline _LIBCPP_HIDE_FROM_ABI float hermitef(unsigned __n, float __x) { return std::hermite(__n, __x); }7172inline _LIBCPP_HIDE_FROM_ABI long double hermitel(unsigned __n, long double __x) { return std::hermite(__n, __x); }7374template <class _Integer, std::enable_if_t<std::is_integral_v<_Integer>, int> = 0>75_LIBCPP_HIDE_FROM_ABI double hermite(unsigned __n, _Integer __x) {76return std::hermite(__n, static_cast<double>(__x));77}7879#endif // _LIBCPP_STD_VER >= 178081_LIBCPP_END_NAMESPACE_STD8283#endif // _LIBCPP___MATH_SPECIAL_FUNCTIONS_H848586