//! `Mul` — two-input f64 product (input 0 times input 1). The fundamental //! multiplication primitive (e.g. squaring a return for a variance estimate: //! `Mul(delta, delta)`). Emits `None` until both inputs have a value. use aura_core::{Cell, Ctx, FieldSpec, Firing, Node, NodeSchema, PortSpec, PrimitiveBuilder, ScalarKind}; /// Two-input f64 product: input 0 times input 1. Emits `None` until both inputs /// have a value. pub struct Mul { out: [Cell; 1], } impl Mul { pub fn new() -> Self { Self { out: [Cell::from_f64(0.0)] } } pub fn builder() -> PrimitiveBuilder { PrimitiveBuilder::new( "Mul", NodeSchema { inputs: vec![ PortSpec { kind: ScalarKind::F64, firing: Firing::Any, name: "lhs".into() }, PortSpec { kind: ScalarKind::F64, firing: Firing::Any, name: "rhs".into() }, ], output: vec![FieldSpec { name: "value".into(), kind: ScalarKind::F64 }], params: vec![], }, |_| Box::new(Mul::new()), ) } } impl Default for Mul { fn default() -> Self { Self::new() } } impl Node for Mul { fn lookbacks(&self) -> Vec { vec![1, 1] } fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Cell]> { let a = ctx.f64_in(0); let b = ctx.f64_in(1); if a.is_empty() || b.is_empty() { return None; } self.out[0] = Cell::from_f64(a[0] * b[0]); Some(&self.out) } fn label(&self) -> String { "Mul".to_string() } } #[cfg(test)] mod tests { use super::*; use aura_core::{AnyColumn, Scalar, Timestamp}; #[test] fn mul_is_product_once_both_inputs_present() { let mut m = Mul::new(); let mut inputs = vec![ AnyColumn::with_capacity(ScalarKind::F64, 1), AnyColumn::with_capacity(ScalarKind::F64, 1), ]; inputs[0].push(Scalar::f64(3.0)).unwrap(); assert_eq!(m.eval(Ctx::new(&inputs, Timestamp(0))), None); // only one leg inputs[1].push(Scalar::f64(4.0)).unwrap(); assert_eq!(m.eval(Ctx::new(&inputs, Timestamp(0))), Some([Cell::from_f64(12.0)].as_slice())); } #[test] fn input_slots_are_named_lhs_rhs() { let names: Vec = Mul::builder().schema().inputs.iter().map(|p| p.name.clone()).collect(); assert_eq!(names, ["lhs", "rhs"]); } }