Пример #1
0
    def test_blocked(self):
        # test alignments offsets for simd instructions
        # alignments for vz + 2 * (vs - 1) + 1
        for dt, sz in [(np.float32, 11), (np.float64, 7), (np.int32, 11)]:
            for out, inp1, inp2, msg in _gen_alignment_data(dtype=dt,
                                                            type='binary',
                                                            max_size=sz):
                exp1 = np.ones_like(inp1)
                inp1[...] = np.ones_like(inp1)
                inp2[...] = np.zeros_like(inp2)
                assert_almost_equal(np.add(inp1, inp2), exp1, err_msg=msg)
                assert_almost_equal(np.add(inp1, 2), exp1 + 2, err_msg=msg)
                assert_almost_equal(np.add(1, inp2), exp1, err_msg=msg)

                np.add(inp1, inp2, out=out)
                assert_almost_equal(out, exp1, err_msg=msg)

                inp2[...] += np.arange(inp2.size, dtype=dt) + 1
                assert_almost_equal(np.square(inp2),
                                    np.multiply(inp2, inp2),
                                    err_msg=msg)
                # skip true divide for ints
                if dt != np.int32 or (sys.version_info.major < 3
                                      and not sys.py3kwarning):
                    assert_almost_equal(np.reciprocal(inp2),
                                        np.divide(1, inp2),
                                        err_msg=msg)

                inp1[...] = np.ones_like(inp1)
                np.add(inp1, 2, out=out)
                assert_almost_equal(out, exp1 + 2, err_msg=msg)
                inp2[...] = np.ones_like(inp2)
                np.add(2, inp2, out=out)
                assert_almost_equal(out, exp1 + 2, err_msg=msg)
Пример #2
0
 def test_lower_align(self):
     # check data that is not aligned to element size
     # i.e doubles are aligned to 4 bytes on i386
     d = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
     o = np.zeros(23 * 8, dtype=np.int8)[4:-4].view(np.float64)
     assert_almost_equal(d + d, d * 2)
     np.add(d, d, out=o)
     np.add(np.ones_like(d), d, out=o)
     np.add(d, np.ones_like(d), out=o)
     np.add(np.ones_like(d), d)
     np.add(d, np.ones_like(d))
Пример #3
0
    def test_ufunc_return_ndarray(self):
        fp = memmap(self.tmpfp, dtype=self.dtype, shape=self.shape)
        fp[:] = self.data

        with suppress_warnings() as sup:
            sup.filter(FutureWarning, "np.average currently does not preserve")
            for unary_op in [sum, average, product]:
                result = unary_op(fp)
                assert_(isscalar(result))
                assert_(result.__class__ is self.data[0, 0].__class__)

                assert_(unary_op(fp, axis=0).__class__ is ndarray)
                assert_(unary_op(fp, axis=1).__class__ is ndarray)

        for binary_op in [add, subtract, multiply]:
            assert_(binary_op(fp, self.data).__class__ is ndarray)
            assert_(binary_op(self.data, fp).__class__ is ndarray)
            assert_(binary_op(fp, fp).__class__ is ndarray)

        fp += 1
        assert (fp.__class__ is memmap)
        add(fp, 1, out=fp)
        assert (fp.__class__ is memmap)
Пример #4
0
def print_coercion_table(ntypes,
                         inputfirstvalue,
                         inputsecondvalue,
                         firstarray,
                         use_promote_types=False):
    print('+', end=' ')
    for char in ntypes:
        print(char, end=' ')
    print()
    for row in ntypes:
        if row == 'O':
            rowtype = GenericObject
        else:
            rowtype = np.obj2sctype(row)

        print(row, end=' ')
        for col in ntypes:
            if col == 'O':
                coltype = GenericObject
            else:
                coltype = np.obj2sctype(col)
            try:
                if firstarray:
                    rowvalue = np.array([rowtype(inputfirstvalue)],
                                        dtype=rowtype)
                else:
                    rowvalue = rowtype(inputfirstvalue)
                colvalue = coltype(inputsecondvalue)
                if use_promote_types:
                    char = np.promote_types(rowvalue.dtype,
                                            colvalue.dtype).char
                else:
                    value = np.add(rowvalue, colvalue)
                    if isinstance(value, np.ndarray):
                        char = value.dtype.char
                    else:
                        char = np.dtype(type(value)).char
            except ValueError:
                char = '!'
            except OverflowError:
                char = '@'
            except TypeError:
                char = '#'
            print(char, end=' ')
        print()
