summaryrefslogtreecommitdiff
path: root/numpy/testing/utils.py
diff options
context:
space:
mode:
authorDavid Cournapeau <cournape@gmail.com>2009-07-28 05:52:36 +0000
committerDavid Cournapeau <cournape@gmail.com>2009-07-28 05:52:36 +0000
commit3feb77ee4e46962b6c9cfab2a37a0c1ef1ceda82 (patch)
treef57893dc010d8037213951925c44cbb39427dc50 /numpy/testing/utils.py
parent5b2699cf291a8f5f8fd90a9b910937cad0e8d043 (diff)
downloadnumpy-3feb77ee4e46962b6c9cfab2a37a0c1ef1ceda82.tar.gz
BUG: fix nan/inf handling for complex dtypes.
Diffstat (limited to 'numpy/testing/utils.py')
-rw-r--r--numpy/testing/utils.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/numpy/testing/utils.py b/numpy/testing/utils.py
index 2fc8729a6..4e82fd510 100644
--- a/numpy/testing/utils.py
+++ b/numpy/testing/utils.py
@@ -351,6 +351,37 @@ def assert_almost_equal(actual,desired,decimal=7,err_msg='',verbose=True):
"""
from numpy.core import ndarray
+ from numpy.lib import iscomplexobj, real, imag
+
+ # Handle complex numbers: separate into real/imag to handle
+ # nan/inf/negative zero correctly
+ # XXX: catch ValueError for subclasses of ndarray where iscomplex fail
+ try:
+ usecomplex = iscomplexobj(actual) or iscomplexobj(desired)
+ except ValueError:
+ usecomplex = False
+
+ if usecomplex:
+ if iscomplexobj(actual):
+ actualr = real(actual)
+ actuali = imag(actual)
+ else:
+ actualr = actual
+ actuali = 0
+ if iscomplexobj(desired):
+ desiredr = real(desired)
+ desiredi = imag(desired)
+ else:
+ desiredr = desired
+ desiredi = 0
+ try:
+ assert_almost_equal(actualr, desiredr)
+ assert_almost_equal(actuali, desiredi)
+ except AssertionError:
+ raise AssertionError("Items are not equal:\n" \
+ "ACTUAL: %s\n" \
+ "DESIRED: %s\n" % (str(actual), str(desired)))
+
if isinstance(actual, ndarray) or isinstance(desired, ndarray):
return assert_array_almost_equal(actual, desired, decimal, err_msg)
msg = build_err_msg([actual, desired], err_msg, verbose=verbose,