[Numpy-discussion] numpy quad and maple

Daπid davidmenhur at gmail.com
Sat May 10 13:43:57 EDT 2014

On 10 May 2014 01:04, Ihab Riad <ifriad at gmail.com> wrote:

> Hi,
> I did the following integral with numpy quad
> quad(lambda h:
> np.exp(-.5*2334.0090702455204-1936.9610182100055*5*log10(h)-(12.5*2132.5498892927189)*log10(h)*log10(h)),0,inf)
> and I get the following values (1.8368139214123403e-126,
> 3.3631976081491865e-126).

The first value is the integral, the second is the error. You have (1.8 +-
3.3) 10^-126. That is, your integral is almost zero.

> When I do the integral with maple and mathematica I get
> 2.643019766*10^(-127)

That is inside the bracket Scipy said. And I wouldn't even trust them to be
correct without doing some checking.

Numbers so small are numerically non trustworthy. Your function is:

E^(A - B log(h) - C log^2(h)) = E^A * h^B' * h ^ (C' log(h))

where B' and C' depend on your numbers. E^A is already 10^-507 (are you
sure this numbers are correct?), so if you strip it out of your integral,
you have a simpler expression, and more reasonable values. This is what is
killing your integral and sending it to 0.

Now, you have to normalise the values. So,  changing variables: h-> sx, dh
= sdx:

sx^B' * (sx)^[C' (log(s) + log(x))]

Expand and simplify, find the value that makes more reasonable values
(order of one).

Last thing: giving quad a limit in infinity can make it behave crazily. If
you look at your second term, that is roughly x^(-log(x)), it decays very
quickly for large values of x (10^-5 for x=20), so you can, without
affecting your integral much, set a cut at a some reasonable h (plot it to
be sure). You can even make an estimate comparing with the analytical
result [1] of that particular piece.

Bottomline: don't use a lambda, use a function and expand your parameters.
That will make the expression simpler.

def f(h):
    log10 = np.log10(h)
    B = 1936.9610182100055*5
    return ...


[1] http://www.wolframalpha.com/input/?i=Integrate[x
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140510/a3459e1d/attachment.html>

More information about the NumPy-Discussion mailing list