Sympy 简明教程

SymPy - Printing

SymPy 中提供有若干打印机。以下是部分列表 -

There are several printers available in SymPy. Following is a partial list −

  1. str

  2. srepr

  3. ASCII pretty printer

  4. Unicode pretty printer

  5. LaTeX

  6. MathML

  7. Dot

SymPy 对象也可以输出为各种语言的代码,例如 C、Fortran、Javascript、Theano 和 Python。

SymPy objects can also be sent as output to code of various languages, such as C, Fortran, Javascript, Theano, and Python.

SymPy 使用 Unicode 字符以漂亮打印的方式呈现输出。如果你使用 Python 控制台执行 SymPy 会话,可以通过调用 init_session() 函数激活最优的漂亮打印环境。

SymPy uses Unicode characters to render output in form of pretty print. If you are using Python console for executing SymPy session, the best pretty printing environment is activated by calling init_session() function.

>>> from sympy import init_session
>>> init_session()

适用于 SymPy 1.5.1 的 IPython 控制台(Python 3.7.4-64 位)(基本类型:python)。

IPython console for SymPy 1.5.1 (Python 3.7.4-64-bit) (ground types: python).

已执行以下命令 -

These commands were executed −

>>> from __future__ import division
>>> from sympy import *
>>> x, y, z, t = symbols('x y z t')
>>> k, m, n = symbols('k m n', integer=True)
>>> f, g, h = symbols('f g h', cls=Function)
>>> init_printing()

可在 https://docs.sympy.org/1.5.1/. 找到文档

Documentation can be found at https://docs.sympy.org/1.5.1/.

>>> Integral(sqrt(1/x),x)

$\int \sqrt\frac{1}{x} dx$

$\int \sqrt\frac{1}{x} dx$

如果未安装 LATEX 但安装了 Matplotlib,则将使用 Matplotlib 渲染引擎。如果未安装 Matplotlib,则将使用 Unicode 漂亮打印机。但是,Jupyter notebook 使用 MathJax 来渲染 LATEX。

If LATEX is not installed, but Matplotlib is installed, it will use the Matplotlib rendering engine. If Matplotlib is not installed, it uses the Unicode pretty printer. However, Jupyter notebook uses MathJax to render LATEX.

在不支持 Unicode 的终端中,将使用 ASCII 漂亮打印机。

In a terminal that does not support Unicode, ASCII pretty printer is used.

ascii pretty printer

要使用 ASCII 打印机,请使用将 use_unicode 属性设置为 False 的 pprint() 函数

To use ASCII printer use pprint() function with use_unicode property set to False

>>> pprint(Integral(sqrt(1/x),x),use_unicode=False)
unicode pretty printer

Unicode 漂亮打印机也可从 pprint() 和 pretty() 访问。如果终端支持 Unicode,则会自动使用它。如果 pprint() 无法检测到终端是否支持 Unicode,则可以传递 use_unicode=True 以强制其使用 Unicode。

The Unicode pretty printer is also accessed from pprint() and pretty(). If the terminal supports Unicode, it is used automatically. If pprint() is not able to detect that the terminal supports unicode, you can pass use_unicode=True to force it to use Unicode.

要获取表达式的 LATEX 形式,请使用 latex() 函数。

To get the LATEX form of an expression, use latex() function.

>>> print(latex(Integral(sqrt(1/x),x)))

\int \sqrt{\frac{1}{x}}\, dx

\int \sqrt{\frac{1}{x}}\, dx

你还可以使用 mathml 打印机。为此,导入 print_mathml 函数。可以通过 mathml() 函数获取字符串版本。

You can also use mathml printer. for that purpose, import print_mathml function. A string version is obtained by mathml() function.

>>> from sympy.printing.mathml import print_mathml
>>> print_mathml(Integral(sqrt(1/x),x))

<apply>

<apply>

<int/>

<int/>

<bvar>

<bvar>

<ci>x</ci>

<ci>x</ci>

</bvar>

</bvar>

<apply>

<apply>

<root/>

<root/>

<apply>

<apply>

<power/>

<power/>

<ci>x</ci>

<ci>x</ci>

<cn>-1</cn>

<cn>-1</cn>

</apply>

</apply>

</apply>

</apply>

</apply>

</apply>

>>>mathml(Integral(sqrt(1/x),x))

'<apply><int/><bvar><ci>x</ci></bvar><apply><root/><apply><power/><ci>x</ci><cn>-1</cn></apply></apply></apply>'

'<apply><int/><bvar><ci>x</ci></bvar><apply><root/><apply><power/><ci>x</ci><cn>-1</cn></apply></apply></apply>'