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

Function PyArray_Newshape

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

NUMPY_API * New shape for an array */

Source from the content-addressed store, hash-verified

198 * New shape for an array
199 */
200NPY_NO_EXPORT PyObject *
201PyArray_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 }

Callers 9

array_reshapeFunction · 0.85
PyArray_ReshapeFunction · 0.85
PyArray_RavelFunction · 0.85
array_vdotFunction · 0.85
unpack_bitsFunction · 0.85
PyArray_MapIterSwapAxesFunction · 0.85
PyArray_MapIterNewFunction · 0.85
_void_compareFunction · 0.85

Calls 10

PyArray_NDIMFunction · 0.85
PyArray_DIMFunction · 0.85
PyArray_ViewFunction · 0.85
_fix_unknown_dimensionFunction · 0.85
_attempt_nocopy_reshapeFunction · 0.85
PyArray_NewCopyFunction · 0.85
PyArray_FLAGSFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_NewFromDescr_intFunction · 0.85
PyArray_DATAFunction · 0.85

Tested by

no test coverage detected