Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1731966

Browse files
committed
MAINT: Change bit_count UFunc to bitwise_count
1 parent b18204c commit 1731966

File tree

16 files changed

+50
-48
lines changed

16 files changed

+50
-48
lines changed

benchmarks/benchmarks/bench_ufunc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55

66
ufuncs = ['abs', 'absolute', 'add', 'arccos', 'arccosh', 'arcsin', 'arcsinh',
7-
'arctan', 'arctan2', 'arctanh', 'bit_count', 'bitwise_and', 'bitwise_not',
7+
'arctan', 'arctan2', 'arctanh', 'bitwise_and', 'bitwise_count', 'bitwise_not',
88
'bitwise_or', 'bitwise_xor', 'cbrt', 'ceil', 'conj', 'conjugate',
99
'copysign', 'cos', 'cosh', 'deg2rad', 'degrees', 'divide', 'divmod',
1010
'equal', 'exp', 'exp2', 'expm1', 'fabs', 'float_power', 'floor',
@@ -58,9 +58,9 @@ def time_ufunc_types(self, ufuncname):
5858
[self.f(*arg) for arg in self.args]
5959

6060
class UFuncSmall(Benchmark):
61-
""" Benchmark for a selection of ufuncs on a small arrays and scalars
61+
""" Benchmark for a selection of ufuncs on a small arrays and scalars
6262
63-
Since the arrays and scalars are small, we are benchmarking the overhead
63+
Since the arrays and scalars are small, we are benchmarking the overhead
6464
of the numpy ufunc functionality
6565
"""
6666
params = ['abs', 'sqrt', 'cos']
@@ -77,7 +77,7 @@ def setup(self, ufuncname):
7777
self.array_int_3 = np.array([1, 2, 3])
7878
self.float64 = np.float64(1.1)
7979
self.python_float = 1.1
80-
80+
8181
def time_ufunc_small_array(self, ufuncname):
8282
self.f(self.array_5)
8383

@@ -92,7 +92,7 @@ def time_ufunc_numpy_scalar(self, ufuncname):
9292

9393
def time_ufunc_python_float(self, ufuncname):
9494
self.f(self.python_float)
95-
95+
9696

9797
class Custom(Benchmark):
9898
def setup(self):
@@ -302,7 +302,7 @@ def setup(self):
302302
self.b32 = np.random.rand(N).astype(np.float32)
303303
self.a64 = np.random.rand(N).astype(np.float64)
304304
self.b64 = np.random.rand(N).astype(np.float64)
305-
305+
306306
def time_pow_32(self):
307307
np.power(self.a32, self.b32)
308308

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
`np.bit_count` to compute the number of 1-bits in an integer
2-
------------------------------------------------------------
1+
`np.bitwise_count` to compute the number of 1-bits in an integer
2+
----------------------------------------------------------------
33

44
This new function counts the number of 1-bits in a number.
55
These work on all the numpy integer types, as well as the
66
builtin arbitrary-precision `Decimal` and `long` types.
7+
Internally, this function calls `np.bit_count`
78

89
.. code-block:: python
910
1011
>>> a = np.array([2**i - 1 for i in range(16)])
11-
>>> np.bit_count(a)
12+
>>> np.bitwise_count(a)
1213
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
1314
dtype=uint8)

doc/source/reference/routines.math.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,4 +180,4 @@ Miscellaneous
180180

181181
interp
182182

183-
bit_count
183+
bitwise_count

numpy/__init__.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,7 +2518,7 @@ class ndarray(_ArrayOrScalarCommon, Generic[_ShapeType, _DType_co]):
25182518
def __dlpack__(self: NDArray[number[Any]], *, stream: None = ...) -> _PyCapsule: ...
25192519
def __dlpack_device__(self) -> tuple[int, L[0]]: ...
25202520

2521-
def bit_count(
2521+
def bitwise_count(
25222522
self,
25232523
out: None | NDArray[Any] = ...,
25242524
*,
@@ -2672,7 +2672,7 @@ class generic(_ArrayOrScalarCommon):
26722672
self: _ScalarType, *shape: SupportsIndex, order: _OrderACF = ...
26732673
) -> ndarray[Any, _dtype[_ScalarType]]: ...
26742674

2675-
def bit_count(
2675+
def bitwise_count(
26762676
self,
26772677
out: None | NDArray[Any] = ...,
26782678
*,
@@ -3252,8 +3252,8 @@ arcsinh: _UFunc_Nin1_Nout1[L['arcsinh'], L[8], None]
32523252
arctan2: _UFunc_Nin2_Nout1[L['arctan2'], L[5], None]
32533253
arctan: _UFunc_Nin1_Nout1[L['arctan'], L[8], None]
32543254
arctanh: _UFunc_Nin1_Nout1[L['arctanh'], L[8], None]
3255-
bit_count: _UFunc_Nin1_Nout1[L['bit_count'], L[11], None]
32563255
bitwise_and: _UFunc_Nin2_Nout1[L['bitwise_and'], L[12], L[-1]]
3256+
bitwise_count: _UFunc_Nin1_Nout1[L['bitwise_count'], L[11], None]
32573257
bitwise_not: _UFunc_Nin1_Nout1[L['invert'], L[12], None]
32583258
bitwise_or: _UFunc_Nin2_Nout1[L['bitwise_or'], L[12], L[0]]
32593259
bitwise_xor: _UFunc_Nin2_Nout1[L['bitwise_xor'], L[12], L[0]]

numpy/core/_methods.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
umr_minimum = um.minimum.reduce
2121
umr_sum = um.add.reduce
2222
umr_prod = um.multiply.reduce
23-
umr_bit_count = um.bit_count
23+
umr_bitwise_count = um.bitwise_count
2424
umr_any = um.logical_or.reduce
2525
umr_all = um.logical_and.reduce
2626

@@ -297,7 +297,7 @@ def _dump(self, file, protocol=2):
297297
def _dumps(self, protocol=2):
298298
return pickle.dumps(self, protocol=protocol)
299299

300-
def _bit_count(a, out=None, *, where=True, casting='same_kind',
300+
def _bitwise_count(a, out=None, *, where=True, casting='same_kind',
301301
order='K', dtype=None, subok=True):
302-
return umr_bit_count(a, out, where=where, casting=casting,
302+
return umr_bitwise_count(a, out, where=where, casting=casting,
303303
order=order, dtype=dtype, subok=subok)

numpy/core/code_generators/generate_umath.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -982,9 +982,9 @@ def english_upper(s):
982982
TD(ints),
983983
TD('O', f='npy_ObjectLCM'),
984984
),
985-
'bit_count':
985+
'bitwise_count':
986986
Ufunc(1, 1, None,
987-
docstrings.get('numpy.core.umath.bit_count'),
987+
docstrings.get('numpy.core.umath.bitwise_count'),
988988
None,
989989
TD(ints, out='B'),
990990
TD(P, f='bit_count'),

numpy/core/code_generators/ufunc_docstrings.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4215,7 +4215,7 @@ def add_newdoc(place, name, doc):
42154215
42164216
""")
42174217

