[Numpy-discussion] Piecewise functions.
Alan G Isaac
aisaac at american.edu
Thu Sep 22 15:18:10 CDT 2005
On Thu, 22 Sep 2005, Andrea Riciputi apparently wrote:
> 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".
If you do it as I suggested, they should not I believe be
evaluated outside of their range. So your function must be
generating an overflow error within this range.
>>> import math
>>> import random
>>> def f1(x): return math.sqrt(1-x**2)
...
>>> def f2(x): return 1./math.sqrt(x**2-1)
...
>>> def f(x): return x<=1 and f1(x) or f2(x)
...
>>> d = [random.uniform(0,2) for i in range(20)]
>>> fd = [f(x) for x in d]
Works fine.
Cheers,
Alan Isaac
More information about the Numpy-discussion
mailing list