使用四元组 (scipy.integrate) 和小数
Using quad (scipy.integrate) and decimals
我需要集成一个非常精确的函数,例如:
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 作为我的集成工具,但不幸的是它似乎将输出转换为浮点数。我希望积分的输出具有更高的精度。我该如何实现?
我会考虑使用 mpmath library. It supports high precision integration (http://mpmath.org/doc/0.19/calculus/integration.html)。
我需要集成一个非常精确的函数,例如:
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 作为我的集成工具,但不幸的是它似乎将输出转换为浮点数。我希望积分的输出具有更高的精度。我该如何实现?
我会考虑使用 mpmath library. It supports high precision integration (http://mpmath.org/doc/0.19/calculus/integration.html)。