如何漂亮地打印到 sympy 中的文件?
How to pretty print to a file in sympy?
假设我有以下代码:
import sympy as sp
from sympy.physics.quantum import TensorProduct
s=sp.eye(2)
a=TensorProduct(s*x,TensorProduct(s,s)).subs(x,x**2+2*x+1)
sp.pprint(a)
代码将生成宽度有限的输出(我讨厌):
我的问题是:
- 为什么我的 window 有足够的 space 而有宽度限制,如何更改?
- 如何将这样的输出打印到文件中?
对于python>=3.4
,
from contextlib import redirect_stdout
import sympy as sp
from sympy.physics.quantum import TensorProduct
s = sp.eye(2)
x = sp.symbols('x')
a = TensorProduct(s*x, TensorProduct(s, s)).subs(x, x**2+2*x+1)
with open('data.txt', 'w') as f:
with redirect_stdout(f):
sp.pprint(a, wrap_line=False)
.
sp.preview(a, viewer='file', filename="out.png", dvioptions=['-D','300'])
将允许您将表达式保存为 LaTeX/png/pdf。
假设我有以下代码:
import sympy as sp
from sympy.physics.quantum import TensorProduct
s=sp.eye(2)
a=TensorProduct(s*x,TensorProduct(s,s)).subs(x,x**2+2*x+1)
sp.pprint(a)
代码将生成宽度有限的输出(我讨厌):
我的问题是:
- 为什么我的 window 有足够的 space 而有宽度限制,如何更改?
- 如何将这样的输出打印到文件中?
对于python>=3.4
,
from contextlib import redirect_stdout
import sympy as sp
from sympy.physics.quantum import TensorProduct
s = sp.eye(2)
x = sp.symbols('x')
a = TensorProduct(s*x, TensorProduct(s, s)).subs(x, x**2+2*x+1)
with open('data.txt', 'w') as f:
with redirect_stdout(f):
sp.pprint(a, wrap_line=False)
.
sp.preview(a, viewer='file', filename="out.png", dvioptions=['-D','300'])
将允许您将表达式保存为 LaTeX/png/pdf。