summaryrefslogtreecommitdiff
path: root/numpy/core/shape_base.py
diff options
context:
space:
mode:
authorMark Harfouche <mark.harfouche@gmail.com>2018-09-19 10:30:59 -0400
committerMark Harfouche <mark.harfouche@gmail.com>2018-09-20 13:20:05 -0400
commit9779e6916169067c6999f4cf55a63167ab33e5a4 (patch)
tree550c06572854caf21afbf56dd135992a3207bae7 /numpy/core/shape_base.py
parent2ccfc32ea1de8656c6c780ad52aa7315294a5151 (diff)
downloadnumpy-9779e6916169067c6999f4cf55a63167ab33e5a4.tar.gz
MAINT: speed up _block by avoiding a recursive closure
Diffstat (limited to 'numpy/core/shape_base.py')
-rw-r--r--numpy/core/shape_base.py33
1 files changed, 15 insertions, 18 deletions
diff --git a/numpy/core/shape_base.py b/numpy/core/shape_base.py
index 30919ed7e..52717abda 100644
--- a/numpy/core/shape_base.py
+++ b/numpy/core/shape_base.py
@@ -7,7 +7,6 @@ __all__ = ['atleast_1d', 'atleast_2d', 'atleast_3d', 'block', 'hstack',
from . import numeric as _nx
from .numeric import array, asanyarray, newaxis
from .multiarray import normalize_axis_index
-from ._internal import recursive
def atleast_1d(*arys):
"""
@@ -438,7 +437,13 @@ def _block_check_depths_match(arrays, parent_index=[]):
return parent_index, _nx.ndim(arrays)
-def _block(arrays, max_depth, result_ndim):
+def _atleast_nd(a, ndim):
+ # Ensures `a` has at least `ndim` dimensions by prepending
+ # ones to `a.shape` as necessary
+ return array(a, ndmin=ndim, copy=False, subok=True)
+
+
+def _block(arrays, max_depth, result_ndim, depth=0):
"""
Internal implementation of block. `arrays` is the argument passed to
block. `max_depth` is the depth of nested lists within `arrays` and
@@ -446,22 +451,14 @@ def _block(arrays, max_depth, result_ndim):
`arrays` and the depth of the lists in `arrays` (see block docstring
for details).
"""
- def atleast_nd(a, ndim):
- # Ensures `a` has at least `ndim` dimensions by prepending
- # ones to `a.shape` as necessary
- return array(a, ndmin=ndim, copy=False, subok=True)
-
- @recursive
- def block_recursion(self, arrays, depth=0):
- if depth < max_depth:
- arrs = [self(arr, depth+1) for arr in arrays]
- return _nx.concatenate(arrs, axis=-(max_depth-depth))
- else:
- # We've 'bottomed out' - arrays is either a scalar or an array
- # type(arrays) is not list
- return atleast_nd(arrays, result_ndim)
-
- return block_recursion(arrays)
+ if depth < max_depth:
+ arrs = [_block(arr, max_depth, result_ndim, depth+1)
+ for arr in arrays]
+ return _nx.concatenate(arrs, axis=-(max_depth-depth))
+ else:
+ # We've 'bottomed out' - arrays is either a scalar or an array
+ # type(arrays) is not list
+ return _atleast_nd(arrays, result_ndim)
def block(arrays):