[Numpy-discussion] Piecewise functions.
Travis Oliphant
oliphant at ee.byu.edu
Mon Sep 26 10:44:26 CDT 2005
Andrea Riciputi wrote:
> Hi all,
> this is probably an already discussed problem, but I've not been able
> to find a solution even after googling a lot.
>
> I've a piecewise defined function:
>
> /
> | f1(x) if x <= a
> f(x) = |
> | f2(x) if x > a
> \
>
> where f1 and f2 are not defined outside the above range. How can I
> define such a function in Python in order to apply (map) it to an
> array ranging from values smaller to values bigger than a?
This is not a trivial problem in current versions of Numeric.
What are you using, Numeric, numarray?
In new scipy core (which replaces Numeric) and, I think, in numarray,
you could say
gta = x>a
lea = x<=a
y = x.copy()
y[gta] = f2(x[gta])
y[lea] = f1(x[lea])
I've also just written a piecewise function for the new scipy core so
you could write
y = piecewise(x, x<=a, [f1,f2])
-Travis Oliphant
More information about the Numpy-discussion
mailing list