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

Function PyArray_CopyAndTranspose

numpy/core/src/multiarray/multiarraymodule.c:1161–1210  ·  view source on GitHub ↗

NUMPY_API * Copy and Transpose * * Could deprecate this function, as there isn't a speed benefit over * calling Transpose and then Copy. */

Source from the content-addressed store, hash-verified

1159 * calling Transpose and then Copy.
1160 */
1161NPY_NO_EXPORT PyObject *
1162PyArray_CopyAndTranspose(PyObject *op)
1163{
1164 /* DEPRECATED 2022-09-30, NumPy 1.24 - see gh-22313 */
1165 if (DEPRECATE(
1166 "fastCopyAndTranspose and the underlying C function "
1167 "PyArray_CopyAndTranspose have been deprecated.\n\n"
1168 "Use the transpose method followed by a C-order copy instead, "
1169 "e.g. ``arr.T.copy()``") < 0) {
1170 return NULL;
1171 }
1172
1173 PyArrayObject *arr, *tmp, *ret;
1174 int i;
1175 npy_intp new_axes_values[NPY_MAXDIMS];
1176 PyArray_Dims new_axes;
1177
1178 /* Make sure we have an array */
1179 arr = (PyArrayObject *)PyArray_FROM_O(op);
1180 if (arr == NULL) {
1181 return NULL;
1182 }
1183
1184 if (PyArray_NDIM(arr) > 1) {
1185 /* Set up the transpose operation */
1186 new_axes.len = PyArray_NDIM(arr);
1187 for (i = 0; i < new_axes.len; ++i) {
1188 new_axes_values[i] = new_axes.len - i - 1;
1189 }
1190 new_axes.ptr = new_axes_values;
1191
1192 /* Do the transpose (always returns a view) */
1193 tmp = (PyArrayObject *)PyArray_Transpose(arr, &new_axes);
1194 if (tmp == NULL) {
1195 Py_DECREF(arr);
1196 return NULL;
1197 }
1198 }
1199 else {
1200 tmp = arr;
1201 arr = NULL;
1202 }
1203
1204 /* TODO: Change this to NPY_KEEPORDER for NumPy 2.0 */
1205 ret = (PyArrayObject *)PyArray_NewCopy(tmp, NPY_CORDER);
1206
1207 Py_XDECREF(arr);
1208 Py_DECREF(tmp);
1209 return (PyObject *)ret;
1210}
1211
1212/*
1213 * Implementation which is common between PyArray_Correlate

Callers 1

Calls 3

PyArray_NDIMFunction · 0.85
PyArray_TransposeFunction · 0.85
PyArray_NewCopyFunction · 0.85

Tested by

no test coverage detected