pub enum ComputationNode<T> {
Leaf {
id: Uuid,
sample: Arc<dyn Fn() -> T + Send + Sync>,
},
BinaryOp {
left: Box<ComputationNode<T>>,
right: Box<ComputationNode<T>>,
operation: BinaryOperation,
},
UnaryOp {
operand: Box<ComputationNode<T>>,
operation: UnaryOperation<T>,
},
Conditional {
condition: Box<ComputationNode<bool>>,
if_true: Box<ComputationNode<T>>,
if_false: Box<ComputationNode<T>>,
},
}Expand description
Computation graph node for lazy evaluation using indirect enum
This enables building complex expressions like (x + y) * 2.0 - z as a computation
graph that’s only evaluated when samples are needed, with proper memoization to
ensure shared variables use the same sample within a single evaluation.
Variants§
Leaf
Leaf node representing a direct sampling function with unique ID
BinaryOp
Binary operation node for combining two uncertain values
UnaryOp
Unary operation node for transforming a single uncertain value
Conditional
Conditional node for if-then-else logic
Fields
condition: Box<ComputationNode<bool>>if_true: Box<ComputationNode<T>>if_false: Box<ComputationNode<T>>Implementations§
Source§impl<T> ComputationNode<T>where
T: Shareable,
impl<T> ComputationNode<T>where
T: Shareable,
Sourcepub fn evaluate(&self, context: &mut SampleContext) -> T
pub fn evaluate(&self, context: &mut SampleContext) -> T
Evaluates the computation graph node with memoization context
This is the core evaluation method that respects memoization to ensure shared variables produce consistent samples within a single evaluation.
§Panics
- Panics if called on a
BinaryOpvariant. Useevaluate_arithmeticinstead for binary operations. - Panics if called on a
Conditionalvariant. Useevaluate_conditionalinstead for conditional operations.
Sourcepub fn evaluate_arithmetic(&self, context: &mut SampleContext) -> Twhere
T: Arithmetic,
pub fn evaluate_arithmetic(&self, context: &mut SampleContext) -> Twhere
T: Arithmetic,
Evaluates arithmetic operations with proper trait bounds
§Panics
Panics if called on a Conditional variant with a boolean condition, as this is not supported in arithmetic context.
Sourcepub fn evaluate_fresh(&self) -> Twhere
T: Arithmetic,
pub fn evaluate_fresh(&self) -> Twhere
T: Arithmetic,
Evaluates the computation graph node in a new context
This creates a fresh context for evaluation, useful when you want independent samples without memoization effects.
Sourcepub fn binary_op(
left: ComputationNode<T>,
right: ComputationNode<T>,
operation: BinaryOperation,
) -> Self
pub fn binary_op( left: ComputationNode<T>, right: ComputationNode<T>, operation: BinaryOperation, ) -> Self
Creates a new binary operation node
Sourcepub fn map<F>(operand: ComputationNode<T>, func: F) -> Self
pub fn map<F>(operand: ComputationNode<T>, func: F) -> Self
Creates a new unary map operation node
Sourcepub fn conditional(
condition: ComputationNode<bool>,
if_true: ComputationNode<T>,
if_false: ComputationNode<T>,
) -> Self
pub fn conditional( condition: ComputationNode<bool>, if_true: ComputationNode<T>, if_false: ComputationNode<T>, ) -> Self
Creates a conditional node
Sourcepub fn node_count(&self) -> usize
pub fn node_count(&self) -> usize
Counts the number of nodes in the computation graph
Sourcepub fn has_conditionals(&self) -> bool
pub fn has_conditionals(&self) -> bool
Checks if the computation graph contains any conditional nodes
Sourcepub fn compute_complexity(&self) -> usize
pub fn compute_complexity(&self) -> usize
Estimate computational complexity of the node for caching decisions
Sourcepub fn structural_hash(&self) -> u64
pub fn structural_hash(&self) -> u64
Generate a structural hash for computation graph caching
Source§impl ComputationNode<bool>
impl ComputationNode<bool>
Sourcepub fn evaluate_bool(&self, context: &mut SampleContext) -> bool
pub fn evaluate_bool(&self, context: &mut SampleContext) -> bool
Evaluates boolean computation nodes
§Panics
Panics if called on a BinaryOp variant as boolean binary operations are not implemented.
Source§impl<T> ComputationNode<T>where
T: Shareable,
impl<T> ComputationNode<T>where
T: Shareable,
Sourcepub fn evaluate_conditional_with_arithmetic(
&self,
context: &mut SampleContext,
) -> Twhere
T: Arithmetic,
pub fn evaluate_conditional_with_arithmetic(
&self,
context: &mut SampleContext,
) -> Twhere
T: Arithmetic,
Evaluates conditional nodes where condition is bool and branches return T
Trait Implementations§
Source§impl<T: Clone> Clone for ComputationNode<T>
impl<T: Clone> Clone for ComputationNode<T>
Source§fn clone(&self) -> ComputationNode<T>
fn clone(&self) -> ComputationNode<T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more