[SciPy-User] Help With Scipy: Integration pack, returning an array for an array input
Vick
vickram.l@gmail....
Wed Dec 15 00:14:47 CST 2010
I hope this is the right way to do this, and I apologize most sincerely
if it's not:
Here is a step by step of what I'm trying to do and what I typed in,
it's a simplified version that gets the same error message as what I'm
trying.
In [1]: import numpy as np
In [2]: import scipy.integrate as inte
In [3]: C = lambda u: inte.quad(lambda s: np.exp(s),0,u)
This yields:
In [4]: C(5)
Out[4]: (147.41315910257663, 1.6366148336841205e-12)
i.e. The integral of exp(s) from 0 to5, andgivestheerror, whichisgreat.
now define:
In [5]: D = lambda t: C(t)[0]
In [6]: D(5)
Out[6]: 147.41315910257663
Just the first element, which is all I really care about, the error
isn't what I need right now.
Now try t = np.arange(0,5) to try and get it to work for a range of
values from 0 to5.
In [7]: t = np.arange(0,5)
In [8]: D(t)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/vick/<ipython console> in <module>()
/home/vick/<ipython console> in <lambda>(t)
/home/vick/<ipython console> in <lambda>(u)
/usr/lib/python2.6/dist-packages/scipy/integrate/quadpack.pyc in quad(func, a, b, args, full_output,epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
183 if type(args) != type(()): args = (args,)
184 if (weight is None):
--> 185 retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
186 else:
187 retval =_quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts)
/usr/lib/python2.6/dist-packages/scipy/integrate/quadpack.pyc in _quad(func, a, b, args, full_output,epsabs, epsrel, limit, points)
231 def _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points):
232 infbounds = 0
--> 233 if (b != Inf and a != -Inf):
234 pass # standard integration
235 elif (b == Inf and a != -Inf):
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
I'd like to get the value of this integral over a range of t's.
I don't really understand why it's doing this, and I'd like to avoid
using a for loop.
More information about the SciPy-User
mailing list