Ask Your Question
0

Symbolic expression of `sng()` which isn’t zero at `sgn(0)`

asked 2020-02-15 13:45:59 -0500

Dan-K gravatar image

sng(0) = 0 but I need a symbolic function or expression that evaluates to +1 or -1 as per the following definition:

$$\mathrm{side}\left(u\right) = \begin{cases} +1, & \text{if $u \geq 0$} \\ -1, & \text{if $u \lt 0$} \end{cases}$$

I’ve tried the following but each have their own problems:

side = sgn(u) # Evaluates to 0 at u = 0
side = u/abs(u) # “ValueError: power::eval(): division by zero” at u = 0
side = 1 - (u < 0)*2 # “TypeError: unable to simplify to float approximation”
# These next two use a Python expression so ‘u’ gets evaluated too early.
side = -1 if u < 0 else 1
side = lambda u: -1 if u < 0 else 1

Is there a way I can define this function symbolically?

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
2

answered 2020-02-15 14:02:40 -0500

nbruin gravatar image
side(u) = sign(u) + kronecker_delta(u,0)

would do the trick ...

edit flag offensive delete link more

Comments

1

You sweet genius

Dan-K gravatar imageDan-K ( 2020-02-15 14:13:09 -0500 )edit
2

answered 2020-02-18 12:32:33 -0500

Juanjo gravatar image

As another option, you can define side as follows:

side(u) = 2*unit_step(u) - 1

Hence

sage: [side(a) for a in range(-2,3)]
[-1, -1, 1, 1, 1]
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-02-15 13:45:59 -0500

Seen: 40 times

Last updated: Feb 18