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

Function PyArray_Squeeze

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

NUMPY_API * * return a new view of the array object with all of its unit-length * dimensions squeezed out if needed, otherwise * return the same array. */

Source from the content-addressed store, hash-verified

528 * return the same array.
529 */
530NPY_NO_EXPORT PyObject *
531PyArray_Squeeze(PyArrayObject *self)
532{
533 PyArrayObject *ret;
534 npy_bool unit_dims[NPY_MAXDIMS];
535 int idim, ndim, any_ones;
536 npy_intp *shape;
537
538 ndim = PyArray_NDIM(self);
539 shape = PyArray_SHAPE(self);
540
541 any_ones = 0;
542 for (idim = 0; idim < ndim; ++idim) {
543 if (shape[idim] == 1) {
544 unit_dims[idim] = 1;
545 any_ones = 1;
546 }
547 else {
548 unit_dims[idim] = 0;
549 }
550 }
551
552 /* If there were no ones to squeeze out, return the same array */
553 if (!any_ones) {
554 Py_INCREF(self);
555 return (PyObject *)self;
556 }
557
558 ret = (PyArrayObject *)PyArray_View(self, NULL, &PyArray_Type);
559 if (ret == NULL) {
560 return NULL;
561 }
562
563 PyArray_RemoveAxesInPlace(ret, unit_dims);
564
565 /*
566 * If self isn't not a base class ndarray, call its
567 * __array_wrap__ method
568 */
569 if (Py_TYPE(self) != &PyArray_Type) {
570 PyArrayObject *tmp = PyArray_SubclassWrap(self, ret);
571 Py_DECREF(ret);
572 ret = tmp;
573 }
574
575 return (PyObject *)ret;
576}
577
578/*
579 * Just like PyArray_Squeeze, but allows the caller to select

Callers 1

array_squeezeFunction · 0.85

Calls 5

PyArray_NDIMFunction · 0.85
PyArray_SHAPEFunction · 0.85
PyArray_ViewFunction · 0.85
PyArray_SubclassWrapFunction · 0.85

Tested by

no test coverage detected