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

Function PyArray_NewLikeArrayWithShape

numpy/core/src/multiarray/ctors.c:1035–1139  ·  view source on GitHub ↗

* Creates a new array with the same shape as the provided one, * with possible memory layout order, data type and shape changes. * * prototype - The array the new one should be like. * order - NPY_CORDER - C-contiguous result. * NPY_FORTRANORDER - Fortran-contiguous result. * NPY_ANYORDER - Fortran if prototype is Fortran, C otherwise. * NPY_KEEPORDER

Source from the content-addressed store, hash-verified

1033 * dtype->subarray is true, dtype will be decrefed.
1034 */
1035NPY_NO_EXPORT PyObject *
1036PyArray_NewLikeArrayWithShape(PyArrayObject *prototype, NPY_ORDER order,
1037 PyArray_Descr *dtype, int ndim, npy_intp const *dims, int subok)
1038{
1039 PyObject *ret = NULL;
1040
1041 if (ndim == -1) {
1042 ndim = PyArray_NDIM(prototype);
1043 dims = PyArray_DIMS(prototype);
1044 }
1045 else if (order == NPY_KEEPORDER && (ndim != PyArray_NDIM(prototype))) {
1046 order = NPY_CORDER;
1047 }
1048
1049 /* If no override data type, use the one from the prototype */
1050 if (dtype == NULL) {
1051 dtype = PyArray_DESCR(prototype);
1052 Py_INCREF(dtype);
1053 }
1054
1055 /* Handle ANYORDER and simple KEEPORDER cases */
1056 switch (order) {
1057 case NPY_ANYORDER:
1058 order = PyArray_ISFORTRAN(prototype) ?
1059 NPY_FORTRANORDER : NPY_CORDER;
1060 break;
1061 case NPY_KEEPORDER:
1062 if (PyArray_IS_C_CONTIGUOUS(prototype) || ndim <= 1) {
1063 order = NPY_CORDER;
1064 break;
1065 }
1066 else if (PyArray_IS_F_CONTIGUOUS(prototype)) {
1067 order = NPY_FORTRANORDER;
1068 break;
1069 }
1070 break;
1071 default:
1072 break;
1073 }
1074
1075 /* If it's not KEEPORDER, this is simple */
1076 if (order != NPY_KEEPORDER) {
1077 ret = PyArray_NewFromDescr(subok ? Py_TYPE(prototype) : &PyArray_Type,
1078 dtype,
1079 ndim,
1080 dims,
1081 NULL,
1082 NULL,
1083 order,
1084 subok ? (PyObject *)prototype : NULL);
1085 }
1086 /* KEEPORDER needs some analysis of the strides */
1087 else {
1088 npy_intp strides[NPY_MAXDIMS], stride;
1089 npy_stride_sort_item strideperm[NPY_MAXDIMS];
1090 int idim;
1091
1092 PyArray_CreateSortedStridePerm(ndim,

Callers 2

PyArray_NewLikeArrayFunction · 0.85
array_empty_likeFunction · 0.85

Calls 7

PyArray_NDIMFunction · 0.85
PyArray_DIMSFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_NewFromDescrFunction · 0.85
PyArray_STRIDESFunction · 0.85
PyArray_FillObjectArrayFunction · 0.85

Tested by

no test coverage detected