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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20979993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:41:28  来源:igfitidea点击:

How to pretty print in ipython notebook via sympy?

pythonjupyter-notebookjupytersympypretty-print

提问by colinfang

I tried pprint, print, the former only prints Unicode version, and the latter doesn't do pretty prints.

我试过pprint, print, 前者只打印 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)

采纳答案by Matt

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+) 中默认导入显示,但我们仍然建议显式导入它。

回答by Kyle

The issue is with your init_printing statement. In a notebook, you do not want to run latex, instead you should use mathjax, so try this instead:

问题在于您的 init_printing 语句。在笔记本中,您不想运行 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 表达式作为单元格的最后一行,我也会在任何地方得到正常的漂亮打印。

回答by acesaif

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.

自己试试吧。

回答by sancho.s ReinstateMonicaCellio

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)

  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 out together the mathematical expression. But it can, e.g., raise numbers for powers, pull fractions by laying out the horizontal line, etc.

  3. 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.

  1. “不漂亮”,纯文字……“低质量”。这就是一个人获得的print(expression)

  2. 漂亮、纯正的文字……“中等质量”。这就是一个人获得的

    import sympy as sym
    sympy.pprint(expression)
    

    它仍然使用相同的字体并且只使用字符来组合数学表达式。但它可以,例如,增加幂的数字,通过布置水平线来提取分数等。

  3. 漂亮,带有图形、符号等......“高品质”。这就是一个人获得的

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

    这与作为笔记本单元的输出获得的相同,但现在作为命令的结果。然后,可以从单个笔记本单元获得多个这样的输出

It is worth noting that:

值得一提的是:

  1. sym.init_printing(...affects the output of sym.pprint.

  2. sym.latex(expression)produces a LaTeX string for the expression. 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).

  1. sym.init_printing(...影响输出sym.pprint

  2. sym.latex(expression)为表达式生成一个 LaTeX 字符串。 disp.Math(...从 LaTeX 生成表达式。这两个可能有用。因此,disp.display(disp.Math(sym.latex(expression)))将产生与 相同的输出 disp.display(expression)