[SciPy-user] Using scipy specfunc in integration

Robert Kern robert.kern at gmail.com
Sat Jun 14 18:25:14 EDT 2008


On Sat, Jun 14, 2008 at 16:57, Neal Becker <ndbecker2 at gmail.com> wrote:
> Anne Archibald wrote:
>
>> 2008/6/13 Robert Kern <robert.kern at gmail.com>:
>>> On Fri, Jun 13, 2008 at 09:21, Neal Becker <ndbecker2 at gmail.com> wrote:
>>>> Any ideas on this?
>>>> from scipy.special import erf
>>>> from math import exp, tan
>>>> def cot(x):
>>>>    return 1/tan(x)
>>>>
>>>> N = 8
>>>> esnodB = 10
>>>> Rd = 10**(.1 * esnodB)
>>>>
>>>> def F (y):
>>>>    return exp (-(y**2)) * erf (y * cot (pi/N))
>>>>
>>>>
>>>> Pe = float(N-1)/float(N) - 0.5 * erf (sqrt (Rd * sin (pi/N))) -
>>>> 1/(sqrt(pi)) * quadrature(F, 0, sqrt(Rd) * sin (pi/N))[0]
>>>> TypeError: only length-1 arrays can be converted to Python scalars
>>>>
>>>> It seems to be complaining about erf.
>>>> erf
>>>> Out[33]: <ufunc 'erf'>
>>>
>>> I think it's complaining about exp(), actually. quadrature() is going
>>> to pass arrays to F(), not scalars.
>>
>> Specifically, math.exp and math.tan do not accept vector arguments;
>> don't use them. Use numpy.exp and numpy.tan instead.
>>
>> Anne
> Thanks, but I'm confused.  The above code is strictly scalar - who's asking
> for vectors?

Like I said, quadrature() passes arrays to the integrand function, not
scalars, and expects the integrand function to evaluate itself
elementwise on those arrays. Look at its docstring. Set vec_func=False
if you want the function to only take scalars.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco



More information about the SciPy-User mailing list