ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 18 Feb 2020 19:32:33 +0100Symbolic expression of `sng()` which isn’t zero at `sgn(0)`https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/ `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?Sat, 15 Feb 2020 20:45:59 +0100https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/Answer by nbruin for <p><code>sng(0) = 0</code> but I need a symbolic function or expression that evaluates to <code>+1</code> or <code>-1</code> as per the following definition:</p>
<p>$$\mathrm{side}\left(u\right) =
\begin{cases}
+1, & \text{if $u \geq 0$} \\
-1, & \text{if $u \lt 0$}
\end{cases}$$</p>
<p>I’ve tried the following but each have their own problems:</p>
<pre><code>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
</code></pre>
<p>Is there a way I can define this function symbolically?</p>
https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/?answer=49936#post-id-49936 side(u) = sign(u) + kronecker_delta(u,0)
would do the trick ...Sat, 15 Feb 2020 21:02:40 +0100https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/?answer=49936#post-id-49936Comment by Dan-K for <pre><code>side(u) = sign(u) + kronecker_delta(u,0)
</code></pre>
<p>would do the trick ...</p>
https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/?comment=49937#post-id-49937You sweet geniusSat, 15 Feb 2020 21:13:09 +0100https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/?comment=49937#post-id-49937Answer by Juanjo for <p><code>sng(0) = 0</code> but I need a symbolic function or expression that evaluates to <code>+1</code> or <code>-1</code> as per the following definition:</p>
<p>$$\mathrm{side}\left(u\right) =
\begin{cases}
+1, & \text{if $u \geq 0$} \\
-1, & \text{if $u \lt 0$}
\end{cases}$$</p>
<p>I’ve tried the following but each have their own problems:</p>
<pre><code>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
</code></pre>
<p>Is there a way I can define this function symbolically?</p>
https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/?answer=49967#post-id-49967As 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]
Tue, 18 Feb 2020 19:32:33 +0100https://ask.sagemath.org/question/49935/symbolic-expression-of-sng-which-isnt-zero-at-sgn0/?answer=49967#post-id-49967