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

Commit d36f54c

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 refactor moveaxis code
1 parent 916fbf2 commit d36f54c

File tree

12 files changed

+1287
-44
lines changed

12 files changed

+1287
-44
lines changed

python/mxnet/_numpy_op_doc.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,3 +653,50 @@ def _np_trace(a, offset=0, axis1=0, axis2=1, out=None):
653653
(2, 3)
654654
"""
655655
pass
656+
657+
658+
def moveaxis(a, source, destination):
659+
"""Move axes of an array to new positions.
660+
661+
Other axes remain in their original order.
662+
663+
Parameters
664+
----------
665+
a : ndarray
666+
The array whose axes should be reordered.
667+
source : int or sequence of int
668+
Original positions of the axes to move. These must be unique.
669+
destination : int or sequence of int
670+
Destination positions for each of the original axes. These must also be
671+
unique.
672+
673+
Returns
674+
-------
675+
result : ndarray
676+
Array with moved axes. This array is a view of the input array.
677+
678+
See Also
679+
--------
680+
transpose: Permute the dimensions of an array.
681+
swapaxes: Interchange two axes of an array.
682+
683+
Examples
684+
--------
685+
>>> x = np.zeros((3, 4, 5))
686+
>>> np.moveaxis(x, 0, -1).shape
687+
(4, 5, 3)
688+
>>> np.moveaxis(x, -1, 0).shape
689+
(5, 3, 4)
690+
691+
These all achieve the same result:
692+
693+
>>> np.transpose(x).shape
694+
(5, 4, 3)
695+
>>> np.swapaxes(x, 0, -1).shape
696+
(5, 4, 3)
697+
>>> np.moveaxis(x, [0, 1], [-1, -2]).shape
698+
(5, 4, 3)
699+
>>> np.moveaxis(x, [0, 1, 2], [-1, -2, -3]).shape
700+
(5, 4, 3)
701+
"""
702+
pass

python/mxnet/ndarray/numpy/_op.py

Lines changed: 163 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
'absolute', 'exp', 'expm1', 'arcsin', 'arccos', 'arctan', 'sign', 'log', 'degrees', 'log2',
3333
'log1p', 'rint', 'radians', 'reciprocal', 'square', 'negative', 'fix', 'ceil', 'floor',
3434
'trunc', 'logical_not', 'arcsinh', 'arccosh', 'arctanh', 'tensordot',
35-
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'concatenate', 'stack', 'vstack', 'mean',
35+
'linspace', 'expand_dims', 'tile', 'arange', 'split', 'hsplit', 'concatenate', 'stack', 'vstack', 'mean',
3636
'maximum', 'minimum', 'swapaxes', 'clip', 'argmax', 'std', 'var', 'indices', 'copysign',
3737
'ravel', 'hanning', 'hamming', 'blackman', 'flip', 'around', 'hypot', 'rad2deg', 'deg2rad',
38-
'unique', 'lcm', 'tril', 'identity', 'take']
38+
'unique', 'lcm', 'tril', 'identity', 'take', 'rot90']
3939

4040

4141
@set_module('mxnet.ndarray.numpy')
@@ -2318,6 +2318,121 @@ def split(ary, indices_or_sections, axis=0):
23182318
# pylint: enable=redefined-outer-name
23192319

23202320

2321+
# pylint: disable=redefined-outer-name
2322+
@set_module('mxnet.ndarray.numpy')
2323+
def hsplit(ary, indices_or_sections):
2324+
"""Split an array into multiple sub-arrays horizontally (column-wise).
2325+
2326+
This is equivalent to ``split`` with ``axis=0`` if ``ary`` has one
2327+
dimension, and otherwise that with ``axis=1``.
2328+
2329+
Parameters
2330+
----------
2331+
ary : ndarray
2332+
Array to be divided into sub-arrays.
2333+
indices_or_sections : int, list of ints or tuple of ints.
2334+
If `indices_or_sections` is an integer, N, the array will be divided
2335+
into N equal arrays along `axis`. If such a split is not possible,
2336+
an error is raised.
2337+
2338+
If `indices_or_sections` is a list of sorted integers, the entries
2339+
indicate where along `axis` the array is split.
2340+
2341+
If an index exceeds the dimension of the array along `axis`,
2342+
it will raises errors. so index must less than or euqal to
2343+
the dimension of the array along axis.
2344+
2345+
Returns
2346+
-------
2347+
sub-arrays : list of ndarrays
2348+
A list of sub-arrays.
2349+
2350+
Notes
2351+
------
2352+
- If `indices_or_sections` is given as an integer, but a split
2353+
does not result in equal division.It will raises ValueErrors.
2354+
2355+
- If indices_or_sections is an integer, and the number is 1, it will
2356+
raises an error. Because single output from split is not supported yet...
2357+
2358+
See Also
2359+
--------
2360+
split : Split an array into multiple sub-arrays of equal size.
2361+
2362+
Examples
2363+
--------
2364+
>>> x = np.arange(16.0).reshape(4, 4)
2365+
>>> x
2366+
array([[ 0., 1., 2., 3.],
2367+
[ 4., 5., 6., 7.],
2368+
[ 8., 9., 10., 11.],
2369+
[12., 13., 14., 15.]])
2370+
>>> np.hsplit(x, 2)
2371+
[array([[ 0., 1.],
2372+
[ 4., 5.],
2373+
[ 8., 9.],
2374+
[12., 13.]]),
2375+
array([[ 2., 3.],
2376+
[ 6., 7.],
2377+
[10., 11.],
2378+
[14., 15.]])]
2379+
>>> np.hsplit(x, [3, 6])
2380+
[array([[ 0., 1., 2.],
2381+
[ 4., 5., 6.],
2382+
[ 8., 9., 10.],
2383+
[12., 13., 14.]]),
2384+
array([[ 3.],
2385+
[ 7.],
2386+
[11.],
2387+
[15.]]),
2388+
array([], shape=(4, 0), dtype=float32)]
2389+
2390+
With a higher dimensional array the split is still along the second axis.
2391+
2392+
>>> x = np.arange(8.0).reshape(2, 2, 2)
2393+
>>> x
2394+
array([[[ 0., 1.],
2395+
[ 2., 3.]],
2396+
[[ 4., 5.],
2397+
[ 6., 7.]]])
2398+
>>> np.hsplit(x, 2)
2399+
[array([[[ 0., 1.]],
2400+
[[ 4., 5.]]]),
2401+
array([[[ 2., 3.]],
2402+
[[ 6., 7.]]])]
2403+
2404+
If ``ary`` has one dimension, 'axis' = 0.
2405+
>>> x = np.arange(4)
2406+
array([0., 1., 2., 3.])
2407+
>>> np.hsplit(x, 2)
2408+
[array([0., 1.]), array([2., 3.])]
2409+
2410+
If you want to produce an empty sub-array, you can see an example.
2411+
>>> np.hsplit(x, [2, 2])
2412+
[array([0., 1.]), array([], dtype=float32), array([2., 3.])]
2413+
"""
2414+
indices = []
2415+
axis = 1
2416+
if (len(ary.shape) == 1):
2417+
axis = 0
2418+
axis_size = ary.shape[axis]
2419+
if isinstance(indices_or_sections, int):
2420+
sections = indices_or_sections
2421+
if axis_size % sections:
2422+
raise ValueError('array hsplit does not result in an equal division')
2423+
section_size = int(axis_size / sections)
2424+
indices = [i * section_size for i in range(sections)]
2425+
elif isinstance(indices_or_sections, (list, set, tuple)):
2426+
indices = [0] + list(indices_or_sections)
2427+
else:
2428+
raise ValueError('indices_or_sections must either int or tuple of ints')
2429+
ret = _npi.hsplit(ary, indices, axis, False)
2430+
if not isinstance(ret, list):
2431+
raise NotImplementedError('single output from hsplit is not supported yet...')
2432+
return ret
2433+
# pylint: enable=redefined-outer-name
2434+
2435+
23212436
@set_module('mxnet.ndarray.numpy')
23222437
def concatenate(seq, axis=0, out=None):
23232438
"""Join a sequence of arrays along an existing axis.
@@ -3464,3 +3579,49 @@ def hypot(x1, x2, out=None):
34643579
[ 5., 5., 5.]])
34653580
"""
34663581
return _ufunc_helper(x1, x2, _npi.hypot, _np.hypot, _npi.hypot_scalar, None, out)
3582+
3583+
3584+
@set_module('mxnet.ndarray.numpy')
3585+
def rot90(m, k=1, axes=(0, 1)):
3586+
"""
3587+
Rotate an array by 90 degrees in the plane specified by axes.
3588+
Rotation direction is from the first towards the second axis.
3589+
Parameters
3590+
----------
3591+
m : ndarray
3592+
Array of two or more dimensions.
3593+
k : integer
3594+
Number of times the array is rotated by 90 degrees.
3595+
axes: (2,) array_like
3596+
The array is rotated in the plane defined by the axes.
3597+
Axes must be different.
3598+
3599+
Returns
3600+
-------
3601+
y : ndarray
3602+
A rotated view of `m`.
3603+
3604+
-----
3605+
rot90(m, k=1, axes=(1,0)) is the reverse of rot90(m, k=1, axes=(0,1))
3606+
rot90(m, k=1, axes=(1,0)) is equivalent to rot90(m, k=-1, axes=(0,1))
3607+
Examples
3608+
--------
3609+
>>> m = np.array([[1,2],[3,4]], 'int')
3610+
>>> m
3611+
array([[1, 2],
3612+
[3, 4]], dtype=int64)
3613+
>>> np.rot90(m)
3614+
array([[2, 4],
3615+
[1, 3]], dtype=int64)
3616+
>>> np.rot90(m, 2)
3617+
array([[4, 3],
3618+
[2, 1]], dtype=int64)
3619+
>>> m = np.arange(8).reshape((2,2,2))
3620+
>>> np.rot90(m, 1, (1,2))
3621+
array([[[1., 3.],
3622+
[0., 2.]],
3623+
3624+
[[5., 7.],
3625+
[4., 6.]]])
3626+
"""
3627+
return _npi.rot90(m, k=k, axes=axes)

0 commit comments

Comments
 (0)