Ejemplo n.º 1
0
def ediff1d(x1, to_end=None, to_begin=None):
    """
    The differences between consecutive elements of an array.

    For full documentation refer to :obj:`numpy.ediff1d`.

    Limitations
    -----------
        Parameter ``x1``, ``to_begin``, ``to_end`` are supported as :obj:`dpnp.ndarray`.
        Otherwise the function will be executed sequentially on CPU.
        Input array data types are limited by supported DPNP :ref:`Data types`.

    ..seealso:: :obj:`dpnp.diff` : Calculate the n-th discrete difference along the given axis.

    Examples
    --------
    >>> import dpnp as np
    >>> a = np.array([1, 2, 4, 7, 0])
    >>> result = np.ediff1d(a)
    >>> [x for x in result]
    [1, 2, 3, -7]
    >>> result = np.ediff1d(a, to_begin=np.array([-99, -88]), to_end=np.array([88, 99]))
    >>> [x for x in result]
    [-99, -88, 1, 2, 3, -7, 88, 99]
    >>> b = np.array([[1, 2, 4], [1, 6, 24]])
    >>> result = np.ediff1d(b)
    >>> [x for x in result]
    [1, 2, -3, 5, 18]

    """

    if not use_origin_backend(x1):

        if not isinstance(x1, dparray):
            pass
        elif not isinstance(to_end, dparray) and to_end is not None:
            pass
        elif not isinstance(to_begin, dparray) and to_begin is not None:
            pass
        else:

            if to_end is None:
                to_end = dpnp.empty(0, dtype=x1.dtype)
            if to_begin is None:
                to_begin = dpnp.empty(0, dtype=x1.dtype)

            return dpnp_ediff1d(x1, to_end=to_end, to_begin=to_begin)

    return call_origin(numpy.ediff1d, x1, to_end=to_end, to_begin=to_begin)
Ejemplo n.º 2
0
    def test_invalid_dtype(self, dtype):

        dp_array = dpnp.arange(10, dtype=dpnp.float64)
        dp_out = dpnp.empty(10, dtype=dtype)

        with pytest.raises(ValueError):
            dpnp.sin(dp_array, out=dp_out)
Ejemplo n.º 3
0
    def test_invalid_shape(self, shape):

        dp_array = dpnp.arange(10, dtype=dpnp.float64)
        dp_out = dpnp.empty(shape, dtype=dpnp.float64)

        with pytest.raises(ValueError):
            dpnp.arctan2(dp_array, dp_array, out=dp_out)
Ejemplo n.º 4
0
 def test_empty_int_huge_size_fill0(self):
     a = cupy.empty(2**31, dtype='b')
     a.fill(0)
     self.assertTrue((a == 0).all())
     # Free huge memory for slow test
     del a
     cupy.get_default_memory_pool().free_all_blocks()
Ejemplo n.º 5
0
 def test_empty_huge_size(self):
     a = cupy.empty((1024, 2048, 1024), dtype='b')
     a.fill(123)
     self.assertTrue((a == 123).all())
     # Free huge memory for slow test
     del a
     cupy.get_default_memory_pool().free_all_blocks()
Ejemplo n.º 6
0
    def test_invalid_shape(self, shape):

        dp_array1 = dpnp.arange(10, dtype=dpnp.float64)
        dp_array2 = dpnp.arange(5, 15, dtype=dpnp.float64)
        dp_out = dpnp.empty(shape, dtype=dpnp.float64)

        with pytest.raises(ValueError):
            dpnp.power(dp_array1, dp_array2, out=dp_out)
Ejemplo n.º 7
0
    def test_empty_like_reshape_cupy_only(self, dtype, order):
        a = testing.shaped_arange((2, 3, 4), cupy, dtype)
        b = cupy.empty_like(a, shape=self.shape)
        b.fill(0)
        c = cupy.empty(self.shape, order=order, dtype=dtype)
        c.fill(0)

        testing.assert_array_equal(b, c)
Ejemplo n.º 8
0
def test_copyto_dtype(in_obj, out_dtype):
    ndarr = numpy.array(in_obj)
    expected = numpy.empty(ndarr.size, dtype=out_dtype)
    numpy.copyto(expected, ndarr)

    dparr = dpnp.array(in_obj)
    result = dpnp.empty(dparr.size, dtype=out_dtype)
    dpnp.copyto(result, dparr)

    numpy.testing.assert_array_equal(result, expected)
