# [Numpy-discussion] Piecewise functions.

Andrea Riciputi ariciputi at pito.com
Thu Sep 22 14:26:48 CDT 2005

I've already tried something like this, but it doesn't work since f1
and f2 return not valid values outside the range over they are
defined. Perhaps an example could clarify; suppose that f1(x) = 1./
sqrt(1 - x**2) for x <= 1, and f2(x) = 1./sqrt(x**2 - 1) for x > 1.

Your suggestion, as the other I've tried, fails with a
"OverflowError: math range error".

Any helps?
Andrea.

On Sep 22, 2005, at 9:33 PM, Alan G Isaac wrote:

> 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
>
>
>
>
>
> -------------------------------------------------------
> SF.Net email is sponsored by:
> Tame your development challenges with Apache's Geronimo App Server.
> Download it for free - -and be entered to win a 42" plasma tv or