Optimize weight and mask
@@ -122,12 +122,13 @@ def map_coordinates(input, coordinates, output=None, order=None,
sides.append([0, 1])
out = cupy.zeros(coordinates.shape[1], dtype=input.dtype)
- for side in itertools.product(*sides):
- ind = []
if input.dtype in (cupy.float64, cupy.complex128):
- weight = cupy.ones(coordinates.shape[1], dtype=cupy.float64)
+ weight = cupy.empty(coordinates.shape[1], dtype=cupy.float64)
else:
- weight = cupy.ones(coordinates.shape[1], dtype=cupy.float32)
+ weight = cupy.empty(coordinates.shape[1], dtype=cupy.float32)
+ for side in itertools.product(*sides):
+ weight.fill(1)
+ ind = []
for i in six.moves.range(input.ndim):
if side[i] == 0:
ind.append(coordinates_floor[i])
@@ -136,15 +137,15 @@ def map_coordinates(input, coordinates, output=None, order=None,
ind.append(coordinates_ceil[i])
weight *= coordinates[i] - coordinates_floor[i]
out += input[ind] * weight
+ del weight
if mode == 'constant':
- mask = cupy.ones(coordinates.shape[1], dtype=bool)
+ mask = cupy.zeros(coordinates.shape[1], dtype=cupy.bool_)
for i in six.moves.range(input.ndim):
- mask *= 0 <= coordinates[i]
- mask *= coordinates[i] <= input.shape[i] - 1
- c = (cupy.ones_like(mask) - mask) * cval
- out *= mask
- out += c
+ mask += coordinates[i] < 0
+ mask += coordinates[i] > input.shape[i] - 1
+ out[mask] = cval
+ del mask
if cupy.issubdtype(output.dtype, cupy.integer):
out = cupy.rint(out)
