()
| 2815 | op_axes=[[0, 1], [-1, -1]]) |
| 2816 | |
| 2817 | def test_iter_reduction(): |
| 2818 | # Test doing reductions with the iterator |
| 2819 | |
| 2820 | a = np.arange(6) |
| 2821 | i = nditer([a, None], ['reduce_ok'], |
| 2822 | [['readonly'], ['readwrite', 'allocate']], |
| 2823 | op_axes=[[0], [-1]]) |
| 2824 | # Need to initialize the output operand to the addition unit |
| 2825 | with i: |
| 2826 | i.operands[1][...] = 0 |
| 2827 | # Do the reduction |
| 2828 | for x, y in i: |
| 2829 | y[...] += x |
| 2830 | # Since no axes were specified, should have allocated a scalar |
| 2831 | assert_equal(i.operands[1].ndim, 0) |
| 2832 | assert_equal(i.operands[1], np.sum(a)) |
| 2833 | |
| 2834 | a = np.arange(6).reshape(2, 3) |
| 2835 | i = nditer([a, None], ['reduce_ok', 'external_loop'], |
| 2836 | [['readonly'], ['readwrite', 'allocate']], |
| 2837 | op_axes=[[0, 1], [-1, -1]]) |
| 2838 | # Need to initialize the output operand to the addition unit |
| 2839 | with i: |
| 2840 | i.operands[1][...] = 0 |
| 2841 | # Reduction shape/strides for the output |
| 2842 | assert_equal(i[1].shape, (6,)) |
| 2843 | assert_equal(i[1].strides, (0,)) |
| 2844 | # Do the reduction |
| 2845 | for x, y in i: |
| 2846 | # Use a for loop instead of ``y[...] += x`` |
| 2847 | # (equivalent to ``y[...] = y[...].copy() + x``), |
| 2848 | # because y has zero strides we use for the reduction |
| 2849 | for j in range(len(y)): |
| 2850 | y[j] += x[j] |
| 2851 | # Since no axes were specified, should have allocated a scalar |
| 2852 | assert_equal(i.operands[1].ndim, 0) |
| 2853 | assert_equal(i.operands[1], np.sum(a)) |
| 2854 | |
| 2855 | # This is a tricky reduction case for the buffering double loop |
| 2856 | # to handle |
| 2857 | a = np.ones((2, 3, 5)) |
| 2858 | it1 = nditer([a, None], ['reduce_ok', 'external_loop'], |
| 2859 | [['readonly'], ['readwrite', 'allocate']], |
| 2860 | op_axes=[None, [0, -1, 1]]) |
| 2861 | it2 = nditer([a, None], ['reduce_ok', 'external_loop', |
| 2862 | 'buffered', 'delay_bufalloc'], |
| 2863 | [['readonly'], ['readwrite', 'allocate']], |
| 2864 | op_axes=[None, [0, -1, 1]], buffersize=10) |
| 2865 | with it1, it2: |
| 2866 | it1.operands[1].fill(0) |
| 2867 | it2.operands[1].fill(0) |
| 2868 | it2.reset() |
| 2869 | for x in it1: |
| 2870 | x[1][...] += x[0] |
| 2871 | for x in it2: |
| 2872 | x[1][...] += x[0] |
| 2873 | assert_equal(it1.operands[1], it2.operands[1]) |
| 2874 | assert_equal(it2.operands[1].sum(), a.size) |
nothing calls this directly
no test coverage detected
searching dependent graphs…