Files
Aura/crates/aura-std/src/scale.rs
T
claude a32dc38d18 feat(std): meaning lines for all 27 shipped node schemas
C29 compile/unit seam, task 3 of the self-description plan: every
aura-std NodeSchema literal threads its one-line doc. Four texts were
corrected against the actual eval/finalize semantics rather than taken
from the plan table verbatim: Delay is a lag-N register (not one-step),
GatedRecorder flushes an ungated final row at finalize, Latch is a
level-sensitive set/reset register (captures no input), SeriesReducer
emits its single summary row at finalize (not per cycle).

Gate: cargo build -p aura-std --lib clean; full std test run follows at
the all-crates gate once strategy/backtest/engine thread their sites
(the earlier per-crate test gate was unsatisfiable in isolation --
std's dev-deps pull crates whose sites belong to later tasks).

refs #316
2026-07-23 15:25:16 +02:00

77 lines
2.5 KiB
Rust

//! `Scale` — a stateless `f64 -> f64` multiply-by-constant: `out = input * factor`.
//! The standard scalar-gain operator (the `*` is fixed by the type; `factor` is the
//! only knob). One f64 input, one f64 output, allocation-free on the hot path.
use aura_core::{
Cell, Ctx, FieldSpec, Firing, Node, NodeSchema, ParamSpec, PortSpec, PrimitiveBuilder,
ScalarKind,
};
/// Stateless scalar gain: emits `input * factor` each cycle. Emits `None` until its
/// input is present (warm-up filter, C8).
pub struct Scale {
factor: f64,
out: [Cell; 1],
}
impl Scale {
/// Build a gain node that multiplies its input by `factor`.
pub fn new(factor: f64) -> Self {
Self { factor, out: [Cell::from_f64(0.0)] }
}
/// The param-generic recipe for a blueprint primitive: declares `factor` and
/// builds through `Scale::new` (the slice is kind-checked before `build` runs).
pub fn builder() -> PrimitiveBuilder {
PrimitiveBuilder::new(
"Scale",
NodeSchema {
inputs: vec![PortSpec { kind: ScalarKind::F64, firing: Firing::Any, name: "signal".into() }],
output: vec![FieldSpec { name: "value".into(), kind: ScalarKind::F64 }],
params: vec![ParamSpec { name: "factor".into(), kind: ScalarKind::F64 }],
doc: "input multiplied by a constant factor param",
},
|p| Box::new(Scale::new(p[0].f64())),
)
}
}
impl Node for Scale {
fn lookbacks(&self) -> Vec<usize> {
vec![1]
}
fn eval(&mut self, ctx: Ctx<'_>) -> Option<&[Cell]> {
let w = ctx.f64_in(0);
if w.is_empty() {
return None; // not yet warmed up (C8 filter)
}
self.out[0] = Cell::from_f64(w[0] * self.factor);
Some(&self.out)
}
fn label(&self) -> String {
format!("Scale({})", self.factor)
}
}
#[cfg(test)]
mod tests {
use super::*;
use aura_core::{AnyColumn, Scalar, Timestamp};
#[test]
fn scale_multiplies_input_by_factor() {
let mut node = Scale::new(2.0);
let mut inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, node.lookbacks()[0])];
inputs[0].push(Scalar::f64(3.0)).unwrap();
assert_eq!(node.eval(Ctx::new(&inputs, Timestamp(0))), Some([Cell::from_f64(6.0)].as_slice()));
}
#[test]
fn scale_is_none_until_input_present() {
let mut node = Scale::new(2.0);
let inputs = vec![AnyColumn::with_capacity(ScalarKind::F64, 1)];
assert_eq!(node.eval(Ctx::new(&inputs, Timestamp(0))), None);
}
}