On Wed, Feb 4, 2015 at 12:18 AM, Warren Weckesser <warren.weckesser@gmail.com> wrote:


On Tue, Feb 3, 2015 at 11:14 PM, Sturla Molden <sturla.molden@gmail.com> wrote:
Warren Weckesser <warren.weckesser@gmail.com> wrote:

>                     0    if x < 0
>     heaviside(x) =  0.5  if x == 0
>                     1    if x > 0
>

This is not correct. The discrete form of the Heaviside step function has
the value 1 for x == 0.

heaviside = lambda x : 1 - (x < 0).astype(int)





By "discrete form", do you mean discrete time (i.e. a function defined on the integers)?  Then I agree, the discrete time unit step function is defined as

    u(k) = 0  k < 0
           1  k >= 0


for integer k.

The domain of the proposed Heaviside function is not discrete; it is defined for arbitrary floating point (real) arguments.  In this case, the choice heaviside(0) = 0.5 is a common convention. See for example,

* http://mathworld.wolfram.com/HeavisideStepFunction.html
* http://www.mathworks.com/help/symbolic/heaviside.html
* http://en.wikipedia.org/wiki/Heaviside_step_function, in particular http://en.wikipedia.org/wiki/Heaviside_step_function#Zero_argument

Other common conventions are the right-continuous version that you prefer (heavisde(0) = 1), or the left-continuous version (heaviside(0) = 0).

We can accommodate the alternatives with an additional argument that sets the value at 0:

    heaviside(x, zero_value=0.5)

What's the usecase for a heaviside function?

I don't think I have needed one since I was using mathematica or maple.

(x < 0).astype(...)   (x <= 0).astype(...)   
np.sign(x, dtype)
look useful enough for most cases, or not?

(What I wish numpy had is conditional place that doesn't calculate all the values. (I think there is a helper function in scipy.stats for that))

Josef

 



Warren

 

Sturla

_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


_______________________________________________
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion