# pypy / pypy / module / micronumpy / test / test_outarg.py

The branch 'numpypy-argminmax' does not exist.
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146``` ```from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest class AppTestOutArg(BaseNumpyAppTest): def test_reduce_out(self): from numpypy import arange, zeros, array a = arange(15).reshape(5, 3) b = arange(12).reshape(4,3) c = a.sum(0, out=b[1]) assert (c == [30, 35, 40]).all() assert (c == b[1]).all() raises(ValueError, 'a.prod(0, out=arange(10))') a=arange(12).reshape(3,2,2) raises(ValueError, 'a.sum(0, out=arange(12).reshape(3,2,2))') raises(ValueError, 'a.sum(0, out=arange(3))') c = array([-1, 0, 1]).sum(out=zeros([], dtype=bool)) #You could argue that this should product False, but # that would require an itermediate result. Cpython numpy # gives True. assert c == True a = array([[-1, 0, 1], [1, 0, -1]]) c = a.sum(0, out=zeros((3,), dtype=bool)) assert (c == [True, False, True]).all() c = a.sum(1, out=zeros((2,), dtype=bool)) assert (c == [True, True]).all() def test_reduce_intermediary(self): from numpypy import arange, array a = arange(15).reshape(5, 3) b = array(range(3), dtype=bool) c = a.prod(0, out=b) assert(b == [False, True, True]).all() def test_ufunc_out(self): from _numpypy import array, negative, zeros, sin from math import sin as msin a = array([[1, 2], [3, 4]]) c = zeros((2,2,2)) b = negative(a + a, out=c[1]) #test for view, and also test that forcing out also forces b assert (c[:, :, 1] == [[0, 0], [-4, -8]]).all() assert (b == [[-2, -4], [-6, -8]]).all() #Test broadcast, type promotion b = negative(3, out=a) assert (a == -3).all() c = zeros((2, 2), dtype=float) b = negative(3, out=c) assert b.dtype.kind == c.dtype.kind assert b.shape == c.shape a = array([1, 2]) b = sin(a, out=c) assert(c == [[msin(1), msin(2)]] * 2).all() b = sin(a, out=c+c) assert (c == b).all() #Test shape agreement a = zeros((3,4)) b = zeros((3,5)) raises(ValueError, 'negative(a, out=b)') b = zeros((1,4)) raises(ValueError, 'negative(a, out=b)') def test_binfunc_out(self): from _numpypy import array, add a = array([[1, 2], [3, 4]]) out = array([[1, 2], [3, 4]]) c = add(a, a, out=out) assert (c == out).all() assert c.shape == a.shape assert c.dtype is a.dtype c[0,0] = 100 assert out[0, 0] == 100 out[:] = 100 raises(ValueError, 'c = add(a, a, out=out[1])') c = add(a[0], a[1], out=out[1]) assert (c == out[1]).all() assert (c == [4, 6]).all() assert (out[0] == 100).all() c = add(a[0], a[1], out=out) assert (c == out[1]).all() assert (c == out[0]).all() out = array(16, dtype=int) b = add(10, 10, out=out) assert b==out assert b.dtype == out.dtype def test_applevel(self): from _numpypy import array, sum, max, min a = array([[1, 2], [3, 4]]) out = array([[0, 0], [0, 0]]) c = sum(a, axis=0, out=out[0]) assert (c == [4, 6]).all() assert (c == out[0]).all() assert (c != out[1]).all() c = max(a, axis=1, out=out[0]) assert (c == [2, 4]).all() assert (c == out[0]).all() assert (c != out[1]).all() def test_ufunc_cast(self): from _numpypy import array, negative, add, sum a = array(16, dtype = int) c = array(0, dtype = float) b = negative(a, out=c) assert b == c b = add(a, a, out=c) assert b == c d = array([16, 16], dtype=int) b = sum(d, out=c) assert b == c try: from _numpypy import version v = version.version.split('.') except: v = ['1', '6', '0'] # numpypy is api compatable to what version? if v[0]<'2': b = negative(c, out=a) assert b == a b = add(c, c, out=a) assert b == a b = sum(array([16, 16], dtype=float), out=a) assert b == a else: cast_error = raises(TypeError, negative, c, a) assert str(cast_error.value) == \ "Cannot cast ufunc negative output from dtype('float64') to dtype('int64') with casting rule 'same_kind'" def test_argminmax(self): from numpypy import arange, argmin, argmax, ones a = arange(15).reshape(5, 3) b = arange(15).reshape(5,3) c = a.argmax(0) assert (c == [4, 4, 4]).all() c = a.argmax(0, out=b[1]) assert (c == [4, 4, 4]).all() assert (c == b[1]).all() c = a.argmax(1, out=b[:,1]) assert (c == [2, 2, 2, 2, 2]).all() assert (c == b[:,1]).all() raises(ValueError, argmin, a, -3) raises(ValueError, argmax, a, 2) b = ones((5,3), dtype=float) c = a.argmin(0, out=b[1]) assert (b[1] == [0, 0, 0]).all() assert(c.dtype is b.dtype) ```