diff options
author | Marten van Kerkwijk <mhvk@astro.utoronto.ca> | 2017-02-14 14:33:08 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-14 14:33:08 -0500 |
commit | 9106adfba085e93bf066d47e2da47b47839c8e08 (patch) | |
tree | c85bdb8fa5df6422bdb575c77372be7ae87cf6ae /numpy/lib | |
parent | bbd5a7750fbed42a4aaf12f196282a9504a1e865 (diff) | |
parent | d9b26f804117ebc1f591dff33d06ce2339af9339 (diff) | |
download | python-numpy-9106adfba085e93bf066d47e2da47b47839c8e08.tar.gz python-numpy-9106adfba085e93bf066d47e2da47b47839c8e08.tar.bz2 python-numpy-9106adfba085e93bf066d47e2da47b47839c8e08.zip |
Merge pull request #8617 from eric-wieser/fix-8561
BUG: Copy meshgrid after broadcasting
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 17 | ||||
-rw-r--r-- | numpy/lib/stride_tricks.py | 2 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 10 | ||||
-rw-r--r-- | numpy/lib/tests/test_stride_tricks.py | 2 |
4 files changed, 19 insertions, 12 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index 2aa104174..ae1420b72 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -4505,24 +4505,21 @@ def meshgrid(*xi, **kwargs): "Valid values for `indexing` are 'xy' and 'ij'.") s0 = (1,) * ndim - output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1::]) + output = [np.asanyarray(x).reshape(s0[:i] + (-1,) + s0[i + 1:]) for i, x in enumerate(xi)] - shape = [x.size for x in output] - if indexing == 'xy' and ndim > 1: # switch first and second axis - output[0].shape = (1, -1) + (1,)*(ndim - 2) - output[1].shape = (-1, 1) + (1,)*(ndim - 2) - shape[0], shape[1] = shape[1], shape[0] - - if copy_: - output = [x.copy() for x in output] + output[0].shape = (1, -1) + s0[2:] + output[1].shape = (-1, 1) + s0[2:] - if not sparse and len(output) > 0: + if not sparse: # Return the full N-D matrix (not only the 1-D vector) output = np.broadcast_arrays(*output, subok=True) + if copy_: + output = [x.copy() for x in output] + return output diff --git a/numpy/lib/stride_tricks.py b/numpy/lib/stride_tricks.py index f390cf49b..545623c38 100644 --- a/numpy/lib/stride_tricks.py +++ b/numpy/lib/stride_tricks.py @@ -179,7 +179,7 @@ def _broadcast_shape(*args): supplied arrays against each other. """ if not args: - raise ValueError('must provide at least one argument') + return () # use the old-iterator because np.nditer does not handle size 0 arrays # consistently b = np.broadcast(*args[:32]) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 5c2446e50..f69c24d59 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -2266,6 +2266,16 @@ class TestMeshgrid(TestCase): assert_(X.dtype == x.dtype) assert_(Y.dtype == y.dtype) + def test_writeback(self): + # Issue 8561 + X = np.array([1.1, 2.2]) + Y = np.array([3.3, 4.4]) + x, y = np.meshgrid(X, Y, sparse=False, copy=True) + + x[0, :] = 0 + assert_equal(x[0, :], 0) + assert_equal(x[1, :], X) + class TestPiecewise(TestCase): diff --git a/numpy/lib/tests/test_stride_tricks.py b/numpy/lib/tests/test_stride_tricks.py index 95df135cf..39a76c2f6 100644 --- a/numpy/lib/tests/test_stride_tricks.py +++ b/numpy/lib/tests/test_stride_tricks.py @@ -266,7 +266,7 @@ def test_broadcast_to_raises(): def test_broadcast_shape(): # broadcast_shape is already exercized indirectly by broadcast_arrays - assert_raises(ValueError, _broadcast_shape) + assert_equal(_broadcast_shape(), ()) assert_equal(_broadcast_shape([1, 2]), (2,)) assert_equal(_broadcast_shape(np.ones((1, 1))), (1, 1)) assert_equal(_broadcast_shape(np.ones((1, 1)), np.ones((3, 4))), (3, 4)) |