def test_sign(self): from numpypy import array, sign, dtype reference = [-1.0, 0.0, 0.0, 1.0] a = array([-5.0, -0.0, 0.0, 6.0]) b = sign(a) for i in range(4): assert b[i] == reference[i] a = sign(array(range(-5, 5))) ref = [-1, -1, -1, -1, -1, 0, 1, 1, 1, 1] for i in range(10): assert a[i] == ref[i] a = sign(array([10+10j, -10+10j, 0+10j, 0-10j, 0+0j, 0-0j], dtype=complex)) ref = [1, -1, 1, -1, 0, 0] assert (a == ref).all()
def test_sign(self): from numpypy import array, sign, dtype reference = [-1.0, 0.0, 0.0, 1.0] a = array([-5.0, -0.0, 0.0, 6.0]) b = sign(a) for i in range(4): assert b[i] == reference[i] a = sign(array(range(-5, 5))) ref = [-1, -1, -1, -1, -1, 0, 1, 1, 1, 1] for i in range(10): assert a[i] == ref[i] a = sign(array([True, False], dtype=bool)) assert a.dtype == dtype("int8") assert a[0] == 1 assert a[1] == 0
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_basic(self): import sys from numpypy import (dtype, add, array, dtype, subtract as sub, multiply, divide, negative, absolute as abs, floor_divide, real, imag, sign) from numpypy import (equal, not_equal, greater, greater_equal, less, less_equal, isnan) assert real(4.0) == 4.0 assert imag(0.0) == 0.0 a = array([complex(3.0, 4.0)]) b = a.real b[0] = 1024 assert a[0].real == 1024 assert b.dtype == dtype(float) a = array(complex(3.0, 4.0)) b = a.real assert b == array(3) assert a.imag == array(4) a.real = 1024 a.imag = 2048 assert a.real == 1024 and a.imag == 2048 assert b.dtype == dtype(float) a = array(4.0) b = a.imag assert b == 0 assert b.dtype == dtype(float) exc = raises(TypeError, 'a.imag = 1024') assert str(exc.value).startswith("array does not have imaginary") exc = raises(ValueError, 'a.real = [1, 3]') assert str(exc.value) == \ "could not broadcast input array from shape (2) into shape ()" a = array('abc') assert str(a.real) == 'abc' assert str(a.imag) == '' for t in 'complex64', 'complex128', 'clongdouble': complex_ = dtype(t).type O = complex(0, 0) c0 = complex_(complex(2.5, 0)) c1 = complex_(complex(1, 2)) c2 = complex_(complex(3, 4)) c3 = complex_(complex(-3, -3)) assert equal(c0, 2.5) assert equal(c1, complex_(complex(1, 2))) assert equal(c1, complex(1, 2)) assert equal(c1, c1) assert not_equal(c1, c2) assert not equal(c1, c2) assert less(c1, c2) assert less_equal(c1, c2) assert less_equal(c1, c1) assert not less(c1, c1) assert greater(c2, c1) assert greater_equal(c2, c1) assert not greater(c1, c2) assert add(c1, c2) == complex_(complex(4, 6)) assert add(c1, c2) == complex(4, 6) assert sub(c0, c0) == sub(c1, c1) == 0 assert sub(c1, c2) == complex(-2, -2) assert negative(complex(1,1)) == complex(-1, -1) assert negative(complex(0, 0)) == 0 assert multiply(1, c1) == c1 assert multiply(2, c2) == complex(6, 8) assert multiply(c1, c2) == complex(-5, 10) assert divide(c0, 1) == c0 assert divide(c2, -1) == negative(c2) assert divide(c1, complex(0, 1)) == complex(2, -1) n = divide(c1, O) assert repr(n.real) == 'inf' assert repr(n.imag).startswith('inf') #can be inf*j or infj assert divide(c0, c0) == 1 res = divide(c2, c1) assert abs(res.real-2.2) < 0.001 assert abs(res.imag+0.4) < 0.001 assert floor_divide(c0, c0) == complex(1, 0) assert isnan(floor_divide(c0, complex(0, 0)).real) assert floor_divide(c0, complex(0, 0)).imag == 0.0 assert abs(c0) == 2.5 assert abs(c2) == 5 assert sign(complex(0, 0)) == 0 assert sign(complex(-42, 0)) == -1 assert sign(complex(42, 0)) == 1 assert sign(complex(-42, 2)) == -1 assert sign(complex(42, 2)) == 1 assert sign(complex(-42, -3)) == -1 assert sign(complex(42, -3)) == 1 assert sign(complex(0, -42)) == -1 assert sign(complex(0, 42)) == 1 inf_c = complex_(complex(float('inf'), 0.)) assert repr(abs(inf_c)) == 'inf' assert repr(abs(complex(float('nan'), float('nan')))) == 'nan' # numpy actually raises an AttributeError, # but numpypy raises a TypeError if '__pypy__' in sys.builtin_module_names: exct, excm = TypeError, 'readonly attribute' else: exct, excm = AttributeError, 'is not writable' exc = raises(exct, 'c2.real = 10.') assert excm in exc.value[0] exc = raises(exct, 'c2.imag = 10.') assert excm in exc.value[0] assert(real(c2) == 3.0) assert(imag(c2) == 4.0)
def test_basic(self): from numpypy import (complex128, complex64, add, array, dtype, subtract as sub, multiply, divide, negative, absolute as abs, floor_divide, real, imag, sign) from numpypy import (equal, not_equal, greater, greater_equal, less, less_equal, isnan) complex_dtypes = [complex64, complex128] try: from numpypy import clongfloat complex_dtypes.append(clongfloat) except: pass assert real(4.0) == 4.0 assert imag(0.0) == 0.0 a = array([complex(3.0, 4.0)]) b = a.real b[0] = 1024 assert a[0].real == 1024 assert b.dtype == dtype(float) a = array(complex(3.0, 4.0)) b = a.real assert b == array(3) assert a.imag == array(4) a.real = 1024 a.imag = 2048 assert a.real == 1024 and a.imag == 2048 assert b.dtype == dtype(float) a = array(4.0) b = a.imag assert b == 0 assert b.dtype == dtype(float) exc = raises(TypeError, 'a.imag = 1024') assert str(exc.value).startswith("array does not have imaginary") exc = raises(ValueError, 'a.real = [1, 3]') assert str(exc.value) == \ "could not broadcast input array from shape (2) into shape ()" a = array('abc') assert str(a.real) == 'abc' # numpy imag for flexible types returns self assert str(a.imag) == 'abc' for complex_ in complex_dtypes: O = complex(0, 0) c0 = complex_(complex(2.5, 0)) c1 = complex_(complex(1, 2)) c2 = complex_(complex(3, 4)) c3 = complex_(complex(-3, -3)) assert equal(c0, 2.5) assert equal(c1, complex_(complex(1, 2))) assert equal(c1, complex(1, 2)) assert equal(c1, c1) assert not_equal(c1, c2) assert not equal(c1, c2) assert less(c1, c2) assert less_equal(c1, c2) assert less_equal(c1, c1) assert not less(c1, c1) assert greater(c2, c1) assert greater_equal(c2, c1) assert not greater(c1, c2) assert add(c1, c2) == complex_(complex(4, 6)) assert add(c1, c2) == complex(4, 6) assert sub(c0, c0) == sub(c1, c1) == 0 assert sub(c1, c2) == complex(-2, -2) assert negative(complex(1,1)) == complex(-1, -1) assert negative(complex(0, 0)) == 0 assert multiply(1, c1) == c1 assert multiply(2, c2) == complex(6, 8) assert multiply(c1, c2) == complex(-5, 10) assert divide(c0, 1) == c0 assert divide(c2, -1) == negative(c2) assert divide(c1, complex(0, 1)) == complex(2, -1) n = divide(c1, O) assert repr(n.real) == 'inf' assert repr(n.imag).startswith('inf') #can be inf*j or infj assert divide(c0, c0) == 1 res = divide(c2, c1) assert abs(res.real-2.2) < 0.001 assert abs(res.imag+0.4) < 0.001 assert floor_divide(c0, c0) == complex(1, 0) assert isnan(floor_divide(c0, complex(0, 0)).real) assert floor_divide(c0, complex(0, 0)).imag == 0.0 assert abs(c0) == 2.5 assert abs(c2) == 5 assert sign(complex(0, 0)) == 0 assert sign(complex(-42, 0)) == -1 assert sign(complex(42, 0)) == 1 assert sign(complex(-42, 2)) == -1 assert sign(complex(42, 2)) == 1 assert sign(complex(-42, -3)) == -1 assert sign(complex(42, -3)) == 1 assert sign(complex(0, -42)) == -1 assert sign(complex(0, 42)) == 1 inf_c = complex_(complex(float('inf'), 0.)) assert repr(abs(inf_c)) == 'inf' assert repr(abs(complex(float('nan'), float('nan')))) == 'nan' # numpy actually raises an AttributeError, # but numpypy raises a TypeError exc = raises((TypeError, AttributeError), 'c2.real = 10.') assert str(exc.value) == "readonly attribute" exc = raises((TypeError, AttributeError), 'c2.imag = 10.') assert str(exc.value) == "readonly attribute" assert(real(c2) == 3.0) assert(imag(c2) == 4.0)