boofun.core.auto_representation

Automatic representation selection for Boolean functions.

This module provides intelligent auto-selection of the most appropriate representation based on: - Number of variables (n) - Sparsity of the function - Memory constraints - Access patterns

Guidelines: - n <= 14: Use dense truth table (fast, fits in cache) - n > 14, sparse: Use sparse truth table - n > 14, dense: Use packed truth table (bitarray) - n > 20: Consider sparse Fourier or symbolic

Functions

auto_select_representation(truth_table[, ...])

Automatically select the best representation for a truth table.

estimate_sparsity(truth_table)

Estimate the sparsity of a truth table.

optimize_representation(f)

Analyze a BooleanFunction and recommend optimal representation.

recommend_representation(n_vars[, sparsity, ...])

Recommend the best representation for given constraints.

Classes

AdaptiveFunction(truth_table[, n_vars, ...])

A Boolean function wrapper that automatically uses the best representation.

boofun.core.auto_representation.estimate_sparsity(truth_table: ndarray) float[source]

Estimate the sparsity of a truth table.

Sparsity = min(ones_ratio, zeros_ratio) Low sparsity means the function is mostly 0s or mostly 1s.

Parameters:

truth_table – Boolean array

Returns:

Sparsity ratio (0 = completely sparse, 0.5 = balanced)

boofun.core.auto_representation.recommend_representation(n_vars: int, sparsity: float | None = None, memory_limit_mb: float | None = None, access_pattern: str = 'random') Dict[str, Any][source]

Recommend the best representation for given constraints.

Parameters:
  • n_vars – Number of variables

  • sparsity – Estimated sparsity (if known)

  • memory_limit_mb – Maximum memory in MB (optional)

  • access_pattern – “random”, “sequential”, or “sparse_queries”

Returns:

Dictionary with recommendation and reasoning

boofun.core.auto_representation.auto_select_representation(truth_table: ndarray, n_vars: int | None = None, memory_limit_mb: float | None = None) Dict[str, Any][source]

Automatically select the best representation for a truth table.

Parameters:
  • truth_table – Boolean array

  • n_vars – Number of variables (computed if not provided)

  • memory_limit_mb – Maximum memory in MB

Returns:

Dictionary with selected representation and converted data

class boofun.core.auto_representation.AdaptiveFunction(truth_table: ndarray, n_vars: int | None = None, memory_limit_mb: float | None = None, force_representation: str | None = None)[source]

A Boolean function wrapper that automatically uses the best representation.

This class analyzes the function and picks the optimal storage format, transparently handling the conversion.

__init__(truth_table: ndarray, n_vars: int | None = None, memory_limit_mb: float | None = None, force_representation: str | None = None)[source]

Initialize with automatic representation selection.

Parameters:
  • truth_table – Boolean array

  • n_vars – Number of variables

  • memory_limit_mb – Maximum memory

  • force_representation – Override auto-selection (“dense”, “packed”, “sparse”)

evaluate(x: int) bool[source]

Evaluate the function at x.

to_dense() ndarray[source]

Convert to dense numpy array.

property format: str

Get current storage format.

property sparsity: float

Get function sparsity.

memory_usage() Dict[str, Any][source]

Get memory usage statistics.

summary() str[source]

Get summary of the adaptive function.

boofun.core.auto_representation.optimize_representation(f: BooleanFunction) Dict[str, Any][source]

Analyze a BooleanFunction and recommend optimal representation.

Parameters:

f – BooleanFunction to analyze

Returns:

Recommendation dictionary