* Just like PyArray_Squeeze, but allows the caller to select * a subset of the size-one dimensions to squeeze out. */
| 580 | * a subset of the size-one dimensions to squeeze out. |
| 581 | */ |
| 582 | NPY_NO_EXPORT PyObject * |
| 583 | PyArray_SqueezeSelected(PyArrayObject *self, npy_bool *axis_flags) |
| 584 | { |
| 585 | PyArrayObject *ret; |
| 586 | int idim, ndim, any_ones; |
| 587 | npy_intp *shape; |
| 588 | |
| 589 | ndim = PyArray_NDIM(self); |
| 590 | shape = PyArray_SHAPE(self); |
| 591 | |
| 592 | /* Verify that the axes requested are all of size one */ |
| 593 | any_ones = 0; |
| 594 | for (idim = 0; idim < ndim; ++idim) { |
| 595 | if (axis_flags[idim] != 0) { |
| 596 | if (shape[idim] == 1) { |
| 597 | any_ones = 1; |
| 598 | } |
| 599 | else { |
| 600 | PyErr_SetString(PyExc_ValueError, |
| 601 | "cannot select an axis to squeeze out " |
| 602 | "which has size not equal to one"); |
| 603 | return NULL; |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | /* If there were no axes to squeeze out, return the same array */ |
| 609 | if (!any_ones) { |
| 610 | Py_INCREF(self); |
| 611 | return (PyObject *)self; |
| 612 | } |
| 613 | |
| 614 | ret = (PyArrayObject *)PyArray_View(self, NULL, &PyArray_Type); |
| 615 | if (ret == NULL) { |
| 616 | return NULL; |
| 617 | } |
| 618 | |
| 619 | PyArray_RemoveAxesInPlace(ret, axis_flags); |
| 620 | |
| 621 | /* |
| 622 | * If self isn't not a base class ndarray, call its |
| 623 | * __array_wrap__ method |
| 624 | */ |
| 625 | if (Py_TYPE(self) != &PyArray_Type) { |
| 626 | PyArrayObject *tmp = PyArray_SubclassWrap(self, ret); |
| 627 | Py_DECREF(ret); |
| 628 | ret = tmp; |
| 629 | } |
| 630 | |
| 631 | return (PyObject *)ret; |
| 632 | } |
| 633 | |
| 634 | /*NUMPY_API |
| 635 | * SwapAxes |
no test coverage detected