The answer to your question is, yes, under certain conditions.
For demonstration purposes, I first choose different bounds than you (11
instead of 1 - x
).
Generally, one can solve these types of integrals using dblquad
:
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 11)[0]
which here returns 66
. That is not an option as you mentioned in the comments.
One can now do this integration stepwise and it works fine for quad
as well as fixed_quad
:
def integrand(x, y):
return x + y
def fint_quad(x):
return integrate.quad(integrand, 0, 11, args=(x, ))[0]
def fint_fixed_quad(x):
return integrate.fixed_quad(integrand, 0, 11, args=(x, ), n=5)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
res_fixed_quad = integrate.fixed_quad(lambda x: fint_fixed_quad(x), 0, 1, n=5)
They both return 66
as well, as expected. That shows that it can work to compute double integrals using scipy.integrate.fixed_quad
.
However, when one now changes the upper bound back to the one you had (from 11
to 1 - x
), it still works for quad
but crashes for fixed_quad
:
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 1 - x)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
both return 0.333333...
, the call with fixed_quad
results in the error you received. One can understand the error by looking on the source code:
x, w = _cached_roots_legendre(n)
x = np.real(x)
if np.isinf(a) or np.isinf(b):
raise ValueError("Gaussian quadrature is only available for "
"finite limits.")
y = (b-a)*(x+1)/2.0 + a
return (b-a)/2.0 * np.sum(w*func(y, *args), axis=-1), None
When one prints a
and b
one gets:
a: 0
b: 1
a: 0
b: [ 0.95308992 0.76923466 0.5 0.23076534 0.04691008]
So for the call with 1-x
, b
is actually a numpy array with n
elements and one cannot compare an array to infinity, that's why it crashes. Whether that is an intended behavior or a bug, I can't answer; might be worth opening an issue on github.
scipy.integrate.dblquad()
but this is not what I'm looking for.result = integrate.dblquad(f, 0, 1, lambda x: 0, lambda x: 1-x)[0]