Is there a heaviside function in Python similar to that of MATLAB's heaviside
?
I am struggling to find one.
The Heaviside step function H(x), also called the unit step function, is a discontinuous function, whose value is zero for negative arguments x < 0 and one for positive arguments x > 0, as illustrated in Fig.
Graph of Heaviside function H(x). The limit exists at all a > 0. No matter how close the point a is to 0, we can always approach it from both sides. The limit exists at all a < 0.
If you are using numpy version 1.13.0 or later, you can use numpy.heaviside
:
In [61]: x Out[61]: array([-2. , -1.5, -1. , -0.5, 0. , 0.5, 1. , 1.5, 2. ]) In [62]: np.heaviside(x, 0.5) Out[62]: 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 [65]: 0.5 * (numpy.sign(x) + 1) Out[65]: array([ 0. , 0. , 0. , 0. , 0.5, 1. , 1. , 1. , 1. ])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With