def shaped_arange(shape, xp=cupy, dtype=numpy.float32):
"""Returns an array with given shape, array module, and dtype.
Args:
shape(tuple of int): Shape of returned ndarray.
xp(numpy or cupy): Array module to use.
dtype(dtype): Dtype of returned ndarray.
Returns:
numpy.ndarray or cupy.ndarray:
The array filled with :math:`1, \cdots, N` with specified dtype
with given shape, array module. Here, :math:`N` is
the size of the returned array.
If ``dtype`` is ``numpy.bool_``, evens (resp. odds) are converted to
``True`` (resp. ``False``).
"""
a = numpy.arange(1, internal.prod(shape) + 1, 1)
if numpy.dtype(dtype).type == numpy.bool_:
return xp.array((a % 2 == 0).reshape(shape))
else:
return xp.array(a.astype(dtype).reshape(shape))
评论列表
文章目录