Skip to main content

TemporalDerivativeKernel

Struct TemporalDerivativeKernel 

Source
pub struct TemporalDerivativeKernel<const N: usize> {
    pub fast: [f32; N],
    pub slow: [f32; N],
    pub alpha_fast: f32,
    pub alpha_slow: f32,
}
Expand description

Dual fast/slow EMA temporal-derivative kernel.

fast tracks the signal with a short time constant; slow with a long one. Their difference (fast − slow) is a signed band-pass derivative that spikes on change and decays to zero when the signal is stationary — the canonical neocortical prediction-error signal.

Generic over N (the signal dimension). Fixed-size array, zero allocation, suitable for embedding inside per-NPC state structs.

§Invariants

  • 0 < alpha_slow < alpha_fast <= 1 (validated at construction).
  • State arrays are zero-initialized on new; use with_initial for warm starts / snapshot restore.
  • All operations are branch-free and in-place; safe to call from hot paths at any tier (plasma → cold).

Fields§

§fast: [f32; N]

Fast EMA — short time constant.

§slow: [f32; N]

Slow EMA — long time constant.

§alpha_fast: f32

Fast EMA coefficient. new_fast = (1 − α_f) · old_fast + α_f · signal.

§alpha_slow: f32

Slow EMA coefficient. new_slow = (1 − α_s) · old_slow + α_s · signal.

Implementations§

Source§

impl<const N: usize> TemporalDerivativeKernel<N>

Source

pub fn new(alpha_fast: f32, alpha_slow: f32) -> Self

Construct a zero-initialized kernel.

Validates 0 < alpha_slow < alpha_fast <= 1. Panics in debug, clamps in release (paper’s ~10× ratio is the canonical default; e.g. alpha_fast=0.3, alpha_slow=0.03).

Source

pub fn with_initial( fast: [f32; N], slow: [f32; N], alpha_fast: f32, alpha_slow: f32, ) -> Self

Construct with initial EMA state — for warm starts or snapshot restore.

Source

pub fn observe(&mut self, signal: &[f32; N]) -> [f32; N]

Observe one signal sample; update both EMAs and return the per-dim signed surprise vector (fast − slow).

Branch-free, no allocations. Reuses simd_fused_decay_write when the simd-implied path is available (always-on in this crate — the kernel dispatches to NEON/AVX2/scalar inside simd).

Paper reference: O’Reilly 2026 §Implementational — the CaMKII/DAPK1 kinase cascade maps onto the (fast − slow) difference; we compute it algebraically rather than biochemically.

Source

pub fn observe_simd(&mut self, signal: &[f32; N]) -> [f32; N]

SIMD-optimized observe (alias — the default observe already routes through simd_fused_decay_write).

Kept as a distinct entry point so callers that want to make the SIMD path explicit in their code can do so, and so that future wider-SIMD specializations have a natural home.

Source

pub fn surprise_norm(&self) -> f32

L2 norm of the current (fast − slow) derivative.

Uses simd_dot_f32 for the inner reduction when N >= 4. Bounded scalar in [0, ∞); typical operating range [0, 1] after normalization.

Source

pub fn derivative_slice(&self, out: &mut [f32; N])

Write (fast − slow) into a caller-provided buffer — zero-alloc read path for consumers that already own a scratch buffer.

Source

pub fn reset(&mut self)

Fill both EMA arrays with zero — for entity respawn / session restart.

Trait Implementations§

Source§

impl<const N: usize> Clone for TemporalDerivativeKernel<N>

Source§

fn clone(&self) -> TemporalDerivativeKernel<N>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const N: usize> Debug for TemporalDerivativeKernel<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const N: usize> Default for TemporalDerivativeKernel<N>

Source§

fn default() -> Self

Default: paper’s ~10× ratio — alpha_fast=0.3, alpha_slow=0.03.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.