Beispiel #1
0
    def test_cupy_array(self, dtype):
        shape = 2, 3
        a = testing.shaped_arange(shape, self.xp, dtype)
        cupy.array(a, copy=self.copy, ndmin=self.ndmin)

        # Check if cupy.ndarray does not alter
        # the shape of the original array.
        assert a.shape == shape
Beispiel #2
0
def test_place1(arr, mask, vals):
    a = numpy.array(arr)
    ia = dpnp.array(a)
    m = numpy.array(mask)
    im = dpnp.array(m)
    numpy.place(a, m, vals)
    dpnp.place(ia, im, vals)
    numpy.testing.assert_array_equal(a, ia)
Beispiel #3
0
def test_take(array, indices):
    a = numpy.array(array)
    ind = numpy.array(indices)
    ia = dpnp.array(a)
    iind = dpnp.array(ind)
    expected = numpy.take(a, ind)
    result = dpnp.take(ia, iind)
    numpy.testing.assert_array_equal(expected, result)
Beispiel #4
0
def test_take_along_axis1():
    a = numpy.arange(64).reshape(4, 4, 4)
    ai = dpnp.array(a)
    ind_r = numpy.array([[[3, 0, 2, 1]]])
    ind_r_i = dpnp.array(ind_r)
    for axis in range(3):
        expected = numpy.take_along_axis(a, ind_r, axis)
        result = dpnp.take_along_axis(ai, ind_r_i, axis)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #5
0
def test_put_along_axis2():
    a = numpy.arange(64).reshape(4, 4, 4)
    ai = dpnp.array(a)
    ind_r = numpy.array([[[3, 0, 2, 1]]])
    ind_r_i = dpnp.array(ind_r)
    for axis in range(3):
        numpy.put_along_axis(a, ind_r, [100, 200, 300, 400], axis)
        dpnp.put_along_axis(ai, ind_r_i, [100, 200, 300, 400], axis)
        numpy.testing.assert_array_equal(a, ai)
Beispiel #6
0
def test_put_along_axis_val_int():
    a = numpy.arange(16).reshape(4, 4)
    ai = dpnp.array(a)
    ind_r = numpy.array([[3, 0, 2, 1]])
    ind_r_i = dpnp.array(ind_r)
    for axis in range(2):
        numpy.put_along_axis(a, ind_r, 777, axis)
        dpnp.put_along_axis(ai, ind_r_i, 777, axis)
        numpy.testing.assert_array_equal(a, ai)
    def test_trapz_without_params(self, y_array, x_array):
        y = numpy.array(y_array)
        iy = inp.array(y)

        x = numpy.array(x_array)
        ix = inp.array(x)

        result = inp.trapz(iy, x=ix)
        expected = numpy.trapz(y, x=x)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #8
0
def test_multiply_dtype(dtype1, dtype2, data):
    np_a = numpy.array(data, dtype=dtype1)
    dpnp_a = dpnp.array(data, dtype=dtype1)

    np_b = numpy.array(data, dtype=dtype2)
    dpnp_b = dpnp.array(data, dtype=dtype2)

    result = numpy.multiply(dpnp_a, dpnp_b)
    expected = numpy.multiply(np_a, np_b)
    numpy.testing.assert_array_equal(result, expected)
Beispiel #9
0
    def test_cross_3x3(self, x1, x2, axisa, axisb, axisc, axis):
        np_x1 = numpy.array(x1)
        dpnp_x1 = dpnp.array(x1)

        np_x2 = numpy.array(x2)
        dpnp_x2 = dpnp.array(x2)

        result = dpnp.cross(dpnp_x1, dpnp_x2, axisa, axisb, axisc, axis)
        expected = numpy.cross(np_x1, np_x2, axisa, axisb, axisc, axis)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #10
0
def test_dot_ones(type):
    n = 10**5
    a = numpy.ones(n, dtype=type)
    b = numpy.ones(n, dtype=type)
    ia = inp.array(a)
    ib = inp.array(b)

    result = inp.dot(ia, ib)
    expected = numpy.dot(a, b)
    numpy.testing.assert_array_equal(expected, result)
