0

在我的 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''

检查两个多精度数组是否足够相等的最佳方法是什么?

4

1 回答 1

3

我查看了代码(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)))

您可能必须用 mpmath 等效项而不是浮点数替换rtoland 。atol

于 2012-07-24T06:07:46.497 回答