boofun.core.optimizations
Performance Optimizations for BooFun
This module provides optimized implementations of critical operations: 1. Fast Walsh-Hadamard Transform (in-place, vectorized) 2. Vectorized influence computation 3. Lazy evaluation helpers
These optimizations are automatically used when available.
Functions
|
Use Numba-accelerated influence computation if available. |
|
Use Numba-accelerated WHT if available. |
|
Efficiently evaluate a Boolean function on a batch of inputs. |
|
Decorator for caching expensive computations on BooleanFunction. |
|
Fast in-place Walsh-Hadamard Transform. |
|
Use Numba-accelerated WHT if available. |
Get the best available Walsh-Hadamard Transform implementation. |
|
Get the global computation cache. |
|
|
Simple memoization for instance methods. |
|
Compute noise stability from Fourier coefficients. |
|
Compute Fourier coefficients for multiple Boolean functions in parallel. |
|
Compute influences for multiple Boolean functions in parallel. |
Compute influences from Fourier coefficients using vectorization. |
|
|
Use Numba-accelerated influence computation if available. |
Compute total influence from Fourier coefficients. |
|
|
Use Numba-accelerated total influence if available. |
|
Convert {0,1} truth table to {-1,+1} representation. |
Classes
|
LRU cache for expensive Boolean function computations. |
|
Lazy wrapper for Fourier coefficients. |
- boofun.core.optimizations.fast_walsh_hadamard(values: ndarray, normalize: bool = True) ndarray[source]
Fast in-place Walsh-Hadamard Transform.
This is the optimized O(n * 2^n) algorithm using butterfly operations. Works in-place to minimize memory allocations.
- Parameters:
values – Input array of length 2^n (will be modified in-place)
normalize – If True, divide by 2^n at the end
- Returns:
Transformed array (same object as input if in-place)
- boofun.core.optimizations.fast_walsh_hadamard_numba(values: ndarray) ndarray[source]
Use Numba-accelerated WHT if available.
- boofun.core.optimizations.vectorized_truth_table_to_pm(truth_table: ndarray) ndarray[source]
Convert {0,1} truth table to {-1,+1} representation.
This is vectorized for speed.
- boofun.core.optimizations.vectorized_influences_from_fourier(fourier_coeffs: ndarray, n_vars: int) ndarray[source]
Compute influences from Fourier coefficients using vectorization.
Influence of variable i: Inf_i(f) = Σ_{S∋i} f̂(S)²
This is faster than iterating over all subsets for each variable.
- boofun.core.optimizations.vectorized_influences_numba(fourier_coeffs: ndarray, n_vars: int) ndarray[source]
Use Numba-accelerated influence computation if available.
- boofun.core.optimizations.vectorized_total_influence_from_fourier(fourier_coeffs: ndarray, n_vars: int) float[source]
Compute total influence from Fourier coefficients.
Total influence = Σ_S |S| · f̂(S)² = Σ_i Inf_i(f)
Uses the formula: I[f] = Σ_S |S| · f̂(S)²
- boofun.core.optimizations.vectorized_total_influence_numba(fourier_coeffs: ndarray) float[source]
Use Numba-accelerated total influence if available.
- boofun.core.optimizations.noise_stability_from_fourier(fourier_coeffs: ndarray, rho: float) float[source]
Compute noise stability from Fourier coefficients.
Stab_ρ[f] = Σ_S ρ^|S| · f̂(S)²
- class boofun.core.optimizations.LazyFourierCoefficients(compute_func: Callable[[], ndarray])[source]
Lazy wrapper for Fourier coefficients.
Delays computation until coefficients are actually needed, and caches the result.
- boofun.core.optimizations.get_best_wht_implementation()[source]
Get the best available Walsh-Hadamard Transform implementation.
Returns tuple of (function, name) where function is the WHT implementation and name is a description.
- boofun.core.optimizations.BEST_WHT(values: ndarray) ndarray
Use Numba-accelerated WHT if available.
- boofun.core.optimizations.BEST_INFLUENCES(fourier_coeffs: ndarray, n_vars: int) ndarray
Use Numba-accelerated influence computation if available.
- boofun.core.optimizations.parallel_batch_influences(functions: list, max_workers: int | None = None, use_threads: bool = True) list[source]
Compute influences for multiple Boolean functions in parallel.
- Parameters:
functions – List of BooleanFunction objects
max_workers – Number of parallel workers (default: CPU count)
use_threads – Use threads instead of processes (faster for small tasks)
- Returns:
List of influence arrays
- boofun.core.optimizations.parallel_batch_fourier(functions: list, max_workers: int | None = None) list[source]
Compute Fourier coefficients for multiple Boolean functions in parallel.
- Parameters:
functions – List of BooleanFunction objects
max_workers – Number of parallel workers
- Returns:
List of Fourier coefficient arrays
- class boofun.core.optimizations.ComputeCache(max_size: int = 1000)[source]
LRU cache for expensive Boolean function computations.
Caches results keyed by function hash and computation type. Automatically evicts least-recently-used entries when full.
- __init__(max_size: int = 1000)[source]
Initialize cache.
- Parameters:
max_size – Maximum number of cached entries
- boofun.core.optimizations.get_global_cache() ComputeCache[source]
Get the global computation cache.
- boofun.core.optimizations.cached_computation(computation_name: str)[source]
Decorator for caching expensive computations on BooleanFunction.
- Usage:
@cached_computation(“influences”) def compute_influences(self):
…