Beispiel #11
0
def test_putmask3(arr, mask, vals):
    a = numpy.array(arr)
    ia = dpnp.array(a)
    m = numpy.array(mask)
    im = dpnp.array(m)
    v = numpy.array(vals)
    iv = dpnp.array(v)
    numpy.putmask(a, m, v)
    dpnp.putmask(ia, im, iv)
    numpy.testing.assert_array_equal(a, ia)
Beispiel #12
0
def test_multiply_dtype(dtype1, dtype2, data):
    a = numpy.array(data, dtype=dtype1)
    ia = inp.array(data, dtype=dtype1)

    b = numpy.array(data, dtype=dtype2)
    ib = inp.array(data, dtype=dtype2)

    result = numpy.multiply(ia, ib)
    expected = numpy.multiply(a, b)
    numpy.testing.assert_array_equal(result, expected)
Beispiel #13
0
    def test_cross_3x3(self, x1, x2, axisa, axisb, axisc, axis):
        x1_ = numpy.array(x1)
        ix1_ = inp.array(x1_)

        x2_ = numpy.array(x2)
        ix2_ = inp.array(x2_)

        result = inp.cross(ix1_, ix2_, axisa, axisb, axisc, axis)
        expected = numpy.cross(x1_, x2_, axisa, axisb, axisc, axis)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #14
0
    def test_trapz_with_x_params(self, y_array, x_array, data_type_y,
                                 data_type_x):
        np_y = numpy.array(y_array, dtype=data_type_y)
        dpnp_y = dpnp.array(y_array, dtype=data_type_y)

        np_x = numpy.array(x_array, dtype=data_type_x)
        dpnp_x = dpnp.array(x_array, dtype=data_type_x)

        result = dpnp.trapz(dpnp_y, dpnp_x)
        expected = numpy.trapz(np_y, np_x)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #15
0
 def test_one_bin(self):
     # Ticket 632
     hist, edges = dpnp.histogram([1, 2, 3, 4], [1, 2])
     numpy.testing.assert_array_equal(hist, [
         2,
     ])
     numpy.testing.assert_array_equal(edges, [1, 2])
     numpy.testing.assert_raises(ValueError, dpnp.histogram, [1, 2], bins=0)
     h, e = dpnp.histogram([1, 2], bins=1)
     numpy.testing.assert_equal(h, dpnp.array([2]))
     numpy.testing.assert_allclose(e, dpnp.array([1., 2.]))
Beispiel #16
0
def test_choose():
    a = numpy.r_[:4]
    ia = dpnp.array(a)
    b = numpy.r_[-4:0]
    ib = dpnp.array(b)
    c = numpy.r_[100:500:100]
    ic = dpnp.array(c)

    expected = numpy.choose([0, 0, 0, 0], [a, b, c])
    result = dpnp.choose([0, 0, 0, 0], [ia, ib, ic])
    numpy.testing.assert_array_equal(expected, result)
Beispiel #17
0
def test_dot_arange(type):
    n = 10**2
    m = 10**3
    a = numpy.hstack((numpy.arange(n, dtype=type),) * m)
    b = numpy.flipud(a)
    ia = inp.array(a)
    ib = inp.array(b)

    result = inp.dot(ia, ib)
    expected = numpy.dot(a, b)
    numpy.testing.assert_allclose(expected, result)
Beispiel #18
0
    def test_trapz_with_x_params(self, y_array, x_array, data_type_y,
                                 data_type_x):
        y = numpy.array(y_array, dtype=data_type_y)
        iy = inp.array(y_array, dtype=data_type_y)

        x = numpy.array(x_array, dtype=data_type_x)
        ix = inp.array(x_array, dtype=data_type_x)

        result = inp.trapz(iy, x=ix)
        expected = numpy.trapz(y, x=x)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #19
