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

BEST_INFLUENCES(fourier_coeffs, n_vars)

Use Numba-accelerated influence computation if available.

BEST_WHT(values)

Use Numba-accelerated WHT if available.

batch_evaluate(f, inputs)

Efficiently evaluate a Boolean function on a batch of inputs.

cached_computation(computation_name)

Decorator for caching expensive computations on BooleanFunction.

fast_walsh_hadamard(values[, normalize])

Fast in-place Walsh-Hadamard Transform.

fast_walsh_hadamard_numba(values)

Use Numba-accelerated WHT if available.

get_best_wht_implementation()

Get the best available Walsh-Hadamard Transform implementation.

get_global_cache()

Get the global computation cache.

memoize_method(func)

Simple memoization for instance methods.

noise_stability_from_fourier(fourier_coeffs, rho)

Compute noise stability from Fourier coefficients.

parallel_batch_fourier(functions[, max_workers])

Compute Fourier coefficients for multiple Boolean functions in parallel.

parallel_batch_influences(functions[, ...])

Compute influences for multiple Boolean functions in parallel.

vectorized_influences_from_fourier(...)

Compute influences from Fourier coefficients using vectorization.

vectorized_influences_numba(fourier_coeffs, ...)

Use Numba-accelerated influence computation if available.

vectorized_total_influence_from_fourier(...)

Compute total influence from Fourier coefficients.

vectorized_total_influence_numba(fourier_coeffs)

Use Numba-accelerated total influence if available.

vectorized_truth_table_to_pm(truth_table)

Convert {0,1} truth table to {-1,+1} representation.

Classes

ComputeCache([max_size])

LRU cache for expensive Boolean function computations.

LazyFourierCoefficients(compute_func)

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.

__init__(compute_func: Callable[[], ndarray])[source]
Parameters:

compute_func – Function that computes the Fourier coefficients

get() ndarray[source]

Get coefficients, computing if necessary.

is_computed() bool[source]

Check if coefficients have been computed.

clear()[source]

Clear cached coefficients.

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

get(func_hash: str, computation: str, *args) Tuple[bool, Any][source]

Try to get cached result.

Returns:

Tuple of (found, value)

put(func_hash: str, computation: str, value: Any, *args)[source]

Store result in cache.

clear()[source]

Clear all cached entries.

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

Get cache statistics.

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):

boofun.core.optimizations.memoize_method(func)[source]

Simple memoization for instance methods.

Caches results in the instance’s __dict__.

boofun.core.optimizations.batch_evaluate(f, inputs: ndarray) ndarray[source]

Efficiently evaluate a Boolean function on a batch of inputs.

Parameters:
  • f – BooleanFunction

  • inputs – 2D array of shape (batch_size, n_vars) or 1D array of indices

Returns:

Boolean array of results