SymPy print only function name

自作多情 提交于 2021-02-04 21:10:45

问题


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).

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.

I have the following code

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

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).

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).

I'm using jupyter with qtconsole.

Cheers,


回答1:


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'

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 )}').



来源:https://stackoverflow.com/questions/40695886/sympy-print-only-function-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!