Source code for hipecta.memory._helpers

from numpy import float64 as _float64
from .._memory import empty

__all__ = ['zeros', 'ones', 'copyto']

[docs]def zeros(shape, dtype=_float64): """ Return a new array, memory aligned, 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 : data-type, optional The desired data-type for the array, default: numpy.float32 Returns ------- Array of zeros with the given shape, dtype. """ array = empty(shape, dtype) array.fill(0.) return array
[docs]def ones(shape, dtype=_float64): """ Return a new array, memory aligned, 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, default: numpy.float32 Returns ------- Array of ones with the given shape, dtype. """ array = empty(shape, dtype) array.fill(1) return array
[docs]def copyto(dst, src): """ Copies values from one array to another. Parameters ---------- dst : ndarray The array into which values are copied. src : array_like The array from which values are copied. """ if src.shape != dst.shape: raise RuntimeError("array must have the same shape)") if len(src.shape) == 3: for i in range(src.shape[0]): for j in range(src.shape[1]): for k in range(src.shape[2]): dst[i][j][k] = src[i][j][k] if len(src.shape) == 2: for i in range(src.shape[0]): for j in range(src.shape[1]): dst[i][j] = src[i][j] if len(src.shape) == 1: for i in range(src.shape[0]): dst[i] = src[i]