MCPcopy
hub / github.com/numpy/numpy / matrix_transpose

Function matrix_transpose

numpy/_core/fromnumeric.py:684–725  ·  view source on GitHub ↗

Transposes a matrix (or a stack of matrices) ``x``. This function is Array API compatible. Parameters ---------- x : array_like Input array having shape (..., M, N) and whose two innermost dimensions form ``MxN`` matrices. Returns ------- out : nda

(x, /)

Source from the content-addressed store, hash-verified

682
683@array_function_dispatch(_matrix_transpose_dispatcher)
684def matrix_transpose(x, /):
685 """
686 Transposes a matrix (or a stack of matrices) ``x``.
687
688 This function is Array API compatible.
689
690 Parameters
691 ----------
692 x : array_like
693 Input array having shape (..., M, N) and whose two innermost
694 dimensions form ``MxN`` matrices.
695
696 Returns
697 -------
698 out : ndarray
699 An array containing the transpose for each matrix and having shape
700 (..., N, M).
701
702 See Also
703 --------
704 transpose : Generic transpose method.
705
706 Examples
707 --------
708 >>> import numpy as np
709 >>> np.matrix_transpose([[1, 2], [3, 4]])
710 array([[1, 3],
711 [2, 4]])
712
713 >>> np.matrix_transpose([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
714 array([[[1, 3],
715 [2, 4]],
716 [[5, 7],
717 [6, 8]]])
718
719 """
720 x = asanyarray(x)
721 if x.ndim < 2:
722 raise ValueError(
723 f"Input array must be at least 2-dimensional, but it is {x.ndim}"
724 )
725 return swapaxes(x, -1, -2)
726
727
728def _partition_dispatcher(a, kth, axis=None, kind=None, order=None):

Callers

nothing calls this directly

Calls 2

asanyarrayFunction · 0.85
swapaxesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…