[SciPy-user] single elements and arrays

Robert Kern robert.kern@gmail....
Tue Jul 1 00:13:58 CDT 2008


On Tue, Jul 1, 2008 at 00:09, Yosef Meller <yosefmel@post.tau.ac.il> wrote:
> On Monday 30 June 2008 21:57:18 Robert Kern wrote:
>> On Mon, Jun 30, 2008 at 13:14, Gideon Simpson <grs2103@columbia.edu> wrote:
>> > foo finds a root where x is a parameter in the equation to be solved.
>> > If x is an array, I iterate through the elements of the array.
>>
>> In that case, just special case it. Use numpy.isscalar() to do the test.
>
> Or else, put this at the beginning:
> x = numpy.atleast_1d(x)

Presumably, he also wants to return a scalar if given a scalar. The
general outline would probably look like this:

def foo(x):
    xisscalar = numpy.isscalar(x)
    x = numpy.atleast_1d(x)
    y = ...
    if xisscalar:
        y = y[0]
    return y

-- 
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