MCKL
Monte Carlo Kernel Library
erf.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/math/erf.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_MATH_ERF_HPP
33 #define MCKL_MATH_ERF_HPP
34 
35 #include <mckl/internal/config.h>
36 #include <mckl/math/constants.hpp>
37 
38 #include <cmath>
39 
40 namespace mckl {
41 
44 template <typename T>
45 inline T erfcinv(T y)
46 {
47  if (y >= 2) {
48  return -const_inf<T>();
49  }
50  if (y <= 0) {
51  return const_inf<T>();
52  }
53 
54  constexpr T a = static_cast<T>(0.70771);
55  constexpr T b = static_cast<T>(2.30753);
56  constexpr T c = static_cast<T>(0.27061);
57  constexpr T d = static_cast<T>(0.99229);
58  constexpr T e = static_cast<T>(0.04481);
59  constexpr T f = static_cast<T>(1.12837916709551257);
60 
61  T z = y < 1 ? y : 2 - y;
62  T t = std::sqrt(-2 * std::log(static_cast<T>(0.5) * z));
63  T x = -a * ((b + t * c) / (1 + t * (d + t * e)) - t);
64  for (int i = 0; i != 2; ++i) {
65  T err = std::erfc(x) - z;
66  x += err / (f * std::exp(-(x * x)) - x * err);
67  }
68 
69  return y < 1 ? x : -x;
70 }
71 
74 template <typename T>
75 inline T erfinv(T y)
76 {
77  return erfcinv<T>(1 - y);
78 }
79 
80 } // namespace mckl
81 
82 #endif // MCKL_MATH_ERF_HPP
T erfcinv(T y)
Inverse complement error function.
Definition: erf.hpp:45
void log(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:226
void sqrt(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:177
Definition: mcmc.hpp:40
void exp(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:221
T erfinv(T y)
Inverse error function.
Definition: erf.hpp:75
void erfc(std::size_t n, const float *a, float *y)
Definition: vmf.hpp:298