[Numpy-discussion] Piecewise functions.

Greg Ewing greg.ewing at canterbury.ac.nz
Thu Sep 22 20:26:10 EDT 2005


Andrea Riciputi wrote:

> On Sep 22, 2005, at 9:33 PM, Alan G Isaac wrote:
> 
>
>> def f(x):
>>     return x<=a and f1(x) or f2(x)
 >
 > I've already tried something like this, but it doesn't work

I wish people would stop suggesting the 'a and b or c' trick,
because it DOESN'T WORK except in special circumstances (i.e.
when you can be sure that b is never false).

What you want is:

   def f(x):
     if x <= a:
       return f1(x)
     else:
       return f2(x)

-- 
Greg Ewing, Computer Science Dept, +--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg.ewing at canterbury.ac.nz	   +--------------------------------------+




More information about the NumPy-Discussion mailing list