use dslcompile::prelude::*;
fn main() -> Result<()> {
println!("=== Simplified Statistical Computing Demo ===\n");
let data = vec![(1.0, 2.1), (2.0, 3.9), (3.0, 6.1), (4.0, 8.0)];
println!("Data points: {data:?}");
println!("Model: y = β₀ + β₁*x");
println!("Goal: Minimize sum of squared residuals\n");
let beta0 = ASTRepr::<f64>::Variable(0); let beta1 = ASTRepr::<f64>::Variable(1);
let mut sum_expr = ASTRepr::<f64>::Constant(0.0);
for i in 0..data.len() {
let x_var = ASTRepr::<f64>::Variable(2 + 2 * i); let y_var = ASTRepr::<f64>::Variable(2 + 2 * i + 1);
let beta1_x = ASTRepr::Mul(Box::new(beta1.clone()), Box::new(x_var));
let prediction = ASTRepr::Add(Box::new(beta0.clone()), Box::new(beta1_x));
let residual = ASTRepr::Sub(Box::new(y_var), Box::new(prediction));
let squared_residual = ASTRepr::Mul(Box::new(residual.clone()), Box::new(residual));
sum_expr = ASTRepr::Add(Box::new(sum_expr), Box::new(squared_residual));
}
println!("Expression created with {} variables", 2 + 2 * data.len());
let compiler = RustCompiler::new();
let generator = RustCodeGenerator::new();
let rust_code = generator.generate_function(&sum_expr, "sum_squared_residuals")?;
let compiled_fn = compiler.compile_and_load(&rust_code, "sum_squared_residuals")?;
println!("Expression compiled successfully!\n");
let test_cases = vec![
(0.0, 1.0), (0.5, 1.5), (1.0, 2.0), ];
for (beta0_val, beta1_val) in test_cases {
let mut vars = vec![beta0_val, beta1_val];
for &(x, y) in &data {
vars.push(x);
vars.push(y);
}
let result = compiled_fn.call_multi_vars(&vars)?;
println!("β₀={beta0_val}, β₁={beta1_val} → Sum of squared residuals = {result:.4}");
}
println!("\n=== Key Insights ===");
println!("1. Statistical computing works perfectly with the general system");
println!("2. No need for specialized 'call_with_data' methods");
println!("3. Just use call_multi_vars() with all variables flattened");
println!("4. Mathematically equivalent but architecturally simpler");
Ok(())
}