在我的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
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''检查两个multiprecision数组是否足够相等的最佳方法是什么?
发布于 2012-07-24 14:07:46
我看了一下代码(numeric.py中的allclose)。它依赖于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等效项,而不是浮点数。
https://stackoverflow.com/questions/11620239
复制相似问题