[SciPy-user] What shape should arg 2 of integrate.trapz be?

Gregory Novak novak at ucolick.org
Thu Feb 2 13:30:54 CST 2006

If I do this:

(Note: nrange is like arange, except you specify the number of samples
instead of dx).

In [474]: x = nrange(-1,1,4) 
In [475]: y = nrange(-1,1,5)
In [476]: z = reshape(arange(20.), (4,5))
In [479]: shape(integrate.trapz(z,axis=0))
Out[479]: (5,)

the output is as I expect: I integrate away the (length 4) x dimension
and am left with an answer for each y value.

However, if I try to specify the x axis, I get the following traceback:

In [480]: shape(integrate.trapz(z,x,axis=0))
/sw/lib/python2.3/site-packages/scipy/integrate/quadrature.py in trapz(y, x, dx, axis)
     96     slice1[axis] = slice(1,None)
     97     slice2[axis] = slice(None,-1)
---> 98     return add.reduce(d * (y[slice1]+y[slice2])/2.0,axis)
    100 def cumtrapz(y, x=None, dx=1.0, axis=-1):

ValueError: frames are not aligned

I can make this work again by explicitly making the x array be 2dim
instead of 1dim:

In [481]: shape(integrate.trapz(z,x[:,NewAxis],axis=0))
Out[481]: (5,)

The question is: Is this the desired behavior?  It seems to me that
In[479] is well-specified and should give the correct result.  Is
there some reason that one would want to pass in a multi-d array as
the grid for the independent variable?


More information about the SciPy-user mailing list