NUMPY_API * New shape for an array */
| 198 | * New shape for an array |
| 199 | */ |
| 200 | NPY_NO_EXPORT PyObject * |
| 201 | PyArray_Newshape(PyArrayObject *self, PyArray_Dims *newdims, |
| 202 | NPY_ORDER order) |
| 203 | { |
| 204 | npy_intp i; |
| 205 | npy_intp *dimensions = newdims->ptr; |
| 206 | PyArrayObject *ret; |
| 207 | int ndim = newdims->len; |
| 208 | npy_bool same; |
| 209 | npy_intp *strides = NULL; |
| 210 | npy_intp newstrides[NPY_MAXDIMS]; |
| 211 | int flags; |
| 212 | |
| 213 | if (order == NPY_ANYORDER) { |
| 214 | order = PyArray_ISFORTRAN(self) ? NPY_FORTRANORDER : NPY_CORDER; |
| 215 | } |
| 216 | else if (order == NPY_KEEPORDER) { |
| 217 | PyErr_SetString(PyExc_ValueError, |
| 218 | "order 'K' is not permitted for reshaping"); |
| 219 | return NULL; |
| 220 | } |
| 221 | /* Quick check to make sure anything actually needs to be done */ |
| 222 | if (ndim == PyArray_NDIM(self)) { |
| 223 | same = NPY_TRUE; |
| 224 | i = 0; |
| 225 | while (same && i < ndim) { |
| 226 | if (PyArray_DIM(self,i) != dimensions[i]) { |
| 227 | same=NPY_FALSE; |
| 228 | } |
| 229 | i++; |
| 230 | } |
| 231 | if (same) { |
| 232 | return PyArray_View(self, NULL, NULL); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * fix any -1 dimensions and check new-dimensions against old size |
| 238 | */ |
| 239 | if (_fix_unknown_dimension(newdims, self) < 0) { |
| 240 | return NULL; |
| 241 | } |
| 242 | /* |
| 243 | * sometimes we have to create a new copy of the array |
| 244 | * in order to get the right orientation and |
| 245 | * because we can't just re-use the buffer with the |
| 246 | * data in the order it is in. |
| 247 | */ |
| 248 | Py_INCREF(self); |
| 249 | if (((order == NPY_CORDER && !PyArray_IS_C_CONTIGUOUS(self)) || |
| 250 | (order == NPY_FORTRANORDER && !PyArray_IS_F_CONTIGUOUS(self)))) { |
| 251 | int success = 0; |
| 252 | success = _attempt_nocopy_reshape(self, ndim, dimensions, |
| 253 | newstrides, order); |
| 254 | if (success) { |
| 255 | /* no need to copy the array after all */ |
| 256 | strides = newstrides; |
| 257 | } |
no test coverage detected