MCPcopy Create free account
hub / github.com/numpy/numpy / assert_array_almost_equal_nulp

Function assert_array_almost_equal_nulp

numpy/testing/_private/utils.py:1508–1568  ·  view source on GitHub ↗

Compare two arrays relatively to their spacing. This is a relatively robust method to compare two arrays whose amplitude is variable. Parameters ---------- x, y : array_like Input arrays. nulp : int, optional The maximum number of unit in the last place

(x, y, nulp=1)

Source from the content-addressed store, hash-verified

1506
1507
1508def assert_array_almost_equal_nulp(x, y, nulp=1):
1509 """
1510 Compare two arrays relatively to their spacing.
1511
1512 This is a relatively robust method to compare two arrays whose amplitude
1513 is variable.
1514
1515 Parameters
1516 ----------
1517 x, y : array_like
1518 Input arrays.
1519 nulp : int, optional
1520 The maximum number of unit in the last place for tolerance (see Notes).
1521 Default is 1.
1522
1523 Returns
1524 -------
1525 None
1526
1527 Raises
1528 ------
1529 AssertionError
1530 If the spacing between `x` and `y` for one or more elements is larger
1531 than `nulp`.
1532
1533 See Also
1534 --------
1535 assert_array_max_ulp : Check that all items of arrays differ in at most
1536 N Units in the Last Place.
1537 spacing : Return the distance between x and the nearest adjacent number.
1538
1539 Notes
1540 -----
1541 An assertion is raised if the following condition is not met::
1542
1543 abs(x - y) <= nulp * spacing(maximum(abs(x), abs(y)))
1544
1545 Examples
1546 --------
1547 >>> x = np.array([1., 1e-10, 1e-20])
1548 >>> eps = np.finfo(x.dtype).eps
1549 >>> np.testing.assert_array_almost_equal_nulp(x, x*eps/2 + x)
1550
1551 >>> np.testing.assert_array_almost_equal_nulp(x, x*eps + x)
1552 Traceback (most recent call last):
1553 ...
1554 AssertionError: X and Y are not equal to 1 ULP (max is 2)
1555
1556 """
1557 __tracebackhide__ = True # Hide traceback for py.test
1558 import numpy as np
1559 ax = np.abs(x)
1560 ay = np.abs(y)
1561 ref = nulp * np.spacing(np.where(ax > ay, ax, ay))
1562 if not np.all(np.abs(x-y) <= ref):
1563 if np.iscomplexobj(x) or np.iscomplexobj(y):
1564 msg = "X and Y are not equal to %d ULP" % nulp
1565 else:

Callers 8

test_log_stridesMethod · 0.90
test_exp_stridesMethod · 0.90
test_strided_float32Method · 0.90
test_float64_passMethod · 0.90
test_float32_passMethod · 0.90
test_float16_passMethod · 0.90
test_complex128_passMethod · 0.90
test_complex64_passMethod · 0.90

Calls 3

nulp_diffFunction · 0.85
allMethod · 0.45
maxMethod · 0.45

Tested by 8

test_log_stridesMethod · 0.72
test_exp_stridesMethod · 0.72
test_strided_float32Method · 0.72
test_float64_passMethod · 0.72
test_float32_passMethod · 0.72
test_float16_passMethod · 0.72
test_complex128_passMethod · 0.72
test_complex64_passMethod · 0.72