sympy怎么看运行过程
时间: 2023-08-10 12:03:24 浏览: 145
如果你想在运行 sympy 时查看其运行过程,可以通过以下两种方法之一来实现:
1. 使用 Python 的 `logging` 模块
你可以使用 Python 自带的 `logging` 模块来记录 sympy 的运行过程。首先,你需要在代码中导入 `logging` 模块:
```python
import logging
```
然后,你需要配置 `logging` 模块的输出级别,设置为 `DEBUG` 级别,以便记录所有详细信息:
```python
logging.basicConfig(level=logging.DEBUG)
```
最后,你可以在代码中使用 `logging.debug()` 函数来记录 sympy 的运行过程:
```python
from sympy import *
import logging
logging.basicConfig(level=logging.DEBUG)
x = symbols('x')
expr = x**2 + 2*x + 1
logging.debug(f"expr = {expr}")
```
在运行以上代码时,你会看到类似下面的输出:
```
DEBUG:root:expr = x**2 + 2*x + 1
```
这就是 `logging` 模块记录的 sympy 运行过程。
2. 使用 IPython 的 `%debug` 魔法命令
如果你在 IPython 中运行 sympy,你可以使用 `%debug` 魔法命令来查看 sympy 的运行过程。当代码发生错误时,你可以在错误信息中输入 `%debug` 命令,进入调试模式,查看代码的运行过程。
例如,假设你有以下代码:
```python
from sympy import *
x = symbols('x')
expr = x**2 + 2*x + 1
result = solve(expr, x)
```
如果在运行 `solve()` 函数时发生错误,你会看到类似下面的错误信息:
```
Traceback (most recent call last):
File "<ipython-input-1-5aef1d3e8baf>", line 4, in <module>
result = solve(expr, x)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1046, in solve
solution = _solve(f[0], *symbols, **flags)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1595, in _solve
soln = checksol(f_num, symbol, soln, solve_for_symbol)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1948, in checksol
if not checksol_all(sol, f_num):
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1931, in checksol_all
return all(checksol(sol, eq) for eq in f)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1931, in <genexpr>
return all(checksol(sol, eq) for eq in f)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1953, in checksol
if not checksol_any(sol, eq):
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1912, in checksol_any
return any(checksol(sol, eq) for eq in eqs)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1912, in <genexpr>
return any(checksol(sol, eq) for eq in eqs)
File "/usr/local/lib/python3.7/site-packages/sympy/solvers/solvers.py", line 1972, in checksol
return eq.subs(sol) == S.Zero
File "/usr/local/lib/python3.7/site-packages/sympy/core/basic.py", line 1091, in subs
rv = self._subs(old, new, **hints)
File "/usr/local/lib/python3.7/site-packages/sympy/core/basic.py", line 1245, in _subs
return self.func(*args)
File "/usr/local/lib/python3.7/site-packages/sympy/core/expr.py", line 291, in __new__
return cls._from_args(cls, args)
File "/usr/local/lib/python3.7/site-packages/sympy/core/expr.py", line 194, in _from_args
return cls._from_args_join(args)
File "/usr/local/lib/python3.7/site-packages/sympy/core/mul.py", line 595, in _from_args_join
con, short = split_mul(args)
File "/usr/local/lib/python3.7/site-packages/sympy/core/mul.py", line 188, in split_mul
for a in args:
TypeError: 'Symbol' object is not iterable
```
在错误信息中输入 `%debug` 命令,你会进入调试模式,可以查看代码的运行过程:
```
> /usr/local/lib/python3.7/site-packages/sympy/core/mul.py(188)split_mul()
186 con, short = con, []
187 else:
--> 188 for a in args:
189 if a.is_commutative:
190 con.append(a)
ipdb> args
(Symbol('x'),)
ipdb> p type(args[0])
<class 'sympy.core.symbol.Symbol'>
ipdb> c
```
在调试模式中,你可以使用 `args` 命令来查看当前函数的参数,使用 `p` 命令来打印变量的值,使用 `c` 命令来继续执行代码。
阅读全文