Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #6183
| From | Peter Falloon <pfalloon@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: LogErfc |
| Date | 2011-11-04 11:10 +0000 |
| Organization | Steven M. Christensen and Associates, Inc and MathTensor, Inc. |
| Message-ID | <j90h82$snb$1@smc.vnet.net> (permalink) |
| References | <j8tkfh$eu0$1@smc.vnet.net> |
On Nov 3, 7:47 pm, Shawn Garbett <shawn.garb...@gmail.com> wrote: > I need to have a LogErfc function to be able to compute another > function in a numerically stable manner. I found the function in the > GSL, I found it in R, and even in another system, there's a copy of code to do > it floating around. However, I need it in Mathematica and Log[Erfc[x]] > doesn't cut in due to underflow. Cody solved this in 1969, and I could > code his solution, but I was hoping that someone had already solved > this in Mathematica. Anyone? As you probably know, the problem here is that Erfc[x] goes to zero like Exp[-z^2], and so quickly reaches an underflow, whereas Log[Erfc[x]] is still a number of tractable magnitude. One way around this is to find a representation in terms of another special function in which the exponential dependence is explicit: that way, with a bit of simple manipulation you can apply the Log function and extract the troublesome exponential dependence. One such representation is in terms of the hypergeometric U function (http:// functions.wolfram.com/06.27.26.0002.01). For real z>0 the annoying (z/ Sqrt[z^2]) factor in that equation reduces to 1, and you get: Erfc[z] == Exp[-z^2] * HypergeometricU[1/2, 1/2, z^2] / Sqrt[Pi] Unfortunately, in Mathematica HypergeometricU[1/2,1/2,z^2] is converted to Exp[z^2]*Gamma[1/2, z^2] internally before computing, so we're back where we started (since Gamma[1/2,z^2] suffers the same Underflow[] issues as Erfc for large z). However, if you use the trick of converting one of the 1/2 factors to finite precision (i.e. Real instead of Integer) then a different algorithm is used, which doesn't involve the pesky exponential term. So to cut to the chase, here is a simple definition which should solve your problem: Clear[logErfc] logErfc[z_?NumericQ /; Precision[z]<Infinity] := -z^2 - (1/2)Log[Pi] + Log[HypergeometricU[SetPrecision[1/2, Precision[z]], 1/2, z^2]] Cheers, Peter.
Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread
LogErfc Shawn Garbett <shawn.garbett@gmail.com> - 2011-11-03 08:47 +0000 Re: LogErfc Peter Falloon <pfalloon@gmail.com> - 2011-11-04 11:10 +0000
csiph-web