def eye(N, M=None, k=0, dtype=float, order='C'): """Returns a 2-D array with ones on the diagonal and zeros elsewhere. Parameters ---------- N : int Number of rows in the output. M : int, optional Number of columns in the output. If None, defaults to *N*. k : int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. dtype : dtype, optional Data-type of the returned array. order : {'C', 'F'}, optional Whether the output should be stored in row-major (C-style) or column-major (Fortran-style) order in memory. Returns ------- I : ndarray An array where all elements are equal to zero, except for the k-th diagonal, whose values are equal to one. See Also -------- identity : Returns the identity array. diag : Extracts a diagonal or construct a diagonal array. Examples -------- >>> import nlcpy as vp >>> vp.eye(2, dtype=int) array([[1, 0], [0, 1]]) >>> vp.eye(3, k=1) array([[0., 1., 0.], [0., 0., 1.], [0., 0., 0.]]) """ if numpy.dtype(dtype).kind == 'V': raise NotImplementedError('void dtype in eye is not implemented yet.') if M is None: M = N out = nlcpy.ndarray(shape=(N, M), dtype=dtype, order=order) if order == 'F': N, M = M, N request._push_request( "nlcpy_eye", "creation_op", (out, int(N), int(M), int(k)), ) return out
def zeros(shape, dtype=float, order='C'): """Returns a new array of given shape and type, filled with zeros. Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)`` or ``2``. dtype : dtype, optional The desired dtype for the array, e.g, ``nlcpy.int64``. Default is ``nlcpy.float64``. order : {'C', 'F'}, optional Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Returns ------- out : ndarray Array of zeros with the given shape, dtype, and order. See Also -------- zeros_like : Returns an array of zeros with the same shape and type as a given array. empty : Returns a new array of given shape and type, without initializing entries. ones : Returns a new array of given shape and type, filled with ones. full : Returns a new array of given shape and type, filled with fill_value. Examples -------- >>> import nlcpy as vp >>> vp.zeros(5) array([0., 0., 0., 0., 0.]) >>> vp.zeros((5,), dtype=int) array([0, 0, 0, 0, 0]) >>> vp.zeros((2, 1)) array([[0.], [0.]]) >>> s = (2,2) >>> vp.zeros(s) array([[0., 0.], [0., 0.]]) """ if numpy.dtype(dtype).kind == 'V': raise NotImplementedError( 'void dtype in zeros is not implemented yet.') out = nlcpy.ndarray(shape=shape, dtype=dtype, order=order) out.fill(0) return out
def empty(shape, dtype=float, order='C'): """Returns a new array of given shape and type, without initializing entries. Parameters ---------- shape : int or sequence of int Shape of the empty array, e.g., (2, 3) or 2. dtype : dtype, optional Desired output dtype for the array, e.g, ``nlcpy.int64``. Default is ``nlcpy.float64``. order : {'C', 'F'}, optional Whether to store multi-dimensional data in row-major (C-style) or column-major (Fortran-style) order in memory. Returns ------- out : ndarray Array of uninitialized (arbitrary) data of the given shape, dtype, and order. Note ---- :func:`empty`, unlike :func:`zeros`, does not set the array 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. See Also -------- empty_like : Returns a new array with the same shape and type as a given array. ones : Returns a new array of given shape and type, filled with ones. zeros : Returns a new array of given shape and type, filled with zeros. full : Returns a new array of given shape and type, filled with fill_value. Examples -------- >>> import nlcpy as vp >>> vp.empty([2, 2]) # doctest: +SKIP array([[0., 0.], [0., 0.]]) # They are not always zero. (uninitialized) >>> vp.empty([2, 2], dtype=int) # doctest: +SKIP array([[0, 0], [0, 0]]) # They are not always zero. (uninitialized) """ return nlcpy.ndarray(shape=shape, dtype=dtype, order=order)
def empty_like(prototype, dtype=None, order='K', subok=False, shape=None): """Returns a new array with the same shape and type as a given array. Parameters ---------- prototype : array_like The shape and dtype of *prototype* define these same attributes of the returned array. dtype : dtype, optional Overrides the data type of the result. order : {'C', 'F'}, 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. subok : bool, optional Not implemented. 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. Returns ------- out : ndarray Array of uninitialized (arbitrary) data with the same shape and type as *prototype*. Note ---- This function does not initialize the returned array; to do that use :func:`zeros_like` or :func:`ones_like` instead. It may be marginally faster than the functions that do set the array values. See Also -------- ones_like : Returns an array of ones with the same shape and type as a given array. zeros_like : Returns an array of zeros with the same shape and type as a given array. full_like : Returns a full array with the same shape and type as a given array. empty : Returns a new array of given shape and type, without initializing entries. Examples -------- >>> import nlcpy as vp >>> a = ([1,2,3], [4,5,6]) # a is array-like >>> vp.empty_like(a) # doctest: +SKIP array([[0, 0, 0], [0, 0, 0]]) # uninitialized >>> a = vp.array([[1., 2., 3.],[4.,5.,6.]]) >>> vp.empty_like(a) # doctest: +SKIP array([[0., 0., 0.], [0., 0., 0.]]) # uninitialized """ if subok is not False: raise NotImplementedError( 'subok in empty_like is not implemented yet.') prototype = nlcpy.asanyarray(prototype) if shape is None: shape = prototype.shape if dtype is None: dtype = prototype.dtype if order is None or order in 'kKaA': if prototype._f_contiguous and not prototype._c_contiguous: order = 'F' else: order = 'C' if numpy.dtype(dtype).kind == 'V': raise NotImplementedError( 'void dtype in empty_like is not implemented yet.') out = nlcpy.ndarray(shape=shape, dtype=dtype, order=order) return out
def full_like(a, fill_value, dtype=None, order='K', subok=False, shape=None): """Returns a full array with the same shape and type as a given array. Parameters ---------- a : array_like The shape and dtype of *a* define these same attributes of the returned array. fill_value : scalar Fill value. dtype : dtype, optional Overrides the data type of the result. 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 *a* is Fortran contiguous, 'C' otherwise. 'K' means match the layout of *a* as closely as possible. subok : bool, optional Not implemented. 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. Returns ------- out : ndarray Array of *fill_value* with the same shape and type as *a*. See Also -------- empty_like : Returns a new array with the same shape and type as a given array. ones_like : Returns an array of ones with the same shape and type as a given array. zeros_like : Returns an array of zeros with the same shape and type as a given array. full : Returns a new array of given shape and type, filled with fill_value. Examples -------- >>> import nlcpy as vp >>> x = vp.arange(6, dtype=int) >>> vp.full_like(x, 1) array([1, 1, 1, 1, 1, 1]) >>> vp.full_like(x, 0.1) array([0, 0, 0, 0, 0, 0]) >>> vp.full_like(x, 0.1, dtype=vp.double) array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) >>> vp.full_like(x, vp.nan, dtype=vp.double) array([nan, nan, nan, nan, nan, nan]) >>> y = vp.arange(6, dtype=vp.double) >>> vp.full_like(y, 0.1) array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) """ if subok is not False: raise NotImplementedError('subok in full_like is not implemented yet.') a = nlcpy.asanyarray(a) if shape is None: shape = a.shape if dtype is None: dtype = a.dtype else: dtype = nlcpy.dtype(dtype) if numpy.dtype(dtype).kind == 'V': raise NotImplementedError( 'void dtype in full_like is not implemented yet.') if order is None or order in 'kKaA': if a._f_contiguous and not a._c_contiguous: order = 'F' else: order = 'C' if numpy.isscalar(fill_value): if numpy.iscomplex(fill_value): if dtype in ('complex64', 'complex128'): pass else: fill_value = numpy.real(fill_value) warnings.warn( 'Casting complex values to real discards the imaginary part', numpy.ComplexWarning, stacklevel=2) out = nlcpy.ndarray(shape=shape, dtype=dtype, order=order) out.fill(fill_value) elif fill_value is None: raise NotImplementedError('fill_value in nlcpy.full_like is None') else: fill_value = nlcpy.asarray(fill_value) out = nlcpy.array(nlcpy.broadcast_to(fill_value, shape=shape), dtype=dtype, order=order) return out
def full(shape, fill_value, dtype=None, order='C'): """Returns a new array of given shape and type, filled with *fill_value*. Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)`` or ``2``. fill_value : scalar Fill value. dtype : dtype, optional The desired dtype for the array, e.g, ``nlcpy.int64``. Default is ``nlcpy.float64``. order : {'C', 'F'}, optional Whether to store multidimensional data in C- or Fortran-contiguous (row- or column-wise) order in memory. Returns ------- out : ndarray Array of *fill_value* with the given shape, dtype, and order. See Also -------- full_like : Returns a full array with the same shape and type as a given array. empty : Returns a new array of given shape and type, without initializing entries. ones : Returns a new array of given shape and type, filled with ones. zeros : Returns a new array of given shape and type, filled with zeros. Examples -------- >>> import nlcpy as vp >>> vp.full((2, 2), vp.inf) array([[inf, inf], [inf, inf]]) >>> vp.full((2, 2), 10) array([[10, 10], [10, 10]]) """ if numpy.dtype(dtype).kind == 'V': raise NotImplementedError('void dtype in full is not implemented yet.') if dtype is None: dtype = numpy.result_type(fill_value) else: dtype = nlcpy.dtype(dtype) if numpy.isscalar(fill_value): if numpy.iscomplex(fill_value): if dtype in ('complex64', 'complex128'): pass else: fill_value = numpy.real(fill_value) warnings.warn( 'Casting complex values to real discards the imaginary part', numpy.ComplexWarning, stacklevel=2) out = nlcpy.ndarray(shape=shape, dtype=dtype, order=order) out.fill(fill_value) elif fill_value is None: raise NotImplementedError('fill_value in nlcpy.full is None') else: fill_value = nlcpy.asarray(fill_value) out = nlcpy.array(nlcpy.broadcast_to(fill_value, shape=shape), dtype=dtype, order=order) return out
def zeros_like(a, dtype=None, order='K', subok=False, shape=None): """Returns an array of zeros with the same shape and type as a given array. Parameters ---------- a : array_like The shape and dtype of *a* define these same attributes of the returned array. dtype : dtype, optional Overrides the data type of the result. 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 *a* is Fortran contiguous, 'C' otherwise. 'K' means match the layout of *a* as closely as possible. subok : bool, optional Not implemented. 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. Returns ------- out : ndarray Array of zeros with the same shape and type as *a*. See Also -------- empty_like : Returns a new array with the same shape and type as a given array. ones_like : Returns an array of ones with the same shape and type as a given array. full_like : Returns a full array with the same shape and type as a given array. zeros : Returns a new array of given shape and type, filled with zeros. Examples -------- >>> import nlcpy as vp >>> x = vp.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> vp.zeros_like(x) array([[0, 0, 0], [0, 0, 0]]) >>> y = vp.arange(3, dtype=float) >>> y array([0., 1., 2.]) >>> vp.zeros_like(y) array([0., 0., 0.]) """ if subok is not False: raise NotImplementedError( 'subok in zeros_like is not implemented yet.') a = nlcpy.asanyarray(a) if shape is None: shape = a.shape if dtype is None: dtype = a.dtype if numpy.dtype(dtype).kind == 'V': raise NotImplementedError( 'void dtype in zeros_like is not implemented yet.') if order is None or order in 'kKaA': if a._f_contiguous and not a._c_contiguous: order = 'F' else: order = 'C' out = nlcpy.ndarray(shape=shape, dtype=dtype, order=order) out.fill(0) return out
def where(condition, x=None, y=None): """Returns elements chosen from *x* or *y* depending on *condition*. Note ---- When only condition is provided, this function is a shorthand for ``nlcpy.asarray(condition).nonzero()``. Using nonzero directly should be preferred, as it behaves correctly for subclasses. The rest of this documentation covers only the case where all three arguments are provided. Parameters ---------- condition : array_like, bool Where True, yield *x*, otherwise yield *y*. x, y : array_like Values from which to choose. *x*, *y* and *condition* need to be broadcastable to some shape. Returns ------- out : ndarray An array with elements from *x* where *condition* is True, and elements from *y* elsewhere. Note ---- If all the arrays are 1-D, :func:`where` is equivalent to:: [xv if c else yv for c, xv, yv in zip(condition, x, y)] See Also -------- nonzero : Returns the indices of the elements that are non-zero. Examples -------- >>> import nlcpy as vp >>> a = vp.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> vp.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90]) This can be used on multidimensional arrays too: >>> vp.where([[True, False], [True, True]], ... [[1, 2], [3, 4]], ... [[9, 8], [7, 6]]) array([[1, 8], [3, 4]]) The shapes of x, y, and the condition are broadcast together: >>> x = vp.arange(3).reshape([3,1]) >>> y = vp.arange(4).reshape([1,4]) >>> vp.where(x < y, x, 10 + y) # both x and 10+y are broadcast array([[10, 0, 0, 0], [10, 11, 1, 1], [10, 11, 12, 2]]) >>> a = vp.array([[0, 1, 2], ... [0, 2, 4], ... [0, 3, 6]]) >>> vp.where(a < 4, a, -1) # -1 is broadcast array([[ 0, 1, 2], [ 0, 2, -1], [ 0, 3, -1]]) """ if condition is None: condition = False arr = nlcpy.asarray(condition) if x is None and y is None: return nlcpy.nonzero(arr) if x is None or y is None: raise ValueError("either both or neither of x and y should be given") if not isinstance(x, nlcpy.ndarray): x = numpy.asarray(x) if not isinstance(y, nlcpy.ndarray): y = numpy.asarray(y) ret_type = numpy.result_type(x, y) arr_x = nlcpy.asarray(x, dtype=ret_type) arr_y = nlcpy.asarray(y, dtype=ret_type) if arr.dtype != bool: arr = (arr != 0) values, shape = core._broadcast_core((arr, arr_x, arr_y)) ret = nlcpy.ndarray(shape=shape, dtype=ret_type) request._push_request( "nlcpy_where", "indexing_op", (ret, values[0], values[1], values[2]), ) return ret
def test_ones_like_subok(self): a = nlcpy.ndarray((2, 3, 4)) return nlcpy.ones_like(a, subok=True)