Skip to content

Commit 57d75ef

Browse files
committed
Implement BigMath.erf(x, prec) and BigMath.erfc(x, prec)
Uses asymptotic expansion of erfc if possible and fallback to taylor series of erf
1 parent d93b542 commit 57d75ef

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

lib/bigdecimal/math.rb

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
# cos (x, prec)
1010
# tan (x, prec)
1111
# atan(x, prec)
12+
# erf (x, prec)
13+
# erfc(x, prec)
1214
# PI (prec)
1315
# E (prec) == exp(1.0,prec)
1416
#
@@ -246,4 +248,128 @@ def E(prec)
246248
BigDecimal::Internal.validate_prec(prec, :E)
247249
BigMath.exp(1, prec)
248250
end
251+
252+
# call-seq:
253+
# erf(decimal, numeric) -> BigDecimal
254+
#
255+
# Computes the error function of +decimal+ to the specified number of digits of
256+
# precision, +numeric+.
257+
#
258+
# If +decimal+ is NaN, returns NaN.
259+
#
260+
# BigMath.erf(BigDecimal('1'), 32).to_s
261+
# #=> "0.84270079294971486934122063508261e0"
262+
#
263+
def erf(x, prec)
264+
BigDecimal::Internal.validate_prec(prec, :erf)
265+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :erf)
266+
return BigDecimal::Internal.nan_computation_result if x.nan?
267+
return BigDecimal(x.infinite?) if x.infinite?
268+
return BigDecimal(0) if x == 0
269+
return -erf(-x, prec) if x < 0
270+
271+
if x > 8 && (erfc1 = _erfc_asymptotic(x, 1))
272+
erfc2 = _erfc_asymptotic(x, [prec + erfc1.exponent, 1].max)
273+
return BigDecimal(1).sub(erfc2, prec) if erfc2
274+
end
275+
276+
prec2 = prec + BigDecimal.double_fig
277+
x_smallprec = x.mult(1, Integer.sqrt(prec2) / 2)
278+
# Taylor series of x with small precision is fast
279+
erf1 = _erf_taylor(x_smallprec, BigDecimal(0), BigDecimal(0), prec2)
280+
# Taylor series converges quickly for small x
281+
_erf_taylor(x - x_smallprec, x_smallprec, erf1, prec2).mult(1, prec)
282+
end
283+
284+
# call-seq:
285+
# erfc(decimal, numeric) -> BigDecimal
286+
#
287+
# Computes the complementary error function of +decimal+ to the specified number of digits of
288+
# precision, +numeric+.
289+
#
290+
# If +decimal+ is NaN, returns NaN.
291+
#
292+
# BigMath.erfc(BigDecimal('10'), 32).to_s
293+
# #=> "0.20884875837625447570007862949578e-44"
294+
#
295+
def erfc(x, prec)
296+
BigDecimal::Internal.validate_prec(prec, :erfc)
297+
x = BigDecimal::Internal.coerce_to_bigdecimal(x, prec, :erfc)
298+
return BigDecimal::Internal.nan_computation_result if x.nan?
299+
return BigDecimal(1 - x.infinite?) if x.infinite?
300+
return BigDecimal(1).sub(erf(x, prec), prec) if x < 0
301+
302+
if x >= 8
303+
y = _erfc_asymptotic(x, prec)
304+
return y.mult(1, prec) if y
305+
end
306+
307+
# erfc(x) = 1 - erf(x) < exp(-x**2)/x/sqrt(pi)
308+
# Precision of erf(x) needs about log10(exp(-x**2)) extra digits
309+
log10 = 2.302585092994046
310+
high_prec = prec + BigDecimal.double_fig + (x**2 / log10).ceil
311+
BigDecimal(1).sub(erf(x, high_prec), prec)
312+
end
313+
314+
315+
private def _erf_taylor(x, a, erf_a, prec)
316+
# Let f(x) = erf(x+a)*exp((x+a)**2)*sqrt(pi)/2
317+
# = c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + ...
318+
# f'(x) = 1+2*(x+a)*f(x)
319+
# f'(x) = c1 + 2*c2*x + 3*c3*x**2 + 4*c4*x**3 + 5*c5*x**4 + ...
320+
# = 1+2*(x+a)*(c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + ...)
321+
# therefore,
322+
# c0 = f(0)
323+
# c1 = 2 * a * c0 + 1
324+
# c2 = (2 * c0 + 2 * a * c1) / 2
325+
# c3 = (2 * c1 + 2 * a * c2) / 3
326+
# c4 = (2 * c2 + 2 * a * c3) / 4
327+
328+
return erf_a if x.zero?
329+
330+
scale = BigDecimal(2).div(sqrt(PI(prec), prec), prec)
331+
c_prev = erf_a.div(scale.mult(BigMath.exp(-a*a, prec), prec), prec)
332+
c_next = (2 * a * c_prev).add(1, prec).mult(x, prec)
333+
v = c_prev.add(c_next, prec)
334+
335+
2.step do |k|
336+
c = (c_prev.mult(x, prec) + a * c_next).mult(2, prec).mult(x, prec).div(k, prec)
337+
v = v.add(c, prec)
338+
c_prev, c_next = c_next, c
339+
break if [c_prev, c_next].all? { |c| c.zero? || (c.exponent < v.exponent - prec) }
340+
end
341+
v = v.mult(scale.mult(BigMath.exp(-(x + a).mult(x + a, prec), prec), prec), prec)
342+
v > 1 ? BigDecimal(1) : v
343+
end
344+
345+
private def _erfc_asymptotic(x, prec)
346+
# Let f(x) = erfc(x)*sqrt(pi)*exp(x**2)/2
347+
# f(x) satisfies the following differential equation:
348+
# 2*x*f(x) = f'(x) + 1
349+
# From the above equation, we can derive the following asymptotic expansion:
350+
# f(x) = (0..kmax).sum { (-1)**k * (2*k)! / 4**k / k! / x**(2*k)) } / x
351+
352+
# This asymptotic expansion does not converge.
353+
# But if there is a k that satisfies (2*k)! / 4**k / k! / x**(2*k) < 10**(-prec),
354+
# It is enough to calculate erfc within the given precision.
355+
# (2*k)! / 4**k / k! can be approximated as sqrt(2) * (k/e)**k by using Stirling's approximation.
356+
prec += BigDecimal.double_fig
357+
xf = x.to_f
358+
log10xf = Math.log10(xf)
359+
kmax = 1
360+
until kmax * Math.log10(kmax / Math::E) + 1 - 2 * kmax * log10xf < -prec
361+
kmax += 1
362+
return if xf * xf < kmax # Unable to calculate with the given precision
363+
end
364+
365+
sum = BigDecimal(1)
366+
x2 = x.mult(x, prec)
367+
d = BigDecimal(1)
368+
(1..kmax).each do |k|
369+
d = d.div(x2, prec).mult(1 - 2 * k, prec).div(2, prec)
370+
sum = sum.add(d, prec)
371+
end
372+
expx2 = BigMath.exp(x.mult(x, prec), prec)
373+
sum.div(expx2.mult(PI(prec).sqrt(prec), prec), prec).div(x, prec)
374+
end
249375
end

