Python 使用mpmath实现numpy.allclose和多精度
在python代码中,我定期使用Python 使用mpmath实现numpy.allclose和多精度,python,numpy,arbitrary-precision,mpmath,Python,Numpy,Arbitrary Precision,Mpmath,在python代码中,我定期使用numpy.allclose验证一些计算。另一方面,除了这些检查之外,实现还能够处理多精度(mpmath.mpc)数字。如果我想为mpmath数字运行验证代码,我会得到如下结果 >>> check(H) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "module.py", line 19, in check_H
numpy.allclose
验证一些计算。另一方面,除了这些检查之外,实现还能够处理多精度(mpmath.mpc
)数字。如果我想为mpmath
数字运行验证代码,我会得到如下结果
>>> check(H)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "module.py", line 19, in check_H
assert(numpy.allclose(H, I, rtol=rtol, atol=atol))
File "/home/gereon/python/numpy/core/numeric.py", line 2025, in allclose
xinf = isinf(x)
TypeError: ufunc 'isinf' not supported for the input types, and the inputs could
not be safely coerced to any supported types according to the casting rule ''safe''
>检查(H)
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“module.py”,第19行,检查
断言(numpy.allclose(H,I,rtol=rtol,atol=atol))
文件“/home/gereon/python/numpy/core/numeric.py”,第2025行,全部关闭
xinf=isinf(x)
TypeError:输入类型不支持ufunc“isinf”,并且输入可能会丢失
不能根据强制转换规则“安全”安全地强制转换为任何受支持的类型
检查两个多精度数组是否足够相等的最佳方法是什么?我查看了代码(allclose
在numeric.py中)。它取决于isinf
函数,这显然不是为mpmath实现的
虽然函数很简单。归结起来是:
def allclose(x, y, rtol=1.e-5, atol=1.e-8):
return all(less_equal(abs(x-y), atol + rtol * abs(y)))
您可能需要将rtol
和atol
替换为mpmath等价物,而不是浮点数。我查看了代码(allclose
在numeric.py中)。它取决于isinf
函数,这显然不是为mpmath实现的
虽然函数很简单。归结起来是:
def allclose(x, y, rtol=1.e-5, atol=1.e-8):
return all(less_equal(abs(x-y), atol + rtol * abs(y)))
您可能需要将rtol
和atol
替换为mpmath等价物,而不是浮点