I have a function returning a numpy array that I need to integrate. I would like to use the scipy.integrate.dblquad
, but it requires the function to return a float.
I tried to use the numpy.vectorize
on dblquad
, it doesn't work. After some thought, I realized the problem with this approach is that the vectorized dblquad
requires a vector of functions, not a function returning vectors.
I want to solve this by any means, except splitting the arrays and integrating case by case (already did that and the code is a mess). The first thing that came in my mind was to transform the function returning the array in a array returning equivalent functions, but I don't know if this is possible.
I'll put some illustrative code. Below, x
and y
are numpy arrays, a
and b are floats.
import numpy as np
from scipy.integrate import dblquad
def foo(a, b, x, y):
return np.exp(a*x + b*y)
I would like to do
x = np.random.rand((3,3))
y = np.random.rand((3,3))
a = 1.5
b = 3.0
I = dblquad(foo, 0, 1, lambda x: 0, lambda x: 1, args=(x,y))
to get an array I
with shape (3,3)
with entry I[i][j]
being the integral of
foo(a,b,x[i][j],y[i][j])
, over 0 and 1 for the first two arguments.
I believe there is a smart way to do this with np.vectorize
, but I couldn't think of any.