Python numpy.core.multiarray.zeros() Examples
The following are 15
code examples of numpy.core.multiarray.zeros().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
numpy.core.multiarray
, or try the search function
.
Example #1
Source File: functions.py From Computable with MIT License | 5 votes |
def zeros(shape, typecode='l', savespace=0, dtype=None): """zeros(shape, dtype=int) returns an array of the given dimensions which is initialized to all zeros """ dtype = convtypecode(typecode, dtype) return mu.zeros(shape, dtype)
Example #2
Source File: functions.py From biskit with GNU General Public License v3.0 | 5 votes |
def zeros(shape, typecode='l', savespace=0, dtype=None): """zeros(shape, dtype=int) returns an array of the given dimensions which is initialized to all zeros """ dtype = convtypecode(typecode, dtype) return mu.zeros(shape, dtype)
Example #3
Source File: high_speed_jacobian.py From GridCal with GNU General Public License v3.0 | 5 votes |
def dSbus_dV(Ybus, V, I): """ Calls functions to calculate dS/dV depending on whether Ybus is sparse or not """ # I is substracted from Y*V, # therefore it must be negative for numba version of dSbus_dV if it is not zeros anyways # calculates sparse data dS_dVm, dS_dVa = dSbus_dV_numba_sparse(Ybus.data, Ybus.indptr, Ybus.indices, V, V / abs(V), I) # generate sparse CSR matrices with computed data and return them return sparse((dS_dVm, Ybus.indices, Ybus.indptr)), sparse((dS_dVa, Ybus.indices, Ybus.indptr)) # @jit(i8(c16[:], c16[:], i4[:], i4[:], i8[:], i8[:], f8[:], i8[:], i8[:]), nopython=True, cache=False)
Example #4
Source File: high_speed_jacobian.py From GridCal with GNU General Public License v3.0 | 5 votes |
def _create_J_with_numba(Ybus, V, pvpq, pq, pvpq_lookup, npv, npq): """ :param Ybus: :param V: :param pvpq: :param pq: :param createJ: :param pvpq_lookup: :param npv: :param npq: :return: """ Ibus = zeros(len(V), dtype=complex128) # create Jacobian from fast calc of dS_dV dVm_x, dVa_x = dSbus_dV_numba_sparse(Ybus.data, Ybus.indptr, Ybus.indices, V, V / abs(V), Ibus) # data in J, space preallocated is bigger than acutal Jx -> will be reduced later on Jx = empty(len(dVm_x) * 4, dtype=float64) # row pointer, dimension = pvpq.shape[0] + pq.shape[0] + 1 Jp = zeros(pvpq.shape[0] + pq.shape[0] + 1, dtype=int32) # indices, same with the preallocated space (see Jx) Jj = empty(len(dVm_x) * 4, dtype=int32) # fill Jx, Jj and Jp # createJ(dVm_x, dVa_x, Ybus.indptr, Ybus.indices, pvpq_lookup, pvpq, pq, Jx, Jj, Jp) if len(pvpq) == len(pq): create_J2(dVm_x, dVa_x, Ybus.indptr, Ybus.indices, pvpq_lookup, pvpq, pq, Jx, Jj, Jp) else: create_J(dVm_x, dVa_x, Ybus.indptr, Ybus.indices, pvpq_lookup, pvpq, pq, Jx, Jj, Jp) # resize before generating the scipy sparse matrix Jx.resize(Jp[-1], refcheck=False) Jj.resize(Jp[-1], refcheck=False) # generate scipy sparse matrix dimJ = npv + npq + npq J = sparse((Jx, Jj, Jp), shape=(dimJ, dimJ)) return J
Example #5
Source File: numeric.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def zeros_like(a, dtype=None, order='K', subok=True): """ Return an array of zeros with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` 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 `a` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of `a` 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. Returns ------- out : ndarray Array of zeros with the same shape and type as `a`. See Also -------- ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. Examples -------- >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.zeros_like(x) array([[0, 0, 0], [0, 0, 0]]) >>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.zeros_like(y) array([ 0., 0., 0.]) """ res = empty_like(a, dtype=dtype, order=order, subok=subok) # needed instead of a 0 to get same result as zeros for for string dtypes z = zeros(1, dtype=res.dtype) multiarray.copyto(res, z, casting='unsafe') return res
Example #6
Source File: numeric.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def ones(shape, dtype=None, order='C'): """ Return a new array of given shape and type, filled with ones. Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)`` or ``2``. dtype : data-type, optional The desired data-type for the array, e.g., `numpy.int8`. Default is `numpy.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 ones with the given shape, dtype, and order. See Also -------- zeros, ones_like Examples -------- >>> np.ones(5) array([ 1., 1., 1., 1., 1.]) >>> np.ones((5,), dtype=np.int) array([1, 1, 1, 1, 1]) >>> np.ones((2, 1)) array([[ 1.], [ 1.]]) >>> s = (2,2) >>> np.ones(s) array([[ 1., 1.], [ 1., 1.]]) """ a = empty(shape, dtype, order) multiarray.copyto(a, 1, casting='unsafe') return a
Example #7
Source File: numeric.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def ones_like(a, dtype=None, order='K', subok=True): """ Return an array of ones with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` 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 `a` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of `a` 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. Returns ------- out : ndarray Array of ones with the same shape and type as `a`. See Also -------- zeros_like : Return an array of zeros with shape and type of input. empty_like : Return an empty array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. Examples -------- >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.ones_like(x) array([[1, 1, 1], [1, 1, 1]]) >>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.ones_like(y) array([ 1., 1., 1.]) """ res = empty_like(a, dtype=dtype, order=order, subok=subok) multiarray.copyto(res, 1, casting='unsafe') return res
Example #8
Source File: numeric.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def full(shape, fill_value, dtype=None, order='C'): """ Return 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 : data-type, optional The desired data-type for the array, e.g., `np.int8`. Default is `float`, but will change to `np.array(fill_value).dtype` in a future release. 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 -------- zeros_like : Return an array of zeros with shape and type of input. ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. full_like : Fill an array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. Examples -------- >>> np.full((2, 2), np.inf) array([[ inf, inf], [ inf, inf]]) >>> np.full((2, 2), 10, dtype=np.int) array([[10, 10], [10, 10]]) """ a = empty(shape, dtype, order) if dtype is None and array(fill_value).dtype != a.dtype: warnings.warn( "in the future, full({0}, {1!r}) will return an array of {2!r}". format(shape, fill_value, array(fill_value).dtype), FutureWarning) multiarray.copyto(a, fill_value, casting='unsafe') return a
Example #9
Source File: numeric.py From auto-alt-text-lambda-api with MIT License | 4 votes |
def full_like(a, fill_value, dtype=None, order='K', subok=True): """ Return a full array with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` define these same attributes of the returned array. fill_value : scalar Fill value. dtype : data-type, 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. 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. Returns ------- out : ndarray Array of `fill_value` with the same shape and type as `a`. See Also -------- zeros_like : Return an array of zeros with shape and type of input. ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. full : Fill a new array. Examples -------- >>> x = np.arange(6, dtype=np.int) >>> np.full_like(x, 1) array([1, 1, 1, 1, 1, 1]) >>> np.full_like(x, 0.1) array([0, 0, 0, 0, 0, 0]) >>> np.full_like(x, 0.1, dtype=np.double) array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) >>> np.full_like(x, np.nan, dtype=np.double) array([ nan, nan, nan, nan, nan, nan]) >>> y = np.arange(6, dtype=np.double) >>> np.full_like(y, 0.1) array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) """ res = empty_like(a, dtype=dtype, order=order, subok=subok) multiarray.copyto(res, fill_value, casting='unsafe') return res
Example #10
Source File: high_speed_jacobian.py From GridCal with GNU General Public License v3.0 | 4 votes |
def dSbus_dV_numba_sparse(Yx, Yp, Yj, V, Vnorm, Ibus): # pragma: no cover """Computes partial derivatives of power injection w.r.t. voltage. Calculates faster with numba and sparse matrices. Input: Ybus in CSR sparse form (Yx = data, Yp = indptr, Yj = indices), V and Vnorm (= V / abs(V)) OUTPUT: data from CSR form of dS_dVm, dS_dVa (index pointer and indices are the same as the ones from Ybus) Translation of: dS_dVm = dS_dVm = diagV * conj(Ybus * diagVnorm) + conj(diagIbus) * diagVnorm dS_dVa = 1j * diagV * conj(diagIbus - Ybus * diagV) """ # transform input # init buffer vector buffer = zeros(len(V), dtype=complex128) dS_dVm = Yx.copy() dS_dVa = Yx.copy() # iterate through sparse matrix for r in range(len(Yp) - 1): for k in range(Yp[r], Yp[r + 1]): # Ibus = Ybus * V buffer[r] += Yx[k] * V[Yj[k]] # Ybus * diag(Vnorm) dS_dVm[k] *= Vnorm[Yj[k]] # Ybus * diag(V) dS_dVa[k] *= V[Yj[k]] Ibus[r] += buffer[r] # conj(diagIbus) * diagVnorm buffer[r] = conj(buffer[r]) * Vnorm[r] for r in range(len(Yp) - 1): for k in range(Yp[r], Yp[r + 1]): # diag(V) * conj(Ybus * diagVnorm) dS_dVm[k] = conj(dS_dVm[k]) * V[r] if r == Yj[k]: # diagonal elements dS_dVa[k] = -Ibus[r] + dS_dVa[k] dS_dVm[k] += buffer[r] # 1j * diagV * conj(diagIbus - Ybus * diagV) dS_dVa[k] = conj(-dS_dVa[k]) * (1j * V[r]) return dS_dVm, dS_dVa
Example #11
Source File: numeric.py From keras-lambda with MIT License | 4 votes |
def zeros_like(a, dtype=None, order='K', subok=True): """ Return an array of zeros with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` 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 `a` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of `a` 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. Returns ------- out : ndarray Array of zeros with the same shape and type as `a`. See Also -------- ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. Examples -------- >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.zeros_like(x) array([[0, 0, 0], [0, 0, 0]]) >>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.zeros_like(y) array([ 0., 0., 0.]) """ res = empty_like(a, dtype=dtype, order=order, subok=subok) # needed instead of a 0 to get same result as zeros for for string dtypes z = zeros(1, dtype=res.dtype) multiarray.copyto(res, z, casting='unsafe') return res
Example #12
Source File: numeric.py From keras-lambda with MIT License | 4 votes |
def ones(shape, dtype=None, order='C'): """ Return a new array of given shape and type, filled with ones. Parameters ---------- shape : int or sequence of ints Shape of the new array, e.g., ``(2, 3)`` or ``2``. dtype : data-type, optional The desired data-type for the array, e.g., `numpy.int8`. Default is `numpy.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 ones with the given shape, dtype, and order. See Also -------- zeros, ones_like Examples -------- >>> np.ones(5) array([ 1., 1., 1., 1., 1.]) >>> np.ones((5,), dtype=np.int) array([1, 1, 1, 1, 1]) >>> np.ones((2, 1)) array([[ 1.], [ 1.]]) >>> s = (2,2) >>> np.ones(s) array([[ 1., 1.], [ 1., 1.]]) """ a = empty(shape, dtype, order) multiarray.copyto(a, 1, casting='unsafe') return a
Example #13
Source File: numeric.py From keras-lambda with MIT License | 4 votes |
def ones_like(a, dtype=None, order='K', subok=True): """ Return an array of ones with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` 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 `a` is Fortran contiguous, 'C' otherwise. 'K' means match the layout of `a` 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. Returns ------- out : ndarray Array of ones with the same shape and type as `a`. See Also -------- zeros_like : Return an array of zeros with shape and type of input. empty_like : Return an empty array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. Examples -------- >>> x = np.arange(6) >>> x = x.reshape((2, 3)) >>> x array([[0, 1, 2], [3, 4, 5]]) >>> np.ones_like(x) array([[1, 1, 1], [1, 1, 1]]) >>> y = np.arange(3, dtype=np.float) >>> y array([ 0., 1., 2.]) >>> np.ones_like(y) array([ 1., 1., 1.]) """ res = empty_like(a, dtype=dtype, order=order, subok=subok) multiarray.copyto(res, 1, casting='unsafe') return res
Example #14
Source File: numeric.py From keras-lambda with MIT License | 4 votes |
def full(shape, fill_value, dtype=None, order='C'): """ Return 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 : data-type, optional The desired data-type for the array, e.g., `np.int8`. Default is `float`, but will change to `np.array(fill_value).dtype` in a future release. 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 -------- zeros_like : Return an array of zeros with shape and type of input. ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. full_like : Fill an array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. Examples -------- >>> np.full((2, 2), np.inf) array([[ inf, inf], [ inf, inf]]) >>> np.full((2, 2), 10, dtype=np.int) array([[10, 10], [10, 10]]) """ a = empty(shape, dtype, order) if dtype is None and array(fill_value).dtype != a.dtype: warnings.warn( "in the future, full({0}, {1!r}) will return an array of {2!r}". format(shape, fill_value, array(fill_value).dtype), FutureWarning) multiarray.copyto(a, fill_value, casting='unsafe') return a
Example #15
Source File: numeric.py From keras-lambda with MIT License | 4 votes |
def full_like(a, fill_value, dtype=None, order='K', subok=True): """ Return a full array with the same shape and type as a given array. Parameters ---------- a : array_like The shape and data-type of `a` define these same attributes of the returned array. fill_value : scalar Fill value. dtype : data-type, 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. 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. Returns ------- out : ndarray Array of `fill_value` with the same shape and type as `a`. See Also -------- zeros_like : Return an array of zeros with shape and type of input. ones_like : Return an array of ones with shape and type of input. empty_like : Return an empty array with shape and type of input. zeros : Return a new array setting values to zero. ones : Return a new array setting values to one. empty : Return a new uninitialized array. full : Fill a new array. Examples -------- >>> x = np.arange(6, dtype=np.int) >>> np.full_like(x, 1) array([1, 1, 1, 1, 1, 1]) >>> np.full_like(x, 0.1) array([0, 0, 0, 0, 0, 0]) >>> np.full_like(x, 0.1, dtype=np.double) array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) >>> np.full_like(x, np.nan, dtype=np.double) array([ nan, nan, nan, nan, nan, nan]) >>> y = np.arange(6, dtype=np.double) >>> np.full_like(y, 0.1) array([ 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) """ res = empty_like(a, dtype=dtype, order=order, subok=subok) multiarray.copyto(res, fill_value, casting='unsafe') return res