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_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_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_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_negative(self): from _numpypy import array, negative a = array([-5.0, 0.0, 1.0]) b = negative(a) for i in range(3): assert b[i] == -a[i] a = array([-5.0, 1.0]) b = negative(a) a[0] = 5.0 assert b[0] == 5.0 a = array(range(30)) assert negative(a + a)[3] == -6
def test_sequence(self): from _numpypy import array, ndarray, negative, minimum a = array(range(3)) b = [2.0, 1.0, 0.0] c = 1.0 b_neg = negative(b) assert isinstance(b_neg, ndarray) for i in range(3): assert b_neg[i] == -b[i] min_a_b = minimum(a, b) assert isinstance(min_a_b, ndarray) for i in range(3): assert min_a_b[i] == min(a[i], b[i]) min_b_a = minimum(b, a) assert isinstance(min_b_a, ndarray) for i in range(3): assert min_b_a[i] == min(a[i], b[i]) min_a_c = minimum(a, c) assert isinstance(min_a_c, ndarray) for i in range(3): assert min_a_c[i] == min(a[i], c) min_c_a = minimum(c, a) assert isinstance(min_c_a, ndarray) for i in range(3): assert min_c_a[i] == min(a[i], c) min_b_c = minimum(b, c) assert isinstance(min_b_c, ndarray) for i in range(3): assert min_b_c[i] == min(b[i], c) min_c_b = minimum(c, b) assert isinstance(min_c_b, ndarray) for i in range(3): assert min_c_b[i] == min(b[i], c)
def test_single_item(self): from _numpypy import negative, sign, minimum assert negative(5.0) == -5.0 assert sign(-0.0) == 0.0 assert minimum(2.0, 3.0) == 2.0
def test_ufunc_negative(self): from _numpypy import array, negative a = array([[1, 2], [3, 4]]) b = negative(a + a) assert (b == [[-2, -4], [-6, -8]]).all()