2D Integration over a flattened Array

Viewed 16

I'm hoping to find a way around the solution offered here to use 2D arrays in order to do 2D numerical integration.

import numpy as np
ksize = 50
a        = 1.0
kdom     = np.pi / a 
x = np.linspace(- kdom, kdom, ksize)  
y = np.linspace(- kdom, kdom, ksize)    
dk = x[1]-x[0]
X,Y = np.meshgrid(x,y)
eigval = np.cos(X)+np.cos(Y)
eigvalflat = eigval.flatten()
intval = np.trapz(np.trapz(eigval,x),y)

sumval = np.sum(eigvalflat)*dk/ksize

print(intval,sumval)

Given my dummy example above, I'd like to find a way to properly integrate the 1D array (eigvalflat) while still as a flattened array even though it is a double integral.

1 Answers

Computationally, if the integrand is not separable, then the answer is that you can't recast the double integral as a single integral, unless you compute the integral one dimension at a time, which is what the assignment to intval is essentially doing.

Analytically, you'll have a better chance by asking yourself the question: given the 2d region of the integral (a rectangle in your example), can one find an integral over the boundary of that region? For that, Green's theorem has you covered with necessary and sufficient conditions.

Related