Any interest in a 'heaviside' ufunc?
I have an implementation of the Heaviside function as numpy ufunc. Is there any interest in adding this to numpy? The function is simply:
0 if x < 0 heaviside(x) = 0.5 if x == 0 1 if x > 0
Warren
That seems useful to me.
On Tue, Feb 3, 2015 at 3:58 PM, Warren Weckesser <warren.weckesser@gmail.com
wrote:
I have an implementation of the Heaviside function as numpy ufunc. Is there any interest in adding this to numpy? The function is simply:
0 if x < 0 heaviside(x) = 0.5 if x == 0 1 if x > 0
Warren
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
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)
Sturla
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 rightcontinuous version that you prefer (heavisde(0) = 1), or the leftcontinuous 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)
Warren
Sturla
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
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 rightcontinuous version that you prefer (heavisde(0) = 1), or the leftcontinuous 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
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
On 04/02/15 06:18, Warren Weckesser wrote:
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
It is the cumulative integral of the delta function, and thus it can never obtain the value 0.5. The delta function is defined to have an integral of 0 or 1.
Sturla
On 4 February 2015 at 11:05, Sturla Molden sturla.molden@gmail.com wrote:
On 04/02/15 06:18, Warren Weckesser wrote:
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
It is the cumulative integral of the delta function, and thus it can never obtain the value 0.5. The delta function is defined to have an integral of 0 or 1.
Sturla
There are several definitions. Abramowitz and Stegun (http://people.math.sfu.ca/~cbm/aands/page_1020.htm) assign the value 0.5 at x=0. It can also be defined as:
H(x) = 1/2 * (1 + sign(x))
Where sign(0) = 0, and therefore H(0) = 1/2.
Actually, Heaviside function is better seen as a distribution instead of a function, and then there is no problem with the value at 0, as long as it is finite.
On 04.02.2015 11:45, Daπid wrote:
There are several definitions. Abramowitz and Stegun (http://people.math.sfu.ca/~cbm/aands/page_1020.htm) assign the value 0.5 at x=0.
The NIST handbook uses the value 0 at x=0.
Perhaps a Heaviside with an optional argument that defines the value at x=0 would be good. I'd love to see that in NumPy.
Actually, Heaviside function is better seen as a distribution instead of a function, and then there is no problem with the value at 0, as long as it is finite.
Understanding a distribution as the limit of a sequence of functions, the value at x=0 then depended on the choice of function in the sequence, I guess. Using something symmetrical such a Gaussian or a centred box then makes the value of 0.5 plausible.
Alex
On Tue, Feb 3, 2015 at 12:58 PM, Warren Weckesser < warren.weckesser@gmail.com> wrote:
I have an implementation of the Heaviside function as numpy ufunc. Is there any interest in adding this to numpy? The function is simply:
0 if x < 0 heaviside(x) = 0.5 if x == 0 1 if x > 0
I don't think there's anything like it in numpy. Wouldn't scipy.special be a better home for it?
Jaime
Warren
NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Le 04/02/2015 06:58, Jaime Fernández del Río a écrit :
I have an implementation of the Heaviside function as numpy ufunc. Is there any interest in adding this to numpy? The function is simply: 0 if x < 0 heaviside(x) = 0.5 if x == 0 1 if x > 0
I don't think there's anything like it in numpy. Wouldn't scipy.special be a better home for it?
scipy.signal could also host it, since it already contains functions for linear systems (e.g. step response, which are closely related), and also some waveform generators like square()
http://docs.scipy.org/doc/scipy0.14.0/reference/signal.html
However, I agree with Joseph when he says that this function is a bit thin.
best, Pierre
participants (8)

Alexander Eberspächer

Aron Ahmadia

Daπid

Jaime Fernández del Río

josef.pktd＠gmail.com

Pierre Haessig

Sturla Molden

Warren Weckesser