boofun.core.conversion_graph

Lazy conversion graph system for Boolean function representations.

This module implements the conversion graph mentioned in the design document, enabling intelligent conversion between representations with optimal paths and caching for efficiency.

Functions

estimate_conversion_cost(source, target[, ...])

Estimate the cost of converting between representations.

find_conversion_path(source, target[, n_vars])

Find optimal conversion path between representations.

get_conversion_graph()

Get the global conversion graph instance.

get_conversion_options(source[, max_cost])

Get all conversion options from a source representation.

register_custom_conversion(source, target, ...)

Register a custom conversion between representations.

Classes

ConversionCost(time_complexity, space_complexity)

Represents the cost of converting between two representations.

ConversionEdge(source, target, cost[, converter])

Represents an edge in the conversion graph.

ConversionGraph()

Manages the graph of possible conversions between representations.

ConversionPath(edges)

Represents a complete conversion path between representations.

class boofun.core.conversion_graph.ConversionCost(time_complexity: float, space_complexity: float, accuracy_loss: float = 0.0, is_exact: bool = True)[source]

Represents the cost of converting between two representations.

Includes time complexity, space complexity, and accuracy considerations.

__init__(time_complexity: float, space_complexity: float, accuracy_loss: float = 0.0, is_exact: bool = True)[source]

Initialize conversion cost.

Parameters:
  • time_complexity – Time cost (higher = more expensive)

  • space_complexity – Space cost (higher = more expensive)

  • accuracy_loss – Information loss (0 = lossless, 1 = complete loss)

  • is_exact – Whether conversion is mathematically exact

class boofun.core.conversion_graph.ConversionEdge(source: str, target: str, cost: ConversionCost, converter: Callable | None = None)[source]

Represents an edge in the conversion graph.

__init__(source: str, target: str, cost: ConversionCost, converter: Callable | None = None)[source]

Initialize conversion edge.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • cost – Conversion cost

  • converter – Optional custom conversion function

class boofun.core.conversion_graph.ConversionPath(edges: List[ConversionEdge])[source]

Represents a complete conversion path between representations.

__init__(edges: List[ConversionEdge])[source]

Initialize conversion path.

Parameters:

edges – List of conversion edges forming the path

execute(data: Any, space: Space, n_vars: int) Any[source]

Execute the conversion path.

Parameters:
  • data – Source data

  • space – Mathematical space

  • n_vars – Number of variables

Returns:

Converted data

class boofun.core.conversion_graph.ConversionGraph[source]

Manages the graph of possible conversions between representations.

Implements Dijkstra’s algorithm to find optimal conversion paths and caches results for efficiency.

__init__()[source]

Initialize conversion graph.

add_edge(source: str, target: str, cost: ConversionCost, converter: Callable | None = None)[source]

Add a conversion edge to the graph.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • cost – Conversion cost

  • converter – Optional custom conversion function

find_optimal_path(source: str, target: str, n_vars: int | None = None) ConversionPath | None[source]

Find the optimal conversion path using Dijkstra’s algorithm.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • n_vars – Number of variables (affects cost calculations)

Returns:

Optimal conversion path or None if no path exists

get_conversion_options(source: str, max_cost: float | None = None) Dict[str, ConversionPath][source]

Get all possible conversion targets from a source representation.

Parameters:
  • source – Source representation name

  • max_cost – Maximum acceptable cost (optional)

Returns:

Dictionary mapping target representations to optimal paths

estimate_conversion_cost(source: str, target: str, n_vars: int | None = None) ConversionCost | None[source]

Estimate conversion cost without finding the full path.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • n_vars – Number of variables

Returns:

Estimated conversion cost or None if no path exists

clear_cache()[source]

Clear the path cache.

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

Get statistics about the conversion graph.

visualize_graph(output_format: str = 'text') str[source]

Create a text visualization of the conversion graph.

Parameters:

output_format – Output format (‘text’ or ‘dot’)

Returns:

String representation of the graph

boofun.core.conversion_graph.get_conversion_graph() ConversionGraph[source]

Get the global conversion graph instance.

boofun.core.conversion_graph.find_conversion_path(source: str, target: str, n_vars: int | None = None) ConversionPath | None[source]

Find optimal conversion path between representations.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • n_vars – Number of variables

Returns:

Optimal conversion path or None

boofun.core.conversion_graph.register_custom_conversion(source: str, target: str, cost: ConversionCost, converter: Callable)[source]

Register a custom conversion between representations.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • cost – Conversion cost

  • converter – Conversion function

boofun.core.conversion_graph.get_conversion_options(source: str, max_cost: float | None = None) Dict[str, ConversionPath][source]

Get all conversion options from a source representation.

Parameters:
  • source – Source representation name

  • max_cost – Maximum acceptable cost

Returns:

Dictionary of conversion options

boofun.core.conversion_graph.estimate_conversion_cost(source: str, target: str, n_vars: int | None = None) ConversionCost | None[source]

Estimate the cost of converting between representations.

Parameters:
  • source – Source representation name

  • target – Target representation name

  • n_vars – Number of variables

Returns:

Estimated conversion cost