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

Function PyArray_ArgPartition

numpy/core/src/multiarray/item_selection.c:1708–1759  ·  view source on GitHub ↗

NUMPY_API * ArgPartition an array */

Source from the content-addressed store, hash-verified

1706 * ArgPartition an array
1707 */
1708NPY_NO_EXPORT PyObject *
1709PyArray_ArgPartition(PyArrayObject *op, PyArrayObject *ktharray, int axis,
1710 NPY_SELECTKIND which)
1711{
1712 PyArrayObject *op2, *kthrvl;
1713 PyArray_ArgPartitionFunc *argpart;
1714 PyArray_ArgSortFunc *argsort;
1715 PyObject *ret;
1716
1717 /*
1718 * As a C-exported function, enum NPY_SELECTKIND loses its enum property
1719 * Check the values to make sure they are in range
1720 */
1721 if ((int)which < 0 || (int)which >= NPY_NSELECTS) {
1722 PyErr_SetString(PyExc_ValueError,
1723 "not a valid partition kind");
1724 return NULL;
1725 }
1726
1727 argpart = get_argpartition_func(PyArray_TYPE(op), which);
1728 if (argpart == NULL) {
1729 /* Use sorting, slower but equivalent */
1730 if (PyArray_DESCR(op)->f->compare) {
1731 argsort = npy_aquicksort;
1732 }
1733 else {
1734 PyErr_SetString(PyExc_TypeError,
1735 "type does not have compare function");
1736 return NULL;
1737 }
1738 }
1739
1740 op2 = (PyArrayObject *)PyArray_CheckAxis(op, &axis, 0);
1741 if (op2 == NULL) {
1742 return NULL;
1743 }
1744
1745 /* Process ktharray even if using sorting to do bounds checking */
1746 kthrvl = partition_prep_kth_array(ktharray, op2, axis);
1747 if (kthrvl == NULL) {
1748 Py_DECREF(op2);
1749 return NULL;
1750 }
1751
1752 ret = _new_argsortlike(op2, axis, argsort, argpart,
1753 PyArray_DATA(kthrvl), PyArray_SIZE(kthrvl));
1754
1755 Py_DECREF(kthrvl);
1756 Py_DECREF(op2);
1757
1758 return ret;
1759}
1760
1761
1762/*NUMPY_API

Callers 1

array_argpartitionFunction · 0.85

Calls 6

PyArray_TYPEFunction · 0.85
PyArray_DESCRFunction · 0.85
PyArray_CheckAxisFunction · 0.85
partition_prep_kth_arrayFunction · 0.85
_new_argsortlikeFunction · 0.85
PyArray_DATAFunction · 0.85

Tested by

no test coverage detected