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)
| 1783 | |
| 1784 | |
| 1785 | def assert_array_almost_equal_nulp(x, y, nulp=1): |
| 1786 | """ |
| 1787 | Compare two arrays relatively to their spacing. |
| 1788 | |
| 1789 | This is a relatively robust method to compare two arrays whose amplitude |
| 1790 | is variable. |
| 1791 | |
| 1792 | Parameters |
| 1793 | ---------- |
| 1794 | x, y : array_like |
| 1795 | Input arrays. |
| 1796 | nulp : int, optional |
| 1797 | The maximum number of unit in the last place for tolerance (see Notes). |
| 1798 | Default is 1. |
| 1799 | |
| 1800 | Returns |
| 1801 | ------- |
| 1802 | None |
| 1803 | |
| 1804 | Raises |
| 1805 | ------ |
| 1806 | AssertionError |
| 1807 | If the spacing between `x` and `y` for one or more elements is larger |
| 1808 | than `nulp`. |
| 1809 | |
| 1810 | See Also |
| 1811 | -------- |
| 1812 | assert_array_max_ulp : Check that all items of arrays differ in at most |
| 1813 | N Units in the Last Place. |
| 1814 | spacing : Return the distance between x and the nearest adjacent number. |
| 1815 | |
| 1816 | Notes |
| 1817 | ----- |
| 1818 | An assertion is raised if the following condition is not met:: |
| 1819 | |
| 1820 | abs(x - y) <= nulp * spacing(maximum(abs(x), abs(y))) |
| 1821 | |
| 1822 | Examples |
| 1823 | -------- |
| 1824 | >>> x = np.array([1., 1e-10, 1e-20]) |
| 1825 | >>> eps = np.finfo(x.dtype).eps |
| 1826 | >>> np.testing.assert_array_almost_equal_nulp(x, x*eps/2 + x) |
| 1827 | |
| 1828 | >>> np.testing.assert_array_almost_equal_nulp(x, x*eps + x) |
| 1829 | Traceback (most recent call last): |
| 1830 | ... |
| 1831 | AssertionError: Arrays are not equal to 1 ULP (max is 2) |
| 1832 | |
| 1833 | """ |
| 1834 | __tracebackhide__ = True # Hide traceback for py.test |
| 1835 | import numpy as np |
| 1836 | ax = np.abs(x) |
| 1837 | ay = np.abs(y) |
| 1838 | ref = nulp * np.spacing(np.where(ax > ay, ax, ay)) |
| 1839 | if not np.all(np.abs(x - y) <= ref): |
| 1840 | if np.iscomplexobj(x) or np.iscomplexobj(y): |
| 1841 | msg = f"Arrays are not equal to {nulp} ULP" |
| 1842 | else: |
searching dependent graphs…