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

Function check_and_fix_dimensions

numpy/f2py/src/fortranobject.c:1122–1311  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1120/*****************************************/
1121
1122static int
1123check_and_fix_dimensions(const PyArrayObject* arr, const int rank,
1124 npy_intp *dims, const char *errmess)
1125{
1126 /*
1127 * This function fills in blanks (that are -1's) in dims list using
1128 * the dimensions from arr. It also checks that non-blank dims will
1129 * match with the corresponding values in arr dimensions.
1130 *
1131 * Returns 0 if the function is successful.
1132 *
1133 * If an error condition is detected, an exception is set and 1 is
1134 * returned.
1135 */
1136 char mess[F2PY_MESSAGE_BUFFER_SIZE];
1137 const npy_intp arr_size =
1138 (PyArray_NDIM(arr)) ? PyArray_Size((PyObject *)arr) : 1;
1139#ifdef DEBUG_COPY_ND_ARRAY
1140 dump_attrs(arr);
1141 printf("check_and_fix_dimensions:init: dims=");
1142 dump_dims(rank, dims);
1143#endif
1144 if (rank > PyArray_NDIM(arr)) { /* [1,2] -> [[1],[2]]; 1 -> [[1]] */
1145 npy_intp new_size = 1;
1146 int free_axe = -1;
1147 int i;
1148 npy_intp d;
1149 /* Fill dims where -1 or 0; check dimensions; calc new_size; */
1150 for (i = 0; i < PyArray_NDIM(arr); ++i) {
1151 d = PyArray_DIM(arr, i);
1152 if (dims[i] >= 0) {
1153 if (d > 1 && dims[i] != d) {
1154 PyErr_Format(
1155 PyExc_ValueError,
1156 "%d-th dimension must be fixed to %" NPY_INTP_FMT
1157 " but got %" NPY_INTP_FMT "\n",
1158 i, dims[i], d);
1159 return 1;
1160 }
1161 if (!dims[i])
1162 dims[i] = 1;
1163 }
1164 else {
1165 dims[i] = d ? d : 1;
1166 }
1167 new_size *= dims[i];
1168 }
1169 for (i = PyArray_NDIM(arr); i < rank; ++i)
1170 if (dims[i] > 1) {
1171 PyErr_Format(PyExc_ValueError,
1172 "%d-th dimension must be %" NPY_INTP_FMT
1173 " but got 0 (not defined).\n",
1174 i, dims[i]);
1175 return 1;
1176 }
1177 else if (free_axe < 0)
1178 free_axe = i;
1179 else

Callers 1

ndarray_from_pyobjFunction · 0.85

Calls 5

PyArray_NDIMFunction · 0.85
PyArray_SizeFunction · 0.85
dump_attrsFunction · 0.85
dump_dimsFunction · 0.85
PyArray_DIMFunction · 0.85

Tested by

no test coverage detected