问题
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.
回答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.
回答2:
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 h_d 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.)
来源:https://stackoverflow.com/questions/49145059/how-to-change-printed-representation-of-functions-derivative-in-sympy