SymPy仅打印功能名称 [英] SymPy print only function name

查看:59
本文介绍了SymPy仅打印功能名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在SymPy中进行一些符号计算,但无法使用乳胶打印并获得所需的图形输出.这一直困扰着我,我花了几个小时(也许几天)试图找到一种方法来自定义对象的打印方式(在 LaTeX 中,在 pprint 表示中有详细记录).

I'm trying to do some symbolic calculations in SymPy but I'm unable to use latex printing and get the graphical output I want. That has always troubled me and have spent several hours (perhaps days) trying to fine a way to customize how objects are printed (in LaTeX, in pprint representation it's well documented).

但是,在这种情况下,我试图对取决于x,y,z的未定义函数psi进行符号计算.但是,这些计算暗示的项具有高达psi * \ vec r的卷度的三倍.

However, in this case I'm trying to make symbolic calculations on an Undefined Function, psi, which depends on x, y, z. However, those calculations imply terms which have up to three times the curl of psi* \vec r.

我有以下代码

init_printing(use_latex=True)

R = ReferenceFrame(r"E", variables=["x", "y", "z"])
psi = Function(r"\psi")(R[0], R[1], R[2])
rpsi = (R[0]*R.x + R[1]*R.y + R[2]*R.z)*psi

如果您不熟悉的话, R [i] 是第i个变量(x,y或z),而 Rx Ry Rz 是单一的笛卡尔向量,在对ReferenceFrame(来自sympy.physics的函数)的调用中实例化.

Where, in case you aren't familiar with it, R[i] is the i-th variable (x, y or z) and R.x, R.y and R.z are the unitary cartesian vectors, instantiated in the call to ReferenceFrame (a function from sympy.physics).

例如,当我尝试查看 curl(curl(rpsi,R),R)时,输出中就充满了"\ psi(x,y,z)"(用乳胶编译并显示为图像)遵循该表达式变得很乏味.

When I try to take a look at curl(curl(rpsi, R), R) for example, the output is so filled with "\psi(x, y, z)" (compiled in latex and showed as an image) that it gets tedious to follow the expression.

有没有一种方法可以定制要编译的乳胶字符串(这将适用于我正在执行的其他计算).如果不可能,那么如何解决此特定问题(即每次打印函数时显示独立变量).

Is there a way to customize the latex string that gets compiled (which would apply to other calculations that I'm doing). If that is not possible, how can solve this particular problem (i.e. independent variables displaying every time the function is printed).

我将qpyconsole与jupyter结合使用.

I'm using jupyter with qtconsole.

干杯

推荐答案

最简单的方法是创建一个自定义函数,该函数可以根据需要打印.您可以通过将 Function 子类化并定义 _latex 来实现此目的(请参见

The simplest way would be to create a custom function, which prints as you want. You can achieve this by subclassing Function and defining _latex (see http://docs.sympy.org/latest/modules/printing.html#sympy.printing.latex.LatexPrinter.printmethod).

In [33]: class psi(Function):
   ....:     def _latex(self, printer):
   ....:         return r'\psi'
   ....:

In [34]: latex(psi(x, y, z))
Out[34]: '\\psi'

如果要打印参数,请使用 self.args 进行访问,并使用 printer._print (例如,普通打印机将类似于 r'\ phi {\ left('+','.join(printer._print(i)for self.args中的i)+'\ right}}').

If you want to print the arguments, access them with self.args and use printer._print (e.g., the usual printer would be something like r'\phi{\left (' + ', '.join(printer._print(i) for i in self.args) + '\right )}').

这篇关于SymPy仅打印功能名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