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

Function test_iter_buffering_reduction

numpy/_core/tests/test_nditer.py:2876–2940  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

2874 assert_equal(it2.operands[1].sum(), a.size)
2875
2876def test_iter_buffering_reduction():
2877 # Test doing buffered reductions with the iterator
2878
2879 a = np.arange(6)
2880 b = np.array(0., dtype='f8').byteswap()
2881 b = b.view(b.dtype.newbyteorder())
2882 i = nditer([a, b], ['reduce_ok', 'buffered'],
2883 [['readonly'], ['readwrite', 'nbo']],
2884 op_axes=[[0], [-1]])
2885 with i:
2886 assert_equal(i[1].dtype, np.dtype('f8'))
2887 assert_(i[1].dtype != b.dtype)
2888 # Do the reduction
2889 for x, y in i:
2890 y[...] += x
2891 # Since no axes were specified, should have allocated a scalar
2892 assert_equal(b, np.sum(a))
2893
2894 a = np.arange(6).reshape(2, 3)
2895 b = np.array([0, 0], dtype='f8').byteswap()
2896 b = b.view(b.dtype.newbyteorder())
2897 i = nditer([a, b], ['reduce_ok', 'external_loop', 'buffered'],
2898 [['readonly'], ['readwrite', 'nbo']],
2899 op_axes=[[0, 1], [0, -1]])
2900 # Reduction shape/strides for the output
2901 with i:
2902 assert_equal(i[1].shape, (3,))
2903 assert_equal(i[1].strides, (0,))
2904 # Do the reduction
2905 for x, y in i:
2906 # Use a for loop instead of ``y[...] += x``
2907 # (equivalent to ``y[...] = y[...].copy() + x``),
2908 # because y has zero strides we use for the reduction
2909 for j in range(len(y)):
2910 y[j] += x[j]
2911 assert_equal(b, np.sum(a, axis=1))
2912
2913 # Iterator inner double loop was wrong on this one
2914 p = np.arange(2) + 1
2915 it = np.nditer([p, None],
2916 ['delay_bufalloc', 'reduce_ok', 'buffered', 'external_loop'],
2917 [['readonly'], ['readwrite', 'allocate']],
2918 op_axes=[[-1, 0], [-1, -1]],
2919 itershape=(2, 2))
2920 with it:
2921 it.operands[1].fill(0)
2922 it.reset()
2923 assert_equal(it[0], [1, 2, 1, 2])
2924
2925 # Iterator inner loop should take argument contiguity into account
2926 x = np.ones((7, 13, 8), np.int8)[4:6, 1:11:6, 1:5].transpose(1, 2, 0)
2927 x[...] = np.arange(x.size).reshape(x.shape)
2928 y_base = np.arange(4 * 4, dtype=np.int8).reshape(4, 4)
2929 y_base_copy = y_base.copy()
2930 y = y_base[::2, :, None]
2931
2932 it = np.nditer([y, x],
2933 ['buffered', 'external_loop', 'reduce_ok'],

Callers

nothing calls this directly

Calls 8

assert_equalFunction · 0.90
assert_Function · 0.90
byteswapMethod · 0.80
reshapeMethod · 0.80
viewMethod · 0.45
dtypeMethod · 0.45
sumMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…