MCPcopy Index your code
hub / github.com/numpy/numpy / test_ufunc_noncontiguous

Function test_ufunc_noncontiguous

numpy/_core/tests/test_ufunc.py:2895–2941  ·  view source on GitHub ↗

Check that contiguous and non-contiguous calls to ufuncs have the same results for values in range(9)

(ufunc)

Source from the content-addressed store, hash-verified

2893@pytest.mark.parametrize('ufunc', [getattr(np, x) for x in dir(np)
2894 if isinstance(getattr(np, x), np.ufunc)])
2895def test_ufunc_noncontiguous(ufunc):
2896 '''
2897 Check that contiguous and non-contiguous calls to ufuncs
2898 have the same results for values in range(9)
2899 '''
2900 for typ in ufunc.types:
2901 # types is a list of strings like ii->i
2902 if any(set('O?mM') & set(typ)):
2903 # bool, object, datetime are too irregular for this simple test
2904 continue
2905 inp, out = typ.split('->')
2906 args_c = [np.empty((6, 6), t) for t in inp]
2907 # non contiguous (2, 3 step on the two dimensions)
2908 args_n = [np.empty((12, 18), t)[::2, ::3] for t in inp]
2909 # alignment != itemsize is possible. So create an array with such
2910 # an odd step manually.
2911 args_o = []
2912 for t in inp:
2913 orig_dt = np.dtype(t)
2914 off_dt = f"S{orig_dt.alignment}" # offset by alignment
2915 dtype = np.dtype([("_", off_dt), ("t", orig_dt)], align=False)
2916 args_o.append(np.empty((6, 6), dtype=dtype)["t"])
2917 for a in args_c + args_n + args_o:
2918 a.flat = range(1, 37)
2919
2920 with warnings.catch_warnings(record=True):
2921 warnings.filterwarnings("always")
2922 res_c = ufunc(*args_c)
2923 res_n = ufunc(*args_n)
2924 res_o = ufunc(*args_o)
2925 if len(out) == 1:
2926 res_c = (res_c,)
2927 res_n = (res_n,)
2928 res_o = (res_o,)
2929 for c_ar, n_ar, o_ar in zip(res_c, res_n, res_o):
2930 dt = c_ar.dtype
2931 if np.issubdtype(dt, np.floating):
2932 # for floating point results allow a small fuss in comparisons
2933 # since different algorithms (libm vs. intrinsics) can be used
2934 # for different input strides
2935 res_eps = np.finfo(dt).eps
2936 tol = 3 * res_eps
2937 assert_allclose(res_c, res_n, atol=tol, rtol=tol)
2938 assert_allclose(res_c, res_o, atol=tol, rtol=tol)
2939 else:
2940 assert_equal(c_ar, n_ar)
2941 assert_equal(c_ar, o_ar)
2942
2943
2944@pytest.mark.parametrize('ufunc', [np.sign, np.equal])

Callers

nothing calls this directly

Calls 5

assert_allcloseFunction · 0.90
assert_equalFunction · 0.90
anyFunction · 0.85
splitMethod · 0.80
dtypeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…