Пример #5
0
 def test_testArithmetic(self):
     # Test of basic arithmetic.
     (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
     a2d = array([[1, 2], [0, 4]])
     a2dm = masked_array(a2d, [[0, 0], [1, 0]])
     assert_(eq(a2d * a2d, a2d * a2dm))
     assert_(eq(a2d + a2d, a2d + a2dm))
     assert_(eq(a2d - a2d, a2d - a2dm))
     for s in [(12,), (4, 3), (2, 6)]:
         x = x.reshape(s)
         y = y.reshape(s)
         xm = xm.reshape(s)
         ym = ym.reshape(s)
         xf = xf.reshape(s)
         assert_(eq(-x, -xm))
         assert_(eq(x + y, xm + ym))
         assert_(eq(x - y, xm - ym))
         assert_(eq(x * y, xm * ym))
         with np.errstate(divide='ignore', invalid='ignore'):
             assert_(eq(x / y, xm / ym))
         assert_(eq(a10 + y, a10 + ym))
         assert_(eq(a10 - y, a10 - ym))
         assert_(eq(a10 * y, a10 * ym))
         with np.errstate(divide='ignore', invalid='ignore'):
             assert_(eq(a10 / y, a10 / ym))
         assert_(eq(x + a10, xm + a10))
         assert_(eq(x - a10, xm - a10))
         assert_(eq(x * a10, xm * a10))
         assert_(eq(x / a10, xm / a10))
         assert_(eq(x ** 2, xm ** 2))
         assert_(eq(abs(x) ** 2.5, abs(xm) ** 2.5))
         assert_(eq(x ** y, xm ** ym))
         assert_(eq(np.add(x, y), add(xm, ym)))
         assert_(eq(np.subtract(x, y), subtract(xm, ym)))
         assert_(eq(np.multiply(x, y), multiply(xm, ym)))
         with np.errstate(divide='ignore', invalid='ignore'):
             assert_(eq(np.divide(x, y), divide(xm, ym)))
Пример #6
0
    def test_half_ufuncs(self):
        """Test the various ufuncs"""

        a = np.array([0, 1, 2, 4, 2], dtype=float16)
        b = np.array([-2, 5, 1, 4, 3], dtype=float16)
        c = np.array([0, -1, -np.inf, np.nan, 6], dtype=float16)

        assert_equal(np.add(a, b), [-2, 6, 3, 8, 5])
        assert_equal(np.subtract(a, b), [2, -4, 1, 0, -1])
        assert_equal(np.multiply(a, b), [0, 5, 2, 16, 6])
        assert_equal(np.divide(a, b), [0, 0.199951171875, 2, 1, 0.66650390625])

        assert_equal(np.equal(a, b), [False, False, False, True, False])
        assert_equal(np.not_equal(a, b), [True, True, True, False, True])
        assert_equal(np.less(a, b), [False, True, False, False, True])
        assert_equal(np.less_equal(a, b), [False, True, False, True, True])
        assert_equal(np.greater(a, b), [True, False, True, False, False])
        assert_equal(np.greater_equal(a, b), [True, False, True, True, False])
        assert_equal(np.logical_and(a, b), [False, True, True, True, True])
        assert_equal(np.logical_or(a, b), [True, True, True, True, True])
        assert_equal(np.logical_xor(a, b), [True, False, False, False, False])
        assert_equal(np.logical_not(a), [True, False, False, False, False])

        assert_equal(np.isnan(c), [False, False, False, True, False])
        assert_equal(np.isinf(c), [False, False, True, False, False])
        assert_equal(np.isfinite(c), [True, True, False, False, True])
        assert_equal(np.signbit(b), [True, False, False, False, False])

        assert_equal(np.copysign(b, a), [2, 5, 1, 4, 3])

        assert_equal(np.maximum(a, b), [0, 5, 2, 4, 3])
        x = np.maximum(b, c)
        assert_(np.isnan(x[3]))
        x[3] = 0
        assert_equal(x, [0, 5, 1, 0, 6])
        assert_equal(np.minimum(a, b), [-2, 1, 1, 4, 2])
        x = np.minimum(b, c)
        assert_(np.isnan(x[3]))
        x[3] = 0
        assert_equal(x, [-2, -1, -np.inf, 0, 3])
        assert_equal(np.fmax(a, b), [0, 5, 2, 4, 3])
        assert_equal(np.fmax(b, c), [0, 5, 1, 4, 6])
        assert_equal(np.fmin(a, b), [-2, 1, 1, 4, 2])
        assert_equal(np.fmin(b, c), [-2, -1, -np.inf, 4, 3])

        assert_equal(np.floor_divide(a, b), [0, 0, 2, 1, 0])
        assert_equal(np.remainder(a, b), [0, 1, 0, 0, 2])
        assert_equal(np.divmod(a, b), ([0, 0, 2, 1, 0], [0, 1, 0, 0, 2]))
        assert_equal(np.square(b), [4, 25, 1, 16, 9])
        assert_equal(np.reciprocal(b),
                     [-0.5, 0.199951171875, 1, 0.25, 0.333251953125])
        assert_equal(np.ones_like(b), [1, 1, 1, 1, 1])
        assert_equal(np.conjugate(b), b)
        assert_equal(np.absolute(b), [2, 5, 1, 4, 3])
        assert_equal(np.negative(b), [2, -5, -1, -4, -3])
        assert_equal(np.positive(b), b)
        assert_equal(np.sign(b), [-1, 1, 1, 1, 1])
        assert_equal(np.modf(b), ([0, 0, 0, 0, 0], b))
        assert_equal(np.frexp(b),
                     ([-0.5, 0.625, 0.5, 0.5, 0.75], [2, 3, 1, 3, 2]))
        assert_equal(np.ldexp(b, [0, 1, 2, 4, 2]), [-2, 10, 4, 64, 12])
Пример #7
0
 def test_leak(self):
     # test leak of scalar objects
     # a leak would show up in valgrind as still-reachable of ~2.6MB
     for i in range(200000):
         np.add(1, 1)