8000 MAINT: avoid nested dispatch in numpy.core.shape_base by shoyer · Pull Request #13634 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: avoid nested dispatch in numpy.core.shape_base #13634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 12, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions numpy/core/shape_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def atleast_2d(*arys):
if ary.ndim == 0:
result = ary.reshape(1, 1)
elif ary.ndim == 1:
result = ary[newaxis,:]
result = ary[newaxis, :]
else:
result = ary
res.append(result)
Expand Down Expand Up @@ -193,9 +193,9 @@ def atleast_3d(*arys):
if ary.ndim == 0:
result = ary.reshape(1, 1, 1)
elif ary.ndim == 1:
result = ary[newaxis,:, newaxis]
result = ary[newaxis, :, newaxis]
elif ary.ndim == 2:
result = ary[:,:, newaxis]
result = ary[:, :, newaxis]
else:
result = ary
res.append(result)
Expand Down Expand Up @@ -416,6 +416,13 @@ def stack(arrays, axis=0, out=None):
return _nx.concatenate(expanded_arrays, axis=axis, out=out)


# Internal functions to elimainate the overhead of repeated dispatch inside
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spelling of eliminate. Also maybe write "repeated dispatch in one of the two possible paths inside np.block"

# np.block. Use getattr to protect against __array_function__ being disabled.
_size = getattr(_nx.size, '__wrapped__', _nx.size)
_ndim = getattr(_nx.ndim, '__wrapped__', _nx.ndim)
_concatenate = getattr(_nx.concatenate, '__wrapped__', _nx.concatenate)


def _block_format_index(index):
"""
Convert a list of indices ``[0, 1, 2]`` into ``"arrays[0][1][2]"``.
Expand Down Expand Up @@ -496,8 +503,8 @@ def _block_check_depths_match(arrays, parent_index=[]):
return parent_index + [None], 0, 0
else:
# We've 'bottomed out' - arrays is either a scalar or an array
size = _nx.size(arrays)
return parent_index, _nx.ndim(arrays), size
size = _size(arrays)
return parent_index, _ndim(arrays), size


def _atleast_nd(a, ndim):
Expand Down Expand Up @@ -640,7 +647,7 @@ def _block(arrays, max_depth, result_ndim, depth=0):
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))
return _concatenate(arrs, axis=-(max_depth-depth))
else:
# We've 'bottomed out' - arrays is either a scalar or an array
# type(arrays) is not list
Expand Down Expand Up @@ -858,7 +865,7 @@ def _block_slicing(arrays, list_ndim, result_ndim):

# Test preferring F only in the case that all input arrays are F
F_order = all(arr.flags['F_CONTIGUOUS'] for arr in arrays)
C_order = all(arr.flags['C_CONTIGUOUS'] for arr in arrays)
C_order = all(arr.flags['C_CONTIGUOUS'] for arr in arrays)
order = 'F' if F_order and not C_order else 'C'
result = _nx.empty(shape=shape, dtype=dtype, order=order)
# Note: In a c implementation, the function
Expand Down
0