Ejemplo n.º 9
0
 def test_empty_like_reshape_contiguity_cupy_only(self, dtype, order):
     a = testing.shaped_arange((2, 3, 4), cupy, dtype)
     b = cupy.empty_like(a, order=order, shape=self.shape)
     b.fill(0)
     c = cupy.empty(self.shape)
     c.fill(0)
     if order in ['f', 'F']:
         self.assertTrue(b.flags.f_contiguous)
     else:
         self.assertTrue(b.flags.c_contiguous)
     testing.assert_array_equal(b, c)
Ejemplo n.º 10
0
 def test_empty_like_reshape_contiguity2_cupy_only(self, dtype, order):
     a = testing.shaped_arange((2, 3, 4), cupy, dtype)
     a = cupy.asfortranarray(a)
     b = cupy.empty_like(a, order=order, shape=self.shape)
     b.fill(0)
     c = cupy.empty(self.shape)
     c.fill(0)
     shape = self.shape if not numpy.isscalar(self.shape) else (
         self.shape, )
     if (order in ['c', 'C']
             or (order in ['k', 'K', None] and len(shape) != a.ndim)):
         self.assertTrue(b.flags.c_contiguous)
     else:
         self.assertTrue(b.flags.f_contiguous)
     testing.assert_array_equal(b, c)
Ejemplo n.º 11
0
def trapz(y, x=None, dx=1.0, **kwargs):
    """
    Integrate along the given axis using the composite trapezoidal rule.

    For full documentation refer to :obj:`numpy.trapz`.

    Limitations
    -----------
        Parameters ``y`` and ``x`` are supported as :obj:`dpnp.ndarray`.
        Keyword arguments ``kwargs`` are currently unsupported.
        Otherwise the functions will be executed sequentially on CPU.
        Input array data types are limited by supported DPNP :ref:`Data types`.

    Examples
    --------
    >>> import dpnp as np
    >>> a = np.array([1, 2, 3])
    >>> b = np.array([4, 6, 8])
    >>> np.trapz(a)
    4.0
    >>> np.trapz(a, x=b)
    8.0
    >>> np.trapz(a, dx=2)
    8.0

    """

    if not use_origin_backend(y):

        if not isinstance(y, dparray):
            pass
        elif not isinstance(x, dparray) and x is not None:
            pass
        elif x is not None and y.size != x.size:
            pass
        elif x is not None and y.shape != x.shape:
            pass
        elif y.ndim > 1:
            pass
        else:
            if x is None:
                x = dpnp.empty(0, dtype=y.dtype)

            return dpnp_trapz(y, x, dx)

    return call_origin(numpy.trapz, y, x=x, dx=dx, **kwargs)
Ejemplo n.º 12
0
    def test_cub_max(self, xp, dtype, axis):
        a = testing.shaped_random(self.shape, xp, dtype)
        if self.order in ('c', 'C'):
            a = xp.ascontiguousarray(a)
        elif self.order in ('f', 'F'):
            a = xp.asfortranarray(a)

        if xp is numpy:
            return a.max(axis=axis)

        # xp is cupy, first ensure we really use CUB
        ret = cupy.empty(())  # Cython checks return type, need to fool it
        if len(axis) == len(self.shape):
            func = 'cupy.core._routines_statistics.cub.device_reduce'
        else:
            func = 'cupy.core._routines_statistics.cub.device_segmented_reduce'
        with testing.AssertFunctionIsCalled(func, return_value=ret):
            a.max(axis=axis)
        # ...then perform the actual computation
        return a.max(axis=axis)
Ejemplo n.º 13
0
 def test_isinstance_numpy_copy_wrong_shape(self):
     for xp in (numpy, cupy):
         a = numpy.arange(100, dtype=numpy.float64).reshape(10, 10)
         b = cupy.empty(100, dtype=a.dtype)
         with pytest.raises(ValueError):
             b[:] = a
Ejemplo n.º 14
0
 def test_nansum_out_wrong_shape(self):
     a = testing.shaped_arange(self.shape)
     a[:, 1] = cupy.nan
     b = cupy.empty((2, 3))
     with self.assertRaises(ValueError):
         cupy.nansum(a, axis=1, out=b)
Ejemplo n.º 15
0
 def test_sum_out_wrong_shape(self):
     a = testing.shaped_arange((2, 3, 4))
     b = cupy.empty((2, 3))
     with self.assertRaises(ValueError):
         a.sum(axis=1, out=b)
Ejemplo n.º 16
0
 def test_empty_zero_sized_array_strides(self, order):
     a = numpy.empty((1, 0, 2), dtype='d', order=order)
     b = cupy.empty((1, 0, 2), dtype='d', order=order)
     self.assertEqual(b.strides, a.strides)