boofun.utils.math

Lightweight math helpers shared across BooFun modules.

Functions

binary_tuple_to_int(bits)

Convert binary tuple (LSB first) to integer.

bits(i, n)

Return n bits of i as a list (MSB first).

cartesian(seqs)

Yield cartesian product rows from sequences.

generate_permutations(n)

Generate all permutations of [0, 1, ..., n-1].

hamming_distance(x, y)

Compute Hamming distance between two integers (number of differing bits).

hamming_weight(x)

Alias for popcnt - number of 1 bits in x.

int_to_binary_tuple(x, n)

Convert integer x to an n-bit binary tuple (LSB first).

krawchouk(n, k, x)

Classical binary Krawchouk polynomial K_k(x; n).

krawchouk2(n, k, x)

Legacy variant with (-2)^j weights (kept for completeness).

num2bin_list(num, n_digits)

Convert num to an n-digit binary list (MSB first).

over(n, k)

Safe binomial coefficient with bounds guarding.

popcnt(x)

Return the population count of an integer.

poppar(x)

Return the parity of the population count.

subsets(a[, k])

Yield subsets of sequence a (optionally fixed size).

tensor_product(A, B)

Compute the Kronecker product of A and B.

boofun.utils.math.popcnt(x: int) int[source]

Return the population count of an integer.

boofun.utils.math.poppar(x: int) int[source]

Return the parity of the population count.

boofun.utils.math.over(n: int, k: int) int[source]

Safe binomial coefficient with bounds guarding.

boofun.utils.math.subsets(a: Sequence[int] | int, k: int | None = None) Iterator[Tuple[int, ...]][source]

Yield subsets of sequence a (optionally fixed size).

boofun.utils.math.cartesian(seqs: Sequence[Sequence]) Iterator[Tuple][source]

Yield cartesian product rows from sequences.

boofun.utils.math.num2bin_list(num: int, n_digits: int) List[int][source]

Convert num to an n-digit binary list (MSB first).

boofun.utils.math.bits(i: int, n: int) List[int][source]

Return n bits of i as a list (MSB first).

boofun.utils.math.tensor_product(A: ndarray | Sequence, B: ndarray | Sequence) ndarray[source]

Compute the Kronecker product of A and B.

boofun.utils.math.krawchouk(n: int, k: int, x: int) int[source]

Classical binary Krawchouk polynomial K_k(x; n).

boofun.utils.math.krawchouk2(n: int, k: int, x: int) int[source]

Legacy variant with (-2)^j weights (kept for completeness).

boofun.utils.math.hamming_distance(x: int, y: int) int[source]

Compute Hamming distance between two integers (number of differing bits).

boofun.utils.math.hamming_weight(x: int) int[source]

Alias for popcnt - number of 1 bits in x.

boofun.utils.math.generate_permutations(n: int) Iterator[Tuple[int, ...]][source]

Generate all permutations of [0, 1, …, n-1].

boofun.utils.math.int_to_binary_tuple(x: int, n: int) Tuple[int, ...][source]

Convert integer x to an n-bit binary tuple (LSB first).

Parameters:
  • x – Integer to convert

  • n – Number of bits

Returns:

Tuple of n bits, e.g., (0, 1, 1) for x=6 with n=3

boofun.utils.math.binary_tuple_to_int(bits: Sequence[int]) int[source]

Convert binary tuple (LSB first) to integer.

Parameters:

bits – Sequence of bits (0 or 1)

Returns:

Integer value