Skip to content

MAINT: Replace setting of array shape by reshape operation #29314

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions numpy/_core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -1106,10 +1106,9 @@ def tensordot(a, b, axes=2):

An extended example taking advantage of the overloading of + and \\*:

>>> a = np.array(range(1, 9))
>>> a.shape = (2, 2, 2)
>>> a = np.array(range(1, 9)).reshape((2, 2, 2))
>>> A = np.array(('a', 'b', 'c', 'd'), dtype=object)
>>> A.shape = (2, 2)
>>> A = A.reshape((2, 2))
>>> a; A
array([[[1, 2],
[3, 4]],
Expand Down
70 changes: 32 additions & 38 deletions numpy/_core/tests/test_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,20 @@ def __rmul__(self, other):
def test_einsum_views(self):
# pass-through
for do_opt in [True, False]:
a = np.arange(6)
a.shape = (2, 3)
a = np.arange(6).reshape((2, 3))

b = np.einsum("...", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)

b = np.einsum(a, [Ellipsis], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)

b = np.einsum("ij", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, a)

b = np.einsum(a, [0, 1], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, a)

# output is writeable whenever input is writeable
Expand All @@ -256,115 +255,110 @@ def test_einsum_views(self):
assert_(not b.flags['WRITEABLE'])

# transpose
a = np.arange(6)
a.shape = (2, 3)
a = np.arange(6).reshape((2, 3))

b = np.einsum("ji", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, a.T)

b = np.einsum(a, [1, 0], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, a.T)

# diagonal
a = np.arange(9)
a.shape = (3, 3)
a = np.arange(9).reshape((3, 3))

b = np.einsum("ii->i", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[i, i] for i in range(3)])

b = np.einsum(a, [0, 0], [0], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[i, i] for i in range(3)])

# diagonal with various ways of broadcasting an additional dimension
a = np.arange(27)
a.shape = (3, 3, 3)
a = np.arange(27).reshape((3, 3, 3))

b = np.einsum("...ii->...i", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [[x[i, i] for i in range(3)] for x in a])

b = np.einsum(a, [Ellipsis, 0, 0], [Ellipsis, 0], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [[x[i, i] for i in range(3)] for x in a])

b = np.einsum("ii...->...i", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [[x[i, i] for i in range(3)]
for x in a.transpose(2, 0, 1)])

b = np.einsum(a, [0, 0, Ellipsis], [Ellipsis, 0], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [[x[i, i] for i in range(3)]
for x in a.transpose(2, 0, 1)])

b = np.einsum("...ii->i...", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[:, i, i] for i in range(3)])

b = np.einsum(a, [Ellipsis, 0, 0], [0, Ellipsis], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[:, i, i] for i in range(3)])

b = np.einsum("jii->ij", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[:, i, i] for i in range(3)])

b = np.einsum(a, [1, 0, 0], [0, 1], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[:, i, i] for i in range(3)])

b = np.einsum("ii...->i...", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a.transpose(2, 0, 1)[:, i, i] for i in range(3)])

b = np.einsum(a, [0, 0, Ellipsis], [0, Ellipsis], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a.transpose(2, 0, 1)[:, i, i] for i in range(3)])

b = np.einsum("i...i->i...", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a.transpose(1, 0, 2)[:, i, i] for i in range(3)])

b = np.einsum(a, [0, Ellipsis, 0], [0, Ellipsis], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a.transpose(1, 0, 2)[:, i, i] for i in range(3)])

b = np.einsum("i...i->...i", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [[x[i, i] for i in range(3)]
for x in a.transpose(1, 0, 2)])

b = np.einsum(a, [0, Ellipsis, 0], [Ellipsis, 0], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [[x[i, i] for i in range(3)]
for x in a.transpose(1, 0, 2)])

# triple diagonal
a = np.arange(27)
a.shape = (3, 3, 3)
a = np.arange(27).reshape((3, 3, 3))

b = np.einsum("iii->i", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[i, i, i] for i in range(3)])

b = np.einsum(a, [0, 0, 0], [0], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, [a[i, i, i] for i in range(3)])

# swap axes
a = np.arange(24)
a.shape = (2, 3, 4)
a = np.arange(24).reshape((2, 3, 4))

b = np.einsum("ijk->jik", a, optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, a.swapaxes(0, 1))

