Skip to main content

ComputationNode

Enum ComputationNode 

Source
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

Fields

§id: Uuid
§sample: Arc<dyn Fn() -> T + Send + Sync>
§

BinaryOp

Binary operation node for combining two uncertain values

Fields

§

UnaryOp

Unary operation node for transforming a single uncertain value

Fields

§operand: Box<ComputationNode<T>>
§operation: UnaryOperation<T>
§

Conditional

Conditional node for if-then-else logic

Fields

§if_true: Box<ComputationNode<T>>
§if_false: Box<ComputationNode<T>>

Implementations§

Source§

impl<T> ComputationNode<T>
where T: Shareable,

Source

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 BinaryOp variant. Use evaluate_arithmetic instead for binary operations.
  • Panics if called on a Conditional variant. Use evaluate_conditional instead for conditional operations.
Source

pub fn evaluate_arithmetic(&self, context: &mut SampleContext) -> T
where 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.

Source

pub fn evaluate_fresh(&self) -> T
where 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.

Source

pub fn leaf<F>(sample: F) -> Self
where F: Fn() -> T + Send + Sync + 'static,

Creates a new leaf node

Source

pub fn binary_op( left: ComputationNode<T>, right: ComputationNode<T>, operation: BinaryOperation, ) -> Self

Creates a new binary operation node

Source

pub fn map<F>(operand: ComputationNode<T>, func: F) -> Self
where F: Fn(T) -> T + Send + Sync + 'static,

Creates a new unary map operation node

Source

pub fn conditional( condition: ComputationNode<bool>, if_true: ComputationNode<T>, if_false: ComputationNode<T>, ) -> Self

Creates a conditional node

Source

pub fn node_count(&self) -> usize

Counts the number of nodes in the computation graph

Source

pub fn depth(&self) -> usize

Gets the depth of the computation graph

Source

pub fn has_conditionals(&self) -> bool

Checks if the computation graph contains any conditional nodes

Source

pub fn compute_complexity(&self) -> usize

Estimate computational complexity of the node for caching decisions

Source

pub fn structural_hash(&self) -> u64

Generate a structural hash for computation graph caching

Source§

impl ComputationNode<bool>

Source

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,

Source

pub fn evaluate_conditional_with_arithmetic( &self, context: &mut SampleContext, ) -> T
where T: Arithmetic,

Evaluates conditional nodes where condition is bool and branches return T

Trait Implementations§

Source§

impl<T: Clone> Clone for ComputationNode<T>

Source§

fn clone(&self) -> ComputationNode<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Shareable for T
where T: Clone + Send + Sync + 'static,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V