def append(arr, values, axis=None): """ Append values to the end of an array. Args: arr (array_like): Values are appended to a copy of this array. values (array_like): These values are appended to a copy of ``arr``. It must be of the correct shape (the same shape as ``arr``, excluding ``axis``). If ``axis`` is not specified, ``values`` can be any shape and will be flattened before use. axis (int or None): The axis along which ``values`` are appended. If ``axis`` is not given, both ``arr`` and ``values`` are flattened before use. Returns: cupy.ndarray A copy of ``arr`` with ``values`` appended to ``axis``. Note that ``append`` does not occur in-place: a new array is allocated and filled. If ``axis`` is None, ``out`` is a flattened array. .. seealso:: :func:`numpy.append` """ # TODO(asi1024): Implement fast path for scalar inputs. arr = cupy.asarray(arr) values = cupy.asarray(values) if axis is None: return _core.concatenate_method((arr.ravel(), values.ravel()), 0).ravel() return _core.concatenate_method((arr, values), axis)
def kron(a, b): """Returns the kronecker product of two arrays. Args: a (~cupy.ndarray): The first argument. b (~cupy.ndarray): The second argument. Returns: ~cupy.ndarray: Output array. .. seealso:: :func:`numpy.kron` """ a_ndim = a.ndim b_ndim = b.ndim if a_ndim == 0 or b_ndim == 0: return cupy.multiply(a, b) ndim = b_ndim a_shape = a.shape b_shape = b.shape if a_ndim != b_ndim: if b_ndim > a_ndim: a_shape = (1,) * (b_ndim - a_ndim) + a_shape else: b_shape = (1,) * (a_ndim - b_ndim) + b_shape ndim = a_ndim axis = ndim - 1 out = _core.tensordot_core( a, b, None, a.size, b.size, 1, a_shape + b_shape) for _ in range(ndim): out = _core.concatenate_method(out, axis=axis) return out
def concatenate(tup, axis=0, out=None, *, dtype=None, casting='same_kind'): """Joins arrays along an axis. Args: tup (sequence of arrays): Arrays to be joined. All of these should have same dimensionalities except the specified axis. axis (int or None): The axis to join arrays along. If axis is None, arrays are flattened before use. Default is 0. out (cupy.ndarray): Output array. dtype (str or dtype): If provided, the destination array will have this dtype. Cannot be provided together with ``out``. casting ({‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}, optional): Controls what kind of data casting may occur. Defaults to ``'same_kind'``. Returns: cupy.ndarray: Joined array. .. seealso:: :func:`numpy.concatenate` """ if axis is None: tup = [m.ravel() for m in tup] axis = 0 return _core.concatenate_method(tup, axis, out, dtype, casting)
def concatenate(tup, axis=0, out=None): """Joins arrays along an axis. Args: tup (sequence of arrays): Arrays to be joined. All of these should have same dimensionalities except the specified axis. axis (int or None): The axis to join arrays along. If axis is None, arrays are flattened before use. Default is 0. out (cupy.ndarray): Output array. Returns: cupy.ndarray: Joined array. .. seealso:: :func:`numpy.concatenate` """ if axis is None: tup = [m.ravel() for m in tup] axis = 0 return _core.concatenate_method(tup, axis, out)
def cov(a, y=None, rowvar=True, bias=False, ddof=None): """Returns the covariance matrix of an array. This function currently does not support ``fweights`` and ``aweights`` options. Args: a (cupy.ndarray): Array to compute covariance matrix. y (cupy.ndarray): An additional set of variables and observations. rowvar (bool): If ``True``, then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed. bias (bool): If ``False``, normalization is by ``(N - 1)``, where N is the number of observations given (unbiased estimate). If ``True``, then normalization is by ``N``. ddof (int): If not ``None`` the default value implied by bias is overridden. Note that ``ddof=1`` will return the unbiased estimate and ``ddof=0`` will return the simple average. Returns: cupy.ndarray: The covariance matrix of the input array. .. seealso:: :func:`numpy.cov` """ if ddof is not None and ddof != int(ddof): raise ValueError('ddof must be integer') if a.ndim > 2: raise ValueError('Input must be <= 2-d') if y is None: dtype = numpy.promote_types(a.dtype, numpy.float64) else: if y.ndim > 2: raise ValueError('y must be <= 2-d') dtype = functools.reduce(numpy.promote_types, (a.dtype, y.dtype, numpy.float64)) X = cupy.array(a, ndmin=2, dtype=dtype) if not rowvar and X.shape[0] != 1: X = X.T if X.shape[0] == 0: return cupy.array([]).reshape(0, 0) if y is not None: y = cupy.array(y, copy=False, ndmin=2, dtype=dtype) if not rowvar and y.shape[0] != 1: y = y.T X = _core.concatenate_method((X, y), axis=0) if ddof is None: ddof = 0 if bias else 1 fact = X.shape[1] - ddof if fact <= 0: warnings.warn('Degrees of freedom <= 0 for slice', RuntimeWarning, stacklevel=2) fact = 0.0 X -= X.mean(axis=1)[:, None] out = X.dot(X.T.conj()) * (1 / cupy.float64(fact)) return out.squeeze()
def cov(a, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None, *, dtype=None): """Returns the covariance matrix of an array. This function currently does not support ``fweights`` and ``aweights`` options. Args: a (cupy.ndarray): Array to compute covariance matrix. y (cupy.ndarray): An additional set of variables and observations. rowvar (bool): If ``True``, then each row represents a variable, with observations in the columns. Otherwise, the relationship is transposed. bias (bool): If ``False``, normalization is by ``(N - 1)``, where N is the number of observations given (unbiased estimate). If ``True``, then normalization is by ``N``. ddof (int): If not ``None`` the default value implied by bias is overridden. Note that ``ddof=1`` will return the unbiased estimate and ``ddof=0`` will return the simple average. fweights (cupy.ndarray, int): 1-D array of integer frequency weights. the number of times each observation vector should be repeated. It is required that fweights >= 0. However, the function will not error when fweights < 0 for performance reasons. aweights (cupy.ndarray): 1-D array of observation vector weights. These relative weights are typically large for observations considered "important" and smaller for observations considered less "important". If ``ddof=0`` the array of weights can be used to assign probabilities to observation vectors. It is required that aweights >= 0. However, the function will not error when aweights < 0 for performance reasons. dtype: Data type specifier. By default, the return data-type will have at least `numpy.float64` precision. Returns: cupy.ndarray: The covariance matrix of the input array. .. seealso:: :func:`numpy.cov` """ if ddof is not None and ddof != int(ddof): raise ValueError('ddof must be integer') if a.ndim > 2: raise ValueError('Input must be <= 2-d') if dtype is None: if y is None: dtype = numpy.promote_types(a.dtype, numpy.float64) else: if y.ndim > 2: raise ValueError('y must be <= 2-d') dtype = functools.reduce(numpy.promote_types, (a.dtype, y.dtype, numpy.float64)) X = cupy.array(a, ndmin=2, dtype=dtype) if not rowvar and X.shape[0] != 1: X = X.T if X.shape[0] == 0: return cupy.array([]).reshape(0, 0) if y is not None: y = cupy.array(y, copy=False, ndmin=2, dtype=dtype) if not rowvar and y.shape[0] != 1: y = y.T X = _core.concatenate_method((X, y), axis=0) if ddof is None: ddof = 0 if bias else 1 w = None if fweights is not None: if not isinstance(fweights, cupy.ndarray): raise TypeError("fweights must be a cupy.ndarray") if fweights.dtype.char not in 'bBhHiIlLqQ': raise TypeError("fweights must be integer") fweights = fweights.astype(dtype=float) if fweights.ndim > 1: raise RuntimeError("cannot handle multidimensional fweights") if fweights.shape[0] != X.shape[1]: raise RuntimeError("incompatible numbers of samples and fweights") w = fweights if aweights is not None: if not isinstance(fweights, cupy.ndarray): raise TypeError("aweights must be a cupy.ndarray") aweights = aweights.astype(dtype=float) if aweights.ndim > 1: raise RuntimeError("cannot handle multidimensional aweights") if aweights.shape[0] != X.shape[1]: raise RuntimeError("incompatible numbers of samples and aweights") if w is None: w = aweights else: w *= aweights avg, w_sum = cupy.average(X, axis=1, weights=w, returned=True) w_sum = w_sum[0] # Determine the normalization if w is None: fact = X.shape[1] - ddof elif ddof == 0: fact = w_sum elif aweights is None: fact = w_sum - ddof else: fact = w_sum - ddof * sum(w * aweights) / w_sum if fact <= 0: warnings.warn('Degrees of freedom <= 0 for slice', RuntimeWarning, stacklevel=2) fact = 0.0 X -= X.mean(axis=1)[:, None] if w is None: X_T = X.T else: X_T = (X * w).T out = X.dot(X_T.conj()) * (1 / cupy.float64(fact)) return out.squeeze()