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

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 close merge delete

Sort by » oldest newest most voted
side(u) = sign(u) + kronecker_delta(u,0)


would do the trick ...

more

1

You sweet genius

( 2020-02-15 21:13:09 +0200 )edit

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]

more