diff --git a/crates/aura-std/src/add.rs b/crates/aura-std/src/add.rs new file mode 100644 index 0000000..2e58cca --- /dev/null +++ b/crates/aura-std/src/add.rs @@ -0,0 +1,69 @@ +//! `Add` — two-input f64 sum (input 0 plus input 1), the companion to `Sub`. +//! Combines two signal streams into one — the most basic combinator for the +//! north-star "combine one signal with another" research move (C10). + +use aura_core::{Ctx, FieldSpec, Firing, InputSpec, Node, NodeSchema, Scalar, ScalarKind}; + +/// Two-input f64 sum: input 0 plus input 1. Emits `None` until both inputs +/// have a value. +pub struct Add { + out: [Scalar; 1], +} + +impl Add { + /// Build an `Add` node. + pub fn new() -> Self { + Self { out: [Scalar::F64(0.0)] } + } +} + +impl Default for Add { + fn default() -> Self { + Self::new() + } +} + +impl Node for Add { + fn schema(&self) -> NodeSchema { + NodeSchema { + inputs: vec![ + InputSpec { kind: ScalarKind::F64, lookback: 1, firing: Firing::Any }, + InputSpec { kind: ScalarKind::F64, lookback: 1, firing: Firing::Any }, + ], + output: vec![FieldSpec { name: "value", kind: ScalarKind::F64 }], + } + } + + fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Scalar]> { + let a = ctx.f64_in(0); + let b = ctx.f64_in(1); + if a.is_empty() || b.is_empty() { + return None; + } + self.out[0] = Scalar::F64(a[0] + b[0]); + Some(&self.out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use aura_core::{AnyColumn, Timestamp}; + + #[test] + fn add_is_sum_once_both_inputs_present() { + let mut add = Add::new(); + let mut inputs = vec![ + AnyColumn::with_capacity(ScalarKind::F64, 1), + AnyColumn::with_capacity(ScalarKind::F64, 1), + ]; + + // only input 0 present -> None + inputs[0].push(Scalar::F64(10.0)).unwrap(); + assert_eq!(add.eval(Ctx::new(&inputs, Timestamp(0))), None); + + // both present -> a + b + inputs[1].push(Scalar::F64(4.0)).unwrap(); + assert_eq!(add.eval(Ctx::new(&inputs, Timestamp(0))), Some([Scalar::F64(14.0)].as_slice())); + } +} diff --git a/crates/aura-std/src/lib.rs b/crates/aura-std/src/lib.rs index c012394..d832bc0 100644 --- a/crates/aura-std/src/lib.rs +++ b/crates/aura-std/src/lib.rs @@ -15,11 +15,15 @@ //! The first block lands with the walking skeleton: [`Sma`], the simple moving //! average — a worked producer node proving the `aura-core` `Node` contract. +mod add; mod exposure; +mod lincomb; mod sim_broker; mod sma; mod sub; +pub use add::Add; pub use exposure::Exposure; +pub use lincomb::LinComb; pub use sim_broker::SimBroker; pub use sma::Sma; pub use sub::Sub; diff --git a/crates/aura-std/src/lincomb.rs b/crates/aura-std/src/lincomb.rs new file mode 100644 index 0000000..6d4849f --- /dev/null +++ b/crates/aura-std/src/lincomb.rs @@ -0,0 +1,111 @@ +//! `LinComb` — weighted sum of `N` f64 inputs (`Σ weights[i] · input[i]`), the +//! general combinator for the north-star "combine signals with weights" move +//! (C10). `LinComb([1.0, 1.0])` is `Add`; `LinComb([1.0, -1.0])` is `Sub`. The +//! weights are the node's tunable parameters (C8/C12) and fix its arity. + +use aura_core::{Ctx, FieldSpec, Firing, InputSpec, Node, NodeSchema, Scalar, ScalarKind}; + +/// Weighted sum of `N` f64 inputs: `Σ weights[i] · input[i]`. The `weights` +/// are the node's tunable parameters and fix its arity (`weights.len()` inputs, +/// in slot order). Emits `None` until *all* inputs have a value. +pub struct LinComb { + weights: Vec, + out: [Scalar; 1], +} + +impl LinComb { + /// Build a `LinComb` with one weight per input (at least one required). + /// + /// # Panics + /// Panics if `weights` is empty. + pub fn new(weights: Vec) -> Self { + assert!(!weights.is_empty(), "LinComb needs at least one weight"); + Self { weights, out: [Scalar::F64(0.0)] } + } +} + +impl Node for LinComb { + fn schema(&self) -> NodeSchema { + NodeSchema { + inputs: self + .weights + .iter() + .map(|_| InputSpec { kind: ScalarKind::F64, lookback: 1, firing: Firing::Any }) + .collect(), + output: vec![FieldSpec { name: "value", kind: ScalarKind::F64 }], + } + } + + fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Scalar]> { + let mut acc = 0.0; + for (i, &w) in self.weights.iter().enumerate() { + let w_in = ctx.f64_in(i); + if w_in.is_empty() { + return None; // not yet warmed up — withhold until every leg is present + } + acc += w * w_in[0]; + } + self.out[0] = Scalar::F64(acc); + Some(&self.out) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use aura_core::{AnyColumn, Timestamp}; + + #[test] + fn lincomb_weighted_sum_once_all_present() { + let mut lc = LinComb::new(vec![0.5, 2.0]); + let mut inputs = vec![ + AnyColumn::with_capacity(ScalarKind::F64, 1), + AnyColumn::with_capacity(ScalarKind::F64, 1), + ]; + + // only input 0 present -> None + inputs[0].push(Scalar::F64(10.0)).unwrap(); + assert_eq!(lc.eval(Ctx::new(&inputs, Timestamp(0))), None); + + // both present -> 0.5*10 + 2.0*3 = 11.0 + inputs[1].push(Scalar::F64(3.0)).unwrap(); + assert_eq!(lc.eval(Ctx::new(&inputs, Timestamp(0))), Some([Scalar::F64(11.0)].as_slice())); + } + + #[test] + fn lincomb_unit_weights_equal_add() { + let mut lc = LinComb::new(vec![1.0, 1.0]); + let mut inputs = vec![ + AnyColumn::with_capacity(ScalarKind::F64, 1), + AnyColumn::with_capacity(ScalarKind::F64, 1), + ]; + inputs[0].push(Scalar::F64(7.0)).unwrap(); + inputs[1].push(Scalar::F64(5.0)).unwrap(); + // unit weights reproduce Add: 7 + 5 + assert_eq!(lc.eval(Ctx::new(&inputs, Timestamp(0))), Some([Scalar::F64(12.0)].as_slice())); + } + + #[test] + fn lincomb_three_inputs_warm_up() { + let mut lc = LinComb::new(vec![1.0, 1.0, 1.0]); + let mut inputs = vec![ + AnyColumn::with_capacity(ScalarKind::F64, 1), + AnyColumn::with_capacity(ScalarKind::F64, 1), + AnyColumn::with_capacity(ScalarKind::F64, 1), + ]; + inputs[0].push(Scalar::F64(1.0)).unwrap(); + inputs[1].push(Scalar::F64(2.0)).unwrap(); + // third leg still cold -> None (withheld until every leg is present) + assert_eq!(lc.eval(Ctx::new(&inputs, Timestamp(0))), None); + + inputs[2].push(Scalar::F64(3.0)).unwrap(); + // all warm -> 1 + 2 + 3 + assert_eq!(lc.eval(Ctx::new(&inputs, Timestamp(0))), Some([Scalar::F64(6.0)].as_slice())); + } + + #[test] + #[should_panic(expected = "LinComb needs at least one weight")] + fn lincomb_empty_weights_panics() { + let _ = LinComb::new(vec![]); + } +}