Python Sympy:修改衍生品的乳胶产量

Python Sympy:修改衍生品的乳胶产量,python,sympy,Python,Sympy,在Sympy中,是否可以修改函数导数使用latex()输出的方式?默认设置相当麻烦。这: f = Function("f")(x,t) print latex(f.diff(x,x)) 将输出 \frac{\partial^{2}}{\partial x^{2}} f{\left (x,t \right )} 这很冗长。如果我喜欢 f_{xx} 有没有办法强制执行此行为?您可以将最新打印机子类化,并定义自己的\u print\u派生工具。这是目前的执行情况 也许像 from sympy

在Sympy中,是否可以修改函数导数使用latex()输出的方式?默认设置相当麻烦。这:

f = Function("f")(x,t)
print latex(f.diff(x,x))
将输出

\frac{\partial^{2}}{\partial x^{2}}  f{\left (x,t \right )} 
这很冗长。如果我喜欢

f_{xx}

有没有办法强制执行此行为?

您可以将
最新打印机子类化,并定义自己的
\u print\u派生工具
。这是目前的执行情况

也许像

from sympy import Symbol
from sympy.printing.latex import LatexPrinter
from sympy.core.function import UndefinedFunction

class MyLatexPrinter(LatexPrinter):
    def _print_Derivative(self, expr):
        # Only print the shortened way for functions of symbols
        function, *vars = expr.args
        if not isinstance(type(function), UndefinedFunction) or not all(isinstance(i, Symbol) for i in vars):
            return super()._print_Derivative(expr)
        return r'%s_{%s}' % (self._print(Symbol(function.func.__name__)), ' '.join([self._print(i) for i in vars]))
这就像

>>> MyLatexPrinter().doprint(f(x, y).diff(x, y))
'f_{x y}'
>>> MyLatexPrinter().doprint(Derivative(x, x))
'\\frac{d}{d x} x'
要在Jupyter笔记本中使用它,请使用

init_printing(latex_printer=MyLatexPrinter().doprint)

看起来不是这样,但看看这里的选项:像个魔咒一样工作!对于python2支持,我将“function,*vars=expr.args”更改为“function,vars=expr.args[0],expr.args[1:”。您还需要将
super()
更改为
super(MyLatexPrinter,self)
。嗯,它在python2.7中运行,没有最后的更改。