4218-
add_newdoc('numpy.core.umath', 'bit_count',
4218+
add_newdoc('numpy.core.umath', 'bitwise_count',
42194219
"""
42204220
Computes the number of 1-bits in the absolute value of ``x``.
42214221
Analogous to the builtin `int.bit_count` or ``popcount`` in C++.
@@ -4241,10 +4241,10 @@ def add_newdoc(place, name, doc):
42414241
42424242
Examples
42434243
--------
4244-
>>> np.bit_count(1023)
4244+
>>> np.bitwise_count(1023)
42454245
10
42464246
>>> a = np.array([2**i - 1 for i in range(16)])
4247-
>>> np.bit_count(a)
4247+
>>> np.bitwise_count(a)
42484248
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
42494249
dtype=uint8)
42504250

numpy/core/src/multiarray/methods.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ array_ptp(PyArrayObject *self, PyObject *args, PyObject *kwds)
355355
}
356356

357357
static PyObject *
358-
array_bit_count(PyArrayObject *self, PyObject *args, PyObject *kwds)
358+
array_bitwise_count(PyArrayObject *self, PyObject *args, PyObject *kwds)
359359
{
360-
NPY_FORWARD_NDARRAY_METHOD("_bit_count");
360+
NPY_FORWARD_NDARRAY_METHOD("_bitwise_count");
361361
}
362362

363363

@@ -3085,8 +3085,8 @@ NPY_NO_EXPORT PyMethodDef array_methods[] = {
30853085
{"__dlpack_device__",
30863086
(PyCFunction)array_dlpack_device,
30873087
METH_NOARGS, NULL},
3088-
{"bit_count",
3089-
(PyCFunction)array_bit_count,
3088+
{"bitwise_count",
3089+
(PyCFunction)array_bitwise_count,
30903090
METH_VARARGS | METH_KEYWORDS, NULL},
30913091
{NULL, NULL, 0, NULL} /* sentinel */
30923092
};

numpy/core/src/umath/loops.c.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ NPY_NO_EXPORT void
570570
}
571571

572572
NPY_NO_EXPORT void
573-
@TYPE@_bit_count(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func))
573+
@TYPE@_bitwise_count(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func))
574574
{
575575
UNARY_LOOP {
576576
const @type@ in1 = *(@type@ *)ip1;

numpy/core/src/umath/loops.h.src

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ NPY_NO_EXPORT void
203203
/**end repeat2**/
204204

205205
NPY_NO_EXPORT void
206-
@S@@TYPE@_bit_count(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func));
206+
@S@@TYPE@_bitwise_count(char **args, npy_intp const *dimensions, npy_intp const *steps, void *NPY_UNUSED(func));
207207

208208
/**end repeat1**/
209209
/**end repeat**/

numpy/core/tests/test_ufunc.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
UNARY_UFUNCS = [obj for obj in np.core.umath.__dict__.values()
2424
if isinstance(obj, np.ufunc)]
2525
UNARY_OBJECT_UFUNCS = [uf for uf in UNARY_UFUNCS if "O->O" in uf.types]
26+
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'bitwise_count'))
2627

2728

2829
class TestUfuncKwargs:

numpy/core/tests/test_umath.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ def assert_complex_equal(x, y):
10851085
assert_complex_equal(np.power(zero, 1+1j), zero)
10861086
assert_complex_equal(np.power(zero, 1+0j), zero)
10871087
assert_complex_equal(np.power(zero, 1-1j), zero)
1088-
#Complex powers will negative real part or 0 (provided imaginary
1088+
#Complex powers will negative real part or 0 (provided imaginary
10891089
# part is not zero) will generate a NAN and hence a RUNTIME warning
10901090
with pytest.warns(expected_warning=RuntimeWarning) as r:
10911091
assert_complex_equal(np.power(zero, -1+1j), cnan)
@@ -2481,24 +2481,24 @@ def test_reduction(self):
24812481
def test_popcount(self, input_dtype_obj, bitsize):
24822482
input_dtype = input_dtype_obj.type
24832483

2484-
# bit_count is only in-built in 3.10+
2484+
# bitwise_count is only in-built in 3.10+
24852485
if sys.version_info < (3, 10) and input_dtype == np.object_:
24862486
pytest.skip()
24872487

24882488
for i in range(1, bitsize):
24892489
num = 2**i - 1
2490-
msg = f"bit_count for {num}"
2491-
assert i == np.bit_count(input_dtype(num)), msg
2490+
msg = f"bitwise_count for {num}"
2491+
assert i == np.bitwise_count(input_dtype(num)), msg
24922492
if np.issubdtype(
24932493
input_dtype, np.signedinteger) or input_dtype == np.object_:
2494-
assert i == np.bit_count(input_dtype(-num)), msg
2494+
assert i == np.bitwise_count(input_dtype(-num)), msg
24952495

24962496
a = np.array([2**i-1 for i in range(1, bitsize)], dtype=input_dtype)
2497-
bit_count_a = np.bit_count(a)
2497+
bitwise_count_a = np.bitwise_count(a)
24982498
expected = np.arange(1, bitsize, dtype=input_dtype)
24992499

2500-
msg = f"array bit_count for {input_dtype}"
2501-
assert all(bit_count_a == expected), msg
2500+
msg = f"array bitwise_count for {input_dtype}"
2501+
assert all(bitwise_count_a == expected), msg
25022502

25032503

25042504
class TestInt:

numpy/core/tests/test_umath_accuracy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
isinstance(obj, np.ufunc)]
1313
UNARY_OBJECT_UFUNCS = [uf for uf in UNARY_UFUNCS if "O->O" in uf.types]
1414
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'invert'))
15-
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'bit_count'))
15+
UNARY_OBJECT_UFUNCS.remove(getattr(np, 'bitwise_count'))
1616

1717
IS_AVX = __cpu_features__.get('AVX512F', False) or \
1818
(__cpu_features__.get('FMA3', False) and __cpu_features__.get('AVX2', False))

numpy/core/umath.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
'UFUNC_PYVALS_NAME', '_add_newdoc_ufunc', 'absolute', 'add',
2323
'arccos', 'arccosh', 'arcsin', 'arcsinh', 'arctan', 'arctan2', 'arctanh',
2424
'bitwise_and', 'bitwise_or', 'bitwise_xor', 'cbrt', 'ceil', 'conj',
25-
'conjugate', 'copysign', 'cos', 'cosh', 'bit_count', 'deg2rad', 'degrees', 'divide',
25+
'conjugate', 'copysign', 'cos', 'cosh', 'bitwise_count', 'deg2rad', 'degrees', 'divide',
2626
'divmod', 'e', 'equal', 'euler_gamma', 'exp', 'exp2', 'expm1', 'fabs',
2727
'floor', 'floor_divide', 'float_power', 'fmax', 'fmin', 'fmod', 'frexp',
2828
'frompyfunc', 'gcd', 'geterrobj', 'greater', 'greater_equal', 'heaviside',

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', 'bit_count',
289+
'prod', 'std', 'ctypes', 'itemset', 'bitwise_count',
290290
]
291291
for attrib in dir(a):
292292
if attrib.startswith('_') or attrib in excluded_methods:

numpy/typing/tests/data/reveal/ufuncs.pyi

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ reveal_type(np.matmul.identity) # E: None
6868
reveal_type(np.matmul(AR_f8, AR_f8)) # E: Any
6969
reveal_type(np.matmul(AR_f8, AR_f8, axes=[(0, 1), (0, 1), (0, 1)])) # E: Any
7070

71-
reveal_type(np.bit_count.__name__) # E: Literal['bit_count']
72-
reveal_type(np.bit_count.ntypes) # E: Literal[11]
73-
reveal_type(np.bit_count.identity) # E: None
74-
reveal_type(np.bit_count.nin) # E: Literal[1]
75-
reveal_type(np.bit_count.nout) # E: Literal[1]
76-
reveal_type(np.bit_count.nargs) # E: Literal[2]
77-
reveal_type(np.bit_count.signature) # E: None
78-
reveal_type(np.bit_count.identity) # E: None
79-
reveal_type(np.bit_count(i8)) # E: Any
80-
reveal_type(np.bit_count(AR_i8)) # E: Any
71+
reveal_type(np.bitwise_count.__name__) # E: Literal['bitwise_count']
72+
reveal_type(np.bitwise_count.ntypes) # E: Literal[11]
73+
reveal_type(np.bitwise_count.identity) # E: None
74+
reveal_type(np.bitwise_count.nin) # E: Literal[1]
75+
reveal_type(np.bitwise_count.nout) # E: Literal[1]
76+
reveal_type(np.bitwise_count.nargs) # E: Literal[2]
77+
reveal_type(np.bitwise_count.signature) # E: None
78+
reveal_type(np.bitwise_count.identity) # E: None
79+
reveal_type(np.bitwise_count(i8)) # E: Any
80+
reveal_type(np.bitwise_count(AR_i8)) # E: Any

0 commit comments

Comments
 (0)