WPILibC++ 2023.4.3-108-ge5452e3
erf.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2022 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21/*
22 * compile-time error function
23 */
24
25#ifndef _gcem_erf_HPP
26#define _gcem_erf_HPP
27
28namespace internal
29{
30
31// see
32// http://functions.wolfram.com/GammaBetaErf/Erf/10/01/0007/
33
34template<typename T>
35constexpr
36T
37erf_cf_large_recur(const T x, const int depth)
38noexcept
39{
40 return( depth < GCEM_ERF_MAX_ITER ? \
41 // if
42 x + 2*depth/erf_cf_large_recur(x,depth+1) :
43 // else
44 x );
45}
46
47template<typename T>
48constexpr
49T
51noexcept
52{
53 return( T(1) - T(2) * ( exp(-x*x) / T(GCEM_SQRT_PI) ) \
54 / erf_cf_large_recur(T(2)*x,1) );
55}
56
57// see
58// http://functions.wolfram.com/GammaBetaErf/Erf/10/01/0005/
59
60template<typename T>
61constexpr
62T
63erf_cf_small_recur(const T xx, const int depth)
64noexcept
65{
66 return( depth < GCEM_ERF_MAX_ITER ? \
67 // if
68 (2*depth - T(1)) - 2*xx \
69 + 4*depth*xx / erf_cf_small_recur(xx,depth+1) :
70 // else
71 (2*depth - T(1)) - 2*xx );
72}
73
74template<typename T>
75constexpr
76T
78noexcept
79{
80 return( T(2) * x * ( exp(-x*x) / T(GCEM_SQRT_PI) ) \
81 / erf_cf_small_recur(x*x,1) );
82}
83
84//
85
86template<typename T>
87constexpr
88T
89erf_begin(const T x)
90noexcept
91{
92 return( x > T(2.1) ? \
93 // if
95 // else
97}
98
99template<typename T>
100constexpr
101T
102erf_check(const T x)
103noexcept
104{
105 return( // NaN check
106 is_nan(x) ? \
107 GCLIM<T>::quiet_NaN() :
108 // +/-Inf
109 is_posinf(x) ? \
110 T(1) :
111 is_neginf(x) ? \
112 - T(1) :
113 // indistinguishable from zero
114 GCLIM<T>::min() > abs(x) ? \
115 T(0) :
116 // else
117 x < T(0) ? \
118 - erf_begin(-x) :
119 erf_begin( x) );
120}
121
122}
123
124/**
125 * Compile-time Gaussian error function
126 *
127 * @param x a real-valued input.
128 * @return computes the Gaussian error function
129 * \f[ \text{erf}(x) = \frac{2}{\sqrt{\pi}} \int_0^x \exp( - t^2) dt \f]
130 * using a continued fraction representation:
131 * \f[ \text{erf}(x) = \frac{2x}{\sqrt{\pi}} \exp(-x^2) \dfrac{1}{1 - 2x^2 + \dfrac{4x^2}{3 - 2x^2 + \dfrac{8x^2}{5 - 2x^2 + \dfrac{12x^2}{7 - 2x^2 + \ddots}}}} \f]
132 */
133
134template<typename T>
135constexpr
136return_t<T>
137erf(const T x)
138noexcept
139{
140 return internal::erf_check( static_cast<return_t<T>>(x) );
141}
142
143#endif
constexpr return_t< T > erf(const T x) noexcept
Compile-time Gaussian error function.
Definition: erf.hpp:137
#define GCEM_SQRT_PI
Definition: gcem_options.hpp:122
#define GCEM_ERF_MAX_ITER
Definition: gcem_options.hpp:137
UnitType abs(const UnitType x) noexcept
Compute absolute value.
Definition: math.h:721
constexpr common_t< T1, T2 > min(const T1 x, const T2 y) noexcept
Compile-time pairwise minimum function.
Definition: min.hpp:35
Definition: Eigen_Colamd.h:50
constexpr bool is_posinf(const T x) noexcept
Definition: is_inf.hpp:81
constexpr bool is_neginf(const T x) noexcept
Definition: is_inf.hpp:34
constexpr T erf_begin(const T x) noexcept
Definition: erf.hpp:89
constexpr T erf_cf_large_recur(const T x, const int depth) noexcept
Definition: erf.hpp:37
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:36
constexpr T erf_cf_large_main(const T x) noexcept
Definition: erf.hpp:50
constexpr T erf_check(const T x) noexcept
Definition: erf.hpp:102
constexpr T erf_cf_small_main(const T x) noexcept
Definition: erf.hpp:77
constexpr T erf_cf_small_recur(const T xx, const int depth) noexcept
Definition: erf.hpp:63