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''

2つの多倍長配列が十分に等しいかどうかを確認する最良の方法は何ですか?

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)))

floatの代わりに同等のmpmathrtolを置き換える必要がある場合があります。atol

于 2012-07-24T06:07:46.497 に答える