unit step (heaviside) function in sympy?

1,582 views
Skip to first unread message

Reckoner

unread,
May 5, 2008, 2:22:35 PM5/5/08
to sympy
is there a unit step (heaviside) function in sympy?

I need to work a conditional into a symbolic expression.

Thanks in advance

Ondrej Certik

unread,
May 5, 2008, 4:39:23 PM5/5/08
to sy...@googlegroups.com
On Mon, May 5, 2008 at 8:22 PM, Reckoner <reck...@gmail.com> wrote:
>
> is there a unit step (heaviside) function in sympy?
>
> I need to work a conditional into a symbolic expression.

We have sign which is basically the same thing:

In [1]: sign(x)
Out[1]: sign(x)

In [2]: sign(1)
Out[2]: 1

In [3]: sign(-5)
Out[3]: -1


Ondrej

Friedrich Hagedorn

unread,
May 5, 2008, 5:07:20 PM5/5/08
to sy...@googlegroups.com

And so you have the heaviside function:

In [1]: H=Lambda(x, (sign(x)+1)/2)

In [2]: H(-1)
Out[2]: 0

In [3]: H(0)
Out[3]: 1

In [4]: H(1)
Out[4]: 1

By, Friedrich

Fredrik Johansson

unread,
May 5, 2008, 8:22:30 PM5/5/08
to sy...@googlegroups.com

I think there is a bug here. sign(0) should be 0 (and as a consequence
H(0) should be 1/2).

Fredrik

Friedrich Hagedorn

unread,
May 6, 2008, 5:50:49 AM5/6/08
to sy...@googlegroups.com
On Tue, May 06, 2008 at 02:22:30AM +0200, Fredrik Johansson wrote:
> > In [1]: H=Lambda(x, (sign(x)+1)/2)
> >
> > In [2]: H(-1)
> > Out[2]: 0
> >
> > In [3]: H(0)
> > Out[3]: 1
> >
> > In [4]: H(1)
> > Out[4]: 1
>
> I think there is a bug here. sign(0) should be 0 (and as a consequence
> H(0) should be 1/2).

I think you are rigth. Here is a patch for this.

By, Friedrich

sign.patch

Ondrej Certik

unread,
May 6, 2008, 6:42:55 AM5/6/08
to sy...@googlegroups.com

It's in, thanks. Now it works as expected:

In [6]: H=Lambda(x, (sign(x)+1)/2)

In [7]: H(-1)
Out[7]: 0

In [8]: H(1)
Out[8]: 1

In [9]: H(0)
Out[9]: 1/2


Ondrej

Reply all
Reply to author
Forward
0 new messages