File size: 12,054 Bytes
c835f15 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
import math
from typing import Any, Optional
import numpy
import cupy
from cupy._core.internal import _get_strides_for_order_K, _update_order_char
from cupy.typing._types import (
_OrderKACF, _OrderCF, _ShapeLike, DTypeLike, NDArray,
)
def empty(
shape: _ShapeLike,
dtype: DTypeLike = float,
order: _OrderCF = 'C',
) -> NDArray[Any]:
"""Returns an array without initializing the elements.
Args:
shape (int or tuple of ints): Dimensionalities of the array.
dtype (data-type, optional): Data type specifier.
order ({'C', 'F'}): Row-major (C-style) or column-major
(Fortran-style) order.
Returns:
cupy.ndarray: A new array with elements not initialized.
.. seealso:: :func:`numpy.empty`
"""
return cupy.ndarray(shape, dtype, order=order)
def _new_like_order_and_strides(
a, dtype, order, shape=None, *, get_memptr=True):
"""
Determine order and strides as in NumPy's PyArray_NewLikeArray.
(see: numpy/core/src/multiarray/ctors.c)
"""
order = order.upper()
if order not in ['C', 'F', 'K', 'A']:
raise ValueError('order not understood: {}'.format(order))
if numpy.isscalar(shape):
shape = (shape,)
# Fallback to c_contiguous if keep order and number of dimensions
# of new shape mismatch
if order == 'K' and shape is not None and len(shape) != a.ndim:
return 'C', None, None
order = chr(_update_order_char(
a.flags.c_contiguous, a.flags.f_contiguous, ord(order)))
if order == 'K':
strides = _get_strides_for_order_K(a, numpy.dtype(dtype), shape)
order = 'C'
size = math.prod(shape) if shape is not None else a.size
memptr = cupy.empty(size, dtype=dtype).data if get_memptr else None
return order, strides, memptr
else:
return order, None, None
def empty_like(
prototype: NDArray[Any],
dtype: DTypeLike = None,
order: _OrderKACF = 'K',
subok: None = None,
shape: Optional[_ShapeLike] = None,
) -> NDArray[Any]:
"""Returns a new array with same shape and dtype of a given array.
This function currently does not support ``subok`` option.
Args:
a (cupy.ndarray): Base array.
dtype (data-type, optional): Data type specifier.
The data type of ``a`` is used by default.
order ({'C', 'F', 'A', or 'K'}): 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: Not supported yet, must be None.
shape (int or tuple of ints): 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:
cupy.ndarray: A new array with same shape and dtype of ``a`` with
elements not initialized.
.. seealso:: :func:`numpy.empty_like`
"""
if subok is not None:
raise TypeError('subok is not supported yet')
if dtype is None:
dtype = prototype.dtype
order, strides, memptr = _new_like_order_and_strides(
prototype, dtype, order, shape)
shape = shape if shape else prototype.shape
return cupy.ndarray(shape, dtype, memptr, strides, order)
def eye(
N: int,
M: Optional[int] = None,
k: int = 0,
dtype: DTypeLike = float,
order: _OrderCF = 'C',
) -> NDArray[Any]:
"""Returns a 2-D array with ones on the diagonals and zeros elsewhere.
Args:
N (int): Number of rows.
M (int): Number of columns. ``M == N`` by default.
k (int): Index of the diagonal. Zero indicates the main diagonal,
a positive index an upper diagonal, and a negative index a lower
diagonal.
dtype (data-type, optional): Data type specifier.
order ({'C', 'F'}): Row-major (C-style) or column-major
(Fortran-style) order.
Returns:
cupy.ndarray: A 2-D array with given diagonals filled with ones and
zeros elsewhere.
.. seealso:: :func:`numpy.eye`
"""
if M is None:
M = N
ret = zeros((N, M), dtype=dtype, order=order)
if k <= -N or k >= M:
return ret
ret.diagonal(k).fill(1)
return ret
def identity(n: int, dtype: DTypeLike = float) -> NDArray[Any]:
"""Returns a 2-D identity array.
It is equivalent to ``eye(n, n, dtype)``.
Args:
n (int): Number of rows and columns.
dtype (data-type, optional): Data type specifier.
Returns:
cupy.ndarray: A 2-D identity array.
.. seealso:: :func:`numpy.identity`
"""
return eye(n, dtype=dtype)
def ones(
shape: _ShapeLike,
dtype: DTypeLike = float,
order: _OrderCF = 'C',
) -> NDArray[Any]:
"""Returns a new array of given shape and dtype, filled with ones.
This function currently does not support ``order`` option.
Args:
shape (int or tuple of ints): Dimensionalities of the array.
dtype (data-type, optional): Data type specifier.
order ({'C', 'F'}): Row-major (C-style) or column-major
(Fortran-style) order.
Returns:
cupy.ndarray: An array filled with ones.
.. seealso:: :func:`numpy.ones`
"""
a = cupy.ndarray(shape, dtype, order=order)
a.fill(1)
return a
def ones_like(
a: NDArray[Any],
dtype: DTypeLike = None,
order: _OrderKACF = 'K',
subok: None = None,
shape: Optional[_ShapeLike] = None,
) -> NDArray[Any]:
"""Returns an array of ones with same shape and dtype as a given array.
This function currently does not support ``subok`` option.
Args:
a (cupy.ndarray): Base array.
dtype (data-type, optional): Data type specifier.
The dtype of ``a`` is used by default.
order ({'C', 'F', 'A', or 'K'}): 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: Not supported yet, must be None.
shape (int or tuple of ints): 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:
cupy.ndarray: An array filled with ones.
.. seealso:: :func:`numpy.ones_like`
"""
if subok is not None:
raise TypeError('subok is not supported yet')
if dtype is None:
dtype = a.dtype
order, strides, memptr = _new_like_order_and_strides(a, dtype, order,
shape)
shape = shape if shape else a.shape
a = cupy.ndarray(shape, dtype, memptr, strides, order)
a.fill(1)
return a
def zeros(
shape: _ShapeLike,
dtype: DTypeLike = float,
order: _OrderCF = 'C',
) -> NDArray[Any]:
"""Returns a new array of given shape and dtype, filled with zeros.
Args:
shape (int or tuple of ints): Dimensionalities of the array.
dtype (data-type, optional): Data type specifier.
order ({'C', 'F'}): Row-major (C-style) or column-major
(Fortran-style) order.
Returns:
cupy.ndarray: An array filled with zeros.
.. seealso:: :func:`numpy.zeros`
"""
a = cupy.ndarray(shape, dtype, order=order)
a.data.memset_async(0, a.nbytes)
return a
def zeros_like(
a: NDArray[Any],
dtype: DTypeLike = None,
order: _OrderKACF = 'K',
subok: None = None,
shape: Optional[_ShapeLike] = None,
) -> NDArray[Any]:
"""Returns an array of zeros with same shape and dtype as a given array.
This function currently does not support ``subok`` option.
Args:
a (cupy.ndarray): Base array.
dtype (data-type, optional): Data type specifier.
The dtype of ``a`` is used by default.
order ({'C', 'F', 'A', or 'K'}): 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: Not supported yet, must be None.
shape (int or tuple of ints): 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:
cupy.ndarray: An array filled with zeros.
.. seealso:: :func:`numpy.zeros_like`
"""
if subok is not None:
raise TypeError('subok is not supported yet')
if dtype is None:
dtype = a.dtype
order, strides, memptr = _new_like_order_and_strides(a, dtype, order,
shape)
shape = shape if shape else a.shape
a = cupy.ndarray(shape, dtype, memptr, strides, order)
a.data.memset_async(0, a.nbytes)
return a
def full(
shape: _ShapeLike,
fill_value: Any,
dtype: DTypeLike = None,
order: _OrderCF = 'C',
) -> NDArray[Any]:
"""Returns a new array of given shape and dtype, filled with a given value.
This function currently does not support ``order`` option.
Args:
shape (int or tuple of ints): Dimensionalities of the array.
fill_value: A scalar value to fill a new array.
dtype (data-type, optional): Data type specifier.
order ({'C', 'F'}): Row-major (C-style) or column-major
(Fortran-style) order.
Returns:
cupy.ndarray: An array filled with ``fill_value``.
.. seealso:: :func:`numpy.full`
"""
if dtype is None:
if isinstance(fill_value, cupy.ndarray):
dtype = fill_value.dtype
else:
dtype = numpy.array(fill_value).dtype
a = cupy.ndarray(shape, dtype, order=order)
cupy.copyto(a, fill_value, casting='unsafe')
return a
def full_like(
a: NDArray[Any],
fill_value: Any,
dtype: DTypeLike = None,
order: _OrderKACF = 'K',
subok: None = None,
shape: Optional[_ShapeLike] = None,
) -> NDArray[Any]:
"""Returns a full array with same shape and dtype as a given array.
This function currently does not support ``subok`` option.
Args:
a (cupy.ndarray): Base array.
fill_value: A scalar value to fill a new array.
dtype (data-type, optional): Data type specifier.
The dtype of ``a`` is used by default.
order ({'C', 'F', 'A', or 'K'}): 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: Not supported yet, must be None.
shape (int or tuple of ints): 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:
cupy.ndarray: An array filled with ``fill_value``.
.. seealso:: :func:`numpy.full_like`
"""
if subok is not None:
raise TypeError('subok is not supported yet')
if dtype is None:
dtype = a.dtype
order, strides, memptr = _new_like_order_and_strides(a, dtype, order,
shape)
shape = shape if shape else a.shape
a = cupy.ndarray(shape, dtype, memptr, strides, order)
cupy.copyto(a, fill_value, casting='unsafe')
return a
|