[Numpy-discussion] Re: Add a bit_width function

2025-03-06 Thread Sebastian Berg
Hi all,

I am not as such against adding more bit-wise functions.  But, I think
if we do, we probably need to discuss namespaces.
(because adding this is a slippery slope to also adding countr/countl,
etc. Unless we can just decide now to add all and those are actually
few.)

True, `bitwise_` naming convention is similar, but there is always
concern about the main namespace being too big to add functions that
don't have wide application.
(Or in other words maybe: Without that, there may need to be a strong
argument that this function will be used enough to be worthwhile.)

(There was some old discussion around this when we added
`bitwise_count`, but may be old enough to not be all that interesting
anymore.)


Also always good to mention that it is very easy to create a small
package with a family of bitwise functions outside of NumPy, like e.g.
`ufunclab` [1].
(And the numpy docs could link to it in the "See also" section for
example.)

- Sebastian


[1] https://github.com/WarrenWeckesser/ufunclab


On Thu, 2025-03-06 at 18:08 +, Carlos Martin wrote:
> Feature request: Add a `bit_width` function to NumPy's [bit-wise
> operations](
> https://numpy.org/doc/stable/reference/routines.bitwise.html) that
> computes the [bit-width](https://en.wikipedia.org/wiki/Bit-width)
> (also called bit-length) of an input.
> 
> For an example, see C++'s
> [`bit_width`](https://en.cppreference.com/w/cpp/numeric/bit_width)
> function.
> ___
> NumPy-Discussion mailing list -- numpy-discussion@python.org
> To unsubscribe send an email to numpy-discussion-le...@python.org
> https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
> Member address: sebast...@sipsolutions.net
> 

___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Add a bit_width function

2025-03-06 Thread Oscar Benjamin via NumPy-Discussion
On Thu, 6 Mar 2025 at 18:12, Carlos Martin  wrote:
>
> Feature request: Add a `bit_width` function to NumPy

In Python this is called bit_length:

 >>> (7).bit_length()
 3
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Add a bit_width function

2025-03-06 Thread Carlos Martin
Hi Stefan,

It should work on unsigned integer types and use exact computation (not 
floating-point computations like np.log2).

For an example, see 
https://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn.
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Add a bit_width function

2025-03-06 Thread Carlos Martin
Feature request: Add a `bit_width` function to NumPy's [bit-wise 
operations](https://numpy.org/doc/stable/reference/routines.bitwise.html) that 
computes the [bit-width](https://en.wikipedia.org/wiki/Bit-width) (also called 
bit-length) of an input.

For an example, see C++'s 
[`bit_width`](https://en.cppreference.com/w/cpp/numeric/bit_width) function.
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com


[Numpy-discussion] Re: Add a bit_width function

2025-03-06 Thread Stefan van der Walt via NumPy-Discussion
Hi Carlos,

On Thu, Mar 6, 2025, at 10:08, Carlos Martin wrote:
> Feature request: Add a `bit_width` function to NumPy's [bit-wise 
> operations](https://numpy.org/doc/stable/reference/routines.bitwise.html) 
> that computes the [bit-width](https://en.wikipedia.org/wiki/Bit-width) 
> (also called bit-length) of an input.

I'm curious, would this be roughly:

def bit_width(x):
y = np.zeros(len(x), dtype=float)
mask = (x != 0)
y[mask] = 1 + np.floor(np.log2(x[mask]))
return y

plus some error checking to ensure you don't run it on unsigned or floating 
point types?

Stéfan
___
NumPy-Discussion mailing list -- numpy-discussion@python.org
To unsubscribe send an email to numpy-discussion-le...@python.org
https://mail.python.org/mailman3/lists/numpy-discussion.python.org/
Member address: arch...@mail-archive.com