ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 10 Feb 2017 10:20:56 -0600Evaluating the derivative of piecewise functionshttp://ask.sagemath.org/question/36451/evaluating-the-derivative-of-piecewise-functions/ Hi,
In Sage 7.5 you can numerically evaluate the derivative of a regular symbolic expression using:
sage: h(x) = sin(x)
sage: diff(h)(2).n()
-0.416146836547142
Old **Piecewise** functions could be treated in the same way:
sage: g = Piecewise([([0,2], sin(x)), ((2,3), cos(x))])
... DeprecationWarning ...
sage: diff(g)(1).n()
0.540302305868140
However, new **piecewise** functions don't:
sage: f = piecewise([([0,2], sin(x)), ((2,3), cos(x))])
sage: diff(f)(1).n()
... Error ...
Thanks in advance.
Sat, 04 Feb 2017 12:43:29 -0600http://ask.sagemath.org/question/36451/evaluating-the-derivative-of-piecewise-functions/Answer by paulmasson for <p>Hi,</p>
<p>In Sage 7.5 you can numerically evaluate the derivative of a regular symbolic expression using:</p>
<pre><code>sage: h(x) = sin(x)
sage: diff(h)(2).n()
-0.416146836547142
</code></pre>
<p>Old <strong>Piecewise</strong> functions could be treated in the same way:</p>
<pre><code>sage: g = Piecewise([([0,2], sin(x)), ((2,3), cos(x))])
... DeprecationWarning ...
sage: diff(g)(1).n()
0.540302305868140
</code></pre>
<p>However, new <strong>piecewise</strong> functions don't:</p>
<pre><code>sage: f = piecewise([([0,2], sin(x)), ((2,3), cos(x))])
sage: diff(f)(1).n()
... Error ...
</code></pre>
<p>Thanks in advance.</p>
http://ask.sagemath.org/question/36451/evaluating-the-derivative-of-piecewise-functions/?answer=36460#post-id-36460The new `piecewise` is unfortunately not very user friendly. Here's one workaround:
diff(f.expression_at(1))(1).n()
Sun, 05 Feb 2017 14:25:05 -0600http://ask.sagemath.org/question/36451/evaluating-the-derivative-of-piecewise-functions/?answer=36460#post-id-36460Comment by franpena for <p>The new <code>piecewise</code> is unfortunately not very user friendly. Here's one workaround:</p>
<pre><code>diff(f.expression_at(1))(1).n()
</code></pre>
http://ask.sagemath.org/question/36451/evaluating-the-derivative-of-piecewise-functions/?comment=36547#post-id-36547Thank you @paulmasson. So that means that `piecewise` cannot operate as similar as `Piecewise` does with symbolic expressions. In my opinion this is a drawback.Fri, 10 Feb 2017 10:20:56 -0600http://ask.sagemath.org/question/36451/evaluating-the-derivative-of-piecewise-functions/?comment=36547#post-id-36547