File size: 2,051 Bytes
beffe4b | 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 | import cupy
from cupy import _core
def tile(A, reps):
"""Construct an array by repeating A the number of times given by reps.
Args:
A (cupy.ndarray): Array to transform.
reps (int or tuple): The number of repeats.
Returns:
cupy.ndarray: Transformed array with repeats.
.. seealso:: :func:`numpy.tile`
"""
try:
tup = tuple(reps)
except TypeError:
tup = (reps,)
d = len(tup)
if tup.count(1) == len(tup) and isinstance(A, cupy.ndarray):
# Fixes the problem that the function does not make a copy if A is a
# array and the repetitions are 1 in all dimensions
return cupy.array(A, copy=True, ndmin=d)
else:
# Note that no copy of zero-sized arrays is made. However since they
# have no data there is no risk of an inadvertent overwrite.
c = cupy.array(A, copy=False, ndmin=d)
if d < c.ndim:
tup = (1,) * (c.ndim - d) + tup
shape_out = tuple(s * t for s, t in zip(c.shape, tup))
if c.size == 0:
return cupy.empty(shape_out, dtype=c.dtype)
c_shape = []
ret_shape = []
for dim_in, nrep in zip(c.shape, tup):
if nrep == 1:
c_shape.append(dim_in)
ret_shape.append(dim_in)
elif dim_in == 1:
c_shape.append(dim_in)
ret_shape.append(nrep)
else:
c_shape.append(1)
c_shape.append(dim_in)
ret_shape.append(nrep)
ret_shape.append(dim_in)
ret = cupy.empty(ret_shape, dtype=c.dtype)
if ret.size:
_core.elementwise_copy(c.reshape(c_shape), ret)
return ret.reshape(shape_out)
def repeat(a, repeats, axis=None):
"""Repeat arrays along an axis.
Args:
a (cupy.ndarray): Array to transform.
repeats (int, list or tuple): The number of repeats.
axis (int): The axis to repeat.
Returns:
cupy.ndarray: Transformed array with repeats.
.. seealso:: :func:`numpy.repeat`
"""
return a.repeat(repeats, axis)
|