Question

In my python code, I regularly verify some calculations using numpy.allclose. On the other hand, apart from these checks the implementation is able to deal with multiprecision (mpmath.mpc) numbers. If I want to run my verification code for the mpmath numbers, I get something like

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

What is the best way to check if two multiprecision arrays are sufficiently equal?

Était-ce utile?

La solution

I looked through the code (allclose in numeric.py). It depends on the isinf function, which is apparently not implemented for mpmath.

The function is simple enough though. It boils down to:

def allclose(x, y, rtol=1.e-5, atol=1.e-8):
    return all(less_equal(abs(x-y), atol + rtol * abs(y)))

You may have to replace rtol and atol with mpmath equivalents instead of floats.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top