Skip to content

Commit 4b640de

Browse files
Add implementaion of dpnp.matrix_transpose()
1 parent 48515c8 commit 4b640de

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

dpnp/dpnp_iface_manipulation.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
"flipud",
7373
"hsplit",
7474
"hstack",
75+
"matrix_transpose",
7576
"moveaxis",
7677
"ndim",
7778
"permute_dims",
@@ -1751,6 +1752,53 @@ def hstack(tup, *, dtype=None, casting="same_kind"):
17511752
return dpnp.concatenate(arrs, axis=1, dtype=dtype, casting=casting)
17521753

17531754

1755+
def matrix_transpose(x, /):
1756+
"""
1757+
Transposes a matrix (or a stack of matrices) ``x``.
1758+
1759+
For full documentation refer to :obj:`numpy.matrix_transpose`.
1760+
1761+
Parameters
1762+
----------
1763+
x : (..., M, N) {dpnp.ndarray, usm_ndarray}
1764+
Input array with ``x.ndim >= 2` and whose two innermost
1765+
dimensions form ``MxN`` matrices.
1766+
1767+
Returns
1768+
-------
1769+
out : dpnp.ndarray
1770+
An array containing the transpose for each matrix and having shape
1771+
(..., N, M).
1772+
1773+
See Also
1774+
--------
1775+
:obj:`dpnp.transpose` : Returns an array with axes transposed.
1776+
1777+
Examples
1778+
--------
1779+
>>> import dpnp as np
1780+
>>> a = np.array([[1, 2], [3, 4]])
1781+
>>> np.matrix_transpose(a)
1782+
array([[1, 3],
1783+
[2, 4]])
1784+
1785+
>>> b = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
1786+
>>> np.matrix_transpose(b)
1787+
array([[[1, 3],
1788+
[2, 4]],
1789+
[[5, 7],
1790+
[6, 8]]])
1791+
1792+
"""
1793+
1794+
dpnp.check_supported_arrays_type(x)
1795+
if x.ndim < 2:
1796+
raise ValueError(
1797+
f"Input array must be at least 2-dimensional, but it is {x.ndim}"
1798+
)
1799+
return dpnp.swapaxes(x, -1, -2)
1800+
1801+
17541802
def moveaxis(a, source, destination):
17551803
"""
17561804
Move axes of an array to new positions. Other axes remain in their original

0 commit comments

Comments
 (0)