MCPcopy Create free account
hub / github.com/numpy/numpy / PyArray_Transpose

Function PyArray_Transpose

numpy/core/src/multiarray/shape.c:668–732  ·  view source on GitHub ↗

NUMPY_API * Return Transpose. */

Source from the content-addressed store, hash-verified

666 * Return Transpose.
667 */
668NPY_NO_EXPORT PyObject *
669PyArray_Transpose(PyArrayObject *ap, PyArray_Dims *permute)
670{
671 npy_intp *axes;
672 int i, n;
673 int permutation[NPY_MAXDIMS], reverse_permutation[NPY_MAXDIMS];
674 PyArrayObject *ret = NULL;
675 int flags;
676
677 if (permute == NULL) {
678 n = PyArray_NDIM(ap);
679 for (i = 0; i < n; i++) {
680 permutation[i] = n-1-i;
681 }
682 }
683 else {
684 n = permute->len;
685 axes = permute->ptr;
686 if (n != PyArray_NDIM(ap)) {
687 PyErr_SetString(PyExc_ValueError,
688 "axes don't match array");
689 return NULL;
690 }
691 for (i = 0; i < n; i++) {
692 reverse_permutation[i] = -1;
693 }
694 for (i = 0; i < n; i++) {
695 int axis = axes[i];
696 if (check_and_adjust_axis(&axis, PyArray_NDIM(ap)) < 0) {
697 return NULL;
698 }
699 if (reverse_permutation[axis] != -1) {
700 PyErr_SetString(PyExc_ValueError,
701 "repeated axis in transpose");
702 return NULL;
703 }
704 reverse_permutation[axis] = i;
705 permutation[i] = axis;
706 }
707 }
708
709 flags = PyArray_FLAGS(ap);
710
711 /*
712 * this allocates memory for dimensions and strides (but fills them
713 * incorrectly), sets up descr, and points data at PyArray_DATA(ap).
714 */
715 Py_INCREF(PyArray_DESCR(ap));
716 ret = (PyArrayObject *) PyArray_NewFromDescrAndBase(
717 Py_TYPE(ap), PyArray_DESCR(ap),
718 n, PyArray_DIMS(ap), NULL, PyArray_DATA(ap),
719 flags, (PyObject *)ap, (PyObject *)ap);
720 if (ret == NULL) {
721 return NULL;
722 }
723
724 /* fix the dimensions and strides of the return-array */
725 for (i = 0; i < n; i++) {

Callers 9

array_transpose_getFunction · 0.85
array_transposeFunction · 0.85
PyArray_SwapAxesFunction · 0.85
PyArray_InnerProductFunction · 0.85
PyArray_CopyAndTransposeFunction · 0.85
PyArray_ToStringFunction · 0.85
PyArray_MapIterSwapAxesFunction · 0.85
_PyArray_ArgMinMaxCommonFunction · 0.85

Calls 9

PyArray_NDIMFunction · 0.85
check_and_adjust_axisFunction · 0.85
PyArray_FLAGSFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_DATAFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_UpdateFlagsFunction · 0.85

Tested by

no test coverage detected