Skip to content

Commit 72f349c

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 07696bc commit 72f349c

File tree

2 files changed

+163
-0
lines changed

2 files changed

+163
-0
lines changed

lib/bigdecimal/math.rb

Lines changed: 124 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,126 @@ 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+
raise ArgumentError, "Zero or negative precision for erf" if prec <= 0
265+
return BigDecimal("NaN") if x.nan?
266+
return BigDecimal(0) if x == 0
267+
return -erf(-x, prec) if x < 0
268+
269+
if x > 8 && (erfc1 = _erfc_asymptotic(x.abs, 1))
270+
erfc2 = _erfc_asymptotic(x.abs, [prec + erfc1.exponent, 1].max)
271+
return BigDecimal(1).sub(erfc2, prec) if erfc2
272+
end
273+
274+
prec2 = prec + BigDecimal.double_fig
275+
base = BigDecimal::BASE ** 2
276+
x_smallprec = (x * base).fix / base
277+
# Taylor series of x with small precision is fast
278+
erf1 = _erf_taylor(x_smallprec, BigDecimal(0), BigDecimal(0), prec2)
279+
# Taylor series converges quickly for small x
280+
v = _erf_taylor(x - x_smallprec, x_smallprec, erf1, prec2).mult(1, prec)
281+
[BigDecimal(1), v].min
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+
raise ArgumentError, "Zero or negative precision for erfc" if prec <= 0
297+
return BigDecimal("NaN") if x.nan?
298+
return BigDecimal(1).sub(erf(x, prec), prec) if x < 0
299+
300+
if x >= 8
301+
y = _erfc_asymptotic(x, prec)
302+
return y.mult(1, prec) if y
303+
end
304+
305+
# erfc(x) = 1 - erf(x) < exp(-x**2)/x/sqrt(pi)
306+
# Precision of erf(x) needs about log10(exp(-x**2)) extra digits
307+
log10 = 2.302585092994046
308+
high_prec = prec + BigDecimal.double_fig + (x**2 / log10).ceil
309+
BigDecimal(1).sub(erf(x, high_prec), prec)
310+
end
311+
312+
313+
private def _erf_taylor(x, a, erf_a, prec)
314+
# Let f(x) = erf(x+a)*exp((x+a)**2)*sqrt(pi)/2
315+
# = c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + ...
316+
# f'(x) = 1+2*(x+a)*f(x)
317+
# f'(x) = c1 + 2*c2*x + 3*c3*x**2 + 4*c4*x**3 + 5*c5*x**4 + ...
318+
# = 1+2*(x+a)*(c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + ...)
319+
# therefore,
320+
# c0 = f(0)
321+
# c1 = 2 * a * c0 + 1
322+
# c2 = (2 * c0 + 2 * a * c1) / 2
323+
# c3 = (2 * c1 + 2 * a * c2) / 3
324+
# c4 = (2 * c2 + 2 * a * c3) / 4
325+
326+
return erf_a if x.zero?
327+
328+
scale = BigDecimal(2).div(sqrt(PI(prec), prec), prec)
329+
c_prev = erf_a.div(scale.mult(BigMath.exp(-a*a, prec), prec), prec)
330+
c_next = (2 * a * c_prev).add(1, prec).mult(x, prec)
331+
v = c_prev.add(c_next, prec)
332+
333+
2.step do |k|
334+
c = (c_prev.mult(x, prec) + a * c_next).mult(2, prec).mult(x, prec).div(k, prec)
335+
v = v.add(c, prec)
336+
c_prev, c_next = c_next, c
337+
break if [c_prev, c_next].all? { |c| c.zero? || (c.exponent < v.exponent - prec) }
338+
end
339+
v = v.mult(scale.mult(BigMath.exp(-(x + a).mult(x + a, prec), prec), prec), prec)
340+
v > 1 ? BigDecimal(1) : v
341+
end
342+
343+
private def _erfc_asymptotic(x, prec)
344+
# Let f(x) = erfc(x)*sqrt(pi)*exp(x**2)/2
345+
# f(x) satisfies the following differential equation:
346+
# 2*x*f(x) = f'(x) + 1
347+
# From the above equation, we can derive the following asymptotic expansion:
348+
# f(x) = sum { (-1)**k * (2*k)! / 4***k / k! / x**(2*k)) } / x
349+
350+
# This asymptotic expansion does not converge.
351+
# But if there is a k that satisfies (2*k)! / 4***k / k! / x**(2*k) < 10**(-prec),
352+
# It is enough to calculate erfc within the given precision.
353+
# (2*k)! / 4**k / k! can be approximated as sqrt(2) * (k/e)**k by using Stirling's approximation.
354+
prec += BigDecimal.double_fig
355+
xf = x.to_f
356+
log10xf = Math.log10(xf)
357+
kmax = 1
358+
until kmax * Math.log10(kmax / Math::E) + 1 - 2 * kmax * log10xf < -prec
359+
kmax += 1
360+
return if xf * xf < kmax # Unable to calculate with the given precision
361+
end
362+
363+
sum = BigDecimal(1)
364+
x2 = x.mult(x, prec)
365+
d = BigDecimal(1)
366+
(1..kmax).each do |k|
367+
d = d.div(x2, prec).mult(1 - 2 * k, prec).div(2, prec)
368+
sum = sum.add(d, prec)
369+
end
370+
expx2 = BigMath.exp(x.mult(x, prec), prec)
371+
sum.div(expx2.mult(PI(prec).sqrt(prec), prec), prec).div(x, prec)
372+
end
249373
end

test/bigdecimal/test_bigmath.rb

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

0 commit comments

Comments
 (0)