boofun.core.representations
- class boofun.core.representations.BooleanFunctionRepresentation[source]
Abstract base class for all Boolean function representations
- abstractmethod evaluate(inputs: ndarray, data: Any, space: Space, n_vars: int) ndarray[source]
Evaluate the function on given inputs using the provided data.
- Parameters:
inputs – Input values (binary array or boolean values)
data – Representation-specific data (coefficients, truth table, etc.)
- Returns:
Boolean result or array of results
- abstractmethod dump(data: DataType, space: Space = None, **kwargs) Dict[str, Any][source]
Export the representation data in a serializable format.
- Parameters:
data – The representation data to export
space – Optional space specification (some representations need this)
**kwargs – Representation-specific options
- Returns:
Dictionary containing the exported representation
- abstractmethod convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert from another representation to this representation.
- Parameters:
source_repr – Source representation strategy
source_data – Data in source format
**kwargs – Conversion options
- Returns:
Data in this representation’s format
- abstractmethod convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert from this representation to target representation.
- Parameters:
target_repr – Target representation strategy
data – Data in this representation’s format
**kwargs – Conversion options
- Returns:
Data in target representation’s format
- abstractmethod create_empty(n_vars: int, **kwargs) DataType[source]
Create empty representation data structure for n variables.
- abstractmethod is_complete(data: DataType) bool[source]
Check if the representation contains complete information.
- class boofun.core.representations.TruthTableRepresentation[source]
Truth table representation using NumPy arrays.
- evaluate(inputs: ndarray, data: ndarray, space: Space, n_vars: int) ndarray[source]
Evaluate the Boolean function using its truth table.
- Parameters:
inputs – Input values - can be: - Integer indices (0 to 2^n-1) - Binary vectors (shape: (n,) or (batch, n)) - Batch of integer indices (shape: (batch,))
data – Truth table as boolean array of length 2^n
space – Evaluation space (affects input interpretation)
n_vars – Number of Boolean variables
- Returns:
Boolean result(s) - scalar for single input, array for batch
- dump(data: ndarray, space=None, **kwargs) Dict[str, Any][source]
Export the truth table.
Returns a serializable dictionary containing: - ‘table’: list of booleans - ‘n_vars’: number of variables
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert from any representation by evaluating all possible inputs.
This is the universal converter - any representation can be converted to truth table by exhaustive evaluation.
- Parameters:
source_repr – Source representation strategy
source_data – Data in source format
space – Mathematical space
n_vars – Number of variables
**kwargs –
Additional options: - lenient (bool): If True, substitute False for failed evaluations
and emit a warning. Default is False (strict mode).
- Returns:
Truth table as boolean array
- Raises:
EvaluationError – If evaluation fails at any index (unless lenient=True)
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert truth table to another representation.
- create_empty(n_vars: int, **kwargs) ndarray[source]
Create an empty (all-False) truth table for n variables.
- get_storage_requirements(n_vars: int) Dict[str, int][source]
Storage grows exponentially: 1 byte per entry (packed to bits).
- class boofun.core.representations.FourierExpansionRepresentation[source]
Fourier expansion representation of Boolean functions
- evaluate(inputs: ndarray, data: ndarray, space: Space, n_vars: int) ndarray[source]
Evaluate the Fourier expansion at given inputs.
- Parameters:
inputs – Binary input vectors (shape: (m, n) or (n,))
data – Fourier coefficients (1D array of length 2**n)
- Returns:
Fourier expansion values (real numbers)
- dump(data: ndarray, space=None, **kwargs) Dict[str, Any][source]
Export Fourier coefficients in serializable format
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Compute Fourier coefficients from any evaluable Boolean representation. Fourier basis: {(-1)^{x·s}} for s ⊆ [n]
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert to another representation from Fourier expansion
- create_empty(n_vars: int, **kwargs) ndarray[source]
Create zero-initialized Fourier coefficients array
- class boofun.core.representations.SymbolicRepresentation[source]
Symbolic representation storing an expression and variable order.
Data format: (expression: str, variables: List[str])
- evaluate(inputs: ndarray, data: Tuple[str, List[Any]], space: Space, n_vars: int) ndarray[source]
Evaluate the symbolic Boolean expression composed of sub-BooleanFunctions using bit slicing.
Inputs are integers representing bitstrings. Only the last n_vars bits are used.
Each sub-function is evaluated on a slice of those bits, passed as an integer or array of integers.
- Parameters:
inputs – np.ndarray of shape () or (batch,) — each integer encodes a bitstring.
data – Tuple (expr: str, funcs: List[BooleanFunction]) — symbolic expression and sub-functions.
space – evaluation space.
kwargs – must include ‘n_vars’: total number of bits in each bitstring.
- Returns:
Single bool or np.ndarray of bools (same shape as inputs).
- issues:
is recomputing repeated variables
- dump(data: Tuple[str, List[str]], space=None, **kwargs) Dict[str, Any][source]
Export the symbolic representation as a dictionary.
- Returns:
- {
“expression”: expr, “variables”: vars
}
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) Tuple[str, List[Any]][source]
Convert from another representation to symbolic form.
Creates a symbolic expression that calls x0(…) etc. on the original function using wrapped variables.
- Parameters:
source_repr – The original representation class.
source_data – The original representation data.
space – The evaluation space (e.g. Boolean cube).
n_vars – Number of variables in the original function.
kwargs – Must include ‘functions’ — the BooleanFunction constructor/class.
- Returns:
Tuple[str, List[BooleanFunction]] — symbolic expression and subfunctions list.
- convert_to(target_repr: BooleanFunctionRepresentation, souce_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert to another representation from Fourier expansion
- create_empty(n_vars: int, **kwargs) Tuple[str, List[str]][source]
Create an empty symbolic representation: - Empty expression string - Default variable symbols [“x0”, “x1”, …, “x{n_vars-1}”]
- is_complete(data: Tuple[str, List[str]]) bool[source]
A symbolic representation is complete if the expression is non-empty.
- class boofun.core.representations.PolynomialRepresentation[source]
Polynomial (ANF) representation using coefficient dictionaries.
Represents Boolean functions as polynomials over GF(2): f(x₁,…,xₙ) = ⊕ᵢ aᵢ ∏ⱼ∈Sᵢ xⱼ
Data format: Dict[frozenset, int] mapping subsets to coefficients
- evaluate(inputs: ndarray, data: Dict[frozenset, int], space: Space, n_vars: int) bool | ndarray[source]
Evaluate the polynomial at given inputs.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – Polynomial coefficients as {subset: coefficient}
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- dump(data: Dict[frozenset, int], space=None, **kwargs) Dict[str, Any][source]
Export polynomial in serializable format.
- Returns:
Dictionary with monomials and coefficients
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) Dict[frozenset, int][source]
Convert from any representation to polynomial using truth table method.
Uses the Möbius transform to compute ANF coefficients.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert from polynomial to another representation.
- create_empty(n_vars: int, **kwargs) Dict[frozenset, int][source]
Create empty polynomial (constant 0).
- is_complete(data: Dict[frozenset, int]) bool[source]
Check if polynomial has any non-zero coefficients.
- time_complexity_rank(n_vars: int) Dict[str, int][source]
Return time complexity for polynomial operations.
- get_storage_requirements(n_vars: int) Dict[str, int][source]
Return storage requirements for polynomial representation.
- get_degree(data: Dict[frozenset, int]) int[source]
Get the degree of the polynomial (size of largest monomial).
- get_monomials(data: Dict[frozenset, int]) List[frozenset][source]
Get all monomials with non-zero coefficients.
- class boofun.core.representations.ANFRepresentation[source]
Algebraic Normal Form representation of Boolean functions.
Stores Boolean functions as sparse polynomials over GF(2) where: - Keys are frozensets representing variable subsets (monomials) - Values are coefficients in {0, 1}
Example: f(x0, x1) = x0 ⊕ x1 ⊕ x0*x1 Representation: {frozenset(): 0, frozenset({0}): 1, frozenset({1}): 1, frozenset({0,1}): 1}
- evaluate(inputs: ndarray, data: Dict[FrozenSet[int], int], space: Space, n_vars: int) bool | ndarray[source]
Evaluate ANF polynomial at given inputs.
- Parameters:
inputs – Input values - can be integer indices or binary vectors
data – ANF coefficients as {monomial: coefficient} dict
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- dump(data: Dict[FrozenSet[int], int], space: Space, **kwargs) Dict[str, Any][source]
Export ANF in serializable format.
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) Dict[FrozenSet[int], int][source]
Convert from another representation to ANF.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Dict[FrozenSet[int], int], space: Space, n_vars: int, **kwargs) Any[source]
Convert from ANF to another representation.
- create_empty(n_vars: int, **kwargs) Dict[FrozenSet[int], int][source]
Create empty ANF (zero function).
- time_complexity_rank(n_vars: int) Dict[str, int][source]
Return time complexity estimates for ANF operations.
- get_storage_requirements(n_vars: int) Dict[str, int][source]
Return memory requirements for ANF representation.
- get_monomials(data: Dict[FrozenSet[int], int]) List[FrozenSet[int]][source]
Get all monomials with non-zero coefficients.
- get_degree_k_terms(data: Dict[FrozenSet[int], int], k: int) Dict[FrozenSet[int], int][source]
Get all terms of exactly degree k.
- class boofun.core.representations.BDDRepresentation[source]
BDD representation for Boolean functions.
- evaluate(inputs: ndarray, data: BDD, space: Space, n_vars: int) bool | ndarray[source]
Evaluate BDD representation.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – BDD
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) BDD[source]
Convert from another representation to BDD.
Uses truth table to build BDD.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert BDD to another representation.
- class boofun.core.representations.CircuitRepresentation[source]
Circuit representation for Boolean functions.
- evaluate(inputs: ndarray, data: BooleanCircuit, space: Space, n_vars: int) bool | ndarray[source]
Evaluate circuit representation.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – Boolean circuit
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- dump(data: BooleanCircuit, space=None, **kwargs) Dict[str, Any][source]
Export circuit representation.
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) BooleanCircuit[source]
Convert from another representation to circuit.
Uses a simple DNF (Disjunctive Normal Form) construction approach.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert circuit to another representation.
- create_empty(n_vars: int, **kwargs) BooleanCircuit[source]
Create empty circuit (constant False).
- is_complete(data: BooleanCircuit) bool[source]
Check if circuit is complete (has output gate).
- time_complexity_rank(n_vars: int) Dict[str, int][source]
Return time complexity for circuit operations.
- get_storage_requirements(n_vars: int) Dict[str, int][source]
Return storage requirements for circuit representation.
- optimize_circuit(circuit: BooleanCircuit) BooleanCircuit[source]
Apply basic circuit optimizations.
- Parameters:
circuit – Circuit to optimize
- Returns:
Optimized circuit
- class boofun.core.representations.SparseTruthTableRepresentation(compression_threshold: float = 0.1)[source]
Sparse truth table representation for memory efficiency.
Stores only non-default values to save memory for large functions. Data format: {
‘default_value’: bool, # Value for unspecified indices ‘exceptions’: Dict[int, bool], # Indices with non-default values ‘n_vars’: int, # Number of variables ‘size’: int # Total size (2^n_vars)
}
- __init__(compression_threshold: float = 0.1)[source]
Initialize sparse representation.
- Parameters:
compression_threshold – Use sparse format if non-default ratio < threshold
- evaluate(inputs: ndarray, data: Dict[str, Any], space: Space, n_vars: int) bool | ndarray[source]
Evaluate the sparse truth table at given inputs.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – Sparse truth table data
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- dump(data: Dict[str, Any], space=None, **kwargs) Dict[str, Any][source]
Export sparse truth table in serializable format.
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) Dict[str, Any][source]
Convert from any representation to sparse truth table.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert sparse truth table to another representation.
- create_empty(n_vars: int, **kwargs) Dict[str, Any][source]
Create empty sparse truth table (all False).
- time_complexity_rank(n_vars: int) Dict[str, int][source]
Return time complexity for sparse operations.
- class boofun.core.representations.LTFRepresentation[source]
Linear Threshold Function representation for Boolean functions.
- evaluate(inputs: ndarray, data: LTFParameters, space: Space, n_vars: int) bool | ndarray[source]
Evaluate LTF representation.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – LTF parameters
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) LTFParameters[source]
Convert from another representation to LTF.
Uses linear programming to find weights and threshold. Note: Not all Boolean functions can be represented as LTFs.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert LTF to another representation.
- create_empty(n_vars: int, **kwargs) LTFParameters[source]
Create empty LTF (constant False).
- is_complete(data: LTFParameters) bool[source]
Check if LTF is complete (has valid parameters).
- class boofun.core.representations.DNFRepresentation[source]
DNF representation for Boolean functions.
- evaluate(inputs: ndarray, data: DNFFormula, space: Space, n_vars: int) bool | ndarray[source]
Evaluate DNF representation.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – DNF formula
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) DNFFormula[source]
Convert from another representation to DNF.
Uses truth table method to generate minterms.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert DNF to another representation.
- create_empty(n_vars: int, **kwargs) DNFFormula[source]
Create empty DNF (constant False).
- is_complete(data: DNFFormula) bool[source]
Check if DNF is complete.
- class boofun.core.representations.CNFRepresentation[source]
CNF representation for Boolean functions.
- evaluate(inputs: ndarray, data: CNFFormula, space: Space, n_vars: int) bool | ndarray[source]
Evaluate CNF representation.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – CNF formula
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) CNFFormula[source]
Convert from another representation to CNF.
Uses truth table method to generate maxterms.
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert CNF to another representation.
- create_empty(n_vars: int, **kwargs) CNFFormula[source]
Create empty CNF (constant True).
- is_complete(data: CNFFormula) bool[source]
Check if CNF is complete.
- class boofun.core.representations.DistributionRepresentation[source]
Distribution representation for Boolean functions.
- evaluate(inputs: ndarray, data: BooleanDistribution, space: Space, n_vars: int) bool | ndarray[source]
Evaluate distribution representation.
- Parameters:
inputs – Input values (integer indices or binary vectors)
data – Boolean distribution
space – Evaluation space
n_vars – Number of variables
- Returns:
Boolean result(s)
- dump(data: BooleanDistribution, space=None, **kwargs) Dict[str, Any][source]
Export distribution representation.
- convert_from(source_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) BooleanDistribution[source]
Convert from another representation to distribution.
- Parameters:
source_repr – Source representation
source_data – Source data
space – Evaluation space
n_vars – Number of variables
**kwargs – Additional arguments (distribution_type, bias)
- Returns:
Boolean distribution
- convert_to(target_repr: BooleanFunctionRepresentation, source_data: Any, space: Space, n_vars: int, **kwargs) ndarray[source]
Convert distribution to another representation.
- create_empty(n_vars: int, **kwargs) BooleanDistribution[source]
Create empty distribution (constant False with uniform input).
- is_complete(data: BooleanDistribution) bool[source]
Check if distribution is complete.
Modules
Algebraic Normal Form (ANF) representation for Boolean functions. |
|
Binary Decision Diagram (BDD) representation for Boolean functions. |
|
Circuit representation for Boolean functions. |
|
Conjunctive Normal Form (CNF) representation for Boolean functions. |
|
Distribution representation for Boolean functions. |
|
Disjunctive Normal Form (DNF) representation for Boolean functions. |
|
Linear Threshold Function (LTF) representation for Boolean functions. |
|
Packed truth table representation using bitarray for memory efficiency. |
|
Polynomial (ANF) representation for Boolean functions. |
|
Sparse truth table representation for Boolean functions. |
|