0
    def test_concatenate(self):
        # Test concatenate function
        # One sequence returns unmodified (but as array)
        r4 = list(range(4))
        numpy.testing.assert_array_equal(dpnp.concatenate((r4, )), r4)
        # Any sequence
        numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), )), r4)
        numpy.testing.assert_array_equal(dpnp.concatenate((dpnp.array(r4), )),
                                         r4)
        # 1D default concatenation
        r3 = list(range(3))
        numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3)), r4 + r3)
        # Mixed sequence types
        numpy.testing.assert_array_equal(dpnp.concatenate((tuple(r4), r3)),
                                         r4 + r3)
        numpy.testing.assert_array_equal(
            dpnp.concatenate((dpnp.array(r4), r3)), r4 + r3)
        # Explicit axis specification
        numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), 0),
                                         r4 + r3)
        # Including negative
        numpy.testing.assert_array_equal(dpnp.concatenate((r4, r3), -1),
                                         r4 + r3)
        # 2D
        a23 = dpnp.array([[10, 11, 12], [13, 14, 15]])
        a13 = dpnp.array([[0, 1, 2]])
        res = dpnp.array([[10, 11, 12], [13, 14, 15], [0, 1, 2]])
        numpy.testing.assert_array_equal(dpnp.concatenate((a23, a13)), res)
        numpy.testing.assert_array_equal(dpnp.concatenate((a23, a13), 0), res)
        numpy.testing.assert_array_equal(dpnp.concatenate((a23.T, a13.T), 1),
                                         res.T)
        numpy.testing.assert_array_equal(dpnp.concatenate((a23.T, a13.T), -1),
                                         res.T)
        # Arrays much match shape
        numpy.testing.assert_raises(ValueError, dpnp.concatenate,
                                    (a23.T, a13.T), 0)
        # 3D
        res = dpnp.reshape(dpnp.arange(2 * 3 * 7), (2, 3, 7))
        a0 = res[..., :4]
        a1 = res[..., 4:6]
        a2 = res[..., 6:]
        numpy.testing.assert_array_equal(dpnp.concatenate((a0, a1, a2), 2),
                                         res)
        numpy.testing.assert_array_equal(dpnp.concatenate((a0, a1, a2), -1),
                                         res)
        numpy.testing.assert_array_equal(
            dpnp.concatenate((a0.T, a1.T, a2.T), 0), res.T)

        out = dpnp.copy(res)
        rout = dpnp.concatenate((a0, a1, a2), 2, out=out)
        numpy.testing.assert_(out is rout)
        numpy.testing.assert_equal(res, rout)
Beispiel #20
0
    def test_ediff1d_args(self):
        a = numpy.array([1, 2, 4, 7, 0])
        ia = inp.array(a)

        to_begin = numpy.array([-20, -30])
        i_to_begin = inp.array(to_begin)

        to_end = numpy.array([20, 15])
        i_to_end = inp.array(to_end)

        result = inp.ediff1d(ia, to_end=i_to_end, to_begin=i_to_begin)
        expected = numpy.ediff1d(a, to_end=to_end, to_begin=to_begin)
        numpy.testing.assert_array_equal(expected, result)
Beispiel #21
0
    def _test_binary_int(self, name):
        data1, data2 = [-3, -2, -1, 0, 1, 2, 3], [0, 1, 2, 3, 4, 5, 6]
        for dtype in (numpy.int32, numpy.int64):
            with self.subTest(dtype=dtype):
                a = inp.array(data1, dtype=dtype)
                b = inp.array(data2, dtype=dtype)
                result = getattr(inp, name)(a, b)

                a = numpy.array(data1, dtype=dtype)
                b = numpy.array(data2, dtype=dtype)
                expected = getattr(numpy, name)(a, b)

                numpy.testing.assert_array_equal(result, expected)
Beispiel #22
0
    def test_sin_ordinary(self):
        array_data = numpy.arange(10)
        out = numpy.empty(10, dtype=numpy.float64)

        # DPNP
        dp_array = dpnp.array(array_data, dtype=dpnp.float64)
        dp_out = dpnp.array(out, dtype=dpnp.float64)
        result = dpnp.sin(dp_array, out=dp_out)

        # original
        np_array = numpy.array(array_data, dtype=numpy.float64)
        expected = numpy.sin(np_array, out=out)

        numpy.testing.assert_array_equal(expected, result)
