Is there a heaviside function in Python similar to that of MATLAB’s `heaviside`?

I am struggling to find one.

If you are using numpy version 1.13.0 or later, you can use `numpy.heaviside`:

``````In : x
Out: array([-2. , -1.5, -1. , -0.5,  0. ,  0.5,  1. ,  1.5,  2. ])

In : np.heaviside(x, 0.5)
Out: array([ 0. ,  0. ,  0. ,  0. ,  0.5,  1. ,  1. ,  1. ,  1. ])
``````

With older versions of numpy you can implement it as `0.5 * (numpy.sign(x) + 1)`

``````In : 0.5 * (numpy.sign(x) + 1)
Out: array([ 0. ,  0. ,  0. ,  0. ,  0.5,  1. ,  1. ,  1. ,  1. ])
``````

Probably the simplest method is just

``````def step(x):
return 1 * (x > 0)
``````

This works for both single numbers and numpy arrays, returns integers, and is zero for x = 0. The last criteria may be preferable over `step(0) => 0.5` in certain circumstances.

It’s part of sympy, which you can install with `pip install sympy`

From the docs:

``````class sympy.functions.special.delta_functions.Heaviside

Heaviside Piecewise function. Heaviside function has the following properties:

1) diff(Heaviside(x),x) = DiracDelta(x)    ( 0, if x<0 )
2) Heaviside(x) = < [*] 1/2 if x==0        ( 1, if x>0 )
``````

You would use it like this:

``````In : from sympy.functions.special.delta_functions import Heaviside

In : Heaviside(1)
Out: 1

In : Heaviside(0)
Out: 1/2

In : Heaviside(-1)
Out: 0
``````

You could also write your own:

``````heaviside = lambda x: 0.5 if x == 0 else 0 if x < 0 else 1
``````

Although that may not meet your needs if you require a symbolic variable.

As of numpy 1.13, it is `numpy.heaviside`.

I’m not sure if it’s there out-of-the-box, but you can always write one:

``````def heaviside(x):
if x == 0:
return 0.5

return 0 if x < 0 else 1
``````

Not sure if the best way getting things done… but here is function that I hacked up.

``````def u(t):
unit_step = numpy.arange(t.shape)
lcv = numpy.arange(t.shape)
for place in lcv:
if t[place] == 0:
unit_step[place] = .5
elif t[place] > 0:
unit_step[place] = 1
elif t[place] < 0:
unit_step[place] = 0
return unit_step
`````` ``````def heaviside(xx):
return numpy.where(xx <= 0, 0.0, 1.0) + numpy.where(xx == 0.0, 0.5, 0.0)
``````

Or, if `numpy.where` is too slow:

``````def heaviside(xx):
yy = numpy.ones_like(xx)
yy[xx < 0.0] = 0.0
yy[xx == 0.0] = 0.5
return yy
``````

The following timings are with numpy 1.8.2; some optimisations were made in numpy 1.9.0, so try this yourself:

``````>>> import timeit
>>> import numpy
>>> array = numpy.arange(10) - 5
>>> def one():
...  return numpy.where(array <= 0, 0.0, 1.0) + numpy.where(array == 0.0, 0.5, 0.0)
...
>>> def two():
...  yy = numpy.ones_like(array)
...  yy[array < 0] = 0.0
...  yy[array == 0] = 0.5
...  return yy
...
>>> timeit.timeit(one, number=100000)
3.026144027709961
>>> timeit.timeit(two, number=100000)
1.5265140533447266
>>> numpy.__version__
'1.8.2'
``````

On a different machine, with a different numpy:

``````>>> timeit.timeit(one, number=100000)
0.5119631290435791
>>> timeit.timeit(two, number=100000)
0.5458788871765137
>>> numpy.__version__
'1.11.1'
>>> def three():
...  return 0.5*(numpy.sign(array) + 1)
...
>>> timeit.timeit(three, number=100000)
0.313539981842041
``````

Easy Solution:

``````import numpy as np
amplitudes = np.array([1*(x >= 0) for x in range(-5,6)])
``````