在ipython笔记本中使用sympy的科学/指数表示法 [英] Scientific/Exponential notation with sympy in an ipython notebook

查看:285
本文介绍了在ipython笔记本中使用sympy的科学/指数表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ipython笔记本中设置科学记数法的门槛如何?我想把所有编号为[0.01,100]的数字设置为以科学记数法打印,但是阈值似乎要高得多。即:

  In [165]:x = sympy.Symbol('x'); 1.e8 * x 
Out [165]:100000000.0 * x

  In [166]:1.e28 * x 
出[166]:1.0e + 28 * x

因为我使用sympy,所以我不能将数字发送到格式化的打印语句,

  In [167]:print%.2e%(1.e8 * x)
ERROR:TypeError:float argument required ,而不是Mul [IPython.core.interactiveshell]


解决方案

相信它取决于底层Float对象的精度。

SymPy使用mpmath来提供任意精度浮点数。默认的精度是15位,但是你可以设置任何精度。它看起来像使用科学记数法时,它不能用给定的精度代表整个数字。你可以强制直接构造Float对象来使用给定的精度:



$ p $ 在[30]中:Float('100' ,3)
Out [30]:100.

在[31]:Float('1000',3)
Out [31]:1.00e + 3

但是,请注意,这也会影响其他内容。

  In [32]:Float('1.23456',3)
Out [32]:1.23

如果你不想在

 在[45]部分中,您可以传入设置的字符串打印机: :from sympy.printing.str import strPrinter 

在[44]:StrPrinter({'full_prec':False})。doprint(Float('10000000',5))
Out [ 44]:'1.0e + 7'

这改变了字符串打印机, sstr print 。它看起来像LaTeX打印机,这是你可能想要在笔记本电脑中使用,自动执行此操作。

我们应该添加一个更简单的方法来设置它,而不会改变精度。这些选项在mpmath中,只是在SymPy打印机中不显示。我已打开 https://github.com/sympy/sympy/issues/7847 为了这。

How is the threshold for scientific notation set in an ipython notebook? I want to set all number outisde of something like [0.01, 100] to be printed in scientific notation, but the threshold seems to be much higher. i.e.

In [165]:  x = sympy.Symbol('x'); 1.e8*x
Out[165]:  100000000.0*x

but

In [166]:  1.e28*x
Out[166]:  1.0e+28*x

Because I'm using sympy, I can't send the number to a formatted print statement, i.e.

In [167]: print "%.2e" % (1.e8*x)
ERROR: TypeError: float argument required, not Mul [IPython.core.interactiveshell]

解决方案

I believe it depends on the precision of the underlying Float object.

SymPy uses mpmath to provide arbitrary precision floating point numbers. The default precision is 15 digits, but you can set any precision. It looks like it uses scientific notation when it can't represent the whole number using the given precision. You can force a given precision to be used by constructing the Float object directly:

In [30]: Float('100', 3)
Out[30]: 100.

In [31]: Float('1000', 3)
Out[31]: 1.00e+3

However, note that this will affect other things as well. All digits past three will be rounded.

In [32]: Float('1.23456', 3)
Out[32]: 1.23

If you don't want the extra zeros before the e+ part, you can pass in a setting the the string printer:

In [45]: from sympy.printing.str import StrPrinter

In [44]: StrPrinter({'full_prec': False}).doprint(Float('10000000', 5))
Out[44]: '1.0e+7'

This changes the string printer, which is what is used by sstr or print. It looks like the LaTeX printer, which is what you probably want to use in the notebook, does this automatically.

We should add an easier way to set this, without changing the precision. The options are there in mpmath, they just aren't exposed in the SymPy printers. I've opened https://github.com/sympy/sympy/issues/7847 for this.

这篇关于在ipython笔记本中使用sympy的科学/指数表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