b = np.einsum(a, [0, 1, 2], [1, 0, 2], optimize=do_opt)
assert_(b.base is a)
assert_(b.base is a.base)
assert_equal(b, a.swapaxes(0, 1))

def check_einsum_sums(self, dtype, do_opt=False):
Expand Down
22 changes: 9 additions & 13 deletions numpy/_core/tests/test_multiarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2281,8 +2281,7 @@ def test_sort_axis(self):

def test_sort_size_0(self):
# check axis handling for multidimensional empty arrays
a = np.array([])
a.shape = (3, 2, 1, 0)
a = np.array([]).reshape((3, 2, 1, 0))
for axis in range(-a.ndim, a.ndim):
msg = f'test empty array sort with axis={axis}'
assert_equal(np.sort(a, axis=axis), a, msg)
Expand Down Expand Up @@ -2543,8 +2542,7 @@ def test_argsort(self):
assert_equal(a.copy().argsort(), c)

# check axis handling for multidimensional empty arrays
a = np.array([])
a.shape = (3, 2, 1, 0)
a = np.array([]).reshape((3, 2, 1, 0))
for axis in range(-a.ndim, a.ndim):
msg = f'test empty array argsort with axis={axis}'
assert_equal(np.argsort(a, axis=axis),
Expand Down Expand Up @@ -2861,8 +2859,7 @@ def test_partition_integer(self):
def test_partition_empty_array(self, kth_dtype):
# check axis handling for multidimensional empty arrays
kth = np.array(0, dtype=kth_dtype)[()]
a = np.array([])
a.shape = (3, 2, 1, 0)
a = np.array([]).reshape((3, 2, 1, 0))
for axis in range(-a.ndim, a.ndim):
msg = f'test empty array partition with axis={axis}'
assert_equal(np.partition(a, kth, axis=axis), a, msg)
Expand All @@ -2873,8 +2870,7 @@ def test_partition_empty_array(self, kth_dtype):
def test_argpartition_empty_array(self, kth_dtype):
# check axis handling for multidimensional empty arrays
kth = np.array(0, dtype=kth_dtype)[()]
a = np.array([])
a.shape = (3, 2, 1, 0)
a = np.array([]).reshape((3, 2, 1, 0))
for axis in range(-a.ndim, a.ndim):
msg = f'test empty array argpartition with axis={axis}'
assert_equal(np.partition(a, kth, axis=axis),
Expand Down Expand Up @@ -5358,7 +5354,7 @@ def test_ip_types(self):
unchecked_types = [bytes, str, np.void]

x = np.random.random(24) * 100
x.shape = 2, 3, 4
x = x.reshape((2, 3, 4))
for types in np._core.sctypes.values():
for T in types:
if T not in unchecked_types:
Expand All @@ -5369,20 +5365,20 @@ def test_ip_types(self):

def test_raise(self):
x = np.random.random(24) * 100
x.shape = 2, 3, 4
x = x.reshape((2, 3, 4))
assert_raises(IndexError, x.take, [0, 1, 2], axis=0)
assert_raises(IndexError, x.take, [-3], axis=0)
assert_array_equal(x.take([-1], axis=0)[0], x[1])

def test_clip(self):
x = np.random.random(24) * 100
x.shape = 2, 3, 4
x = x.reshape((2, 3, 4))
assert_array_equal(x.take([-1], axis=0, mode='clip')[0], x[0])
assert_array_equal(x.take([2], axis=0, mode='clip')[0], x[1])

def test_wrap(self):
x = np.random.random(24) * 100
x.shape = 2, 3, 4
x = x.reshape((2, 3, 4))
assert_array_equal(x.take([-1], axis=0, mode='wrap')[0], x[1])
assert_array_equal(x.take([2], axis=0, mode='wrap')[0], x[0])
assert_array_equal(x.take([3], axis=0, mode='wrap')[0], x[1])
Expand Down Expand Up @@ -5962,7 +5958,7 @@ class TestFlat:
def setup_method(self):
a0 = np.arange(20.0)
a = a0.reshape(4, 5)
a0.shape = (4, 5)
a0 = a0.reshape((4, 5))
a.flags.writeable = False
self.a = a
self.b = a[::2, ::2]
Expand Down
4 changes: 2 additions & 2 deletions numpy/lib/_format_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,10 +879,10 @@ def read_array(fp, allow_pickle=False, pickle_kwargs=None, *,
)

if fortran_order:
array.shape = shape[::-1]
array = array.reshape(shape[::-1])
array = array.transpose()
else:
array.shape = shape
array = array.reshape(shape)

return array

Expand Down
5 changes: 4 additions & 1 deletion numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1317,7 +1317,10 @@ def gradient(f, *varargs, axis=None, edge_order=1):
# fix the shape for broadcasting
shape = np.ones(N, dtype=int)
shape[axis] = -1
a.shape = b.shape = c.shape = shape

a = a.reshape(shape)
b = b.reshape(shape)
c = c.reshape(shape)
# 1D equivalent -- out[1:-1] = a * f[:-2] + b * f[1:-1] + c * f[2:]
out[tuple(slice1)] = a * f[tuple(slice2)] + b * f[tuple(slice3)] \
+ c * f[tuple(slice4)]
Expand Down
3 changes: 1 addition & 2 deletions numpy/lib/tests/test_arrayterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ def test():
ndims = randint(5) + 1
shape = tuple(randint(10) + 1 for dim in range(ndims))
els = reduce(mul, shape)
a = np.arange(els)
a.shape = shape
a = np.arange(els).reshape(shape)

buf_size = randint(2 * els)
b = Arrayterator(a, buf_size)
Expand Down
9 changes: 3 additions & 6 deletions numpy/linalg/_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,7 @@ def tensorsolve(a, b, axes=None):
Examples
--------
>>> import numpy as np
>>> a = np.eye(2*3*4)
>>> a.shape = (2*3, 4, 2, 3, 4)
>>> a = np.eye(2*3*4).reshape((2*3, 4, 2, 3, 4))
>>> rng = np.random.default_rng()
>>> b = rng.normal(size=(2*3, 4))
>>> x = np.linalg.tensorsolve(a, b)
Expand Down Expand Up @@ -495,8 +494,7 @@ def tensorinv(a, ind=2):
Examples
--------
>>> import numpy as np
>>> a = np.eye(4*6)
>>> a.shape = (4, 6, 8, 3)
>>> a = np.eye(4*6).reshape((4, 6, 8, 3))
>>> ainv = np.linalg.tensorinv(a, ind=2)
>>> ainv.shape
(8, 3, 4, 6)
Expand All @@ -505,8 +503,7 @@ def tensorinv(a, ind=2):
>>> np.allclose(np.tensordot(ainv, b), np.linalg.tensorsolve(a, b))
True

>>> a = np.eye(4*6)
>>> a.shape = (24, 8, 3)
>>> a = np.eye(4*6).reshape((24, 8, 3))
>>> ainv = np.linalg.tensorinv(a, ind=1)
>>> ainv.shape
(8, 3, 24)
Expand Down
9 changes: 3 additions & 6 deletions numpy/linalg/tests/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -2203,8 +2203,7 @@ def test_non_square_handling(self, arr, ind):
((24, 8, 3), 1),
])
def test_tensorinv_shape(self, shape, ind):
a = np.eye(24)
a.shape = shape
a = np.eye(24).reshape(shape)
ainv = linalg.tensorinv(a=a, ind=ind)
expected = a.shape[ind:] + a.shape[:ind]
actual = ainv.shape
Expand All @@ -2214,15 +2213,13 @@ def test_tensorinv_shape(self, shape, ind):
0, -2,
])
def test_tensorinv_ind_limit(self, ind):
a = np.eye(24)
a.shape = (4, 6, 8, 3)
a = np.eye(24).reshape((4, 6, 8, 3))
with assert_raises(ValueError):
linalg.tensorinv(a=a, ind=ind)

def test_tensorinv_result(self):
# mimic a docstring example
a = np.eye(24)
a.shape = (24, 8, 3)
a = np.eye(24).reshape((24, 8, 3))
ainv = linalg.tensorinv(a, ind=1)
b = np.ones(24)
assert_allclose(np.tensordot(ainv, b, 1), np.linalg.tensorsolve(a, b))
Expand Down
2 changes: 1 addition & 1 deletion numpy/linalg/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_eig_build(self):
1.51971555e-15 + 0.j,
-1.51308713e-15 + 0.j])
a = arange(13 * 13, dtype=float64)
a.shape = (13, 13)
a = a.reshape((13, 13))
a = a % 17
va, ve = linalg.eig(a)
va.sort()
Expand Down
Loading
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy