Skip to main content

more_math/
more_math.rs

1//! Complex math example demonstrating the new Operation-based API
2//!
3//! This example shows:
4//! - Multiple mathematical operations as separate structs
5//! - Complex dependency chains
6//! - Quadratic formula computation
7//! - Distance calculations
8
9use neal::{DerivedNode, Graph, GraphError, InputNode, Operation};
10use std::hash::{Hash, Hasher};
11
12// Value type for numbers
13#[derive(Clone, PartialEq, Debug, Default)]
14struct Number(f64);
15
16impl Hash for Number {
17    fn hash<H: Hasher>(&self, state: &mut H) {
18        self.0.to_bits().hash(state);
19    }
20}
21
22// Mathematical operations
23struct Add;
24impl Operation<(Number, Number), Number> for Add {
25    fn execute(&self, inputs: &[&dyn neal::Value]) -> Result<Number, GraphError> {
26        let a = inputs[0]
27            .as_any()
28            .downcast_ref::<Number>()
29            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
30        let b = inputs[1]
31            .as_any()
32            .downcast_ref::<Number>()
33            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
34        Ok(Number(a.0 + b.0))
35    }
36    fn input_count(&self) -> Option<usize> {
37        Some(2)
38    }
39}
40
41struct Multiply;
42impl Operation<(Number, Number), Number> for Multiply {
43    fn execute(&self, inputs: &[&dyn neal::Value]) -> Result<Number, GraphError> {
44        let a = inputs[0]
45            .as_any()
46            .downcast_ref::<Number>()
47            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
48        let b = inputs[1]
49            .as_any()
50            .downcast_ref::<Number>()
51            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
52        Ok(Number(a.0 * b.0))
53    }
54    fn input_count(&self) -> Option<usize> {
55        Some(2)
56    }
57}
58
59struct Square;
60impl Operation<(Number,), Number> for Square {
61    fn execute(&self, inputs: &[&dyn neal::Value]) -> Result<Number, GraphError> {
62        let x = inputs[0]
63            .as_any()
64            .downcast_ref::<Number>()
65            .ok_or_else(|| GraphError::InvalidOperation("Expected Number".into()))?;
66        Ok(Number(x.0 * x.0))
67    }
68    fn input_count(&self) -> Option<usize> {
69        Some(1)
70    }
71}
72
73fn main() -> Result<(), GraphError> {
74    println!("Math operations with new API");
75
76    let mut graph = Graph::new();
77
78    // Simple computation: (a + b) * (a + b) = (a + b)²
79    let a = graph.add_node(InputNode::new(Number(3.0)));
80    let b = graph.add_node(InputNode::new(Number(4.0)));
81
82    let sum = graph.add_node(DerivedNode::new(vec![a.id(), b.id()], Add));
83    let square = graph.add_node(DerivedNode::new(vec![sum.id()], Square));
84    let product = graph.add_node(DerivedNode::new(vec![sum.id(), sum.id()], Multiply));
85
86    graph.compute()?;
87
88    if let Some(sum_val) = sum.get_value(&graph) {
89        println!("a + b = {:.1}", sum_val.0);
90    }
91
92    if let Some(square_val) = square.get_value(&graph) {
93        println!("(a + b)² = {:.1}", square_val.0);
94    }
95
96    if let Some(product_val) = product.get_value(&graph) {
97        println!("(a + b) * (a + b) = {:.1}", product_val.0);
98    }
99
100    Ok(())
101}