def test_subtract_other(self): from _numpypy import array a = array(range(5)) b = array([1, 1, 1, 1, 1]) c = a - b for i in range(5): assert c[i] == i - 1
def test_add_other(self): from _numpypy import array a = array(range(5)) b = array([i for i in reversed(range(5))]) c = a + b for i in range(5): assert c[i] == 4
def test_div_other(self): from _numpypy import array a = array(range(5)) b = array([2, 2, 2, 2, 2], float) c = a / b for i in range(5): assert c[i] == i / 2.0
def test_power_float(self): import math from _numpypy import power, array a = array([1., 2., 3.]) b = power(a, 3) for i in range(len(a)): assert b[i] == a[i] ** 3 a = array([1., 2., 3.]) b = array([1., 2., 3.]) c = power(a, b) for i in range(len(a)): assert c[i] == a[i] ** b[i] assert power(2, float('inf')) == float('inf') assert power(float('inf'), float('inf')) == float('inf') assert power(12345.0, 12345.0) == float('inf') assert power(-12345.0, 12345.0) == float('-inf') assert power(-12345.0, 12346.0) == float('inf') assert math.isnan(power(-1, 1.1)) assert math.isnan(power(-1, -1.1)) assert power(-2.0, -1) == -0.5 assert power(-2.0, -2) == 0.25 assert power(12345.0, -12345.0) == 0 assert power(float('-inf'), 2) == float('inf') assert power(float('-inf'), 2.5) == float('inf') assert power(float('-inf'), 3) == float('-inf')
def test_tolist_singledim(self): from _numpypy import array a = array(range(5)) assert a.tolist() == [0, 1, 2, 3, 4] assert type(a.tolist()[0]) is int b = array([0.2, 0.4, 0.6]) assert b.tolist() == [0.2, 0.4, 0.6]
def test_pow_other(self): from _numpypy import array a = array(range(5), float) b = array([2, 2, 2, 2, 2]) c = a ** b for i in range(5): assert c[i] == i ** 2
def test_index_int64(self): from _numpypy import array, int64 a = array(range(10), dtype=int64) b = array([0] * 10, dtype=int64) for idx in b: a[idx] += 1
def test_binop_types(self): from _numpypy import array, dtype tests = [('b', 'B', 'h'), ('b', 'h', 'h'), ('b', 'H', 'i'), ('b', 'i', 'i'), ('b', 'l', 'l'), ('b', 'q', 'q'), ('b', 'Q', 'd'), ('B', 'h', 'h'), ('B', 'H', 'H'), ('B', 'i', 'i'), ('B', 'I', 'I'), ('B', 'l', 'l'), ('B', 'L', 'L'), ('B', 'q', 'q'), ('B', 'Q', 'Q'), ('h', 'H', 'i'), ('h', 'i', 'i'), ('h', 'l', 'l'), ('h', 'q', 'q'), ('h', 'Q', 'd'), ('H', 'i', 'i'), ('H', 'I', 'I'), ('H', 'l', 'l'), ('H', 'L', 'L'), ('H', 'q', 'q'), ('H', 'Q', 'Q'), ('i', 'l', 'l'), ('i', 'q', 'q'), ('i', 'Q', 'd'), ('I', 'L', 'L'), ('I', 'q', 'q'), ('I', 'Q', 'Q'), ('q', 'Q', 'd'), ('b', 'f', 'f'), ('B', 'f', 'f'), ('h', 'f', 'f'), ('H', 'f', 'f'), ('i', 'f', 'd'), ('I', 'f', 'd'), ('l', 'f', 'd'), ('L', 'f', 'd'), ('q', 'f', 'd'), ('Q', 'f', 'd'), ('q', 'd', 'd')] if dtype('i').itemsize == dtype('l').itemsize: # 32-bit tests.extend([('b', 'I', 'q'), ('b', 'L', 'q'), ('h', 'I', 'q'), ('h', 'L', 'q'), ('i', 'I', 'q'), ('i', 'L', 'q')]) else: tests.extend([('b', 'I', 'l'), ('b', 'L', 'd'), ('h', 'I', 'l'), ('h', 'L', 'd'), ('i', 'I', 'l'), ('i', 'L', 'd')]) for d1, d2, dout in tests: assert (array([1], d1) + array([1], d2)).dtype is dtype(dout)
def test_mod_other(self): from _numpypy import array a = array(range(5)) b = array([2, 2, 2, 2, 2]) c = a % b for i in range(5): assert c[i] == i % 2
def test_power_float(self): import math from _numpypy import power, array a = array([1., 2., 3.]) b = power(a, 3) for i in range(len(a)): assert b[i] == a[i]**3 a = array([1., 2., 3.]) b = array([1., 2., 3.]) c = power(a, b) for i in range(len(a)): assert c[i] == a[i]**b[i] assert power(2, float('inf')) == float('inf') assert power(float('inf'), float('inf')) == float('inf') assert power(12345.0, 12345.0) == float('inf') assert power(-12345.0, 12345.0) == float('-inf') assert power(-12345.0, 12346.0) == float('inf') assert math.isnan(power(-1, 1.1)) assert math.isnan(power(-1, -1.1)) assert power(-2.0, -1) == -0.5 assert power(-2.0, -2) == 0.25 assert power(12345.0, -12345.0) == 0 assert power(float('-inf'), 2) == float('inf') assert power(float('-inf'), 2.5) == float('inf') assert power(float('-inf'), 3) == float('-inf')
def test_bool_binop_types(self): from _numpypy import array, dtype types = ["?", "b", "B", "h", "H", "i", "I", "l", "L", "q", "Q", "f", "d"] a = array([True], "?") for t in types: assert (a + array([0], t)).dtype is dtype(t)
def test_pow_other(self): from _numpypy import array a = array(range(5), float) b = array([2, 2, 2, 2, 2]) c = a**b for i in range(5): assert c[i] == i**2
def test_binop_types(self): from _numpypy import array, dtype tests = [ ("b", "B", "h"), ("b", "h", "h"), ("b", "H", "i"), ("b", "i", "i"), ("b", "l", "l"), ("b", "q", "q"), ("b", "Q", "d"), ("B", "h", "h"), ("B", "H", "H"), ("B", "i", "i"), ("B", "I", "I"), ("B", "l", "l"), ("B", "L", "L"), ("B", "q", "q"), ("B", "Q", "Q"), ("h", "H", "i"), ("h", "i", "i"), ("h", "l", "l"), ("h", "q", "q"), ("h", "Q", "d"), ("H", "i", "i"), ("H", "I", "I"), ("H", "l", "l"), ("H", "L", "L"), ("H", "q", "q"), ("H", "Q", "Q"), ("i", "l", "l"), ("i", "q", "q"), ("i", "Q", "d"), ("I", "L", "L"), ("I", "q", "q"), ("I", "Q", "Q"), ("q", "Q", "d"), ("b", "f", "f"), ("B", "f", "f"), ("h", "f", "f"), ("H", "f", "f"), ("i", "f", "d"), ("I", "f", "d"), ("l", "f", "d"), ("L", "f", "d"), ("q", "f", "d"), ("Q", "f", "d"), ("q", "d", "d"), ] if dtype("i").itemsize == dtype("l").itemsize: # 32-bit tests.extend( [("b", "I", "q"), ("b", "L", "q"), ("h", "I", "q"), ("h", "L", "q"), ("i", "I", "q"), ("i", "L", "q")] ) else: tests.extend( [("b", "I", "l"), ("b", "L", "d"), ("h", "I", "l"), ("h", "L", "d"), ("i", "I", "l"), ("i", "L", "d")] ) for d1, d2, dout in tests: assert (array([1], d1) + array([1], d2)).dtype is dtype(dout)
def test_bool_binop_types(self): from _numpypy import array, dtype types = [ '?', 'b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q', 'f', 'd' ] a = array([True], '?') for t in types: assert (a + array([0], t)).dtype is dtype(t)
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_all(self): from _numpypy import array a = array(range(5)) assert a.all() == False a[0] = 3.0 assert a.all() == True b = array([]) assert b.all() == True
def test_add(self): from _numpypy import array, add a = array([-5.0, -0.0, 1.0]) b = array([ 3.0, -2.0,-3.0]) c = add(a, b) for i in range(3): assert c[i] == a[i] + b[i]
def test_multiply(self): from _numpypy import array, multiply a = array([-5.0, -0.0, 1.0]) b = array([ 3.0, -2.0,-3.0]) c = multiply(a, b) for i in range(3): assert c[i] == a[i] * b[i]
def test_minimum(self): from _numpypy import array, minimum a = array([-5.0, -0.0, 1.0]) b = array([3.0, -2.0, -3.0]) c = minimum(a, b) for i in range(3): assert c[i] == min(a[i], b[i])
def test_subtract(self): from _numpypy import array, subtract a = array([-5.0, -0.0, 1.0]) b = array([ 3.0, -2.0,-3.0]) c = subtract(a, b) for i in range(3): assert c[i] == a[i] - b[i]
def test_multiply(self): from _numpypy import array, multiply a = array([-5.0, -0.0, 1.0]) b = array([3.0, -2.0, -3.0]) c = multiply(a, b) for i in range(3): assert c[i] == a[i] * b[i]
def test_add(self): from _numpypy import array, add a = array([-5.0, -0.0, 1.0]) b = array([3.0, -2.0, -3.0]) c = add(a, b) for i in range(3): assert c[i] == a[i] + b[i]
def test_subtract(self): from _numpypy import array, subtract a = array([-5.0, -0.0, 1.0]) b = array([3.0, -2.0, -3.0]) c = subtract(a, b) for i in range(3): assert c[i] == a[i] - b[i]
def test_minimum(self): from _numpypy import array, minimum a = array([-5.0, -0.0, 1.0]) b = array([ 3.0, -2.0,-3.0]) c = minimum(a, b) for i in range(3): assert c[i] == min(a[i], b[i])
def test_nonzero(self): from _numpypy import array a = array([1, 2]) raises(ValueError, bool, a) raises(ValueError, bool, a == a) assert bool(array(1)) assert not bool(array(0)) assert bool(array([1])) assert not bool(array([0]))
def test_dot(self): from _numpypy import array, dot a = array(range(5)) assert a.dot(a) == 30.0 a = array(range(5)) assert a.dot(range(5)) == 30 assert dot(range(5), range(5)) == 30 assert (dot(5, [1, 2, 3]) == [5, 10, 15]).all()
def test_array_interface(self): from _numpypy import array a = array([1, 2, 3]) i = a.__array_interface__ assert isinstance(i['data'][0], int) a = a[::2] i = a.__array_interface__ assert isinstance(i['data'][0], int) raises(TypeError, getattr, array(3), '__array_interface__')
def test_pow(self): from _numpypy import array a = array(range(5), float) b = a ** a for i in range(5): assert b[i] == i ** i a = array(range(5)) assert (a ** 2 == a * a).all()
def test_slice_assignment(self): from _numpypy import array a = array(range(5)) a[::-1] = a assert (a == [0, 1, 2, 1, 0]).all() # but we force intermediates a = array(range(5)) a[::-1] = a + a assert (a == [8, 6, 4, 2, 0]).all()
def test_pow(self): from _numpypy import array a = array(range(5), float) b = a**a for i in range(5): assert b[i] == i**i a = array(range(5)) assert (a**2 == a * a).all()
def test_true_divide(self): from _numpypy import array, true_divide a = array([0, 1, 2, 3, 4, 1, -1]) b = array([4, 4, 4, 4, 4, 0, 0]) c = true_divide(a, b) assert (c == [0.0, 0.25, 0.5, 0.75, 1.0, float('inf'), float('-inf')]).all() assert math.isnan(true_divide(0, 0))
def test_abs(self): from _numpypy import array a = array([1., -2., 3., -4., -5.]) b = abs(a) for i in range(5): assert b[i] == abs(a[i]) a = abs(array(range(-5, 5), dtype="int8")) for i in range(-5, 5): assert a[i + 5] == abs(i)
def test_setslice_array(self): from _numpypy import array a = array(range(5)) b = array(range(2)) a[1:4:2] = b assert a[1] == 0. assert a[3] == 1. b[::-1] = b assert b[0] == 0. assert b[1] == 0.
def test_sum(self): from _numpypy import array a = array(range(5)) assert a.sum() == 10.0 assert a[:4].sum() == 6.0 a = array([True] * 5, bool) assert a.sum() == 5 raises(TypeError, 'a.sum(2, 3)')
def test_divide(self): from _numpypy import array, divide a = array([-5.0, -0.0, 1.0]) b = array([ 3.0, -2.0,-3.0]) c = divide(a, b) for i in range(3): assert c[i] == a[i] / b[i] assert (divide(array([-10]), array([2])) == array([-5])).all()
def test_pos(self): from _numpypy import array a = array([1., -2., 3., -4., -5.]) b = +a for i in range(5): assert b[i] == a[i] a = +array(range(5)) for i in range(5): assert a[i] == i
def test_neg(self): from _numpypy import array a = array([1., -2., 3., -4., -5.]) b = -a for i in range(5): assert b[i] == -a[i] a = -array(range(5), dtype="int8") for i in range(5): assert a[i] == -i
def test_divide(self): from _numpypy import array, divide a = array([-5.0, -0.0, 1.0]) b = array([3.0, -2.0, -3.0]) c = divide(a, b) for i in range(3): assert c[i] == a[i] / b[i] assert (divide(array([-10]), array([2])) == array([-5])).all()
def test_overflow(self): from _numpypy import array, dtype assert array([128], 'b')[0] == -128 assert array([256], 'B')[0] == 0 assert array([32768], 'h')[0] == -32768 assert array([65536], 'H')[0] == 0 if dtype('l').itemsize == 4: # 32-bit raises(OverflowError, "array([2**32/2], 'i')") raises(OverflowError, "array([2**32], 'I')") raises(OverflowError, "array([2**64/2], 'q')") raises(OverflowError, "array([2**64], 'Q')")
def test_init_2(self): import _numpypy raises(ValueError, _numpypy.array, [[1], 2]) raises(ValueError, _numpypy.array, [[1, 2], [3]]) raises(ValueError, _numpypy.array, [[[1, 2], [3, 4], 5]]) raises(ValueError, _numpypy.array, [[[1, 2], [3, 4], [5]]]) a = _numpypy.array([[1, 2], [4, 5]]) assert a[0, 1] == 2 assert a[0][1] == 2 a = _numpypy.array(([[[1, 2], [3, 4], [5, 6]]])) assert (a[0, 1] == [3, 4]).all()