[SciPy-user] 2d trapz

Pauli Virtanen pav@iki...
Tue Mar 10 15:30:28 CDT 2009

Tue, 10 Mar 2009 20:54:40 +0100, Christian K. wrote:
> I notice that I made not clear that by 'numerical' I meant integration
> of fixed sample data. So dblquad is not what I was looking for.

Use trapz twice?

import scipy as sp
import numpy as np
x = np.linspace(-10, 10, 200)
y = np.linspace(-10, 10, 80)

f = np.exp(-x[:,np.newaxis]**2 - y[np.newaxis,:]**2/7)/(np.sqrt(7)*np.pi)
print sp.trapz(sp.trapz(f, y[np.newaxis,:], axis=1), x, axis=0)
# -> 0.99999990566


There's a bug in Numpy <= 1.2.1 that requires that `f` and `y` to have 
same number of dimensions, so that's why the newaxis.

Pauli Virtanen

More information about the SciPy-user mailing list