8765674 8765674 - 1 month ago 9
Python Question

Does a heaviside step function exist?

Is there a heaviside function in Python similar to that of MATLAB's

?

I am struggling to find one.

Answer

If you are using numpy, you could implement it as 0.5 * (numpy.sign(x) + 1)

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

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