Skip to content

Commit

Permalink
Merge daa99a0 into 1cb7f60
Browse files Browse the repository at this point in the history
  • Loading branch information
unnonouno committed Mar 28, 2018
2 parents 1cb7f60 + daa99a0 commit 4b45106
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cupy/core/core.pyx
Expand Up @@ -2108,6 +2108,8 @@ cpdef vector.vector[Py_ssize_t] normalize_axis_tuple(axis, Py_ssize_t ndim) \
except *:
"""Normalizes an axis argument into a tuple of non-negative integer axes.
Arguments `allow_duplicate` and `axis_name` are not supported.
"""
if numpy.isscalar(axis):
axis = (axis,)
Expand All @@ -2117,7 +2119,10 @@ cpdef vector.vector[Py_ssize_t] normalize_axis_tuple(axis, Py_ssize_t ndim) \
if ax >= ndim or ax < -ndim:
raise _AxisError('axis {} is out of bounds for array of '
'dimension {}'.format(ax, ndim))
if _has_element(ret, ax):
raise _AxisError('repeated axis')
ret.push_back(ax % ndim)

return ret


Expand All @@ -2137,9 +2142,9 @@ cpdef ndarray moveaxis(ndarray a, source, destination):
if not _has_element(src, n):
order.push_back(n)

for i in range(len(src)):
n = <Py_ssize_t>i
order.insert(order.begin() + dest[n], src[n])
cdef Py_ssize_t d, s
for d, s in sorted(zip(dest, src)):
order.insert(order.begin() + d, s)

return a.transpose(order)

Expand Down
40 changes: 40 additions & 0 deletions tests/cupy_tests/manipulation_tests/test_transpose.py
Expand Up @@ -21,6 +21,30 @@ def test_moveaxis2(self, xp):
a = testing.shaped_arange((2, 3, 4), xp)
return xp.moveaxis(a, 1, -1)

@testing.numpy_cupy_array_equal()
@testing.with_requires('numpy>=1.11')
def test_moveaxis3(self, xp):
a = testing.shaped_arange((2, 3, 4), xp)
return xp.moveaxis(a, [0, 2], [1, 0])

@testing.numpy_cupy_array_equal()
@testing.with_requires('numpy>=1.11')
def test_moveaxis4(self, xp):
a = testing.shaped_arange((2, 3, 4), xp)
return xp.moveaxis(a, [2, 0], [1, 0])

@testing.numpy_cupy_array_equal()
@testing.with_requires('numpy>=1.11')
def test_moveaxis5(self, xp):
a = testing.shaped_arange((2, 3, 4), xp)
return xp.moveaxis(a, [2, 0], [0, 1])

@testing.numpy_cupy_array_equal()
@testing.with_requires('numpy>=1.11')
def test_moveaxis6(self, xp):
a = testing.shaped_arange((2, 3, 4, 5, 6), xp)
return xp.moveaxis(a, [0, 2, 1], [3, 4, 0])

# dim is too large
@testing.numpy_cupy_raises()
@testing.with_requires('numpy>=1.13')
Expand Down Expand Up @@ -59,6 +83,22 @@ def test_moveaxis_invalid4(self, xp):
a = testing.shaped_arange((2, 3, 4), xp)
return xp.moveaxis(a, [0, 1], [1, 2, 0])

# Use the same axis twice
def test_moveaxis_invalid5_1(self):
a = testing.shaped_arange((2, 3, 4), cupy)
with self.assertRaises(cupy.core.core._AxisError):
return cupy.moveaxis(a, [1, -1], [1, 3])

def test_moveaxis_invalid5_2(self):
a = testing.shaped_arange((2, 3, 4), cupy)
with self.assertRaises(cupy.core.core._AxisError):
return cupy.moveaxis(a, [0, 1], [-1, 2])

def test_moveaxis_invalid5_3(self):
a = testing.shaped_arange((2, 3, 4), cupy)
with self.assertRaises(cupy.core.core._AxisError):
return cupy.moveaxis(a, [0, 1], [1, 1])

@testing.numpy_cupy_array_equal()
def test_rollaxis(self, xp):
a = testing.shaped_arange((2, 3, 4), xp)
Expand Down

0 comments on commit 4b45106

Please sign in to comment.