UNPKG

5.49 kBJavaScriptView Raw
1'use strict'
2
3const deepMap = require('../../utils/collection/deepMap')
4const sign = require('../../utils/number').sign
5
6function factory (type, config, load, typed) {
7 /**
8 * Compute the erf function of a value using a rational Chebyshev
9 * approximations for different intervals of x.
10 *
11 * This is a translation of W. J. Cody's Fortran implementation from 1987
12 * ( https://www.netlib.org/specfun/erf ). See the AMS publication
13 * "Rational Chebyshev Approximations for the Error Function" by W. J. Cody
14 * for an explanation of this process.
15 *
16 * For matrices, the function is evaluated element wise.
17 *
18 * Syntax:
19 *
20 * math.erf(x)
21 *
22 * Examples:
23 *
24 * math.erf(0.2) // returns 0.22270258921047847
25 * math.erf(-0.5) // returns -0.5204998778130465
26 * math.erf(4) // returns 0.9999999845827421
27 *
28 * @param {number | Array | Matrix} x A real number
29 * @return {number | Array | Matrix} The erf of `x`
30 */
31 const erf = typed('erf', {
32 'number': function (x) {
33 const y = Math.abs(x)
34
35 if (y >= MAX_NUM) {
36 return sign(x)
37 }
38 if (y <= THRESH) {
39 return sign(x) * erf1(y)
40 }
41 if (y <= 4.0) {
42 return sign(x) * (1 - erfc2(y))
43 }
44 return sign(x) * (1 - erfc3(y))
45 },
46
47 // TODO: Not sure if there's a way to guarantee some degree of accuracy here.
48 // Perhaps it would be best to set the precision of the number to that which
49 // is guaranteed by erf()
50 'BigNumber': function (n) {
51 return new type.BigNumber(erf(n.toNumber()))
52 },
53
54 'Array | Matrix': function (n) {
55 return deepMap(n, erf)
56 }
57
58 // TODO: For complex numbers, use the approximation for the Faddeeva function
59 // from "More Efficient Computation of the Complex Error Function" (AMS)
60
61 })
62
63 /**
64 * Approximates the error function erf() for x <= 0.46875 using this function:
65 * n
66 * erf(x) = x * sum (p_j * x^(2j)) / (q_j * x^(2j))
67 * j=0
68 */
69 function erf1 (y) {
70 const ysq = y * y
71 let xnum = P[0][4] * ysq
72 let xden = ysq
73 let i
74
75 for (i = 0; i < 3; i += 1) {
76 xnum = (xnum + P[0][i]) * ysq
77 xden = (xden + Q[0][i]) * ysq
78 }
79 return y * (xnum + P[0][3]) / (xden + Q[0][3])
80 }
81
82 /**
83 * Approximates the complement of the error function erfc() for
84 * 0.46875 <= x <= 4.0 using this function:
85 * n
86 * erfc(x) = e^(-x^2) * sum (p_j * x^j) / (q_j * x^j)
87 * j=0
88 */
89 function erfc2 (y) {
90 let xnum = P[1][8] * y
91 let xden = y
92 let i
93
94 for (i = 0; i < 7; i += 1) {
95 xnum = (xnum + P[1][i]) * y
96 xden = (xden + Q[1][i]) * y
97 }
98 const result = (xnum + P[1][7]) / (xden + Q[1][7])
99 const ysq = parseInt(y * 16) / 16
100 const del = (y - ysq) * (y + ysq)
101 return Math.exp(-ysq * ysq) * Math.exp(-del) * result
102 }
103
104 /**
105 * Approximates the complement of the error function erfc() for x > 4.0 using
106 * this function:
107 *
108 * erfc(x) = (e^(-x^2) / x) * [ 1/sqrt(pi) +
109 * n
110 * 1/(x^2) * sum (p_j * x^(-2j)) / (q_j * x^(-2j)) ]
111 * j=0
112 */
113 function erfc3 (y) {
114 let ysq = 1 / (y * y)
115 let xnum = P[2][5] * ysq
116 let xden = ysq
117 let i
118
119 for (i = 0; i < 4; i += 1) {
120 xnum = (xnum + P[2][i]) * ysq
121 xden = (xden + Q[2][i]) * ysq
122 }
123 let result = ysq * (xnum + P[2][4]) / (xden + Q[2][4])
124 result = (SQRPI - result) / y
125 ysq = parseInt(y * 16) / 16
126 const del = (y - ysq) * (y + ysq)
127 return Math.exp(-ysq * ysq) * Math.exp(-del) * result
128 }
129
130 erf.toTex = { 1: `erf\\left(\${args[0]}\\right)` }
131
132 return erf
133}
134
135/**
136 * Upper bound for the first approximation interval, 0 <= x <= THRESH
137 * @constant
138 */
139const THRESH = 0.46875
140
141/**
142 * Constant used by W. J. Cody's Fortran77 implementation to denote sqrt(pi)
143 * @constant
144 */
145const SQRPI = 5.6418958354775628695e-1
146
147/**
148 * Coefficients for each term of the numerator sum (p_j) for each approximation
149 * interval (see W. J. Cody's paper for more details)
150 * @constant
151 */
152const P = [[
153 3.16112374387056560e00, 1.13864154151050156e02,
154 3.77485237685302021e02, 3.20937758913846947e03,
155 1.85777706184603153e-1
156], [
157 5.64188496988670089e-1, 8.88314979438837594e00,
158 6.61191906371416295e01, 2.98635138197400131e02,
159 8.81952221241769090e02, 1.71204761263407058e03,
160 2.05107837782607147e03, 1.23033935479799725e03,
161 2.15311535474403846e-8
162], [
163 3.05326634961232344e-1, 3.60344899949804439e-1,
164 1.25781726111229246e-1, 1.60837851487422766e-2,
165 6.58749161529837803e-4, 1.63153871373020978e-2
166]]
167
168/**
169 * Coefficients for each term of the denominator sum (q_j) for each approximation
170 * interval (see W. J. Cody's paper for more details)
171 * @constant
172 */
173const Q = [[
174 2.36012909523441209e01, 2.44024637934444173e02,
175 1.28261652607737228e03, 2.84423683343917062e03
176], [
177 1.57449261107098347e01, 1.17693950891312499e02,
178 5.37181101862009858e02, 1.62138957456669019e03,
179 3.29079923573345963e03, 4.36261909014324716e03,
180 3.43936767414372164e03, 1.23033935480374942e03
181], [
182 2.56852019228982242e00, 1.87295284992346047e00,
183 5.27905102951428412e-1, 6.05183413124413191e-2,
184 2.33520497626869185e-3
185]]
186
187/**
188 * Maximum/minimum safe numbers to input to erf() (in ES6+, this number is
189 * Number.[MAX|MIN]_SAFE_INTEGER). erf() for all numbers beyond this limit will
190 * return 1
191 */
192const MAX_NUM = Math.pow(2, 53)
193
194exports.name = 'erf'
195exports.factory = factory