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

Commit 9fd6c3d

Browse files
gyshireminisce
authored andcommitted
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 refactor moveaxis code
1 parent 8562adc commit 9fd6c3d

File tree

12 files changed

+1271
-44
lines changed

12 files changed

+1271
-44
lines changed

python/mxnet/_numpy_op_doc.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,3 +704,47 @@ def _np_squeeze(a, axis=None, out=None):
704704
(1, 3)
705705
"""
706706
pass
707+
708+
709+
def _np_moveaxis(a, source, destination):
710+
"""Move axes of an array to new positions.
711+
Other axes remain in their original order.
712+
713+
Parameters
714+
----------
715+
a : ndarray
716+
The array whose axes should be reordered.
717+
source : int or sequence of int
718+
Original positions of the axes to move. These must be unique.
719+
destination : int or sequence of int
720+
Destination positions for each of the original axes. These must also be
721+
unique.
722+
723+
Returns
724+
-------
725+
result : ndarray
726+
Array with moved axes. This array is a view of the input array.
727+
728+
See Also
729+
--------
730+
transpose: Permute the dimensions of an array.
731+
swapaxes: Interchange two axes of an array.
732+
733+
Examples
734+
--------
735+
>>> x = np.zeros((3, 4, 5))
736+
>>> np.moveaxis(x, 0, -1).shape
737+
(4, 5, 3)
738+
>>> np.moveaxis(x, -1, 0).shape
739+
(5, 3, 4)
740+
These all achieve the same result:
741+
>>> np.transpose(x).shape
742+
(5, 4, 3)
743+
>>> np.swapaxes(x, 0, -1).shape
744+
(5, 4, 3)
745+
>>> np.moveaxis(x, [0, 1], [-1, -2]).shape
746+
(5, 4, 3)
747+
>>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape
748+
(5, 4, 3)
749+
"""
750+
pass

python/mxnet/ndarray/numpy/_op.py

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
'stack', 'vstack', 'dstack', 'mean', 'maximum', 'minimum', 'swapaxes', 'clip', 'argmax',
3737
'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming', 'blackman', 'flip',
3838
'around', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril', 'identity', 'take',
39-
'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal']
39+
'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less', 'greater_equal', 'less_equal',
40+
'hsplit', 'rot90']
4041

4142

4243
@set_module('mxnet.ndarray.numpy')
@@ -2479,6 +2480,121 @@ def split(ary, indices_or_sections, axis=0):
24792480
# pylint: enable=redefined-outer-name
24802481

24812482

2483+
# pylint: disable=redefined-outer-name
2484+
@set_module('mxnet.ndarray.numpy')
2485+
def hsplit(ary, indices_or_sections):
2486+
"""Split an array into multiple sub-arrays horizontally (column-wise).
2487+
2488+
This is equivalent to ``split`` with ``axis=0`` if ``ary`` has one
2489+
dimension, and otherwise that with ``axis=1``.
2490+
2491+
Parameters
2492+
----------
2493+
ary : ndarray
2494+
Array to be divided into sub-arrays.
2495+
indices_or_sections : int, list of ints or tuple of ints.
2496+
If `indices_or_sections` is an integer, N, the array will be divided
2497+
into N equal arrays along `axis`. If such a split is not possible,
2498+
an error is raised.
2499+
2500+
If `indices_or_sections` is a list of sorted integers, the entries
2501+
indicate where along `axis` the array is split.
2502+
2503+
If an index exceeds the dimension of the array along `axis`,
2504+
it will raises errors. so index must less than or euqal to
2505+
the dimension of the array along axis.
2506+
2507+
Returns
2508+
-------
2509+
sub-arrays : list of ndarrays
2510+
A list of sub-arrays.
2511+
2512+
Notes
2513+
------
2514+
- If `indices_or_sections` is given as an integer, but a split
2515+
does not result in equal division.It will raises ValueErrors.
2516+
2517+
- If indices_or_sections is an integer, and the number is 1, it will
2518+
raises an error. Because single output from split is not supported yet...
2519+
2520+
See Also
2521+
--------
2522+
split : Split an array into multiple sub-arrays of equal size.
2523+
2524+
Examples
2525+
--------
2526+
>>> x = np.arange(16.0).reshape(4, 4)
2527+
>>> x
2528+
array([[ 0., 1., 2., 3.],
2529+
[ 4., 5., 6., 7.],
2530+
[ 8., 9., 10., 11.],
2531+
[12., 13., 14., 15.]])
2532+
>>> np.hsplit(x, 2)
2533+
[array([[ 0., 1.],
2534+
[ 4., 5.],
2535+
[ 8., 9.],
2536+
[12., 13.]]),
2537+
array([[ 2., 3.],
2538+
[ 6., 7.],
2539+
[10., 11.],
2540+
[14., 15.]])]
2541+
>>> np.hsplit(x, [3, 6])
2542+
[array([[ 0., 1., 2.],
2543+
[ 4., 5., 6.],
2544+
[ 8., 9., 10.],
2545+
[12., 13., 14.]]),
2546+
array([[ 3.],
2547+
[ 7.],
2548+
[11.],
2549+
[15.]]),
2550+
array([], shape=(4, 0), dtype=float32)]
2551+
2552+
With a higher dimensional array the split is still along the second axis.
2553+
2554+
>>> x = np.arange(8.0).reshape(2, 2, 2)
2555+
>>> x
2556+
array([[[ 0., 1.],
2557+
[ 2., 3.]],
2558+
[[ 4., 5.],
2559+
[ 6., 7.]]])
2560+
>>> np.hsplit(x, 2)
2561+
[array([[[ 0., 1.]],
2562+
[[ 4., 5.]]]),
2563+
array([[[ 2., 3.]],
2564+
[[ 6., 7.]]])]
2565+
2566+
If ``ary`` has one dimension, 'axis' = 0.
2567+
>>> x = np.arange(4)
2568+
array([0., 1., 2., 3.])
2569+
>>> np.hsplit(x, 2)
2570+
[array([0., 1.]), array([2., 3.])]
2571+
2572+
If you want to produce an empty sub-array, you can see an example.
2573+
>>> np.hsplit(x, [2, 2])
2574+
[array([0., 1.]), array([], dtype=float32), array([2., 3.])]
2575+
"""
2576+
indices = []
2577+
axis = 1
2578+
if (len(ary.shape) == 1):
2579+
axis = 0
2580+
axis_size = ary.shape[axis]
2581+
if isinstance(indices_or_sections, int):
2582+
sections = indices_or_sections
2583+
if axis_size % sections:
2584+
raise ValueError('array hsplit does not result in an equal division')
2585+
section_size = int(axis_size / sections)
2586+
indices = [i * section_size for i in range(sections)]
2587+
elif isinstance(indices_or_sections, (list, set, tuple)):
2588+
indices = [0] + list(indices_or_sections)
2589+
else:
2590+
raise ValueError('indices_or_sections must either int or tuple of ints')
2591+
ret = _npi.hsplit(ary, indices, axis, False)
2592+
if not isinstance(ret, list):
2593+
raise NotImplementedError('single output from hsplit is not supported yet...')
2594+
return ret
2595+
# pylint: enable=redefined-outer-name
2596+
2597+
24822598
@set_module('mxnet.ndarray.numpy')
24832599
def vsplit(ary, indices_or_sections):
24842600
r"""
@@ -4124,3 +4240,49 @@ def less_equal(x1, x2, out=None):
41244240
"""
41254241
return _ufunc_helper(x1, x2, _npi.less_equal, _np.less_equal, _npi.less_equal_scalar,
41264242
_npi.greater_equal_scalar, out)
4243+
4244+
4245+
@set_module('mxnet.ndarray.numpy')
4246+
def rot90(m, k=1, axes=(0, 1)):
4247+
"""
4248+
Rotate an array by 90 degrees in the plane specified by axes.
4249+
Rotation direction is from the first towards the second axis.
4250+
Parameters
4251+
----------
4252+
m : ndarray
4253+
Array of two or more dimensions.
4254+
k : integer
4255+
Number of times the array is rotated by 90 degrees.
4256+
axes: (2,) array_like
4257+
The array is rotated in the plane defined by the axes.
4258+
Axes must be different.
4259+
4260+
Returns
4261+
-------
4262+
y : ndarray
4263+
A rotated view of `m`.
4264+
4265+
-----
4266+
rot90(m, k=1, axes=(1,0)) is the reverse of rot90(m, k=1, axes=(0,1))
4267+
rot90(m, k=1, axes=(1,0)) is equivalent to rot90(m, k=-1, axes=(0,1))
4268+
Examples
4269+
--------
4270+
>>> m = np.array([[1,2],[3,4]], 'int')
4271+
>>> m
4272+
array([[1, 2],
4273+
[3, 4]], dtype=int64)
4274+
>>> np.rot90(m)
4275+
array([[2, 4],
4276+
[1, 3]], dtype=int64)
4277+
>>> np.rot90(m, 2)
4278+
array([[4, 3],
4279+
[2, 1]], dtype=int64)
4280+
>>> m = np.arange(8).reshape((2,2,2))
4281+
>>> np.rot90(m, 1, (1,2))
4282+
array([[[1., 3.],
4283+
[0., 2.]],
4284+
4285+
[[5., 7.],
4286+
[4., 6.]]])
4287+
"""
4288+
return _npi.rot90(m, k=k, axes=axes)

python/mxnet/numpy/multiarray.py

Lines changed: 135 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,7 @@
5555
'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign', 'ravel', 'hanning', 'hamming',
5656
'blackman', 'flip', 'around', 'arctan2', 'hypot', 'rad2deg', 'deg2rad', 'unique', 'lcm', 'tril',
5757
'identity', 'take', 'ldexp', 'vdot', 'inner', 'outer', 'equal', 'not_equal', 'greater', 'less',
58-
'greater_equal', 'less_equal']
59-
58+
'greater_equal', 'less_equal', 'hsplit', 'rot90']
6059

6160
# Return code for dispatching indexing function call
6261
_NDARRAY_UNSUPPORTED_INDEXING = -1
@@ -4114,8 +4113,7 @@ def vsplit(ary, indices_or_sections):
41144113
[4., 5., 6., 7.]]), array([[ 8., 9., 10., 11.],
41154114
[12., 13., 14., 15.]])]
41164115
4117-
With a higher dimensional array the split is still along the first axis.
4118-
4116+
>>> # With a higher dimensional array the split is still along the first axis.
41194117
>>> x = np.arange(8.0).reshape(2, 2, 2)
41204118
>>> x
41214119
array([[[ 0., 1.],
@@ -5651,3 +5649,136 @@ def less_equal(x1, x2, out=None):
56515649
array([True])
56525650
"""
56535651
return _mx_nd_np.less_equal(x1, x2, out)
5652+
5653+
5654+
@set_module('mxnet.numpy')
5655+
def rot90(m, k=1, axes=(0, 1)):
5656+
"""
5657+
Rotate an array by 90 degrees in the plane specified by axes.
5658+
Rotation direction is from the first towards the second axis.
5659+
Parameters
5660+
----------
5661+
m : ndarray
5662+
Array of two or more dimensions.
5663+
k : integer
5664+
Number of times the array is rotated by 90 degrees.
5665+
axes: (2,) array_like
5666+
The array is rotated in the plane defined by the axes.
5667+
Axes must be different.
5668+
5669+
Returns
5670+
-------
5671+
y : ndarray
5672+
A rotated view of `m`.
5673+
5674+
-----
5675+
rot90(m, k=1, axes=(1,0)) is the reverse of rot90(m, k=1, axes=(0,1))
5676+
rot90(m, k=1, axes=(1,0)) is equivalent to rot90(m, k=-1, axes=(0,1))
5677+
Examples
5678+
--------
5679+
>>> m = np.array([[1,2],[3,4]], 'int')
5680+
>>> m
5681+
array([[1, 2],
5682+
[3, 4]], dtype=int64)
5683+
>>> np.rot90(m)
5684+
array([[2, 4],
5685+
[1, 3]], dtype=int64)
5686+
>>> np.rot90(m, 2)
5687+
array([[4, 3],
5688+
[2, 1]], dtype=int64)
5689+
>>> m = np.arange(8).reshape((2,2,2))
5690+
>>> np.rot90(m, 1, (1,2))
5691+
array([[[1., 3.],
5692+
[0., 2.]],
5693+
5694+
[[5., 7.],
5695+
[4., 6.]]])
5696+
"""
5697+
return _mx_nd_np.rot90(m, k=k, axes=axes)
5698+
5699+
5700+
@set_module('mxnet.numpy')
5701+
def hsplit(ary, indices_or_sections):
5702+
"""Split an array into multiple sub-arrays horizontally (column-wise).
5703+
This is equivalent to ``split`` with ``axis=0`` if ``ary`` has one
5704+
dimension, and otherwise that with ``axis=1``.
5705+
5706+
Parameters
5707+
----------
5708+
ary : ndarray
5709+
Array to be divided into sub-arrays.
5710+
indices_or_sections : int, list of ints or tuple of ints.
5711+
If `indices_or_sections` is an integer, N, the array will be divided
5712+
into N equal arrays along `axis`. If such a split is not possible,
5713+
an error is raised.
5714+
If `indices_or_sections` is a list of sorted integers, the entries
5715+
indicate where along `axis` the array is split.
5716+
If an index exceeds the dimension of the array along `axis`,
5717+
it will raises errors. so index must less than or euqal to
5718+
the dimension of the array along axis.
5719+
5720+
Returns
5721+
-------
5722+
sub-arrays : list of ndarrays
5723+
A list of sub-arrays.
5724+
5725+
Notes
5726+
------
5727+
- If `indices_or_sections` is given as an integer, but a split
5728+
does not result in equal division.It will raises ValueErrors.
5729+
- If indices_or_sections is an integer, and the number is 1, it will
5730+
raises an error. Because single output from split is not supported yet...
5731+
5732+
See Also
5733+
--------
5734+
split : Split an array into multiple sub-arrays of equal size.
5735+
5736+
Examples
5737+
--------
5738+
>>> x = np.arange(16.0).reshape(4, 4)
5739+
>>> x
5740+
array([[ 0., 1., 2., 3.],
5741+
[ 4., 5., 6., 7.],
5742+
[ 8., 9., 10., 11.],
5743+
[12., 13., 14., 15.]])
5744+
>>> np.hsplit(x, 2)
5745+
[array([[ 0., 1.],
5746+
[ 4., 5.],
5747+
[ 8., 9.],
5748+
[12., 13.]]),
5749+
array([[ 2., 3.],
5750+
[ 6., 7.],
5751+
[10., 11.],
5752+
[14., 15.]])]
5753+
>>> np.hsplit(x, [3, 6])
5754+
[array([[ 0., 1., 2.],
5755+
[ 4., 5., 6.],
5756+
[ 8., 9., 10.],
5757+
[12., 13., 14.]]),
5758+
array([[ 3.],
5759+
[ 7.],
5760+
[11.],
5761+
[15.]]),
5762+
array([], shape=(4, 0), dtype=float32)]
5763+
With a higher dimensional array the split is still along the second axis.
5764+
>>> x = np.arange(8.0).reshape(2, 2, 2)
5765+
>>> x
5766+
array([[[ 0., 1.],
5767+
[ 2., 3.]],
5768+
[[ 4., 5.],
5769+
[ 6., 7.]]])
5770+
>>> np.hsplit(x, 2)
5771+
[array([[[ 0., 1.]],
5772+
[[ 4., 5.]]]),
5773+
array([[[ 2., 3.]],
5774+
[[ 6., 7.]]])]
5775+
If ``ary`` has one dimension, 'axis' = 0.
5776+
>>> x = np.arange(4)
5777+
array([0., 1., 2., 3.])
5778+
>>> np.hsplit(x, 2)
5779+
[array([0., 1.]), array([2., 3.])]
5780+
If you want to produce an empty sub-array, you can see an example.
5781+
>>> np.hsplit(x, [2, 2])
5782+
[array([0., 1.]), array([], dtype=float32), array([2., 3.])]
5783+
"""
5784+
return _mx_nd_np.hsplit(ary, indices_or_sections)

0 commit comments

Comments
 (0)