2

I need to compute a numerical (triple) integral, but do not need very high precision on the value, and would therefore like to sacrifice some precision for speed when using nquad. I thought that I might be able to do this by increasing the epsrel and/or epsabs options, but they seem to have no effect. For example (note, this is just an example integrand - I don't actually need to compute this particular integral...):

import numpy as np
from scipy.integrate import nquad

def integrand(l, b, d, sigma=250):
    x = d * np.cos(l) * np.cos(b)
    y = d * np.sin(l) * np.cos(b)
    z = d * np.sin(b)
    return np.exp(-0.5 * z**2 / sigma**2) / np.sqrt(2*np.pi * sigma**2)

ranges = [
    (0, 2*np.pi),
    (0.5, np.pi/2),
    (0, 1000.)
]

# No specification of `opts` - use the default epsrel and epsabs:
result1 = nquad(integrand, ranges=ranges, full_output=True)

# Set some `quad` opts:
result2 = nquad(integrand, ranges=ranges, full_output=True,
                opts=dict(epsabs=1e-1, epsrel=0, limit=3))

Both outputs are identical:

>>> print(result1)
(4.252394424844468, 1.525272379143154e-12, {'neval': 9261})
>>> print(result2)
(4.252394424844468, 1.525272379143154e-12, {'neval': 9261})

A full example is included here: https://gist.github.com/adrn/b9aa92c236df011dbcdc131aa94ed9f9

Is this not the right approach, or is scipy.integrate ignoring my inputted opts?

ampw
  • 175
  • 10

1 Answers1

0

From the scipy.integrate.nquad it is stated that opts can only be passed to quad as can be seen here:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.nquad.html

Example of application:

import numpy as np
from scipy.integrate import quad

def integrand(a, sigma=250):
    x = 2 * np.sin(a) * np.cos(a)
    return x


# No specification of `opts` - use the default epsrel and epsabs:
result1 = quad(integrand,0, 2*np.pi)

# Set some `quad` opts:
result2 = quad(integrand,0, 4*np.pi,epsabs=1e-6, epsrel=1e-6, limit=40)

returns:

result1: (-1.3690011097614755e-16, 4.4205541621600365e-14)
result2: (-1.7062635631484713e-15, 9.096805257467047e-14)

The reason nquad doesn't complain about the presence of options is because nquad includes quad, dbquad and tplquad.