简体   繁体   English

如何通过 sympy 在 ipython notebook 中漂亮地打印?

[英]How to pretty print in ipython notebook via sympy?

I tried pprint , print , the former only prints Unicode version, and the latter doesn't do pretty prints.我试过pprintprint ,前者只打印 Unicode 版本,后者不做漂亮的打印。

from sympy import symbols, Function
import sympy.functions as sym
from sympy import init_printing
init_printing(use_latex=True)
from sympy import pprint
from sympy import Symbol

x = Symbol('x')

# If a cell contains only the following, it will render perfectly.
(pi + x)**2

# However I would like to control what to print in a function, 
# so that multiple expressions can be printed from a single notebook cell.
pprint((pi + x)**2)

you need to use display:你需要使用显示:

from IPython.display import display

display(yourobject)

It will choose the appropriate representation (text/LaTex/png...), in recent enough version of IPython (6.0+) display is imported by default, still we recommend to explicitly import it.它将选择适当的表示(text/LaTex/png...),在最近的足够版本的 IPython (6.0+) 中默认导入显示,但我们仍然建议显式导入它。

The issue is with your init_printing statement.问题在于您的 init_printing 语句。 In a notebook, you do not want to run latex, instead you should use mathjax, so try this instead:在笔记本中,您不想运行 latex,而应该使用 mathjax,因此请尝试以下操作:

init_printing(use_latex='mathjax')

When I use this, I get normal pretty printing everywhere, even when I have a sympy expression as the last line of the cell.当我使用它时,即使我有一个 sympy 表达式作为单元格的最后一行,我也会在任何地方得到正常的漂亮打印。

This works,这有效,

from IPython.display import display, Latex
from sympy import *

x = symbols('x')
display(x)

int_x = Integral(cos(x)*exp(x), x)
result = "$${} = {}$$".format(latex(int_x), latex(int_x.doit()))
display(Latex(result))

derv_x = Derivative(cos(x)*exp(x), x)
result = "$${} = {}$$".format(latex(derv_x), latex(derv_x.doit()))
display(Latex(result))

try it for yourself.自己试试吧。

I have asked a similar question (now linked to this one).我问过一个类似的问题(现在链接到这个问题)。 After reading the answers and tinkering a bit, I conclude that there are 3 types of output one can get:阅读答案并稍加修改后,我得出结论,可以得到 3 种类型的输出:

  1. "Not pretty", pure text... "low quality". “不漂亮”,纯文字……“低质量”。 This is what one obtains with print(expression)这是用print(expression)

  2. Pretty, pure text... "medium quality".漂亮、纯正的文字……“中等质量”。 This is what one obtains with这就是一个人获得的

     import sympy as sym sympy.pprint(expression)

It still uses the same font and uses only characters to put together the mathematical expression.它仍然使用相同的字体并且仅使用字符来组合数学表达式。 But it can, eg, raise numbers for powers, pull fractions by laying out the horizontal line, etc.但它可以,例如,增加幂的数字,通过布置水平线来提取分数等。

  1. Pretty, with graphics, symbols, etc... "high quality".漂亮,带有图形、符号等......“高品质”。 This is what one obtains with这就是一个人获得的

     import IPython.display as disp disp.display(expression)

This is the same as what one obtains as an output of the notebook cell, but now as a result of a command.这与作为笔记本单元的输出获得的相同,但现在作为命令的结果。 Then, one can have multiple such outputs from a single notebook cell .然后,可以从单个笔记本单元获得多个这样的输出

It is worth noting that:值得注意的是:

  1. sym.init_printing(... affects the output of sym.pprint . sym.init_printing(...影响sym.pprint的输出。

  2. sym.latex(expression) produces a LaTeX string for the expression. sym.latex(expression)为表达式生成一个 LaTeX 字符串。 disp.Math(... produces the expression from LaTeX. These two may come in useful. Thus, disp.display(disp.Math(sym.latex(expression))) would produce the same output as disp.display(expression) . disp.Math(...从 LaTeX 产生表达式。这两个可能有用。因此, disp.display(disp.Math(sym.latex(expression)))将产生与disp.display(expression)相同的输出.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM