Пример #1
0
    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)')
Пример #2
0
    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
Пример #3
0
 def test_sequence(self):
     from numpypy import array, negative, minimum
     a = array(range(3))
     b = [2.0, 1.0, 0.0]
     c = 1.0
     b_neg = negative(b)
     assert isinstance(b_neg, array)
     for i in range(3):
         assert b_neg[i] == -b[i]
     min_a_b = minimum(a, b)
     assert isinstance(min_a_b, array)
     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, array)
     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, array)
     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, array)
     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, array)
     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, array)
     for i in range(3):
         assert min_c_b[i] == min(b[i], c)
Пример #4
0
    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

        a = array([[1, 2], [3, 4]])
        b = negative(a + a)
        assert (b == [[-2, -4], [-6, -8]]).all()
Пример #5
0
 def test_ufunc_cast(self):
     from numpypy import array, negative, add
     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 = d.sum(out=c)
     assert b == c
Пример #6
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()
Пример #7
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
Пример #8
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)
Пример #9
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)