def empty(shape, dtype=numpy.float64, order='C'): """ Return a new array of given shape and type, without initializing entries. For full documentation refer to :obj:`numpy.empty`. Limitations ----------- Parameter ``order`` is supported only with default value ``"C"``. See Also -------- :obj:`dpnp.empty_like` : Return an empty array with shape and type of input. :obj:`dpnp.ones` : Return a new array setting values to one. :obj:`dpnp.zeros` : Return a new array setting values to zero. :obj:`dpnp.full` : Return a new array of given shape filled with value. Examples -------- >>> import dpnp as np >>> x = np.empty(4) >>> [i for i in x] [0.0, 0.0, 1e-323, -3.5935729608842025e+22] """ if (not use_origin_backend()): if order not in ('C', 'c', None): checker_throw_value_error("empty", "order", order, 'C') return dparray(shape, dtype) return numpy.empty(shape, dtype, order)
def repeat(x1, repeats, axis=None): """ Repeat elements of an array. """ is_x1_dparray = isinstance(x1, dparray) if (not use_origin_backend(x1) and is_x1_dparray and (axis is None or axis == 0) and (x1.ndim < 2)): repeat_val = repeats if isinstance(repeats, (tuple, list)): if (len(repeats) > 1): checker_throw_value_error("repeat", "len(repeats)", len(repeats), 1) repeat_val = repeats[0] return dpnp_repeat(x1, repeat_val, axis) input1 = dpnp.asnumpy(x1) if is_x1_dparray else x1 # TODO need to put dparray memory into NumPy call result_numpy = numpy.repeat(input1, repeats, axis=axis) result = result_numpy if isinstance(result, numpy.ndarray): result = dparray(result_numpy.shape, dtype=result_numpy.dtype) for i in range(result.size): result._setitem_scalar(i, result_numpy.item(i)) return result
def place(arr, mask, vals): """ Change elements of an array based on conditional and input values. For full documentation refer to :obj:`numpy.place`. Limitations ----------- Input arrays ``arr`` and ``mask`` are supported as :obj:`dpnp.ndarray`. Parameter ``vals`` is supported as 1-D sequence. """ if not use_origin_backend(arr): if not isinstance(arr, dparray): pass elif not isinstance(mask, dparray): pass elif not isinstance(vals, dparray): if not isinstance(vals, collections.Sequence): pass else: vals_len = len(vals) vals_arr = dparray(vals_len, dtype=arr.dtype) for i in range(vals_len): vals_arr[i] = vals[i] return dpnp_place(arr, mask, vals_arr) else: return dpnp_place(arr, mask, vals) return call_origin(numpy.place, arr, mask, vals)
def gen_dparray(size, dtype=numpy.float64, low=None, high=None, seed=None): """ Generate dparray of random numbers of specified size and type. Parameters ---------- size : int size of output array dtype : dtype data type of output array low : int lowest integers to be generated high : int highest integers to be generated seed : int random seed Returns ------- dparray generated dparray """ ndarr = gen_ndarray(size, dtype=dtype, low=low, high=high, seed=seed) dparr = dparray(ndarr.shape, dtype=dtype) for i in range(dparr.size): dparr._setitem_scalar(i, ndarr.item(i)) return dparr
def max(input, axis=None, out=None): """ Return the maximum of an array or maximum along an axis. Parameters ---------- input : array_like Input data. axis : None or int or tuple of ints, optional Axis or axes along which to operate. By default, flattened input is used. .. versionadded:: 1.7.0 If this is a tuple of ints, the maximum is selected over multiple axes, instead of a single axis or all the axes as before. out : ndarray, optional Alternative output array in which to place the result. Must be of the same shape and buffer length as the expected output. See `ufuncs-output-type` for more details. Returns ------- amax : ndarray or scalar Maximum of `a`. If `axis` is None, the result is a scalar value. If `axis` is given, the result is an array of dimension ``a.ndim - 1``. """ dim_input = input.ndim is_input_dparray = isinstance(input, dparray) if not use_origin_backend(input) and is_input_dparray: if out is not None: checker_throw_value_error("max", "out", type(out), None) result = dpnp_max(input, axis=axis) # scalar returned if result.shape == (1, ): return result.dtype.type(result[0]) return result input1 = dpnp.asnumpy(input) if is_input_dparray else input # TODO need to put dparray memory into NumPy call result_numpy = numpy.max(input1, axis=axis) result = result_numpy if isinstance(result, numpy.ndarray): result = dparray(result_numpy.shape, dtype=result_numpy.dtype) for i in range(result.size): result._setitem_scalar(i, result_numpy.item(i)) return result
def repeat(x1, repeats, axis=None): """ Repeat elements of an array. For full documentation refer to :obj:`numpy.repeat`. Limitations ----------- Input array is supported as :obj:`dpnp.ndarray`. Parameter ``axis`` is supported with value either ``None`` or ``0``. Dimension of input array are supported to be less than ``2``. Otherwise the function will be executed sequentially on CPU. If ``repeats`` is ``tuple`` or ``list``, should be ``len(repeats) > 1``. Input array data types are limited by supported DPNP :ref:`Data types`. .. seealso:: :obj:`numpy.tile` tile an array. Examples -------- >>> import dpnp as np >>> x = np.repeat(3, 4) >>> [i for i in x] [3, 3, 3, 3] """ is_x1_dparray = isinstance(x1, dparray) if (not use_origin_backend(x1) and is_x1_dparray and (axis is None or axis == 0) and (x1.ndim < 2)): repeat_val = repeats if isinstance(repeats, (tuple, list)): if (len(repeats) > 1): checker_throw_value_error("repeat", "len(repeats)", len(repeats), 1) repeat_val = repeats[0] return dpnp_repeat(x1, repeat_val, axis) input1 = dpnp.asnumpy(x1) if is_x1_dparray else x1 # TODO need to put dparray memory into NumPy call result_numpy = numpy.repeat(input1, repeats, axis=axis) result = result_numpy if isinstance(result, numpy.ndarray): result = dparray(result_numpy.shape, dtype=result_numpy.dtype) for i in range(result.size): result._setitem_scalar(i, result_numpy.item(i)) return result
def max(input, axis=None, out=None): """ Return the maximum of an array or maximum along an axis. Limitations ----------- Input array is supported as :obj:`dpnp.ndarray`. Otherwise the function will be executed sequentially on CPU. Prameters ``out`` is supported only with default value ``None``. Input array data types are limited by supported DPNP :ref:`Data types`. Examples -------- >>> import dpnp as np >>> a = np.arange(4).reshape((2,2)) >>> a.shape (2, 2) >>> [i for i in a] [0, 1, 2, 3] >>> np.max(a) 3 """ dim_input = input.ndim is_input_dparray = isinstance(input, dparray) if not use_origin_backend(input) and is_input_dparray: if out is not None: checker_throw_value_error("max", "out", type(out), None) result = dpnp_max(input, axis=axis) # scalar returned if result.shape == (1, ): return result.dtype.type(result[0]) return result input1 = dpnp.asnumpy(input) if is_input_dparray else input # TODO need to put dparray memory into NumPy call result_numpy = numpy.max(input1, axis=axis) result = result_numpy if isinstance(result, numpy.ndarray): result = dparray(result_numpy.shape, dtype=result_numpy.dtype) for i in range(result.size): result._setitem_scalar(i, result_numpy.item(i)) return result
def mean(input, axis=None): """ Compute the arithmetic mean along the specified axis. Returns the average of the array elements. Parameters ---------- input : array_like Array containing numbers whose mean is desired. If `input` is not an array, a conversion is attempted. axis : None or int or tuple of ints, optional Axis or axes along which the means are computed. The default is to compute the mean of the flattened array. .. versionadded:: 1.7.0 If this is a tuple of ints, a mean is performed over multiple axes, instead of a single axis or all the axes as before. Returns ------- m : ndarray, see dtype parameter above If `out=None`, returns a new array containing the mean values, otherwise a reference to the output array is returned. """ is_input_dparray = isinstance(input, dparray) if not use_origin_backend(input) and is_input_dparray: result = dpnp_mean(input, axis=axis) # scalar returned if result.shape == (1,): return result.dtype.type(result[0]) return result input1 = dpnp.asnumpy(input) if is_input_dparray else input # TODO need to put dparray memory into NumPy call result_numpy = numpy.mean(input1, axis=axis) result = result_numpy if isinstance(result, numpy.ndarray): result = dparray(result_numpy.shape, dtype=result_numpy.dtype) for i in range(result.size): result._setitem_scalar(i, result_numpy.item(i)) return result
def erf(in_array1): """ Return the 'error function' at in_array1. .. seealso:: :obj:`math.erf` """ if isinstance(in_array1, dparray): result = dparray(in_array1.shape, dtype=in_array1.dtype) for i in range(result.size): result[i] = math.erf(in_array1[i]) return result return math.erf(in_array1)
def irfft(x1, n=None, axis=-1, norm=None): """ Compute the one-dimensional inverse discrete Fourier Transform for real input.. Limitations ----------- Parameter ``norm`` is unsupported. Parameter ``x1`` supports ``dpnp.int32``, ``dpnp.int64``, ``dpnp.float32``, ``dpnp.float64`` and ``dpnp.complex128`` datatypes only. For full documentation refer to :obj:`numpy.fft.irfft`. """ x1_desc = dpnp.get_dpnp_descriptor(x1) if x1_desc and 0: if axis is None: axis_param = -1 # the most right dimension (default value) else: axis_param = axis if n is None: input_boundarie = x1_desc.shape[axis_param] else: input_boundarie = n if x1_desc.size < 1: pass # let fallback to handle exception elif input_boundarie < 1: pass # let fallback to handle exception elif norm is not None: pass else: output_boundarie = 2 * (input_boundarie - 1) result = dpnp_fft(x1_desc, input_boundarie, output_boundarie, axis_param, True) # TODO tmp = utils.create_output_array(result_shape, result_c_type, out) tmp = dparray(result.shape, dtype=dpnp.float64) for it in range(tmp.size): tmp[it] = result[it].real return tmp return call_origin(numpy.fft.irfft, x1, n, axis, norm)
def empty(shape, dtype=numpy.float64, order='C'): """Return a new matrix of given shape and type, without initializing entries. Parameters ---------- shape : int or tuple of int Shape of the empty matrix. dtype : data-type, optional Desired output data-type. order : {'C', 'F'}, optional Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. See Also -------- :obj:`dpnp.empty_like`, :obj:`dpnp.zeros` Notes ----- :obj:`dpnp.empty`, unlike :obj:`dpnp.zeros`, does not set the matrix values to zero, and may therefore be marginally faster. On the other hand, it requires the user to manually set all the values in the array, and should be used with caution. Examples -------- >>> import numpy.matlib >>> np.matlib.empty((2, 2)) # filled with random data matrix([[ 6.76425276e-320, 9.79033856e-307], # random [ 7.39337286e-309, 3.22135945e-309]]) >>> np.matlib.empty((2, 2), dtype=int) matrix([[ 6600475, 0], # random [ 6586976, 22740995]]) """ if (not use_origin_backend()): if order not in ('C', 'c', None): checker_throw_value_error("empty", "order", order, 'C') return dparray(shape, dtype) return numpy.empty(shape, dtype, order)
def empty_like(prototype, dtype=None, order='C', subok=False, shape=None): """ Return a new array with the same shape and type as a given array. For full documentation refer to :obj:`numpy.empty_like`. Limitations ----------- Parameter ``order`` is supported only with default value ``"C"``. Parameter ``subok`` is supported only with default value ``False``. See Also -------- :obj:`dpnp.ones_like` : Return an array of ones with shape and type of input. :obj:`dpnp.zeros_like` : Return an array of zeros with shape and type of input. :obj:`dpnp.full_like` : Return a new array with shape of input filled with value. :obj:`dpnp.empty` : Return a new uninitialized array. Examples -------- >>> import dpnp as np >>> prototype = np.array([1, 2, 3]) >>> x = np.empty_like(prototype) >>> [i for i in x] [0, 0, 0] """ if (not use_origin_backend()): if order not in ('C', 'c', None): checker_throw_value_error("empty_like", "order", order, 'C') if subok is not False: checker_throw_value_error("empty_like", "subok", subok, False) _shape = shape if shape is not None else prototype.shape _dtype = dtype if dtype is not None else prototype.dtype.type return dparray(_shape, _dtype) return numpy.empty_like(prototype, dtype, order, subok, shape)
def matrix_power(input, count): """ Raise a square matrix to the (integer) power `count`. Parameters ---------- input : sequence of array_like Returns ------- output : dparray Returns the dot product of the supplied arrays. See Also -------- :obj:`numpy.linalg.matrix_power` """ is_input_dparray = isinstance(input, dparray) if not use_origin_backend(input) and is_input_dparray and count > 0: result = input for id in range(count - 1): result = dpnp.matmul(result, input) return result input1 = dpnp.asnumpy(input) if is_input_dparray else input # TODO need to put dparray memory into NumPy call result_numpy = numpy.linalg.matrix_power(input1, count) result = result_numpy if isinstance(result, numpy.ndarray): result = dparray(result_numpy.shape, dtype=result_numpy.dtype) for i in range(result.size): result._setitem_scalar(i, result_numpy.item(i)) return result
def erf(in_array1): """ Returns the error function of complex argument. For full documentation refer to :obj:`scipy.special.erf`. Limitations ----------- Parameter ``in_array1`` is 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:`math.erf` Examples -------- >>> import dpnp as np >>> x = np.linspace(2.0, 3.0, num=5) >>> [i for i in x] [2.0, 2.25, 2.5, 2.75, 3.0] >>> out = np.erf(x) >>> [i for i in out] [0.99532227, 0.99853728, 0.99959305, 0.99989938, 0.99997791] """ if not use_origin_backend(in_array1): if not isinstance(in_array1, dparray): pass else: return dpnp_erf(in_array1) result = dparray(in_array1.shape, dtype=in_array1.dtype) for i in range(result.size): result[i] = math.erf(in_array1[i]) return result
def empty_like(prototype, dtype=None, order='C', subok=False, shape=None): """ Return a new array with the same shape and type as a given array. Parameters ---------- prototype : array_like The shape and data-type of `prototype` define these same attributes of the returned array. dtype : data-type, optional Overrides the data type of the result. .. versionadded:: 1.6.0 order : {'C', 'F', 'A', or 'K'}, optional Overrides the memory layout of the result. 'C' means C-order, 'F' means F-order, 'A' means 'F' if ``prototype`` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of ``prototype`` as closely as possible. .. versionadded:: 1.6.0 subok : bool, optional. If True, then the newly created array will use the sub-class type of 'a', otherwise it will be a base-class array. Defaults to True. shape : int or sequence of ints, optional. Overrides the shape of the result. If order='K' and the number of dimensions is unchanged, will try to keep order, otherwise, order='C' is implied. .. versionadded:: 1.17.0 Returns ------- out : ndarray Array of uninitialized (arbitrary) data with the same shape and type as `prototype`. See Also -------- :obj:`dpnp.ones_like` : Return an array of ones with shape and type of input. :obj:`dpnp.zeros_like` : Return an array of zeros with shape and type of input. :obj:`dpnp.full_like` : Return a new array with shape of input filled with value. :obj:`dpnp.empty` : Return a new uninitialized array. Notes ----- This function does *not* initialize the returned array; to do that use :obj:`dpnp.zeros_like` or :obj:`dpnp.ones_like` instead. It may be marginally faster than the functions that do set the array values. Examples -------- >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> np.empty_like(a) array([[-1073741821, -1073741821, 3], # uninitialized [ 0, 0, -1073741821]]) >>> a = np.array([[1., 2., 3.],[4.,5.,6.]]) >>> np.empty_like(a) array([[ -2.00000715e+000, 1.48219694e-323, -2.00000572e+000], # uninitialized [ 4.38791518e-305, -2.00000715e+000, 4.17269252e-309]]) """ if (not use_origin_backend()): if order not in ('C', 'c', None): checker_throw_value_error("empty_like", "order", order, 'C') if subok is not False: checker_throw_value_error("empty_like", "subok", subok, False) _shape = shape if shape is not None else prototype.shape _dtype = dtype if dtype is not None else prototype.dtype.type return dparray(_shape, _dtype) return numpy.empty_like(prototype, dtype, order, subok, shape)
def put_along_axis(arr, indices, values, axis): """ Put values into the destination array by matching 1d index and data slices. For full documentation refer to :obj:`numpy.put_along_axis`. See Also -------- :obj:`take_along_axis` : Take values from the input array by matching 1d index and data slices. """ if not use_origin_backend(arr): if not isinstance(arr, dparray): pass elif not isinstance(indices, dparray): pass elif arr.ndim != indices.ndim: pass elif not isinstance(axis, int): pass elif axis >= arr.ndim: pass elif not isinstance( values, (dparray, tuple, list)) and not dpnp.isscalar(values): pass elif not dpnp.isscalar(values) and ( (isinstance(values, dparray) and indices.size != values.size) or ((isinstance(values, (tuple, list)) and indices.size != len(values)))): pass elif arr.ndim == indices.ndim: val_list = [] for i in list(indices.shape)[:-1]: if i == 1: val_list.append(True) else: val_list.append(False) if not all(val_list): pass else: if dpnp.isscalar(values): values_size = 1 values_ = dparray(values_size, dtype=arr.dtype) values_[0] = values elif isinstance(values, dparray): values_ = values else: values_size = len(values) values_ = dparray(values_size, dtype=arr.dtype) for i in range(values_size): values_[i] = values[i] return dpnp_put_along_axis(arr, indices, values_, axis) else: if dpnp.isscalar(values): values_size = 1 values_ = dparray(values_size, dtype=arr.dtype) values_[0] = values elif isinstance(values, dparray): values_ = values else: values_size = len(values) values_ = dparray(values_size, dtype=arr.dtype) for i in range(values_size): values_[i] = values[i] return dpnp_put_along_axis(arr, indices, values_, axis) return call_origin(numpy.put_along_axis, arr, indices, values, axis)