SymPy符号Python库演示:符号求解数学和积分以及漂亮的输出
来自@Autoplectic:
sympy正是你所需要的。
虽然我有时倾向于在Stack Overflow上写一些最长的答案,但这是我在Stack Overflow上看到的最短的答案。
让我们添加一个基本演示。
您需要的参考和教程:
- https://www.sympy.org/zh/index.html --> 点击“开始教程”
- 教程索引:https://docs.sympy.org/latest/tutorial/index.html 示例页面:
- 基本操作
- 打印
- 简化
- 微积分
我想出的微积分演示(参见此处以查看我用来入门的主要教程):
简短版:
from sympy import *
x, y, z = symbols('x y z')
init_printing(use_unicode=True)
integral = Integral(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
print(pretty(integral))
result = integral.doit()
print(pretty(result))
更长的版本:
eRCaGuy_hello_world/math/sympy_integral_and_printing.py:
from sympy import *
x, y, z = symbols('x y z')
init_printing(use_unicode=True)
integral = Integral(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
print("ORIGINAL INTEGRAL:")
print(pretty(integral))
pprint(integral)
print(pretty(integral, use_unicode=False))
print(integral)
print(latex(integral))
result = integral.doit()
print("\nRESULT:")
print(pretty(result))
pprint(result)
print(pretty(result, use_unicode=False))
print(result)
print(latex(result))
请注意,使用
pprint(integral)
与
print(pretty(integral))
相同。
运行上述命令的输出:
ORIGINAL INTEGRAL:
∞ ∞
⌠ ⌠
⎮ ⎮ 2 2
⎮ ⎮ - x - y
⎮ ⎮ ℯ dx dy
⌡ ⌡
-∞ -∞
∞ ∞
⌠ ⌠
⎮ ⎮ 2 2
⎮ ⎮ - x - y
⎮ ⎮ ℯ dx dy
⌡ ⌡
-∞ -∞
oo oo
/ /
| |
| | 2 2
| | - x - y
| | e dx dy
| |
/ /
-oo -oo
Integral(exp(-x**2 - y**2), (x, -oo, oo), (y, -oo, oo))
\int\limits_{-\infty}^{\infty}\int\limits_{-\infty}^{\infty} e^{- x^{2} - y^{2}}\, dx\, dy
RESULT:
π
π
pi
pi
\pi
Python中的SymPy符号数学库可以处理几乎任何类型的数学问题,例如解方程、简化、因式分解、替换变量值、漂亮的打印输出、转换为LaTeX格式等等。在我非常有限的使用中,它似乎是一个相当强大的求解器。我建议尝试一下。
对于我来说(在Linux Ubuntu上测试),安装它就像这样简单:
pip3 install sympy