8000 ENH: Added `bit_count` to `np.int*` · numpy/numpy@87377d4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 87377d4

Browse files
committed
ENH: Added bit_count to np.int*
1 parent b3ee1e8 commit 87377d4

File tree

4 files changed

+19
-2
lines changed

4 files changed

+19
-2
lines changed

numpy/core/_methods.py

Lines changed: 5 additions & 10000 ; 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
umr_minimum = um.minimum.reduce
2020
umr_sum = um.add.reduce
2121
umr_prod = um.multiply.reduce
22+
umr_bit_count = um.bit_count
2223
umr_any = um.logical_or.reduce
2324
umr_all = um.logical_and.reduce
2425

@@ -51,6 +52,10 @@ def _prod(a, axis=None, dtype=None, out=None, keepdims=False,
5152
initial=_NoValue, where=True):
5253
return umr_prod(a, axis, dtype, out, keepdims, initial, where)
5354

55+
def _bit_count(a, out=None, where=True, casting='same_kind',
56+
order='K', dtype=None, subok=True):
57+
return umr_bit_count(a, dtype)
58+
5459
def _any(a, axis=None, dtype=None, out=None, keepdims=False, *, where=True):
5560
# Parsing keyword arguments is currently fairly slow, so avoid it for now
5661
if where is True:

numpy/core/src/multiarray/methods.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,12 @@ array_ptp(PyArrayObject *self, PyObject *args, PyObject *kwds)
348348
NPY_FORWARD_NDARRAY_METHOD("_ptp");
349349
}
350350

351+
static PyObject *
352+
array_bit_count(PyArrayObject *self, PyObject *args, PyObject *kwds)
353+
{
354+
NPY_FORWARD_NDARRAY_METHOD("_bit_count");
355+
}
356+
351357

352358
static PyObject *
353359
array_swapaxes(PyArrayObject *self, PyObject *args)
@@ -2914,5 +2920,8 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
29142920
{"view",
29152921
(PyCFunction)array_view,
29162922
METH_FASTCALL | METH_KEYWORDS, NULL},
2923+
{"bit_count",
2924+
(PyCFunction)array_bit_count,
2925+
METH_VARARGS | METH_KEYWORDS, NULL},
29172926
{NULL, NULL, 0, NULL} /* sentinel */
29182927
};

numpy/core/src/multiarray/scalartypes.c.src

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ gentype_byteswap(PyObject *self, PyObject *args, PyObject *kwds)
15351535
* std, var, sum, cumsum, prod, cumprod, compress, sort, argsort,
15361536
* round, argmax, argmin, max, min, ptp, any, all, astype, resize,
15371537
* reshape, choose, tostring, tobytes, copy, searchsorted, view,
1538-
* flatten, ravel, squeeze#
1538+
* flatten, ravel, squeeze, bit_count#
15391539
*/
15401540
static PyObject *
15411541
gentype_@name@(PyObject *self, PyObject *args, PyObject *kwds)
@@ -2070,6 +2070,9 @@ static PyMethodDef gentype_methods[] = {
20702070
{"sum",
20712071
(PyCFunction)gentype_sum,
20722072
METH_VARARGS | METH_KEYWORDS, NULL},
2073+
{"bit_count",
2074+
(PyCFunction)gentype_bit_count,
2075+
METH_VARARGS | METH_KEYWORDS, NULL},
20732076
{"cumsum",
20742077
(PyCFunction)gentype_cumsum,
20752078
METH_VARARGS | METH_KEYWORDS, NULL},

numpy/matrixlib/tests/test_defmatrix.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def test_instance_methods(self):
286286
'partition', 'argpartition',
287287
'take', 'tofile', 'tolist', 'tostring', 'tobytes', 'all', 'any',
288288
'sum', 'argmax', 'argmin', 'min', 'max', 'mean', 'var', 'ptp',
289-
'prod', 'std', 'ctypes', 'itemset',
289+
'prod', 'std', 'ctypes', 'itemset', 'bit_count',
290290
]
291291
for attrib in dir(a):
292292
if attrib.startswith('_') or attrib in excluded_methods:

0 commit comments

Comments
 (0)
0