1 | import { deepMap } from '../../utils/collection'
|
2 | import { sign } from '../../utils/number'
|
3 | import { factory } from '../../utils/factory'
|
4 |
|
5 | const name = 'erf'
|
6 | const dependencies = [
|
7 | 'typed'
|
8 | ]
|
9 |
|
10 | export const createErf = factory(name, dependencies, ({ typed }) => {
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | return typed('name', {
|
36 | number: function (x) {
|
37 | const y = Math.abs(x)
|
38 |
|
39 | if (y >= MAX_NUM) {
|
40 | return sign(x)
|
41 | }
|
42 | if (y <= THRESH) {
|
43 | return sign(x) * erf1(y)
|
44 | }
|
45 | if (y <= 4.0) {
|
46 | return sign(x) * (1 - erfc2(y))
|
47 | }
|
48 | return sign(x) * (1 - erfc3(y))
|
49 | },
|
50 |
|
51 | 'Array | Matrix': function (n) {
|
52 | return deepMap(n, this)
|
53 | }
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | })
|
59 |
|
60 | |
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | function erf1 (y) {
|
67 | const ysq = y * y
|
68 | let xnum = P[0][4] * ysq
|
69 | let xden = ysq
|
70 | let i
|
71 |
|
72 | for (i = 0; i < 3; i += 1) {
|
73 | xnum = (xnum + P[0][i]) * ysq
|
74 | xden = (xden + Q[0][i]) * ysq
|
75 | }
|
76 | return y * (xnum + P[0][3]) / (xden + Q[0][3])
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 | function erfc2 (y) {
|
87 | let xnum = P[1][8] * y
|
88 | let xden = y
|
89 | let i
|
90 |
|
91 | for (i = 0; i < 7; i += 1) {
|
92 | xnum = (xnum + P[1][i]) * y
|
93 | xden = (xden + Q[1][i]) * y
|
94 | }
|
95 | const result = (xnum + P[1][7]) / (xden + Q[1][7])
|
96 | const ysq = parseInt(y * 16) / 16
|
97 | const del = (y - ysq) * (y + ysq)
|
98 | return Math.exp(-ysq * ysq) * Math.exp(-del) * result
|
99 | }
|
100 |
|
101 | |
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function erfc3 (y) {
|
111 | let ysq = 1 / (y * y)
|
112 | let xnum = P[2][5] * ysq
|
113 | let xden = ysq
|
114 | let i
|
115 |
|
116 | for (i = 0; i < 4; i += 1) {
|
117 | xnum = (xnum + P[2][i]) * ysq
|
118 | xden = (xden + Q[2][i]) * ysq
|
119 | }
|
120 | let result = ysq * (xnum + P[2][4]) / (xden + Q[2][4])
|
121 | result = (SQRPI - result) / y
|
122 | ysq = parseInt(y * 16) / 16
|
123 | const del = (y - ysq) * (y + ysq)
|
124 | return Math.exp(-ysq * ysq) * Math.exp(-del) * result
|
125 | }
|
126 | })
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 | const THRESH = 0.46875
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 | const SQRPI = 5.6418958354775628695e-1
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | const P = [[
|
146 | 3.16112374387056560e00, 1.13864154151050156e02,
|
147 | 3.77485237685302021e02, 3.20937758913846947e03,
|
148 | 1.85777706184603153e-1
|
149 | ], [
|
150 | 5.64188496988670089e-1, 8.88314979438837594e00,
|
151 | 6.61191906371416295e01, 2.98635138197400131e02,
|
152 | 8.81952221241769090e02, 1.71204761263407058e03,
|
153 | 2.05107837782607147e03, 1.23033935479799725e03,
|
154 | 2.15311535474403846e-8
|
155 | ], [
|
156 | 3.05326634961232344e-1, 3.60344899949804439e-1,
|
157 | 1.25781726111229246e-1, 1.60837851487422766e-2,
|
158 | 6.58749161529837803e-4, 1.63153871373020978e-2
|
159 | ]]
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 | const Q = [[
|
167 | 2.36012909523441209e01, 2.44024637934444173e02,
|
168 | 1.28261652607737228e03, 2.84423683343917062e03
|
169 | ], [
|
170 | 1.57449261107098347e01, 1.17693950891312499e02,
|
171 | 5.37181101862009858e02, 1.62138957456669019e03,
|
172 | 3.29079923573345963e03, 4.36261909014324716e03,
|
173 | 3.43936767414372164e03, 1.23033935480374942e03
|
174 | ], [
|
175 | 2.56852019228982242e00, 1.87295284992346047e00,
|
176 | 5.27905102951428412e-1, 6.05183413124413191e-2,
|
177 | 2.33520497626869185e-3
|
178 | ]]
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | const MAX_NUM = Math.pow(2, 53)
|