martin
martin

Reputation: 99

2D numerical integration in Python

I know how to perform a double integral in python

import numpy as np
import scipy.integrate as integrate

integrate.dblquad(x*y, 0, 1, lambda x: -np.sqrt(1-x**2), lambda x: np.sqrt(1-x**2))

where x and y are, say, (200,) numpy arrays.

However, what if the integrand (x*y) above is a 2D array rather than a function? In my case, I have an array Z which has a value at every coordinate (x,y), i.e. it has shape (200,200). However, I do not know in advance the continuous function(s) that it would correspond to.

How would I perform this integral? Thanks.

Upvotes: 1

Views: 3521

Answers (1)

Nico Schlömer
Nico Schlömer

Reputation: 58721

So you're trying to integrate a function over the unit disk? You can use quadpy (a project of mine) to do that. It does support vector-valued integrands, too:

import numpy
import quadpy

scheme = quadpy.disk.lether(6)  # there are many other schemes available
# scheme.show()
val = scheme.integrate(
    lambda x: [numpy.exp(x[0] + x[1]), numpy.cos(x[0])],
    [0.0, 0.0],  # disk center
    1.0  # disk radius
)
print(val)
[3.99523707 2.76491937]

Upvotes: 2

Related Questions