How to change printed representation of function's derivative in sympy
Asked Answered
B

3

5

In a dynamic system my base values are all functions of time, d(t). I create the variable d using d = Function('d')(t) where t = S('t')

Obviously it's very common to have derivatives of d (rates of change like velocity etc.). However the default printing of diff(d(t)) gives:-

Derivative(d(t), t)

and using pretty printing in ipython (for e.g.) gives a better looking version of:-

d/dt (d(t))

The functions which include the derivatives of d(t) are fairly long in my problems however, and I'd like the printed representation to be something like d'(t) or \dot(d)(t) (Latex).

Is this possible in sympy? I can probably workaround this using subs but would prefer a generic sympy_print function or something I could tweak.

Burial answered 7/3, 2018 at 5:55 Comment(1)
And of course right after I post this question my browsing leads me to the mechanics module (docs.sympy.org/latest/modules/physics/mechanics/index.html). Should this question be closed, or considered a superset question (I wonder if the general question of printed representation is still useful even if my specific use of it is a solved problem).Burial
I
1

The vector printing module that you already found is the only place where such printing is implemented in SymPy.

from sympy.physics.vector import dynamicsymbols
from sympy.physics.vector.printing import vpprint, vlatex
d = dynamicsymbols('d')
vpprint(d.diff())     # ḋ
vlatex(d.diff())      # '\\dot{d}'

The regular printers (pretty, LaTeX, etc) do not support either prime or dot notation for derivatives. Their _print_Derivative methods are written so that they also work for multivariable expressions, where one has to specify a variable by using some sort of d/dx notation.

It would be nice to have an option for shorter derivative notation in general.

Inlaw answered 7/3, 2018 at 13:53 Comment(1)
Unfortunately after some fiddling around this both doesn't support greek letters as well as non-basic-character names (like o_1)Burial
B
5

I do this by substitution. It is horribly stupid, but it works like a charm:

q = Function('q')(t)
q_d = Function('\\dot{q}')(t)

and then substitute with

alias = {q.diff(t):q_d, } # and higher derivatives etc..
hd = q.diff(t).subs(alias)

And the output hd has a pretty dot over it's head!

As I said: this is a work-around and works, but you have to be careful in order to substitute correctly (Also for q_d.diff(t), which must be q_d2 and so on! You can have one big list with all replacements for printing and just apply it after the relevant mathematical steps.)

Bagman answered 13/5, 2018 at 12:2 Comment(1)
Note that \d is undefined in Python string literal. r'\dot{q}' or '\\dot{q}' is better.Gobi
I
1

The vector printing module that you already found is the only place where such printing is implemented in SymPy.

from sympy.physics.vector import dynamicsymbols
from sympy.physics.vector.printing import vpprint, vlatex
d = dynamicsymbols('d')
vpprint(d.diff())     # ḋ
vlatex(d.diff())      # '\\dot{d}'

The regular printers (pretty, LaTeX, etc) do not support either prime or dot notation for derivatives. Their _print_Derivative methods are written so that they also work for multivariable expressions, where one has to specify a variable by using some sort of d/dx notation.

It would be nice to have an option for shorter derivative notation in general.

Inlaw answered 7/3, 2018 at 13:53 Comment(1)
Unfortunately after some fiddling around this both doesn't support greek letters as well as non-basic-character names (like o_1)Burial
E
0

I use this tweak, you may supress writing the time dependeny everywhere, or also you may extend the existing rules with higher derivatives:

from sympy import *
latexReplaceRules = {
    # r'{\left(t \right)}':r' ',
    r'\frac{d}{d t}':r'\dot',
    r'\frac{d^{2}}{d t^{2}}':r'\ddot',
}
def latexNew(expr,**kwargs):
    retStr = latex(expr,**kwargs)
    for _,__ in latexReplaceRules.items():
        retStr = retStr.replace(_,__)
    return retStr
init_printing(latex_printer=latexNew)

# Test
t = symbols('t',real=True)
theta = Function('theta',real=True)(t)
theta.diff(t),theta.diff(t,2)
Edmundedmunda answered 21/2, 2024 at 19:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.