是否可以通过 SymPy 中的 symbols() 分配偏导数的符号?
Is it possible to assign a symbol of partial derivative via symbols() in SymPy?
我想象征性地将其中一个变量表示为偏导数,例如:
dF_dx = 符号('dF/dx')
这样 display(dF_dx) 就会显示为:
有办法吗?官方的参考对新手来说不是很清楚。谢谢。
_print_Derivative
currently decides based on requires_partial
if it's going to use the rounded d符号与否。 requires_partial
函数检查表达式使用了多少个自由符号,如果它使用了多个符号,那么它将使用 rounded d 符号,否则它只会使用 d
代替。
要覆盖此行为,我们只需将自定义 LatexPrinter class 传递给 init_printing
from sympy import *
from sympy.printing.latex import LatexPrinter
from sympy.core.function import _coeff_isneg, AppliedUndef, Derivative
from sympy.printing.precedence import precedence, PRECEDENCE
class CustomPrint(LatexPrinter):
def _print_Derivative(self, expr):
diff_symbol = r'\partial'
tex = ""
dim = 0
for x, num in reversed(expr.variable_count):
dim += num
if num == 1:
tex += r"%s %s" % (diff_symbol, self._print(x))
else:
tex += r"%s %s^{%s}" % (diff_symbol,
self.parenthesize_super(self._print(x)),
self._print(num))
if dim == 1:
tex = r"\frac{%s}{%s}" % (diff_symbol, tex)
else:
tex = r"\frac{%s^{%s}}{%s}" % (diff_symbol, self._print(dim), tex)
if any(_coeff_isneg(i) for i in expr.args):
return r"%s %s" % (tex, self.parenthesize(expr.expr,
PRECEDENCE["Mul"],
is_neg=True,
strict=True))
return r"%s %s" % (tex, self.parenthesize(expr.expr,
PRECEDENCE["Mul"],
is_neg=False,
strict=True))
def custom_print_func(expr, **settings):
return CustomPrint().doprint(expr)
x = symbols('x')
F = Function('F')(x)
init_printing(use_latex=True,latex_mode="plain",latex_printer=custom_print_func)
dF_dx = F.diff(x)
display(dF_dx)
输出:
请参阅this post as well了解如何使用自定义 Latex 打印机。
此 post 中的所有代码都可用 in this repo。
我想象征性地将其中一个变量表示为偏导数,例如:
dF_dx = 符号('dF/dx')
这样 display(dF_dx) 就会显示为:
有办法吗?官方的参考对新手来说不是很清楚。谢谢。
_print_Derivative
currently decides based on requires_partial
if it's going to use the rounded d符号与否。 requires_partial
函数检查表达式使用了多少个自由符号,如果它使用了多个符号,那么它将使用 rounded d 符号,否则它只会使用 d
代替。
要覆盖此行为,我们只需将自定义 LatexPrinter class 传递给 init_printing
from sympy import *
from sympy.printing.latex import LatexPrinter
from sympy.core.function import _coeff_isneg, AppliedUndef, Derivative
from sympy.printing.precedence import precedence, PRECEDENCE
class CustomPrint(LatexPrinter):
def _print_Derivative(self, expr):
diff_symbol = r'\partial'
tex = ""
dim = 0
for x, num in reversed(expr.variable_count):
dim += num
if num == 1:
tex += r"%s %s" % (diff_symbol, self._print(x))
else:
tex += r"%s %s^{%s}" % (diff_symbol,
self.parenthesize_super(self._print(x)),
self._print(num))
if dim == 1:
tex = r"\frac{%s}{%s}" % (diff_symbol, tex)
else:
tex = r"\frac{%s^{%s}}{%s}" % (diff_symbol, self._print(dim), tex)
if any(_coeff_isneg(i) for i in expr.args):
return r"%s %s" % (tex, self.parenthesize(expr.expr,
PRECEDENCE["Mul"],
is_neg=True,
strict=True))
return r"%s %s" % (tex, self.parenthesize(expr.expr,
PRECEDENCE["Mul"],
is_neg=False,
strict=True))
def custom_print_func(expr, **settings):
return CustomPrint().doprint(expr)
x = symbols('x')
F = Function('F')(x)
init_printing(use_latex=True,latex_mode="plain",latex_printer=custom_print_func)
dF_dx = F.diff(x)
display(dF_dx)
输出:
请参阅this post as well了解如何使用自定义 Latex 打印机。
此 post 中的所有代码都可用 in this repo。