test/bigdecimal/test_bigmath.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,48 @@ def test_log
161161
end
162162
SRC
163163
end
164+
165+
def test_erf
166+
[-0.5, 0.1, 0.3, 2.1, 3.3].each do |x|
167+
assert_in_epsilon(Math.erf(x), BigMath.erf(BigDecimal(x.to_s), N))
168+
end
169+
assert_equal(1, BigMath.erf(PINF, N))
170+
assert_equal(-1, BigMath.erf(MINF, N))
171+
assert_equal(1, BigMath.erf(BigDecimal(1000), 100))
172+
assert_equal(-1, BigMath.erf(BigDecimal(-1000), 100))
173+
assert_not_equal(1, BigMath.erf(BigDecimal(10), 45))
174+
assert_not_equal(1, BigMath.erf(BigDecimal(15), 100))
175+
assert_equal(
176+
BigDecimal("0.9953222650189527341620692563672529286108917970400600767383523262004372807199951773676290080196806805"),
177+
BigMath.erf(BigDecimal("2"), 100)
178+
)
179+
assert_converge_in_precision {|n| BigMath.erf(BigDecimal("1e-30"), n) }
180+
assert_converge_in_precision {|n| BigMath.erf(BigDecimal("0.3"), n) }
181+
assert_converge_in_precision {|n| BigMath.erf(SQRT2, n) }
182+
end
183+
184+
def test_erfc
185+
[-0.5, 0.1, 0.3, 2.1, 3.3].each do |x|
186+
assert_in_epsilon(Math.erfc(x), BigMath.erfc(BigDecimal(x.to_s), N))
187+
end
188+
assert_equal(0, BigMath.erfc(PINF, N))
189+
assert_equal(2, BigMath.erfc(MINF, N))
190+
191+
# erfc with taylor series
192+
assert_equal(
193+
BigDecimal("2.088487583762544757000786294957788611560818119321163727012213713938174695833440290610766384285723554e-45"),
194+
BigMath.erfc(BigDecimal("10"), 100)
195+
)
196+
assert_converge_in_precision {|n| BigMath.erfc(BigDecimal("0.3"), n) }
197+
assert_converge_in_precision {|n| BigMath.erfc(SQRT2, n) }
198+
assert_converge_in_precision {|n| BigMath.erfc(BigDecimal("8"), n) }
199+
# erfc with asymptotic expansion
200+
assert_equal(
201+
BigDecimal("1.896961059966276509268278259713415434936907563929186183462834752900411805205111886605256690776760041e-697"),
202+
BigMath.erfc(BigDecimal("40"), 100)
203+
)
204+
assert_converge_in_precision {|n| BigMath.erfc(BigDecimal("30"), n) }
205+
assert_converge_in_precision {|n| BigMath.erfc(30 * SQRT2, n) }
206+
assert_converge_in_precision {|n| BigMath.erfc(BigDecimal("50"), n) }
207+
end
164208
end

0 commit comments

Comments
 (0)