Sage: Ticket #6344: Typesetting partial derivatives in new symbolics
https://trac.sagemath.org/ticket/6344
<p>
New symbolics uses "D" format for derivatives instead of old "diff" format.
</p>
<p>
See the threads below for discussion on various related issues
</p>
<p>
<strong>[1]</strong> <a class="ext-link" href="http://groups.google.com/group/sage-devel/browse_thread/thread/7479c3eeb96348a2"><span class="icon"></span>http://groups.google.com/group/sage-devel/browse_thread/thread/7479c3eeb96348a2</a>
</p>
<p>
<strong>[2]</strong> <a class="ext-link" href="http://groups.google.com/group/sage-devel/browse_thread/thread/2c8068f27c1fb642"><span class="icon"></span>http://groups.google.com/group/sage-devel/browse_thread/thread/2c8068f27c1fb642</a>
</p>
<p>
Some patches attached to <a class="closed ticket" href="https://trac.sagemath.org/ticket/5711" title="enhancement: [with patch, positive review] Enhanced Typesetting of Symbolic Functions (closed: fixed)">#5711</a> also has code for this.
</p>
en-usSagehttps://trac.sagemath.org/chrome/site/logo_sagemath_trac.png
https://trac.sagemath.org/ticket/6344
Trac 1.1.6burcinWed, 24 Jun 2009 21:43:19 GMTattachment set
https://trac.sagemath.org/ticket/6344
https://trac.sagemath.org/ticket/6344
<ul>
<li><strong>attachment</strong>
set to <em>trac_6344-mma_style_attempt.patch</em>
</li>
</ul>
<p>
an attempt at implementing the MMA style, for testing only
</p>
TicketburcinWed, 24 Jun 2009 21:49:13 GMTdescription, summary changed; milestone set
https://trac.sagemath.org/ticket/6344#comment:1
https://trac.sagemath.org/ticket/6344#comment:1
<ul>
<li><strong>milestone</strong>
set to <em>sage-4.1</em>
</li>
<li><strong>description</strong>
modified (<a href="/ticket/6344?action=diff&version=1">diff</a>)
</li>
<li><strong>summary</strong>
changed from <em>Typesetting partial derivatives in new Symbolics</em> to <em>[with patch, needs work] Typesetting partial derivatives in new Symbolics</em>
</li>
</ul>
<p>
<a class="attachment" href="https://trac.sagemath.org/attachment/ticket/6344/trac_6344-mma_style_attempt.patch" title="Attachment 'trac_6344-mma_style_attempt.patch' in Ticket #6344">attachment:trac_6344-mma_style_attempt.patch</a><a class="trac-rawlink" href="https://trac.sagemath.org/raw-attachment/ticket/6344/trac_6344-mma_style_attempt.patch" title="Download"></a> implements an approximation to MMA style. It is just for testing, needs much more work for inclusion.
</p>
<p>
It doesn't look good in text only mode:
</p>
<pre class="wiki">sage: f = function('f')
sage: f(x).derivative(x,3)
f^{(3)}(x)
</pre>
TicketburcinTue, 14 Jul 2009 21:39:10 GMTstatus, summary changed; cc, owner, author set
https://trac.sagemath.org/ticket/6344#comment:2
https://trac.sagemath.org/ticket/6344#comment:2
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>assigned</em>
</li>
<li><strong>cc</strong>
<em>jason</em> <em>mvngu</em> added
</li>
<li><strong>summary</strong>
changed from <em>[with patch, needs work] Typesetting partial derivatives in new Symbolics</em> to <em>[with patch, needs review] Typesetting partial derivatives in new symbolics</em>
</li>
<li><strong>owner</strong>
changed from <em>(none)</em> to <em>burcin</em>
</li>
<li><strong>author</strong>
set to <em>Burcin Erocal</em>
</li>
</ul>
<p>
I uploaded a new patch at <a class="attachment" href="https://trac.sagemath.org/attachment/ticket/6344/trac_6344-symbolic_derivative_print.patch" title="Attachment 'trac_6344-symbolic_derivative_print.patch' in Ticket #6344">attachment:trac_6344-symbolic_derivative_print.patch</a><a class="trac-rawlink" href="https://trac.sagemath.org/raw-attachment/ticket/6344/trac_6344-symbolic_derivative_print.patch" title="Download"></a>.
</p>
<p>
The new patch keeps the text mode printing similar to the old one, but changes the printed parameters to indicate how many times each argument is differentiated. E.g., old output:
</p>
<pre class="wiki">sage: var('x,y')
sage: f = function('f')
sage: f(x).derivative(x)
D[0](f)(x)
sage: f(x,x).derivative(x,2)
D[0, 0](f)(x, x) + 2*D[0, 1](f)(x, x) + D[1, 1](f)(x, x)
</pre><p>
New output:
</p>
<pre class="wiki">sage: f(x).derivative(x)
D[1](f)(x)
sage: f(x,x).derivative(x,2)
D[2, 0](f)(x, x) + 2*D[1, 1](f)(x, x) + D[0, 2](f)(x, x)
</pre><p>
New latex output:
</p>
<pre class="wiki">sage: latex(f(x).derivative(x))
f'\left(x\right)
sage: latex(f(x,x).derivative(x,2))
f^{(2,0)}\left(x, x\right) + 2 \, f^{(1,1)}\left(x, x\right) + f^{(0,2)}\left(x, x\right)
</pre><p>
It would be better to add more documentation to explain the output, provide conversions to "textbook style" and fix other problems that pop up when printing derivatives:
</p>
<pre class="wiki">sage: binomial(x,y).derivative(x)
<boom>
sage: latex(floor(x).derivative(x))
D[0]\left \lfloor x \right \rfloor
sage: latex(ceil(x).derivative(x))
D[0]\left \lceil x \right \rceil
</pre><p>
However, I think we should settle on an output style ASAP, without letting too many releases go by.
</p>
<p>
Jason, Minh, can one (or both) of you review this?
</p>
TicketburcinTue, 14 Jul 2009 21:39:39 GMTattachment set
https://trac.sagemath.org/ticket/6344
https://trac.sagemath.org/ticket/6344
<ul>
<li><strong>attachment</strong>
set to <em>trac_6344-symbolic_derivative_print.patch</em>
</li>
</ul>
<p>
change printing of symbolic derivatives
</p>
TicketkcrismanThu, 29 Oct 2009 18:19:26 GMTstatus changed
https://trac.sagemath.org/ticket/6344#comment:3
https://trac.sagemath.org/ticket/6344#comment:3
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>needs_work</em>
</li>
</ul>
<p>
This needs a slight rebasing to be applied and tested to Sage 4.2, in calculus/tests.py and symbolic/pynac.pyx.
</p>
TicketkcrismanThu, 29 Oct 2009 18:55:36 GMTsummary changed
https://trac.sagemath.org/ticket/6344#comment:4
https://trac.sagemath.org/ticket/6344#comment:4
<ul>
<li><strong>summary</strong>
changed from <em>[with patch, needs review] Typesetting partial derivatives in new symbolics</em> to <em>Typesetting partial derivatives in new symbolics</em>
</li>
</ul>
TicketrobertwbThu, 05 Nov 2009 07:00:28 GMTcc changed
https://trac.sagemath.org/ticket/6344#comment:5
https://trac.sagemath.org/ticket/6344#comment:5
<ul>
<li><strong>cc</strong>
<em>robertwb</em> added
</li>
</ul>
Ticketbascorp2Wed, 26 May 2010 08:41:58 GMTupstream set
https://trac.sagemath.org/ticket/6344#comment:6
https://trac.sagemath.org/ticket/6344#comment:6
<ul>
<li><strong>upstream</strong>
set to <em>N/A</em>
</li>
</ul>
TicketjdemeyerTue, 13 Aug 2013 15:35:53 GMTmilestone changed
https://trac.sagemath.org/ticket/6344#comment:7
https://trac.sagemath.org/ticket/6344#comment:7
<ul>
<li><strong>milestone</strong>
changed from <em>sage-5.11</em> to <em>sage-5.12</em>
</li>
</ul>
TicketeviatarbachThu, 10 Oct 2013 21:15:25 GMTcc changed
https://trac.sagemath.org/ticket/6344#comment:8
https://trac.sagemath.org/ticket/6344#comment:8
<ul>
<li><strong>cc</strong>
<em>eviatarbach</em> added
</li>
</ul>
Ticketvbraun_spamThu, 30 Jan 2014 21:20:52 GMTmilestone changed
https://trac.sagemath.org/ticket/6344#comment:9
https://trac.sagemath.org/ticket/6344#comment:9
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.1</em> to <em>sage-6.2</em>
</li>
</ul>
TicketburcinWed, 12 Mar 2014 08:50:15 GMT
https://trac.sagemath.org/ticket/6344#comment:10
https://trac.sagemath.org/ticket/6344#comment:10
<p>
<a class="closed ticket" href="https://trac.sagemath.org/ticket/14517" title="enhancement: Akward notation of differential operator (closed: duplicate)">#14517</a> has a similar complaint.
</p>
Ticketvbraun_spamTue, 06 May 2014 15:20:58 GMTmilestone changed
https://trac.sagemath.org/ticket/6344#comment:11
https://trac.sagemath.org/ticket/6344#comment:11
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.2</em> to <em>sage-6.3</em>
</li>
</ul>
Ticketvbraun_spamSun, 10 Aug 2014 16:51:03 GMTmilestone changed
https://trac.sagemath.org/ticket/6344#comment:12
https://trac.sagemath.org/ticket/6344#comment:12
<ul>
<li><strong>milestone</strong>
changed from <em>sage-6.3</em> to <em>sage-6.4</em>
</li>
</ul>
TicketschymansWed, 03 Dec 2014 21:41:12 GMT
https://trac.sagemath.org/ticket/6344#comment:13
https://trac.sagemath.org/ticket/6344#comment:13
<p>
I tried to apply the most recent patch, but did not succeed. It is just too old and when looking at the diff I couldn't make any sense of it. I tried to implement differential notation myself, but got stuck, perhaps someone can help?
</p>
<p>
All I wanted to do is to change the _latex_ representation of the FDerivativeOperator and added the following lines to src/sage/symbolic/operators.py:
</p>
<pre class="wiki">def _latex_(self):
"""
Return the LaTeX representation of X.
EXAMPLES::
sage: from sage.symbolic.operators import FDerivativeOperator
sage: var('x z')
sage: f = function('f', x, z)
sage: op = FDerivativeOperator(f, [0,1]); latex(op)
\frac{\partial \frac{\partial f }{\partial x } }{\partial z }
"""
fname = self._f.operator()
vars = self._f.operands()
difvars = self._parameter_set
str1 = str(fname)
for difvar in difvars:
str1 = '\\frac{\partial '+str1+'}{\partial '+str(vars[difvar])+'}'
return str1
</pre><p>
Unfortunately, this does not have any effect on the latex representation of f.diff:
</p>
<pre class="wiki">sage: f = function('f', x, z)
sage: g = diff(f, x,z)
sage: latex(g)
D[0, 1]\left(f\right)\left(x, z\right)
</pre><p>
Does anyone have an idea what else I need to modify? Thanks in advance!
</p>
TicketschymansWed, 03 Dec 2014 21:44:52 GMTcc changed
https://trac.sagemath.org/ticket/6344#comment:14
https://trac.sagemath.org/ticket/6344#comment:14
<ul>
<li><strong>cc</strong>
<em>schymans</em> added
</li>
</ul>
TicketnbruinWed, 03 Dec 2014 22:48:26 GMT
https://trac.sagemath.org/ticket/6344#comment:15
https://trac.sagemath.org/ticket/6344#comment:15
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/6344#comment:13" title="Comment 13">schymans</a>:
</p>
<blockquote class="citation">
<p>
All I wanted to do is to change the _latex_ representation of the FDerivativeOperator and added the following lines to src/sage/symbolic/operators.py:
</p>
</blockquote>
<p>
Looks like the wrong place to hook into this. For symbolic expressions. <code>self._latex_</code> invokes <code>SR._latex_element_</code> which calls straight into Pynac via <code> GEx_to_str_latex(&x._gobj)</code>. Your experiment shows that this doesn't dispatch to <code>_latex_</code> methods on operators. Perhaps there's another hook?
</p>
<p>
Incidentally, your code wouldn't work, because that's not how FDerivateOperators occur in code:
</p>
<pre class="wiki">sage: var("x,y")
(x, y)
sage: function('f',x,y)
f(x, y)
sage: g=diff(f(x,y),x,y)
sage: g
D[0, 1](f)(x, y)
sage: g.operator()
D[0, 1](f)
sage: type(g.operator())
<class 'sage.symbolic.operators.FDerivativeOperator'>
sage: g.operator()._f
f
</pre><p>
As you can see, there are no variable names to refer to. That's why this ticket has stalled: if you want to do this, you need to recognize on the level of <code>g</code> that the operator is an <code>FDerivativeOperator</code> and hence that, if the operands of <code>g</code> are distinct, simple symbolic variables, that the derivative could be written in Leibnitz notation.
</p>
<p>
Clearly, people haven't found the effort required worth the payoff.
</p>
TicketschymansThu, 04 Dec 2014 00:40:08 GMT
https://trac.sagemath.org/ticket/6344#comment:16
https://trac.sagemath.org/ticket/6344#comment:16
<p>
Thanks for the quick answer! From the user's perspective, the best way would be to have an option similar to derivative_func in the function definition, allowing to define the notation for derivatives. The efforts required seems indeed amazingly high.
</p>
<p>
Wouldn't it be possible to write some parsing code to convert something like D[0,1](f)(x,y) to any kind of notation? It should even be possible to convert it back to diff(f(x,y),x,y), as requested here: <a class="ext-link" href="http://comments.gmane.org/gmane.comp.mathematics.sage.devel/58040"><span class="icon"></span>http://comments.gmane.org/gmane.comp.mathematics.sage.devel/58040</a>
</p>
TicketnbruinThu, 04 Dec 2014 01:26:06 GMT
https://trac.sagemath.org/ticket/6344#comment:17
https://trac.sagemath.org/ticket/6344#comment:17
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/6344#comment:16" title="Comment 16">schymans</a>:
</p>
<blockquote class="citation">
<p>
Wouldn't it be possible to write some parsing code to convert something like D[0,1](f)(x,y) to any kind of notation? It should even be possible to convert it back to diff(f(x,y),x,y), as requested here: <a class="ext-link" href="http://comments.gmane.org/gmane.comp.mathematics.sage.devel/58040"><span class="icon"></span>http://comments.gmane.org/gmane.comp.mathematics.sage.devel/58040</a>
</p>
</blockquote>
<p>
It's really much easier to do on the expression tree than on a string. We do it for conversions already. See e.g. <a class="ext-link" href="https://github.com/sagemath/sage/blob/master/src/sage/interfaces/maxima_lib.py#L1564"><span class="icon"></span>https://github.com/sagemath/sage/blob/master/src/sage/interfaces/maxima_lib.py#L1564</a>. The approach is straightforward. The hard work is that you need to reach into Pynac to make the change. So it takes someone conversant with Pynac who cares enough to do it. Doing it on strings afterwards is going to be horrible.
</p>
<p>
Incidentally, watch out that an expression like
</p>
<pre class="wiki">D[0,1](f)(x,x+1)
</pre><p>
is almost impossible to write in Leibnitz notation unless you introduce auxiliary variables (which is what happens in the maxima_lib code). So you should probably just stick with operator notation for those cases (maple does).
</p>
TicketschymansThu, 04 Dec 2014 08:34:10 GMT
https://trac.sagemath.org/ticket/6344#comment:18
https://trac.sagemath.org/ticket/6344#comment:18
<p>
I see. Would it be easier to allow the user to define custom latex representations for just some standard differentials one anticipates when defining a function? For all others, the system could fall back to the D[0,1] (f)(x,z) notation. A simple replacement rule when latexing an expression could do it.
</p>
<p>
By the way, it took me a quite some time of digging in the code to understand the meaning of e.g. D[0,1](f)(x,z). Now that I have understood it, I see its use. I was expecting a description in the documentation of the diff() or differential() command, but I didn't find it there. Where is the right place to look?
</p>
TicketkcrismanThu, 04 Dec 2014 14:19:29 GMT
https://trac.sagemath.org/ticket/6344#comment:19
https://trac.sagemath.org/ticket/6344#comment:19
<blockquote class="citation">
<p>
By the way, it took me a quite some time of digging in the code to understand the meaning of e.g. D[0,1](f)(x,z). Now that I have understood it, I see its use. I was expecting a description in the documentation of the diff() or differential() command, but I didn't find it there. Where is the right place to look?
</p>
</blockquote>
<p>
If anyone should know, I should, but I don't. It would be <em>wonderful</em> to have some better documentation of that where it belongs - want to take a stab? If so, open a ticket and cc: me.
</p>
TicketschymansThu, 04 Dec 2014 22:16:55 GMT
https://trac.sagemath.org/ticket/6344#comment:20
https://trac.sagemath.org/ticket/6344#comment:20
<p>
Thanks, this is now <a class="ext-link" href="http://trac.sagemath.org/ticket/17445"><span class="icon"></span>http://trac.sagemath.org/ticket/17445</a>
There, I mentioned some other, related tickets, which made me realise that I do not fully understand the meaning of the D-notation, so it wouldn't make much sense for me to write a documentation for it. Sorry!
</p>
TicketkcrismanFri, 05 Dec 2014 04:11:42 GMT
https://trac.sagemath.org/ticket/6344#comment:21
https://trac.sagemath.org/ticket/6344#comment:21
<p>
Wow, after reading the threads in question I see that at this point Sage has existed about as long with this new format as it did with the previous one. I'm reluctant to make this wontfix, at least the <em>option</em> as indicated there
</p>
<pre class="wiki">psi(x) = function('psi',x)
g = diff(psi(x),x)
latex(g)
\frac{d \psi\left(x\right)}{d x}
# Switch to D format
sage.symbolic.pynac.typeset_d_as_diff=False
latex(g)
D[0]\psi\left(x\right)
</pre><p>
should exist, except of course the other way around for the default nowadays, I guess. Maybe that piece of <a class="attachment" href="https://trac.sagemath.org/attachment/ticket/5711/enhanced-symbolic-typesetting-rebased_to_4.0.1.patch" title="Attachment 'enhanced-symbolic-typesetting-rebased_to_4.0.1.patch' in Ticket #5711">attachment:enhanced-symbolic-typesetting-rebased_to_4.0.1.patch:ticket:5711</a><a class="trac-rawlink" href="https://trac.sagemath.org/raw-attachment/ticket/5711/enhanced-symbolic-typesetting-rebased_to_4.0.1.patch" title="Download"></a> should be implemented here instead?
</p>
TicketschymansFri, 05 Dec 2014 09:27:35 GMT
https://trac.sagemath.org/ticket/6344#comment:22
https://trac.sagemath.org/ticket/6344#comment:22
<p>
+1 from me! I was very sad to find out that this had not been implemented.
</p>
TicketkcrismanFri, 05 Dec 2014 14:08:23 GMTcc changed
https://trac.sagemath.org/ticket/6344#comment:23
https://trac.sagemath.org/ticket/6344#comment:23
<ul>
<li><strong>cc</strong>
<em>pbruin</em> <em>nbruin</em> added
</li>
</ul>
<p>
Nils and/or Peter, would you have objections to the following, based on code from <a class="closed ticket" href="https://trac.sagemath.org/ticket/5711" title="enhancement: [with patch, positive review] Enhanced Typesetting of Symbolic Functions (closed: fixed)">#5711</a> (if possible)?
</p>
<pre class="wiki">psi(x) = function('psi',x)
g = diff(psi(x),x)
latex(g)
D[0]\psi\left(x\right)
# Switch to D format
sage.symbolic.pynac.typeset_d_as_diff=True
latex(g)
\frac{d \psi\left(x\right)}{d x}
</pre>
TicketnbruinFri, 05 Dec 2014 17:39:38 GMT
https://trac.sagemath.org/ticket/6344#comment:24
https://trac.sagemath.org/ticket/6344#comment:24
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/6344#comment:23" title="Comment 23">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
Nils and/or Peter, would you have objections to the following, based on code from <a class="closed ticket" href="https://trac.sagemath.org/ticket/5711" title="enhancement: [with patch, positive review] Enhanced Typesetting of Symbolic Functions (closed: fixed)">#5711</a> (if possible)?
</p>
<pre class="wiki">...
sage.symbolic.pynac.typeset_d_as_diff=True
latex(g)
\frac{d \psi\left(x\right)}{d x}
</pre></blockquote>
<p>
Only do that when the argument list consists only of distinct symbolic variables. Then you can even print "diff" for the normal rep. See links above for code that makes this distinction already. This is what Maple does too, by the way.
</p>
TicketkcrismanFri, 05 Dec 2014 17:52:15 GMT
https://trac.sagemath.org/ticket/6344#comment:25
https://trac.sagemath.org/ticket/6344#comment:25
<p>
Oh, so that's where the <code>t0</code> and friends come from. Funny thing... what if those variables are already taken? <ducks />
</p>
TicketnbruinFri, 05 Dec 2014 17:58:51 GMT
https://trac.sagemath.org/ticket/6344#comment:26
https://trac.sagemath.org/ticket/6344#comment:26
<p>
Replying to <a class="ticket" href="https://trac.sagemath.org/ticket/6344#comment:25" title="Comment 25">kcrisman</a>:
</p>
<blockquote class="citation">
<p>
Oh, so that's where the <code>t0</code> and friends come from. Funny thing... what if those variables are already taken?
</p>
</blockquote>
<p>
Thought about that, no problem. They get substituted right away.
</p>
TicketnbruinThu, 22 Mar 2018 01:28:35 GMTstatus, milestone changed
https://trac.sagemath.org/ticket/6344#comment:27
https://trac.sagemath.org/ticket/6344#comment:27
<ul>
<li><strong>status</strong>
changed from <em>needs_work</em> to <em>needs_review</em>
</li>
<li><strong>milestone</strong>
changed from <em>sage-6.4</em> to <em>sage-duplicate/invalid/wontfix</em>
</li>
</ul>
<p>
<a class="closed ticket" href="https://trac.sagemath.org/ticket/21286" title="enhancement: Improve printing of FDerivative by adapting the appropriate hook in PyNaC (closed: fixed)">#21286</a> has dealt with with this. Close as duplicate/invalid ?
</p>
TicketchapotonSun, 15 Apr 2018 11:46:09 GMTstatus changed
https://trac.sagemath.org/ticket/6344#comment:28
https://trac.sagemath.org/ticket/6344#comment:28
<ul>
<li><strong>status</strong>
changed from <em>needs_review</em> to <em>positive_review</em>
</li>
</ul>
TicketvdelecroixFri, 18 May 2018 17:16:26 GMTstatus changed; resolution set
https://trac.sagemath.org/ticket/6344#comment:29
https://trac.sagemath.org/ticket/6344#comment:29
<ul>
<li><strong>status</strong>
changed from <em>positive_review</em> to <em>closed</em>
</li>
<li><strong>resolution</strong>
set to <em>wontfix</em>
</li>
</ul>
<p>
closing positively reviewed duplicates
</p>
Ticket