9.7 — Drop-in implementation — C (embedded-friendly, double math)
#include <math.h>
static inline double clamp_a(double a, double eps_a){
double lo = -1.0 + eps_a, hi = 1.0 - eps_a;
if (a < lo) return lo;
if (a > hi) return hi;
return a;
}
static inline double a_mul(double a1, double a2, double eps_a){
return tanh( atanh(clamp_a(a1,eps_a)) + atanh(clamp_a(a2,eps_a)) );
}
static inline double a_div(double af, double ag, double eps_a){
return tanh( atanh(clamp_a(af,eps_a)) - atanh(clamp_a(ag,eps_a)) );
}
static inline void uw_accum(double *U, double *W, double a, double w, double eps_a){
*U += w * atanh( clamp_a(a,eps_a) );
*W += w;
}
static inline double uw_flush(double U, double W, double eps_w){
return tanh( U / fmax(W, eps_w) );
}
9.8 — Drop-in implementation — Rust (std or no_std)
#[inline]
pub fn clamp_a(a: f64, eps_a: f64) -> f64 {
let lo = -1.0 + eps_a;
let hi = 1.0 - eps_a;
if a < lo { lo } else if a > hi { hi } else { a }
}
#[inline]
pub fn a_mul(a1: f64, a2: f64, eps_a: f64) -> f64 {
((clamp_a(a1, eps_a)).atanh() + (clamp_a(a2, eps_a)).atanh()).tanh()
}
#[inline]
pub fn a_div(af: f64, ag: f64, eps_a: f64) -> f64 {
((clamp_a(af, eps_a)).atanh() - (clamp_a(ag, eps_a)).atanh()).tanh()
}
#[inline]
pub fn uw_accum(u: &mut f64, wsum: &mut f64, a: f64, w: f64, eps_a: f64) {
*u += w * clamp_a(a, eps_a).atanh();
*wsum += w;
}
#[inline]
pub fn uw_flush(u: f64, wsum: f64, eps_w: f64) -> f64 {
(u / wsum.max(eps_w)).tanh()
}
// Note: With no_std, link to libm for tanh/atanh.
9.9 — Minimal unit tests (calculator-fast)
# multiply identity (E2)
assert abs( ( (0.8+0.2)/(1+0.8*0.2) ) - ( __import__("math").tanh(__import__("math").atanh(0.8)+__import__("math").atanh(0.2)) ) ) <= 1e-12
# divide identity (E3)
assert abs( ( (0.8-0.2)/(1-0.8*0.2) ) - ( __import__("math").tanh(__import__("math").atanh(0.8)-__import__("math").atanh(0.2)) ) ) <= 1e-12
# order-invariant U/W (E5)
from math import atanh, tanh
A = [0.2, 0.5, -0.3]
S = sum(atanh(a) for a in A); W = len(A)
ref = tanh(S/max(W,1e-12))
for perm in [(0,1,2),(2,0,1),(1,2,0)]:
U=Wsum=0.0
for i in perm:
U += atanh(A[i]); Wsum += 1.0
assert abs(ref - tanh(U/max(Wsum,1e-12))) <= 1e-12
# collapse parity
def collapse(m,a): return m # phi((m,a)) = m
assert collapse(123.45, 0.7) == 123.45
Navigation
Back: Shunyaya Symbolic Mathematical Hardware – Reference Knobs, Error Budgeting & Python (9.4–9.6)
Next: Shunyaya Symbolic Mathematical Hardware – Software Reference: I/O & Gotchas (9.10–9.11)
Directory of Pages
SSMH – Table of Contents