def test_slice_reshape(self): from _numpypy import zeros, arange a = zeros((4, 2, 3)) b = a[::2, :, :] b.shape = (2, 6) exc = raises(AttributeError, "b.shape = 12") assert str(exc.value) == \ "incompatible shape for a non-contiguous array" b = a[::2, :, :].reshape((2, 6)) assert b.shape == (2, 6) b = arange(20)[1:17:2] b.shape = (4, 2) assert (b == [[1, 3], [5, 7], [9, 11], [13, 15]]).all() c = b.reshape((2, 4)) assert (c == [[1, 3, 5, 7], [9, 11, 13, 15]]).all() z = arange(96).reshape((12, -1)) assert z.shape == (12, 8) y = z.reshape((4, 3, 8)) v = y[:, ::2, :] w = y.reshape(96) u = v.reshape(64) assert y[1, 2, 1] == z[5, 1] y[1, 2, 1] = 1000 # z, y, w, v are views of eachother assert z[5, 1] == 1000 assert v[1, 1, 1] == 1000 assert w[41] == 1000 # u is not a view, it is a copy! assert u[25] == 41 a = zeros((5, 2)) assert a.reshape(-1).shape == (10, ) raises(ValueError, arange(10).reshape, (5, -1, -1))
def test_slice_reshape(self): from _numpypy import zeros, arange a = zeros((4, 2, 3)) b = a[::2, :, :] b.shape = (2, 6) exc = raises(AttributeError, "b.shape = 12") assert str(exc.value) == \ "incompatible shape for a non-contiguous array" b = a[::2, :, :].reshape((2, 6)) assert b.shape == (2, 6) b = arange(20)[1:17:2] b.shape = (4, 2) assert (b == [[1, 3], [5, 7], [9, 11], [13, 15]]).all() c = b.reshape((2, 4)) assert (c == [[1, 3, 5, 7], [9, 11, 13, 15]]).all() z = arange(96).reshape((12, -1)) assert z.shape == (12, 8) y = z.reshape((4, 3, 8)) v = y[:, ::2, :] w = y.reshape(96) u = v.reshape(64) assert y[1, 2, 1] == z[5, 1] y[1, 2, 1] = 1000 # z, y, w, v are views of eachother assert z[5, 1] == 1000 assert v[1, 1, 1] == 1000 assert w[41] == 1000 # u is not a view, it is a copy! assert u[25] == 41 a = zeros((5, 2)) assert a.reshape(-1).shape == (10,) raises(ValueError, arange(10).reshape, (5, -1, -1))
def test_reduceND(self): from _numpypy import add, arange a = arange(12).reshape(3, 4) assert (add.reduce(a, 0) == [12, 15, 18, 21]).all() assert (add.reduce(a, 1) == [6.0, 22.0, 38.0]).all() raises(ValueError, add.reduce, a, 2)
def test_broadcast_virtualview(self): from _numpypy import arange, zeros a = arange(8).reshape([2, 2, 2]) b = (a + a)[1, 1] c = zeros((2, 2, 2)) c[:] = b assert (c == [[[12, 14], [12, 14]], [[12, 14], [12, 14]]]).all()
def test_count_reduce_items(self): from _numpypy import count_reduce_items, arange a = arange(24).reshape(2, 3, 4) assert count_reduce_items(a) == 24 assert count_reduce_items(a, 1) == 3 assert count_reduce_items(a, (1, 2)) == 3 * 4
def test_count_nonzero(self): from _numpypy import where, count_nonzero, arange a = arange(10) assert count_nonzero(a) == 9 a[9] = 0 assert count_nonzero(a) == 8
def test_reduce_keepdims(self): from _numpypy import add, arange a = arange(12).reshape(3, 4) b = add.reduce(a, 0, keepdims=True) assert b.shape == (1, 4) assert (add.reduce(a, 0, keepdims=True) == [12, 15, 18, 21]).all()
def test_virtual_views(self): from _numpypy import arange a = arange(15) c = (a + a) d = c[::2] assert d[3] == 12 c[6] = 5 assert d[3] == 5 a = arange(15) c = (a + a) d = c[::2][::2] assert d[1] == 8 b = a + a c = b[::2] c[:] = 3 assert b[0] == 3 assert b[1] == 2
def test_bitwise(self): from _numpypy import bitwise_and, bitwise_or, bitwise_xor, arange, array a = arange(6).reshape(2, 3) assert (a & 1 == [[0, 1, 0], [1, 0, 1]]).all() assert (a & 1 == bitwise_and(a, 1)).all() assert (a | 1 == [[1, 1, 3], [3, 5, 5]]).all() assert (a | 1 == bitwise_or(a, 1)).all() assert (a ^ 3 == bitwise_xor(a, 3)).all() raises(TypeError, 'array([1.0]) & 1')
def test_count_reduce_items(self): from _numpypy import count_reduce_items, arange a = arange(24).reshape(2, 3, 4) assert count_reduce_items(a) == 24 assert count_reduce_items(a, 1) == 3 assert count_reduce_items(a, (1, 2)) == 3 * 4 raises(ValueError, count_reduce_items, a, -4) raises(ValueError, count_reduce_items, a, (0, 2, -4))
def test_copy(self): from _numpypy import arange, array a = arange(5) b = a.copy() for i in xrange(5): assert b[i] == a[i] a[3] = 22 assert b[3] == 3 a = array(1) assert a.copy() == a a = arange(8) b = a[::2] c = b.copy() assert (c == b).all() a = arange(15).reshape(5,3) b = a.copy() assert (b == a).all()
def test_copy(self): from _numpypy import arange, array a = arange(5) b = a.copy() for i in xrange(5): assert b[i] == a[i] a[3] = 22 assert b[3] == 3 a = array(1) assert a.copy() == a a = arange(8) b = a[::2] c = b.copy() assert (c == b).all() a = arange(15).reshape(5, 3) b = a.copy() assert (b == a).all()
def test_app_reshape(self): from _numpypy import arange, array, dtype, reshape a = arange(12) b = reshape(a, (3, 4)) assert b.shape == (3, 4) a = range(12) b = reshape(a, (3, 4)) assert b.shape == (3, 4) a = array(range(105)).reshape(3, 5, 7) assert a.reshape(1, -1).shape == (1, 105) assert a.reshape(1, 1, -1).shape == (1, 1, 105) assert a.reshape(-1, 1, 1).shape == (105, 1, 1)
def test_repr_multi(self): from _numpypy import arange, zeros a = zeros((3, 4)) assert repr(a) == '''array([[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]])''' a = zeros((2, 3, 4)) assert repr(a) == '''array([[[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]], [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0]]])''' a = arange(1002).reshape((2, 501)) assert repr(a) == '''array([[0, 1, 2, ..., 498, 499, 500], [501, 502, 503, ..., 999, 1000, 1001]])''' assert repr(a.T) == '''array([[0, 501],
def test_arange(self): from _numpypy import arange, array, dtype a = arange(3) assert (a == [0, 1, 2]).all() assert a.dtype is dtype(int) a = arange(3.0) assert (a == [0., 1., 2.]).all() assert a.dtype is dtype(float) a = arange(3, 7) assert (a == [3, 4, 5, 6]).all() assert a.dtype is dtype(int) a = arange(3, 7, 2) assert (a == [3, 5]).all() a = arange(3, dtype=float) assert (a == [0., 1., 2.]).all() assert a.dtype is dtype(float) a = arange(0, 0.8, 0.1) assert len(a) == 8 assert arange(False, True, True).dtype is dtype(int)
def test_reduce_nd(self): from numpypy import arange, array, multiply a = arange(15).reshape(5, 3) assert a.sum() == 105 assert a.max() == 14 assert array([]).sum() == 0.0 raises(ValueError, 'array([]).max()') assert (a.sum(0) == [30, 35, 40]).all() assert (a.sum(axis=0) == [30, 35, 40]).all() assert (a.sum(1) == [3, 12, 21, 30, 39]).all() assert (a.max(0) == [12, 13, 14]).all() assert (a.max(1) == [2, 5, 8, 11, 14]).all() assert ((a + a).max() == 28) assert ((a + a).max(0) == [24, 26, 28]).all() assert ((a + a).sum(1) == [6, 24, 42, 60, 78]).all() assert (multiply.reduce(a) == array([0, 3640, 12320])).all() a = array(range(105)).reshape(3, 5, 7) assert (a[:, 1, :].sum(0) == [126, 129, 132, 135, 138, 141, 144]).all() assert (a[:, 1, :].sum(1) == [70, 315, 560]).all() raises(ValueError, 'a[:, 1, :].sum(2)') assert ((a + a).T.sum(2).T == (a + a).sum(0)).all() assert (a.reshape(1, -1).sum(0) == range(105)).all() assert (a.reshape(1, -1).sum(1) == 5460)
def test_reduce_nd(self): from numpypy import arange, array, multiply a = arange(15).reshape(5, 3) assert a.sum() == 105 assert a.max() == 14 assert array([]).sum() == 0.0 raises(ValueError, 'array([]).max()') assert (a.sum(0) == [30, 35, 40]).all() assert (a.sum(axis=0) == [30, 35, 40]).all() assert (a.sum(1) == [3, 12, 21, 30, 39]).all() assert (a.max(0) == [12, 13, 14]).all() assert (a.max(1) == [2, 5, 8, 11, 14]).all() assert ((a + a).max() == 28) assert ((a + a).max(0) == [24, 26, 28]).all() assert ((a + a).sum(1) == [6, 24, 42, 60, 78]).all() assert (multiply.reduce(a) == array([0, 3640, 12320])).all() a = array(range(105)).reshape(3, 5, 7) assert (a[:, 1, :].sum(0) == [126, 129, 132, 135, 138, 141, 144]).all() assert (a[:, 1, :].sum(1) == [70, 315, 560]).all() raises (ValueError, 'a[:, 1, :].sum(2)') assert ((a + a).T.sum(2).T == (a + a).sum(0)).all() assert (a.reshape(1,-1).sum(0) == range(105)).all() assert (a.reshape(1,-1).sum(1) == 5460)
def test_true_divide(self): from _numpypy import arange, array, true_divide assert (true_divide(arange(3), array([2, 2, 2])) == array([0, 0.5, 1])).all()
def test_reshape_varargs(self): from _numpypy import arange z = arange(96).reshape(12, -1) y = z.reshape(4, 3, 8) assert y.shape == (4, 3, 8)
def test_reduceND(self): from _numpypy import add, arange a = arange(12).reshape(3, 4) assert (add.reduce(a, 0) == [12, 15, 18, 21]).all() assert (add.reduce(a, 1) == [6.0, 22.0, 38.0]).all()