1 | 'use strict'
|
2 |
|
3 | const deepMap = require('../../utils/collection/deepMap')
|
4 | const sign = require('../../utils/number').sign
|
5 |
|
6 | function factory (type, config, load, typed) {
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
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 |
|
48 |
|
49 |
|
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 |
|
59 |
|
60 |
|
61 | })
|
62 |
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
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 |
|
84 |
|
85 |
|
86 |
|
87 |
|
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 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
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 |
|
137 |
|
138 |
|
139 | const THRESH = 0.46875
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | const SQRPI = 5.6418958354775628695e-1
|
146 |
|
147 |
|
148 |
|
149 |
|
150 |
|
151 |
|
152 | const 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 |
|
170 |
|
171 |
|
172 |
|
173 | const 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 |
|
189 |
|
190 |
|
191 |
|
192 | const MAX_NUM = Math.pow(2, 53)
|
193 |
|
194 | exports.name = 'erf'
|
195 | exports.factory = factory
|