[SciPy-user] I can't find unit step function.

Anne Archibald peridot.faceted@gmail....
Tue Mar 20 02:45:12 CDT 2007


On 20/03/07, David Warde-Farley <david.warde.farley@utoronto.ca> wrote:
> On Tue, 2007-03-20 at 16:20 +0900, Hui Chang Moon wrote:
> > Hello, users,
> >
> > Scipy has many convenient functions, so I can't find the function that
> > I am willing to use.
> > So, anybody help me find the unit step function.
>
> That might be just a little too basic for a built-in. But you can easily
> define it.
>
> def step(x):
>         if x == 0: return 0.5
>         return x > 0 and 1 or 0

You might prefer that it be vectorized:

In [7]: def step(x):
   ...:     return asarray(x>=0,dtype=float)
   ...:

In [8]: step(linspace(-1,1,9))
Out[8]: array([ 0.,  0.,  0.,  0.,  1.,  1.,  1.,  1.,  1.])

Though if you prefer step(0)==0.5 it's a bit of a pain.

Anne


More information about the SciPy-user mailing list