初始化

init_printing()会为你启用当前环境下可用的最佳的输出效果。

1
2
from sympy import init_printing
init_printing()

如果你使用交互式命令行,init_session() 函数将自动导入SymPy中的所有内容,创建一些常用符号,设置绘图,并运行 init_printing()

1
2
3
4
5
6
7
8
9
10
11
12
13
>>> from sympy import init_session
>>> init_session()
Python console for SymPy 0.7.3 (Python 2.7.5-64-bit) (ground types: gmpy)

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() # doctest: +SKIP

Documentation can be found at https://www.sympy.org/

用Jupyter美化输出

但是终端的输出效果实在是有点丑

这是设置了init_printing(use_unicode=False),不允许使用Unicode字符的输出

1
2
3
4
5
6
7
8
9
>>> Integral(sqrt(1/x), x)
/
|
| ___
| / 1
| / - dx
| \/ x
|
/

这是设置了init_printing(use_unicode=True),允许使用Unicode字符的输出

1
2
3
4
5
6
7
>>> Integral(sqrt(1/x), x)

⎮ ___
⎮ ╱ 1
⎮ ╱ ─ dx
⎮ ╲╱ x

确保你安装好了Jupyter(推荐直接使用Anaconda一次搞定)

这是Jupyter notebook里的输出,使用了MathML渲染的LaTeX\LaTeX

输出了很漂亮的公式,以后都可以这样使用。

本文属于系列文章:SymPy学习笔记