Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Commit 1c57454

Browse files
committed
add numpy op bitwise_xor, hsplit, moveaxis, rot90
add numpy op bitwise_xor fix some format problems add numpy op moveaxis fix code style add numpy op rot90 fix pylint error updata rot90 address comments add numpy op hsplit refactor split compute function
1 parent 72b4d9b commit 1c57454

File tree

12 files changed

+1329
-49
lines changed

12 files changed

+1329
-49
lines changed

python/mxnet/_numpy_op_doc.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,50 @@ def _np_trace(a, offset=0, axis1=0, axis2=1, out=None):
677677
(2, 3)
678678
"""
679679
pass
680+
681+
682+
def moveaxis(a, source, destination):
683+
"""Move axes of an array to new positions.
684+
685+
Other axes remain in their original order.
686+
687+
Parameters
688+
----------
689+
a : ndarray
690+
The array whose axes should be reordered.
691+
source : int or sequence of int
692+
Original positions of the axes to move. These must be unique.
693+
destination : int or sequence of int
694+
Destination positions for each of the original axes. These must also be
695+
unique.
696+
697+
Returns
698+
-------
699+
result : ndarray
700+
Array with moved axes. This array is a view of the input array.
701+
702+
See Also
703+
--------
704+
transpose: Permute the dimensions of an array.
705+
swapaxes: Interchange two axes of an array.
706+
Examples
707+
--------
708+
709+
>>> x = np.zeros((3, 4, 5))
710+
>>> np.moveaxis(x, 0, -1).shape
711+
(4, 5, 3)
712+
>>> np.moveaxis(x, -1, 0).shape
713+
(5, 3, 4)
714+
715+
These all achieve the same result:
716+
717+
>>> np.transpose(x).shape
718+
(5, 4, 3)
719+
>>> np.swapaxes(x, 0, -1).shape
720+
(5, 4, 3)
721+
>>> np.moveaxis(x, [0, 1], [-1, -2]).shape
722+
(5, 4, 3)
723+
>>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape
724+
(5, 4, 3)
725+
"""
726+
pass

python/mxnet/ndarray/numpy/_op.py

Lines changed: 165 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
'arctan2', 'sin', 'cos', 'tan', 'sinh', 'cosh', 'tanh', 'log10', 'sqrt', 'cbrt', 'abs',
3232
'absolute', 'exp', 'expm1', 'arcsin', 'arccos', 'arctan', 'sign', 'log', 'degrees', 'log2',
3333
'log1p', 'rint', 'radians', 'reciprocal', 'square', 'negative', 'fix', 'ceil', 'floor',
34-
'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot',
35-
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'vstack', 'mean',
36-
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign',
37-
'ravel', 'hanning', 'hamming', 'blackman', 'flip', 'around', 'hypot', 'rad2deg', 'deg2rad']
34+
'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot', 'linspace', 'expand_dims',
35+
'tile', 'arange', 'split', 'hsplit', 'concatenate', 'stack', 'vstack', 'mean', 'maximum',
36+
'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign', 'ravel',
37+
'hanning', 'hamming', 'blackman', 'flip', 'around', 'hypot', 'rad2deg', 'deg2rad', 'rot90']
3838

3939

4040
@set_module('mxnet.ndarray.numpy')
@@ -2012,6 +2012,121 @@ def split(ary, indices_or_sections, axis=0):
20122012
# pylint: enable=redefined-outer-name
20132013

20142014

2015+
# pylint: disable=redefined-outer-name
2016+
@set_module('mxnet.ndarray.numpy')
2017+
def hsplit(ary, indices_or_sections):
2018+
"""Split an array into multiple sub-arrays horizontally (column-wise).
2019+
2020+
This is equivalent to ``split`` with ``axis=0`` if ``ary`` has one
2021+
dimension, and otherwise that with ``axis=1``.
2022+
2023+
Parameters
2024+
----------
2025+
ary : ndarray
2026+
Array to be divided into sub-arrays.
2027+
indices_or_sections : int, list of ints or tuple of ints.
2028+
If `indices_or_sections` is an integer, N, the array will be divided
2029+
into N equal arrays along `axis`. If such a split is not possible,
2030+
an error is raised.
2031+
2032+
If `indices_or_sections` is a list of sorted integers, the entries
2033+
indicate where along `axis` the array is split.
2034+
2035+
If an index exceeds the dimension of the array along `axis`,
2036+
it will raises errors. so index must less than or euqal to
2037+
the dimension of the array along axis.
2038+
2039+
Returns
2040+
-------
2041+
sub-arrays : list of ndarrays
2042+
A list of sub-arrays.
2043+
2044+
Notes
2045+
------
2046+
- If `indices_or_sections` is given as an integer, but a split
2047+
does not result in equal division.It will raises ValueErrors.
2048+
2049+
- If indices_or_sections is an integer, and the number is 1, it will
2050+
raises an error. Because single output from split is not supported yet...
2051+
2052+
See Also
2053+
--------
2054+
split : Split an array into multiple sub-arrays of equal size.
2055+
2056+
Examples
2057+
--------
2058+
>>> x = np.arange(16.0).reshape(4, 4)
2059+
>>> x
2060+
array([[ 0., 1., 2., 3.],
2061+
[ 4., 5., 6., 7.],
2062+
[ 8., 9., 10., 11.],
2063+
[12., 13., 14., 15.]])
2064+
>>> np.hsplit(x, 2)
2065+
[array([[ 0., 1.],
2066+
[ 4., 5.],
2067+
[ 8., 9.],
2068+
[12., 13.]]),
2069+
array([[ 2., 3.],
2070+
[ 6., 7.],
2071+
[10., 11.],
2072+
[14., 15.]])]
2073+
>>> np.hsplit(x, [3, 6])
2074+
[array([[ 0., 1., 2.],
2075+
[ 4., 5., 6.],
2076+
[ 8., 9., 10.],
2077+
[12., 13., 14.]]),
2078+
array([[ 3.],
2079+
[ 7.],
2080+
[11.],
2081+
[15.]]),
2082+
array([], shape=(4, 0), dtype=float32)]
2083+
2084+
With a higher dimensional array the split is still along the second axis.
2085+
2086+
>>> x = np.arange(8.0).reshape(2, 2, 2)
2087+
>>> x
2088+
array([[[ 0., 1.],
2089+
[ 2., 3.]],
2090+
[[ 4., 5.],
2091+
[ 6., 7.]]])
2092+
>>> np.hsplit(x, 2)
2093+
[array([[[ 0., 1.]],
2094+
[[ 4., 5.]]]),
2095+
array([[[ 2., 3.]],
2096+
[[ 6., 7.]]])]
2097+
2098+
If ``ary`` has one dimension, 'axis' = 0.
2099+
>>> x = np.arange(4)
2100+
array([0., 1., 2., 3.])
2101+
>>> np.hsplit(x, 2)
2102+
[array([0., 1.]), array([2., 3.])]
2103+
2104+
If you want to produce an empty sub-array, you can see an example.
2105+
>>> np.hsplit(x, [2, 2])
2106+
[array([0., 1.]), array([], dtype=float32), array([2., 3.])]
2107+
"""
2108+
indices = []
2109+
axis = 1
2110+
if (len(ary.shape) == 1):
2111+
axis = 0
2112+
axis_size = ary.shape[axis]
2113+
if isinstance(indices_or_sections, int):
2114+
sections = indices_or_sections
2115+
if axis_size % sections:
2116+
raise ValueError('array hsplit does not result in an equal division')
2117+
section_size = int(axis_size / sections)
2118+
indices = [i * section_size for i in range(sections)]
2119+
elif isinstance(indices_or_sections, (list, set, tuple)):
2120+
indices = [0] + list(indices_or_sections)
2121+
else:
2122+
raise ValueError('indices_or_sections must either int or tuple of ints')
2123+
ret = _npi.hsplit(ary, indices, axis, False)
2124+
if not isinstance(ret, list):
2125+
raise NotImplementedError('single output from hsplit is not supported yet...')
2126+
return ret
2127+
# pylint: enable=redefined-outer-name
2128+
2129+
20152130
@set_module('mxnet.ndarray.numpy')
20162131
def concatenate(seq, axis=0, out=None):
20172132
"""Join a sequence of arrays along an existing axis.
@@ -3158,3 +3273,49 @@ def hypot(x1, x2, out=None):
31583273
[ 5., 5., 5.]])
31593274
"""
31603275
return _ufunc_helper(x1, x2, _npi.hypot, _np.hypot, _npi.hypot_scalar, None, out)
3276+
3277+
3278+
@set_module('mxnet.ndarray.numpy')
3279+
def rot90(m, k=1, axes=(0, 1)):
3280+
"""
3281+
Rotate an array by 90 degrees in the plane specified by axes.
3282+
Rotation direction is from the first towards the second axis.
3283+
Parameters
3284+
----------
3285+
m : ndarray
3286+
Array of two or more dimensions.
3287+
k : integer
3288+
Number of times the array is rotated by 90 degrees.
3289+
axes: (2,) array_like
3290+
The array is rotated in the plane defined by the axes.
3291+
Axes must be different.
3292+
3293+
Returns
3294+
-------
3295+
y : ndarray
3296+
A rotated view of `m`.
3297+
3298+
-----
3299+
rot90(m, k=1, axes=(1,0)) is the reverse of rot90(m, k=1, axes=(0,1))
3300+
rot90(m, k=1, axes=(1,0)) is equivalent to rot90(m, k=-1, axes=(0,1))
3301+
Examples
3302+
--------
3303+
>>> m = np.array([[1,2],[3,4]], 'int')
3304+
>>> m
3305+
array([[1, 2],
3306+
[3, 4]], dtype=int64)
3307+
>>> np.rot90(m)
3308+
array([[2, 4],
3309+
[1, 3]], dtype=int64)
3310+
>>> np.rot90(m, 2)
3311+
array([[4, 3],
3312+
[2, 1]], dtype=int64)
3313+
>>> m = np.arange(8).reshape((2,2,2))
3314+
>>> np.rot90(m, 1, (1,2))
3315+
array([[[1., 3.],
3316+
[0., 2.]],
3317+
3318+
[[5., 7.],
3319+
[4., 6.]]])
3320+
"""
3321+
return _npi.rot90(m, k=k, axes=axes)

0 commit comments

Comments
 (0)