8000 MAINT: Use moveaxis instead of rollaxis internally (#9475) · numpy/numpy@029863e · GitHub
[go: up one dir, main page]

Skip to content

Commit 029863e

Browse files
nschloeeric-wieser
authored andcommitted
MAINT: Use moveaxis instead of rollaxis internally (#9475)
Also add a hint to the documentation advising the use of moveaxis over rollaxis. Tests for rollaxis are left alone.
1 parent 4c18530 commit 029863e

File tree

13 files changed

+53
-52
lines changed

13 files changed

+53
-52
lines changed

numpy/core/numeric.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,10 @@ def rollaxis(a, axis, start=0):
14351435
"""
14361436
Roll the specified axis backwards, until it lies in a given position.
14371437
1438+
This function continues to be supported for backward compatibility, but you
1439+
should prefer `moveaxis`. The `moveaxis` function was added in NumPy
1440+
1.11.
1441+
14381442
Parameters
14391443
----------
14401444
a : ndarray
@@ -1617,7 +1621,7 @@ def moveaxis(a, source, destination):
16171621

16181622
# fix hack in scipy which imports this function
16191623
def _move_axis_to_0(a, axis):
1620-
return rollaxis(a, axis, 0)
1624+
return moveaxis(a, axis, 0)
16211625

16221626

16231627
def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
@@ -1742,8 +1746,8 @@ def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
17421746
axisb = normalize_axis_index(axisb, b.ndim, msg_prefix='axisb')
17431747

17441748
# Move working axis to the end of the shape
1745-
a = rollaxis(a, axisa, a.ndim)
1746-
b = rollaxis(b, axisb, b.ndim)
1749+
a = moveaxis(a, axisa, -1)
1750+
b = moveaxis(b, axisb, -1)
17471751
msg = ("incompatible dimensions for cross product\n"
17481752
"(dimension must be 2 or 3)")
17491753
if a.shape[-1] not in (2, 3) or b.shape[-1] not in (2, 3):
@@ -1814,8 +1818,7 @@ def cross(a, b, axisa=-1, axisb=-1, axisc=-1, axis=None):
18141818
multiply(a0, b1, out=cp2)
18151819
cp2 -= a1 * b0
18161820

1817-
# This works because we are moving the last axis
1818-
return rollaxis(cp, -1, axisc)
1821+
return moveaxis(cp, -1, axisc)
18191822

18201823

18211824
# Use numarray's printing function

numpy/core/tests/test_shape_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def test_exceptions(self):
208208
np.concatenate((a, b), axis=axis[0]) # OK
209209
assert_raises(ValueError, np.concatenate, (a, b), axis=axis[1])
210210
assert_raises(ValueError, np.concatenate, (a, b), axis=axis[2])
211-
a = np.rollaxis(a, -1)
212-
b = np.rollaxis(b, -1)
211+
a = np.moveaxis(a, -1, 0)
212+
b = np.moveaxis(b, -1, 0)
213213
axis.append(axis.pop(0))
214214

215215
# No arrays to concatenate raises ValueError

numpy/lib/function_base.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4336,7 +4336,7 @@ def _percentile(a, q, axis=None, out=None,
43364336

43374337
ap.partition(indices, axis=axis)
43384338
# ensure axis with qth is first
4339-
ap = np.rollaxis(ap, axis, 0)
4339+
ap = np.moveaxis(ap, axis, 0)
43404340
axis = 0
43414341

43424342
# Check if the array contains any nan's
@@ -4369,9 +4369,9 @@ def _percentile(a, q, axis=None, out=None,
43694369
ap.partition(concatenate((indices_below, indices_above)), axis=axis)
43704370

43714371
# ensure axis with qth is first
4372-
ap = np.rollaxis(ap, axis, 0)
4373-
weights_below = np.rollaxis(weights_below, axis, 0)
4374-
weights_above = np.rollaxis(weights_above, axis, 0)
4372+
ap = np.moveaxis(ap, axis, 0)
4373+
weights_below = np.moveaxis(weights_below, axis, 0)
4374+
weights_above = np.moveaxis(weights_above, axis, 0)
43754375
axis = 0
43764376

43774377
# Check if the array contains any nan's
@@ -4383,8 +4383,8 @@ def _percentile(a, q, axis=None, out=None,
43834383
x2 = take(ap, indices_above, axis=axis) * weights_above
43844384

43854385
# ensure axis with qth is first
4386-
x1 = np.rollaxis(x1, axis, 0)
4387-
x2 = np.rollaxis(x2, axis, 0)
4386+
x1 = np.moveaxis(x1, axis, 0)
4387+
x2 = np.moveaxis(x2, axis, 0)
43884388

43894389
if zerod:
43904390
x1 = x1.squeeze(0)
@@ -5040,7 +5040,7 @@ def insert(arr, obj, values, axis=None):
50405040
# broadcasting is very different here, since a[:,0,:] = ... behaves
50415041
# very different from a[:,[0],:] = ...! This changes values so that
50425042
# it works likes the second case. (here a[:,0:1,:])
5043-
values = np.rollaxis(values, 0, (axis % values.ndim) + 1)
5043+
values = np.moveaxis(values, 0, axis)
50445044
numnew = values.shape[axis]
50455045
newshape[axis] += numnew
50465046
new = empty(newshape, arr.dtype, arrorder)

numpy/lib/nanfunctions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1174,7 +1174,7 @@ def _nanpercentile(a, q, axis=None, out=None, overwrite_input=False,
11741174
# Move that axis to the beginning to match percentile's
11751175
# convention.
11761176
if q.ndim != 0:
1177-
result = np.rollaxis(result, axis)
1177+
result = np.moveaxis(result, axis, 0)
11781178

11791179
if out is not None:
11801180
out[...] = result

numpy/lib/tests/test_function_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3011,7 +3011,7 @@ def test_extended_axis(self):
30113011
o = np.random.normal(size=(71, 23))
30123012
x = np.dstack([o] * 10)
30133013
assert_equal(np.percentile(x, 30, axis=(0, 1)), np.percentile(o, 30))
3014-
x = np.rollaxis(x, -1, 0)
3014+
x = np.moveaxis(x, -1, 0)
30153015
assert_equal(np.percentile(x, 30, axis=(-2, -1)), np.percentile(o, 30))
30163016
x = x.swapaxes(0, 1).copy()
30173017
assert_equal(np.percentile(x, 30, axis=(0, -1)), np.percentile(o, 30))
@@ -3392,7 +3392,7 @@ def test_extended_axis(self):
33923392
o = np.random.normal(size=(71, 23))
33933393
x = np.dstack([o] * 10)
33943394
assert_equal(np.median(x, axis=(0, 1)), np.median(o))
3395-
x = np.rollaxis(x, -1, 0)
3395+
x = np.moveaxis(x, -1, 0)
33963396
assert_equal(np.median(x, axis=(-2, -1)), np.median(o))
33973397
x = x.swapaxes(0, 1).copy()
33983398
assert_equal(np.median(x, axis=(0, -1)), np.median(o))

numpy/lib/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,7 @@ def _median_nancheck(data, result, axis, out):
11381138
"""
11391139
if data.size == 0:
11401140
return result
1141-
data = np.rollaxis(data, axis, data.ndim)
1141+
data = np.moveaxis(data, axis, -1)
11421142
n = np.isnan(data[..., -1])
11431143
# masked NaN values are ok
11441144
if np.ma.isMaskedArray(n):

numpy/linalg/linalg.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
array, asarray, zeros, empty, empty_like, transpose, intc, single, double,
2323
csingle, cdouble, inexact, complexfloating, newaxis, ravel, all, Inf, dot,
2424
add, multiply, sqrt, maximum, fastCopyAndTranspose, sum, isfinite, size,
25-
finfo, errstate, geterrobj, longdouble, rollaxis, amin, amax, product, abs,
25+
finfo, errstate, geterrobj, longdouble, moveaxis, amin, amax, product, abs,
2626
broadcast, atleast_2d, intp, asanyarray, isscalar, object_, ones
2727
)
2828
from numpy.core.multiarray import normalize_axis_index
@@ -2004,9 +2004,7 @@ def _multi_svd_norm(x, row_axis, col_axis, op):
20042004
is `numpy.amin` or `numpy.amax` or `numpy.sum`.
20052005
20062006
"""
2007-
if row_axis > col_axis:
2008-
row_axis -= 1
2009-
y = rollaxis(rollaxis(x, col_axis, x.ndim), row_axis, -1)
2007+
y = moveaxis(x, (row_axis, col_axis), (-2, -1))
20102008
result = op(svd(y, compute_uv=0), axis=-1)
20112009
return result
20122010

numpy/polynomial/chebyshev.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -944,7 +944,7 @@ def chebder(c, m=1, scl=1, axis=0):
944944
if cnt == 0:
945945
return c
946946

947-
c = np.rollaxis(c, iaxis)
947+
c = np.moveaxis(c, iaxis, 0)
948948
n = len(c)
949949
if cnt >= n:
950950
c = c[:1]*0
@@ -960,7 +960,7 @@ def chebder(c, m=1, scl=1, axis=0):
960960
der[1] = 4*c[2]
961961
der[0] = c[1]
962962
c = der
963-
c = np.rollaxis(c, 0, iaxis + 1)
963+
c = np.moveaxis(c, 0, iaxis)
964964
return c
965965

966966

@@ -1069,7 +1069,7 @@ def chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
10691069
if cnt == 0:
10701070
return c
10711071

1072-
c = np.rollaxis(c, iaxis)
1072+
c = np.moveaxis(c, iaxis, 0)
10731073
k = list(k) + [0]*(cnt - len(k))
10741074
for i in range(cnt):
10751075
n = len(c)
@@ -1088,7 +1088,7 @@ def chebint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
10881088
tmp[j - 1] -= c[j]/(2*(j - 1))
10891089
tmp[0] += k[i] - chebval(lbnd, tmp)
10901090
c = tmp
1091-
c = np.rollaxis(c, 0, iaxis + 1)
1091+
c = np.moveaxis(c, 0, iaxis)
10921092
return c
10931093

10941094

@@ -1460,7 +1460,7 @@ def chebvander(x, deg):
14601460
v[1] = x
14611461
for i in range(2, ideg + 1):
14621462
v[i] = v[i-1]*x2 - v[i-2]
1463-
return np.rollaxis(v, 0, v.ndim)
1463+
return np.moveaxis(v, 0, -1)
14641464

14651465

14661466
def chebvander2d(x, y, deg):

numpy/polynomial/hermite.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ def hermder(c, m=1, scl=1, axis=0):
706706
if cnt == 0:
707707
return c
708708

709-
c = np.rollaxis(c, iaxis)
709+
c = np.moveaxis(c, iaxis, 0)
710710
n = len(c)
711711
if cnt >= n:
712712
c = c[:1]*0
@@ -718,7 +718,7 @@ def hermder(c, m=1, scl=1, axis=0):
718718
for j in range(n, 0, -1):
719719
der[j - 1] = (2*j)*c[j]
720720
c = der
721-
c = np.rollaxis(c, 0, iaxis + 1)
721+
c = np.moveaxis(c, 0, iaxis)
722722
return c
723723

724724

@@ -825,7 +825,7 @@ def hermint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
825825
if cnt == 0:
826826
return c
827827

828-
c = np.rollaxis(c, iaxis)
828+
c = np.moveaxis(c, iaxis, 0)
829829
k = list(k) + [0]*(cnt - len(k))
830830
for i in range(cnt):
831831
n = len(c)
@@ -840,7 +840,7 @@ def hermint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
840840
tmp[j + 1] = c[j]/(2*(j + 1))
841841
tmp[0] += k[i] - hermval(lbnd, tmp)
842842
c = tmp
843-
c = np.rollaxis(c, 0, iaxis + 1)
843+
c = np.moveaxis(c, 0, iaxis)
844844
return c
845845

846846

@@ -1229,7 +1229,7 @@ def hermvander(x, deg):
12291229
v[1] = x2
12301230
for i in range(2, ideg + 1):
12311231
v[i] = (v[i-1]*x2 - v[i-2]*(2*(i - 1)))
1232-
return np.rollaxis(v, 0, v.ndim)
1232+
return np.moveaxis(v, 0, -1)
12331233

12341234

12351235
def hermvander2d(x, y, deg):

numpy/polynomial/hermite_e.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ def hermeder(c, m=1, scl=1, axis=0):
705705
if cnt == 0:
706706
return c
707707

708-
c = np.rollaxis(c, iaxis)
708+
c = np.moveaxis(c, iaxis, 0)
709709
n = len(c)
710710
if cnt >= n:
711711
return c[:1]*0
@@ -717,7 +717,7 @@ def hermeder(c, m=1, scl=1, axis=0):
717717
for j in range(n, 0, -1):
718718
der[j - 1] = j*c[j]
719719
c = der
720-
c = np.rollaxis(c, 0, iaxis + 1)
720+
c = np.moveaxis(c, 0, iaxis)
721721
return c
722722

723723

@@ -824,7 +824,7 @@ def hermeint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
824824
if cnt == 0:
825825
return c
826826

827-
c = np.rollaxis(c, iaxis)
827+
c = np.moveaxis(c, iaxis, 0)
828828
k = list(k) + [0]*(cnt - len(k))
829829
for i in range(cnt):
830830
n = len(c)
@@ -839,7 +839,7 @@ def hermeint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
839839
tmp[j + 1] = c[j]/(j + 1)
840840
tmp[0] += k[i] - hermeval(lbnd, tmp)
841841
c = tmp
842-
c = np.rollaxis(c, 0, iaxis + 1)
842+
c = np.moveaxis(c, 0, iaxis)
843843
return c
844844

845845

@@ -1226,7 +1226,7 @@ def hermevander(x, deg):
12261226
v[1] = x
12271227
for i in range(2, ideg + 1):
12281228
v[i] = (v[i-1]*x - v[i-2]*(i - 1))
1229-
return np.rollaxis(v, 0, v.ndim)
1229+
return np.moveaxis(v, 0, -1)
12301230

12311231

12321232
def hermevander2d(x, y, deg):

numpy/polynomial/laguerre.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def lagder(c, m=1, scl=1, axis=0):
703703
if cnt == 0:
704704
return c
705705

706-
c = np.rollaxis(c, iaxis)
706+
c = np.moveaxis(c, iaxis, 0)
707707
n = len(c)
708708
if cnt >= n:
709709
c = c[:1]*0
@@ -717,7 +717,7 @@ def lagder(c, m=1, scl=1, axis=0):
717717
c[j - 1] += c[j]
718718
der[0] = -c[1]
719719
c = der
720-
c = np.rollaxis(c, 0, iaxis + 1)
720+
c = np.moveaxis(c, 0, iaxis)
721721
return c
722722

723723

@@ -825,7 +825,7 @@ def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
825825
if cnt == 0:
826826
return c
827827

828-
c = np.rollaxis(c, iaxis)
828+
c = np.moveaxis(c, iaxis, 0)
829829
k = list(k) + [0]*(cnt - len(k))
830830
for i in range(cnt):
831831
n = len(c)
@@ -841,7 +841,7 @@ def lagint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
841841
tmp[j + 1] = -c[j]
842842
tmp[0] += k[i] - lagval(lbnd, tmp)
843843
c = tmp
844-
c = np.rollaxis(c, 0, iaxis + 1)
844+
c = np.moveaxis(c, 0, iaxis)
845845
return c
846846

847847

@@ -1228,7 +1228,7 @@ def lagvander(x, deg):
12281228
v[1] = 1 - x
12291229
for i in range(2, ideg + 1):
12301230
v[i] = (v[i-1]*(2*i - 1 - x) - v[i-2]*(i - 1))/i
1231-
return np.rollaxis(v, 0, v.ndim)
1231+
return np.moveaxis(v, 0, -1)
12321232

12331233

12341234
def lagvander2d(x, y, deg):

numpy/polynomial/legendre.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -742,7 +742,7 @@ def legder(c, m=1, scl=1, axis=0):
742742
if cnt == 0:
743743
return c
744744

745-
c = np.rollaxis(c, iaxis)
745+
c = np.moveaxis(c, iaxis, 0)
746746
n = len(c)
747747
if cnt >= n:
748748
c = c[:1]*0
@@ -758,7 +758,7 @@ def legder(c, m=1, scl=1, axis=0):
758758
der[1] = 3*c[2]
759759
der[0] = c[1]
760760
c = der
761-
c = np.rollaxis(c, 0, iaxis + 1)
761+
c = np.moveaxis(c, 0, iaxis)
762762
return c
763763

764764

@@ -867,7 +867,7 @@ def legint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
867867
if cnt == 0:
868868
return c
869869

870-
c = np.rollaxis(c, iaxis)
870+
c = np.moveaxis(c, iaxis, 0)
871871
k = list(k) + [0]*(cnt - len(k))
872872
for i in range(cnt):
873873
n = len(c)
@@ -886,7 +886,7 @@ def legint(c, m=1, k=[], lbnd=0, scl=1, axis=0):
886886
tmp[j - 1] -= t
887887
tmp[0] += k[i] - legval(lbnd, tmp)
888888
c = tmp
889-
c = np.rollaxis(c, 0, iaxis + 1)
889+
c = np.moveaxis(c, 0, iaxis)
890890
return c
891891

892892

@@ -1259,7 +1259,7 @@ def legvander(x, deg):
12591259
v[1] = x
12601260
for i in range(2, ideg + 1):
12611261
v[i] = (v[i-1]*x*(2*i - 1) - v[i-2]*(i - 1))/i
1262-
return np.rollaxis(v, 0, v.ndim)
1262+
return np.moveaxis(v, 0, -1)
12631263

12641264

12651265
def legvander2d(x, y, deg):

0 commit comments

Comments
 (0)
0