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.Wed, 30 Dec 2020 10:19:06 +0100Pretty print derivative in Newton notation with dot?https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/Is there any way to get the pretty printer to produce Newton's notation? - ie. a single dot centred over the variable for first derivative with respect to time, 2 dots for second derivative etc.
Example:
t, y = var('t, y')
x = function('x')(t)
pretty_print(y == 2*diff(diff(x,t),t) - 3 * diff(x,t) + 5)
gives:
![sage math output](https://www.sandyscott.net/wp-content/uploads/2020/12/partialleibniz.png)
but I'd like to see:
![Newton's Notation](https://www.sandyscott.net/wp-content/uploads/2020/12/newton.png)Mon, 28 Dec 2020 23:37:51 +0100https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/Comment by slelievre for <p>Is there any way to get the pretty printer to produce Newton's notation? - ie. a single dot centred over the variable for first derivative with respect to time, 2 dots for second derivative etc.</p>
<p>Example:</p>
<pre><code>t, y = var('t, y')
x = function('x')(t)
pretty_print(y == 2*diff(diff(x,t),t) - 3 * diff(x,t) + 5)
</code></pre>
<p>gives:</p>
<p><img alt="sage math output" src="https://www.sandyscott.net/wp-content/uploads/2020/12/partialleibniz.png"></p>
<p>but I'd like to see:</p>
<p><img alt="Newton's Notation" src="https://www.sandyscott.net/wp-content/uploads/2020/12/newton.png"></p>
https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?comment=54976#post-id-54976Welcome to Ask Sage!
Thank you for your question!Mon, 28 Dec 2020 23:47:42 +0100https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?comment=54976#post-id-54976Comment by slelievre for <p>Is there any way to get the pretty printer to produce Newton's notation? - ie. a single dot centred over the variable for first derivative with respect to time, 2 dots for second derivative etc.</p>
<p>Example:</p>
<pre><code>t, y = var('t, y')
x = function('x')(t)
pretty_print(y == 2*diff(diff(x,t),t) - 3 * diff(x,t) + 5)
</code></pre>
<p>gives:</p>
<p><img alt="sage math output" src="https://www.sandyscott.net/wp-content/uploads/2020/12/partialleibniz.png"></p>
<p>but I'd like to see:</p>
<p><img alt="Newton's Notation" src="https://www.sandyscott.net/wp-content/uploads/2020/12/newton.png"></p>
https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?comment=54981#post-id-54981Ideally, add
- an example of input that one can paste in a fresh Sage session
- the current output
- the desired outputTue, 29 Dec 2020 10:15:23 +0100https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?comment=54981#post-id-54981Answer by slelievre for <p>Is there any way to get the pretty printer to produce Newton's notation? - ie. a single dot centred over the variable for first derivative with respect to time, 2 dots for second derivative etc.</p>
<p>Example:</p>
<pre><code>t, y = var('t, y')
x = function('x')(t)
pretty_print(y == 2*diff(diff(x,t),t) - 3 * diff(x,t) + 5)
</code></pre>
<p>gives:</p>
<p><img alt="sage math output" src="https://www.sandyscott.net/wp-content/uploads/2020/12/partialleibniz.png"></p>
<p>but I'd like to see:</p>
<p><img alt="Newton's Notation" src="https://www.sandyscott.net/wp-content/uploads/2020/12/newton.png"></p>
https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?answer=55005#post-id-55005This gets requested from time to time and we should support that!
Not sure the pretty printer can do that, but there are workarounds.
One of them consists in defining "x dot" and "x dot dot" functions,
and to substitute them in the equation.
sage: t, y = SR.var('t, y')
sage: x = function('x')(t)
sage: xdot = function('xdot', latex_name=r'\dot x')
sage: xddot = function('xddot', latex_name=r'\ddot x')
sage: de = y == 2*diff(diff(x, t), t) - 3 * diff(x, t) + 5
sage: dde = de.subs({diff(x, t): xdot(t), diff(diff(x, t), t): xddot(t)})
sage: view(dde)
$$
y = 2 \ddot x\left(t\right) - 3 \dot x\left(t\right) + 5
$$
Even better, when defining a function, you can say what its derivative is.
Using that, start by defining the furthest derivative you'll need
(here `xdd` for "x dot dot"), then work backwards until you define `x`.
Then the pretty printer gets things right without the need for `subs`.
sage: t, y = SR.var('t, y')
sage: xdd = function('xdd', latex_name=r'\ddot x')
sage: xd = function('xd', latex_name=r'\dot x',
....: derivative_func=lambda self, *aa, **ka: xdd(*aa))
sage: x = function('x', derivative_func=lambda self, *aa, **ka: xd(*aa))
sage: de = y == 2*diff(diff(x(t), t), t) - 3 * diff(x(t), t) + 5
sage: view(de)
$$
y = -3 \, \dot x\left(t\right) + 2 \, \ddot x\left(t\right) + 5
$$Wed, 30 Dec 2020 09:52:36 +0100https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?answer=55005#post-id-55005Comment by slelievre for <p>This gets requested from time to time and we should support that!</p>
<p>Not sure the pretty printer can do that, but there are workarounds.</p>
<p>One of them consists in defining "x dot" and "x dot dot" functions,
and to substitute them in the equation.</p>
<pre><code>sage: t, y = SR.var('t, y')
sage: x = function('x')(t)
sage: xdot = function('xdot', latex_name=r'\dot x')
sage: xddot = function('xddot', latex_name=r'\ddot x')
sage: de = y == 2*diff(diff(x, t), t) - 3 * diff(x, t) + 5
sage: dde = de.subs({diff(x, t): xdot(t), diff(diff(x, t), t): xddot(t)})
sage: view(dde)
</code></pre>
<p>$$
y = 2 \ddot x\left(t\right) - 3 \dot x\left(t\right) + 5
$$</p>
<p>Even better, when defining a function, you can say what its derivative is.</p>
<p>Using that, start by defining the furthest derivative you'll need
(here <code>xdd</code> for "x dot dot"), then work backwards until you define <code>x</code>.</p>
<p>Then the pretty printer gets things right without the need for <code>subs</code>.</p>
<pre><code>sage: t, y = SR.var('t, y')
sage: xdd = function('xdd', latex_name=r'\ddot x')
sage: xd = function('xd', latex_name=r'\dot x',
....: derivative_func=lambda self, *aa, **ka: xdd(*aa))
sage: x = function('x', derivative_func=lambda self, *aa, **ka: xd(*aa))
sage: de = y == 2*diff(diff(x(t), t), t) - 3 * diff(x(t), t) + 5
sage: view(de)
</code></pre>
<p>$$
y = -3 \, \dot x\left(t\right) + 2 \, \ddot x\left(t\right) + 5
$$</p>
https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?comment=55006#post-id-55006Inspired by @nbruin's answer to [Ask Sage question 44598: Substitute differential operators in an expression](https://ask.sagemath.org/question/44598).Wed, 30 Dec 2020 10:19:06 +0100https://ask.sagemath.org/question/54975/pretty-print-derivative-in-newton-notation-with-dot/?comment=55006#post-id-55006