Beispiel #23
0
def test_less():
    a = numpy.array([1, 2, 3, 4, 5, 6, 7, 8])
    ia = dpnp.array(a)
    for i in range(len(a) + 1):
        np_res = (a < i)
        dpnp_res = (ia < i)
        numpy.testing.assert_equal(dpnp_res, np_res)
Beispiel #24
0
def test_any(type, shape):
    size = 1
    for i in range(len(shape)):
        size *= shape[i]

    for i in range(2**size):
        t = i

        a = numpy.empty(size, dtype=type)

        for j in range(size):
            a[j] = 0 if t % 2 == 0 else j + 1
            t = t >> 1

        a = a.reshape(shape)

        ia = dpnp.array(a)

        np_res = numpy.any(a)
        dpnp_res = dpnp.any(ia)
        numpy.testing.assert_allclose(dpnp_res, np_res)

        np_res = a.any()
        dpnp_res = ia.any()
        numpy.testing.assert_allclose(dpnp_res, np_res)
Beispiel #25
0
def test_unique(array):
    np_a = numpy.array(array)
    dpnp_a = dpnp.array(array)

    expected = numpy.unique(np_a)
    result = dpnp.unique(dpnp_a)
    numpy.testing.assert_array_equal(expected, result)
Beispiel #26
0
def test_eig_arange(type, size):
    a = numpy.arange(size * size, dtype=type).reshape((size, size))
    symm_orig = numpy.tril(a) + numpy.tril(a, -1).T + numpy.diag(
        numpy.full((size, ), size * size, dtype=type))
    symm = symm_orig
    dpnp_symm_orig = inp.array(symm)
    dpnp_symm = dpnp_symm_orig

    dpnp_val, dpnp_vec = inp.linalg.eig(dpnp_symm)
    np_val, np_vec = numpy.linalg.eig(symm)

    # DPNP sort val/vec by abs value
    vvsort(dpnp_val, dpnp_vec, size)

    # NP sort val/vec by abs value
    vvsort(np_val, np_vec, size)

    # NP change sign of vectors
    for i in range(np_vec.shape[1]):
        if np_vec[0, i] * dpnp_vec[0, i] < 0:
            np_vec[:, i] = -np_vec[:, i]

    numpy.testing.assert_array_equal(symm_orig, symm)
    numpy.testing.assert_array_equal(dpnp_symm_orig, dpnp_symm)

    assert (dpnp_val.dtype == np_val.dtype)
    assert (dpnp_vec.dtype == np_vec.dtype)
    assert (dpnp_val.shape == np_val.shape)
    assert (dpnp_vec.shape == np_vec.shape)

    numpy.testing.assert_allclose(dpnp_val, np_val, rtol=1e-05, atol=1e-05)
    numpy.testing.assert_allclose(dpnp_vec, np_vec, rtol=1e-05, atol=1e-05)
Beispiel #27
0
def test_amin_int(type):
    a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9])
    ia = dpnp.array(a)

    result = dpnp.amin(ia)
    expected = numpy.amin(a)
    numpy.testing.assert_array_equal(expected, result)
Beispiel #28
0
def test_absolute_int(type):
    a = numpy.array([1, 0, 2, -3, -1, 2, 21, -9])
    ia = inp.array(a)

    result = inp.absolute(ia)
    expected = numpy.absolute(a)
    numpy.testing.assert_array_equal(expected, result)
Beispiel #29
0
def test_absolute_float(type):
    a = numpy.array([[-2., 3., 9.1], [-2., 5.0, -2], [1.0, -2., 5.0]])
    ia = inp.array(a)

    result = inp.absolute(ia)
    expected = numpy.absolute(a)
    numpy.testing.assert_array_equal(expected, result)
Beispiel #30
0
def test_absolute_float_3(type):
    a = numpy.array([[[-2., 3.], [9.1, 0.2]], [[-2., 5.0], [-2, -1.2]], [[1.0, -2.], [5.0, -1.1]]])
    ia = inp.array(a)

    result = inp.absolute(ia)
    expected = numpy.absolute(a)
    numpy.testing.assert_array_equal(expected, result)