[Numpy-discussion] Piecewise functions.

Alan G Isaac aisaac at american.edu
Thu Sep 22 12:31:36 EDT 2005


On Thu, 22 Sep 2005, Andrea Riciputi apparently wrote: 
> 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? 

I suspect I do not understand your question.
But perhaps you want this:

def f(x):
    return x<=a and f1(x) or f2(x)

fwiw,
Alan







More information about the NumPy-Discussion mailing list