【问题标题】:Using quad (scipy.integrate) and decimals使用四边形(scipy.integrate)和小数
【发布时间】:2015-12-11 02:11:13
【问题描述】:

我需要非常精确地集成一个函数,例如:

from decimal import * 
from scipy.integrate import quad
getcontext().prec = 30
a = Decimal('1.1') + Decimal('1.2') 
print type(a)

def f(X):
    return quad(lambda x: X, 0, 1)[0]

b = f(a)
print "{0:.30f}".format(b), type(b)

我得到了

<class 'decimal.Decimal'>
2.300000000000000266453525910038 <type 'float'>

我想使用 quad 作为我的集成工具,但不幸的是,它似乎将输出转换为浮点数。我希望积分的输出具有更高的精度。我如何做到这一点?

【问题讨论】:

  • 使用scipy.integrate.quad,你不能。 quad 是 QUADPACK 的封装,这是一种使用双精度浮点编写的 Fortran 代码。

标签: python scipy decimal


【解决方案1】:

我会考虑使用mpmath 库。支持高精度集成(http://mpmath.org/doc/0.19/calculus/integration.html)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多