From 82ce0a1e8ccf1b83badb9fac3c2160e5896051b5 Mon Sep 17 00:00:00 2001 From: Eric Wieser Date: Sat, 18 Feb 2017 16:06:21 +0000 Subject: ENH: Improve the efficiency of indices Previously, this special cased something that needed no special case, and did addition with zero to encourage broadcasting that would happen anyway. --- numpy/core/numeric.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'numpy/core/numeric.py') diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index b90b0a9c9..97d19f008 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -2089,15 +2089,12 @@ def indices(dimensions, dtype=int): """ dimensions = tuple(dimensions) N = len(dimensions) - if N == 0: - return array([], dtype=dtype) + shape = (1,)*N res = empty((N,)+dimensions, dtype=dtype) for i, dim in enumerate(dimensions): - tmp = arange(dim, dtype=dtype) - tmp.shape = (1,)*i + (dim,)+(1,)*(N-i-1) - newdim = dimensions[:i] + (1,) + dimensions[i+1:] - val = zeros(newdim, dtype) - add(tmp, val, res[i]) + res[i] = arange(dim, dtype=dtype).reshape( + shape[:i] + (dim,) + shape[i+1:] + ) return res -- cgit v1.2.1