def test_broadcast_wrong_shapes(self): from _numpypy import zeros a = zeros((4, 3, 2)) b = zeros((4, 2)) exc = raises(ValueError, lambda: a + b) assert str(exc.value) == "operands could not be broadcast" \ " together with shapes (4,3,2) (4,2)"
def test_str(self): from _numpypy import array, zeros a = array(range(5), float) assert str(a) == "[0.0 1.0 2.0 3.0 4.0]" assert str((2 * a)[:]) == "[0.0 2.0 4.0 6.0 8.0]" a = zeros(1001) assert str(a) == "[0.0 0.0 0.0 ..., 0.0 0.0 0.0]" a = array(range(5), dtype=long) assert str(a) == "[0 1 2 3 4]" a = array([True, False, True, False], dtype="?") assert str(a) == "[True False True False]" a = array(range(5), dtype="int8") assert str(a) == "[0 1 2 3 4]" a = array(range(5), dtype="int16") assert str(a) == "[0 1 2 3 4]" a = array((range(5), range(5, 10)), dtype="int16") assert str(a) == "[[0 1 2 3 4]\n [5 6 7 8 9]]" a = array(3, dtype=int) assert str(a) == "3" a = zeros((400, 400), dtype=int) assert str(a) == "[[0 0 0 ..., 0 0 0]\n [0 0 0 ..., 0 0 0]\n" \ " [0 0 0 ..., 0 0 0]\n ...,\n [0 0 0 ..., 0 0 0]\n" \ " [0 0 0 ..., 0 0 0]\n [0 0 0 ..., 0 0 0]]" a = zeros((2, 2, 2)) r = str(a) assert r == '[[[0.0 0.0]\n [0.0 0.0]]\n\n [[0.0 0.0]\n [0.0 0.0]]]'
def test_repr(self): from _numpypy import array, zeros int_size = array(5).dtype.itemsize a = array(range(5), float) assert repr(a) == "array([0.0, 1.0, 2.0, 3.0, 4.0])" a = array([], float) assert repr(a) == "array([], dtype=float64)" a = zeros(1001) assert repr(a) == "array([0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.0])" a = array(range(5), long) if a.dtype.itemsize == int_size: assert repr(a) == "array([0, 1, 2, 3, 4])" else: assert repr(a) == "array([0, 1, 2, 3, 4], dtype=int64)" a = array(range(5), 'int32') if a.dtype.itemsize == int_size: assert repr(a) == "array([0, 1, 2, 3, 4])" else: assert repr(a) == "array([0, 1, 2, 3, 4], dtype=int32)" a = array([], long) assert repr(a) == "array([], dtype=int64)" a = array([True, False, True, False], "?") assert repr(a) == "array([True, False, True, False], dtype=bool)" a = zeros([]) assert repr(a) == "array(0.0)" a = array(0.2) assert repr(a) == "array(0.2)"
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_shape(self): import _numpypy assert _numpypy.zeros(1).shape == (1,) assert _numpypy.zeros((2, 2)).shape == (2, 2) assert _numpypy.zeros((3, 1, 2)).shape == (3, 1, 2) assert _numpypy.array([[1], [2], [3]]).shape == (3, 1) assert len(_numpypy.zeros((3, 1, 2))) == 3 raises(TypeError, len, _numpypy.zeros(())) raises(ValueError, _numpypy.array, [[1, 2], 3])
def test_shape(self): import _numpypy assert _numpypy.zeros(1).shape == (1, ) assert _numpypy.zeros((2, 2)).shape == (2, 2) assert _numpypy.zeros((3, 1, 2)).shape == (3, 1, 2) assert _numpypy.array([[1], [2], [3]]).shape == (3, 1) assert len(_numpypy.zeros((3, 1, 2))) == 3 raises(TypeError, len, _numpypy.zeros(())) raises(ValueError, _numpypy.array, [[1, 2], 3])
def test_multidim_setslice(self): from _numpypy import zeros, ones a = zeros((3, 3)) b = ones((3, 3)) a[:, 1:3] = b[:, 1:3] assert (a == [[0, 1, 1], [0, 1, 1], [0, 1, 1]]).all() a = zeros((3, 3)) b = ones((3, 3)) a[:, ::2] = b[:, ::2] assert (a == [[1, 0, 1], [1, 0, 1], [1, 0, 1]]).all()
def test_broadcast_shape_agreement(self): from _numpypy import zeros, array a = zeros((3, 1, 3)) b = array(((10, 11, 12), (20, 21, 22), (30, 31, 32))) c = ((a + b) == [b, b, b]) assert c.all() a = array((((10, 11, 12), ), ((20, 21, 22), ), ((30, 31, 32), ))) assert (a.shape == (3, 1, 3)) d = zeros((3, 3)) c = ((a + d) == [b, b, b]) c = ((a + d) == array([[[10., 11., 12.]] * 3, [[20., 21., 22.]] * 3, [[30., 31., 32.]] * 3])) assert c.all()
def test_broadcast_shape_agreement(self): from _numpypy import zeros, array a = zeros((3, 1, 3)) b = array(((10, 11, 12), (20, 21, 22), (30, 31, 32))) c = ((a + b) == [b, b, b]) assert c.all() a = array((((10, 11, 12), ), ((20, 21, 22), ), ((30, 31, 32), ))) assert(a.shape == (3, 1, 3)) d = zeros((3, 3)) c = ((a + d) == [b, b, b]) c = ((a + d) == array([[[10., 11., 12.]] * 3, [[20., 21., 22.]] * 3, [[30., 31., 32.]] * 3])) assert c.all()
def test_zeros_long(self): from _numpypy import zeros, int64 a = zeros(10, dtype=long) for i in range(10): assert isinstance(a[i], int64) assert a[1] == 0
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_slices(self): import _numpypy a = _numpypy.zeros((4, 3, 2)) raises(IndexError, a.__getitem__, (4,)) raises(IndexError, a.__getitem__, (3, 3)) raises(IndexError, a.__getitem__, (slice(None), 3)) a[0, 1, 1] = 13 a[1, 2, 1] = 15 b = a[0] assert len(b) == 3 assert b.shape == (3, 2) assert b[1, 1] == 13 b = a[1] assert b.shape == (3, 2) assert b[2, 1] == 15 b = a[:, 1] assert b.shape == (4, 2) assert b[0, 1] == 13 b = a[:, 1, :] assert b.shape == (4, 2) assert b[0, 1] == 13 b = a[1, 2] assert b[1] == 15 b = a[:] assert b.shape == (4, 3, 2) assert b[1, 2, 1] == 15 assert b[0, 1, 1] == 13 b = a[:][:, 1][:] assert b[2, 1] == 0.0 assert b[0, 1] == 13 raises(IndexError, b.__getitem__, (4, 1)) assert a[0][1][1] == 13 assert a[1][2][1] == 15
def test_slices(self): import _numpypy a = _numpypy.zeros((4, 3, 2)) raises(IndexError, a.__getitem__, (4, )) raises(IndexError, a.__getitem__, (3, 3)) raises(IndexError, a.__getitem__, (slice(None), 3)) a[0, 1, 1] = 13 a[1, 2, 1] = 15 b = a[0] assert len(b) == 3 assert b.shape == (3, 2) assert b[1, 1] == 13 b = a[1] assert b.shape == (3, 2) assert b[2, 1] == 15 b = a[:, 1] assert b.shape == (4, 2) assert b[0, 1] == 13 b = a[:, 1, :] assert b.shape == (4, 2) assert b[0, 1] == 13 b = a[1, 2] assert b[1] == 15 b = a[:] assert b.shape == (4, 3, 2) assert b[1, 2, 1] == 15 assert b[0, 1, 1] == 13 b = a[:][:, 1][:] assert b[2, 1] == 0.0 assert b[0, 1] == 13 raises(IndexError, b.__getitem__, (4, 1)) assert a[0][1][1] == 13 assert a[1][2][1] == 15
def test_init(self): from _numpypy import zeros a = zeros(15) # Check that storage was actually zero'd. assert a[10] == 0.0 # And check that changes stick. a[13] = 5.3 assert a[13] == 5.3
def test_any(self): from _numpypy import array, zeros a = array(range(5)) assert a.any() == True b = zeros(5) assert b.any() == False c = array([]) assert c.any() == False
def test_setslice_of_slice_array(self): from _numpypy import array, zeros a = zeros(5) a[::2] = array([9., 10., 11.]) assert a[0] == 9. assert a[2] == 10. assert a[4] == 11. a[1:4:2][::-1] = array([1., 2.]) assert a[0] == 9. assert a[1] == 2. assert a[2] == 10. assert a[3] == 1. assert a[4] == 11. a = zeros(10) a[::2][::-1][::2] = array(range(1, 4)) assert a[8] == 1. assert a[4] == 2. assert a[0] == 3.
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_broadcast_scalar(self): from _numpypy import zeros a = zeros((4, 5), 'd') a[:, 1] = 3 assert a[2, 1] == 3 assert a[0, 2] == 0 a[0, :] = 5 assert a[0, 3] == 5 assert a[2, 1] == 3 assert a[3, 2] == 0
def test_getsetitem(self): import _numpypy a = _numpypy.zeros((2, 3, 1)) raises(IndexError, a.__getitem__, (2, 0, 0)) raises(IndexError, a.__getitem__, (0, 3, 0)) raises(IndexError, a.__getitem__, (0, 0, 1)) assert a[1, 1, 0] == 0 a[1, 2, 0] = 3 assert a[1, 2, 0] == 3 assert a[1, 1, 0] == 0 assert a[1, -1, 0] == 3
def test_reshape(self): from _numpypy import array, zeros a = array(range(12)) exc = raises(ValueError, "b = a.reshape((3, 10))") assert str(exc.value) == "total size of new array must be unchanged" b = a.reshape((3, 4)) assert b.shape == (3, 4) assert (b == [range(4), range(4, 8), range(8, 12)]).all() b[:, 0] = 1000 assert (a == [1000, 1, 2, 3, 1000, 5, 6, 7, 1000, 9, 10, 11]).all() a = zeros((4, 2, 3)) a.shape = (12, 2)
def test_str_slice(self): from _numpypy import array, zeros a = array(range(5), float) b = a[1::2] assert str(b) == "[1.0 3.0]" a = zeros(2002) b = a[::2] assert str(b) == "[0.0 0.0 0.0 ..., 0.0 0.0 0.0]" a = array((range(5), range(5, 10)), dtype="int16") b = a[1, 2:] assert str(b) == "[7 8 9]" b = a[2:1, ] assert str(b) == "[]"
def test_repr_slice(self): from _numpypy import array, zeros a = array(range(5), float) b = a[1::2] assert repr(b) == "array([1.0, 3.0])" a = zeros(2002) b = a[::2] assert repr(b) == "array([0.0, 0.0, 0.0, ..., 0.0, 0.0, 0.0])" a = array((range(5), range(5, 10)), dtype="int16") b = a[1, 2:] assert repr(b) == "array([7, 8, 9], dtype=int16)" # an empty slice prints its shape b = a[2:1, ] assert repr(b) == "array([], shape=(0, 5), dtype=int16)"
def test_setitem_slice(self): import _numpypy a = _numpypy.zeros((3, 4)) a[1] = [1, 2, 3, 4] assert a[1, 2] == 3 raises(TypeError, a[1].__setitem__, [1, 2, 3]) a = _numpypy.array([[1, 2], [3, 4]]) assert (a == [[1, 2], [3, 4]]).all() a[1] = _numpypy.array([5, 6]) assert (a == [[1, 2], [5, 6]]).all() a[:, 1] = _numpypy.array([8, 10]) assert (a == [[1, 8], [5, 10]]).all() a[0, ::-1] = _numpypy.array([11, 12]) assert (a == [[12, 11], [5, 10]]).all()
def test_debug_repr(self): from _numpypy import zeros, sin from _numpypy.pypy import debug_repr a = zeros(1) assert debug_repr(a) == 'Array' assert debug_repr(a + a) == 'Call2(add, Array, Array)' assert debug_repr(a[::2]) == 'Slice' assert debug_repr(a + 2) == 'Call2(add, Array, Scalar)' assert debug_repr(a + a.flat) == 'Call2(add, Array, Slice)' assert debug_repr(sin(a)) == 'Call1(sin, Array)' b = a + a b[0] = 3 assert debug_repr(b) == 'Array'
def test_setitem_slice(self): import _numpypy a = _numpypy.zeros((3, 4)) a[1] = [1, 2, 3, 4] assert a[1, 2] == 3 raises(TypeError, a[1].__setitem__, [1, 2, 3]) a = _numpypy.array([[1, 2], [3, 4]]) assert (a == [[1, 2], [3, 4]]).all() a[1] = _numpypy.array([5, 6]) assert (a == [[1, 2], [5, 6]]).all() a[:, 1] = _numpypy.array([8, 10]) assert (a == [[1, 8], [5, 10]]).all() a[0, :: -1] = _numpypy.array([11, 12]) assert (a == [[12, 11], [5, 10]]).all()
def arange(start, stop=None, step=1, dtype=None): '''arange([start], stop[, step], dtype=None) Generate values in the half-interval [start, stop). ''' if stop is None: stop = start start = 0 if dtype is None: test = _numpypy.array([start, stop, step, 0]) dtype = test.dtype arr = _numpypy.zeros(int(math.ceil((stop - start) / step)), dtype=dtype) i = start for j in range(arr.size): arr[j] = i i += step return arr
def eye(n, m=None, k=0, dtype=None): if m is None: m = n a = _numpypy.zeros((n, m), dtype=dtype) ni = 0 mi = 0 if k < 0: p = n + k ni = -k else: p = n - k mi = k while ni < n and mi < m: a[ni][mi] = 1 ni += 1 mi += 1 return a
def identity(n, dtype=None): a = _numpypy.zeros((n,n), dtype=dtype) for i in range(n): a[i][i] = 1 return a
def test_init(self): import _numpypy a = _numpypy.zeros((2, 2)) assert len(a) == 2
def test_broadcast_setslice(self): from _numpypy import zeros, ones a = zeros((10, 10)) b = ones(10) a[:, :] = b assert a[3, 5] == 1
def test_broadcast_call2(self): from _numpypy import zeros, ones a = zeros((4, 1, 5)) b = ones((4, 3, 5)) b[:] = (a + a) assert (b == zeros((4, 3, 5))).all()
def test_slice_copy(self): from _numpypy import zeros a = zeros((10, 10)) b = a[0].copy() assert (b == zeros(10)).all()
def test_where(self): from _numpypy import where, ones, zeros, array a = [1, 2, 3, 0, -3] a = where(array(a) > 0, ones(5), zeros(5)) assert (a == [1, 1, 1, 0, 0]).all()
def test_zeros_bool(self): from _numpypy import zeros, False_ a = zeros(10, dtype=bool) for i in range(10): assert a[i] is False_
def identity(n, dtype=None): a = _numpypy.zeros((n, n), dtype=dtype) for i in range(n): a[i][i] = 1 return a
def test_zeros_long(self): from _numpypy import zeros, longlong a = zeros(10, dtype=long) for i in range(10): assert isinstance(a[i], longlong) assert a[1] == 0
def test_where_invalidates(self): from _numpypy import where, ones, zeros, array a = array([1, 2, 3, 0, -3]) b = where(a > 0, ones(5), zeros(5)) a[0] = 0 assert (b == [1, 1, 1, 0, 0]).all()