pub fn sgn(x: f64) -> f64 {
if x > 0.0 {
1.0
} else if x < 0.0 {
-1.0
} else {
0.0
}
}
pub fn pwr(x: f64, y: f64) -> f64 {
x.abs().powf(y)
}
pub fn pwrs(x: f64, y: f64) -> f64 {
sgn(x) * x.abs().powf(y)
}
pub fn u(x: f64) -> f64 {
if x > 0.0 {
1.0
} else {
0.0
}
}
pub fn uramp(x: f64) -> f64 {
if x > 0.0 {
x
} else {
0.0
}
}
pub fn buf(x: f64) -> f64 {
if x > 0.5 {
1.0
} else {
0.0
}
}
pub fn inv(x: f64) -> f64 {
if x > 0.5 {
0.0
} else {
1.0
}
}
pub fn if_then_else(x: f64, y: f64, z: f64) -> f64 {
if x > 0.5 {
y
} else {
z
}
}
pub fn limit(x: f64, y: f64, z: f64) -> f64 {
let (lo, hi) = if y <= z { (y, z) } else { (z, y) };
x.clamp(lo, hi)
}
pub fn table(x: f64, points: &[(f64, f64)]) -> f64 {
assert!(!points.is_empty(), "table needs at least one point");
if x <= points[0].0 {
return points[0].1;
}
let last = points.len() - 1;
if x >= points[last].0 {
return points[last].1;
}
let i = points.partition_point(|&(px, _)| px < x);
let (x0, y0) = points[i - 1];
let (x1, y1) = points[i];
y0 + (y1 - y0) * (x - x0) / (x1 - x0)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MathFn1 {
Abs,
Acos,
Acosh,
Asin,
Asinh,
Atan,
Atanh,
Buf,
Ceil,
Cos,
Cosh,
Exp,
Floor,
Int,
Inv,
Ln,
Log10,
Round,
Sgn,
Sin,
Sinh,
Sqrt,
Tan,
Tanh,
U,
Uramp,
}
impl MathFn1 {
pub fn from_name(name: &str) -> Option<Self> {
use MathFn1::*;
Some(match name {
"abs" => Abs,
"acos" => Acos,
"acosh" => Acosh,
"asin" => Asin,
"asinh" => Asinh,
"atan" => Atan,
"atanh" => Atanh,
"buf" => Buf,
"ceil" => Ceil,
"cos" => Cos,
"cosh" => Cosh,
"exp" => Exp,
"floor" => Floor,
"int" => Int,
"inv" => Inv,
"ln" => Ln,
"log10" => Log10,
"round" => Round,
"sgn" => Sgn,
"sin" => Sin,
"sinh" => Sinh,
"sqrt" => Sqrt,
"tan" => Tan,
"tanh" => Tanh,
"u" => U,
"uramp" => Uramp,
_ => return None,
})
}
pub fn name(self) -> &'static str {
use MathFn1::*;
match self {
Abs => "abs",
Acos => "acos",
Acosh => "acosh",
Asin => "asin",
Asinh => "asinh",
Atan => "atan",
Atanh => "atanh",
Buf => "buf",
Ceil => "ceil",
Cos => "cos",
Cosh => "cosh",
Exp => "exp",
Floor => "floor",
Int => "int",
Inv => "inv",
Ln => "ln",
Log10 => "log10",
Round => "round",
Sgn => "sgn",
Sin => "sin",
Sinh => "sinh",
Sqrt => "sqrt",
Tan => "tan",
Tanh => "tanh",
U => "u",
Uramp => "uramp",
}
}
pub fn call(self, x: f64) -> f64 {
use MathFn1::*;
match self {
Abs => x.abs(),
Acos => x.acos(),
Acosh => x.acosh(),
Asin => x.asin(),
Asinh => x.asinh(),
Atan => x.atan(),
Atanh => x.atanh(),
Buf => buf(x),
Ceil => x.ceil(),
Cos => x.cos(),
Cosh => x.cosh(),
Exp => x.exp(),
Floor => x.floor(),
Int => x.trunc(),
Inv => inv(x),
Ln => x.ln(),
Log10 => x.log10(),
Round => x.round(),
Sgn => sgn(x),
Sin => x.sin(),
Sinh => x.sinh(),
Sqrt => x.sqrt(),
Tan => x.tan(),
Tanh => x.tanh(),
U => u(x),
Uramp => uramp(x),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MathFn2 {
Atan2,
AngleWrapped,
Hypot,
Max,
Min,
Pow,
Pwr,
Pwrs,
}
impl MathFn2 {
pub fn from_name(name: &str) -> Option<Self> {
use MathFn2::*;
Some(match name {
"atan2" => Atan2,
"anglewrap" => AngleWrapped,
"hypot" => Hypot,
"max" => Max,
"min" => Min,
"pow" => Pow,
"pwr" => Pwr,
"pwrs" => Pwrs,
_ => return None,
})
}
pub fn name(self) -> &'static str {
use MathFn2::*;
match self {
Atan2 => "atan2",
AngleWrapped => "anglewrap",
Hypot => "hypot",
Max => "max",
Min => "min",
Pow => "pow",
Pwr => "pwr",
Pwrs => "pwrs",
}
}
pub fn call(self, x: f64, y: f64) -> f64 {
use MathFn2::*;
match self {
Atan2 => x.atan2(y),
AngleWrapped => crate::coordinate_transforms::anglewrap(x, y),
Hypot => x.hypot(y),
Max => x.max(y),
Min => x.min(y),
Pow => x.powf(y),
Pwr => pwr(x, y),
Pwrs => pwrs(x, y),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MathFn3 {
If,
Limit,
}
impl MathFn3 {
pub fn from_name(name: &str) -> Option<Self> {
use MathFn3::*;
Some(match name {
"if" => If,
"limit" => Limit,
_ => return None,
})
}
pub fn name(self) -> &'static str {
match self {
MathFn3::If => "if",
MathFn3::Limit => "limit",
}
}
pub fn call(self, x: f64, y: f64, z: f64) -> f64 {
match self {
MathFn3::If => if_then_else(x, y, z),
MathFn3::Limit => limit(x, y, z),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::f64::consts::{E, PI};
#[test]
fn sgn_returns_zero_at_origin_unlike_f64_signum() {
assert_eq!(sgn(5.0), 1.0);
assert_eq!(sgn(-5.0), -1.0);
assert_eq!(sgn(0.0), 0.0);
assert_eq!(0.0_f64.signum(), 1.0);
}
#[test]
fn trig_and_exp_match_hand_known_values() {
assert!((MathFn1::Cos.call(0.0) - 1.0).abs() < 1e-12);
assert!((MathFn1::Sin.call(PI / 2.0) - 1.0).abs() < 1e-12);
assert!((MathFn1::Exp.call(1.0) - E).abs() < 1e-12);
assert!((MathFn1::Ln.call(E) - 1.0).abs() < 1e-12);
assert!((MathFn1::Sqrt.call(4.0) - 2.0).abs() < 1e-12);
assert!((MathFn1::Log10.call(100.0) - 2.0).abs() < 1e-12);
}
#[test]
fn u_and_uramp_match_hand_derived_step_and_ramp() {
assert_eq!(u(-1.0), 0.0);
assert_eq!(u(1.0), 1.0);
assert_eq!(uramp(-3.0), 0.0);
assert_eq!(uramp(3.0), 3.0);
}
#[test]
fn buf_and_inv_threshold_at_one_half() {
assert_eq!(buf(0.4), 0.0);
assert_eq!(buf(0.6), 1.0);
assert_eq!(inv(0.4), 1.0);
assert_eq!(inv(0.6), 0.0);
}
#[test]
fn if_then_else_selects_by_threshold() {
assert_eq!(if_then_else(1.0, 10.0, 20.0), 10.0);
assert_eq!(if_then_else(0.0, 10.0, 20.0), 20.0);
}
#[test]
fn limit_clamps_to_the_span_of_its_two_bounds_either_order() {
assert_eq!(limit(5.0, 0.0, 3.0), 3.0); assert_eq!(limit(-2.0, 0.0, 3.0), 0.0); assert_eq!(limit(1.0, 0.0, 3.0), 1.0); assert_eq!(limit(5.0, 2.0, 0.0), 2.0); }
#[test]
fn pow_pwr_pwrs_hand_derived() {
assert!((MathFn2::Pow.call(2.0, 3.0) - 8.0).abs() < 1e-12);
assert!((pwr(-8.0, 1.0 / 3.0) - 2.0).abs() < 1e-9);
assert!((pwrs(-8.0, 1.0 / 3.0) - (-2.0)).abs() < 1e-9);
}
#[test]
fn atan2_and_hypot_match_hand_derived_values() {
assert!((MathFn2::Atan2.call(1.0, 1.0) - PI / 4.0).abs() < 1e-12);
assert!((MathFn2::Hypot.call(3.0, 4.0) - 5.0).abs() < 1e-12);
}
#[test]
fn anglewrap_dispatch_matches_direct_call() {
assert_eq!(
MathFn2::AngleWrapped.call(-1.0, -1.0),
crate::coordinate_transforms::anglewrap(-1.0, -1.0)
);
assert_eq!(MathFn2::from_name("anglewrap"), Some(MathFn2::AngleWrapped));
}
#[test]
fn min_max_pick_the_right_side() {
assert_eq!(MathFn2::Min.call(3.0, 7.0), 3.0);
assert_eq!(MathFn2::Max.call(3.0, 7.0), 7.0);
}
#[test]
fn table_interpolates_linearly_and_clamps_outside_range() {
let points = [(0.0, 0.0), (1.0, 10.0), (2.0, 10.0), (3.0, 0.0)];
assert_eq!(table(0.5, &points), 5.0); assert_eq!(table(1.5, &points), 10.0); assert_eq!(table(-1.0, &points), 0.0); assert_eq!(table(10.0, &points), 0.0); }
#[test]
fn from_name_matches_conventional_names_and_rejects_unknown() {
assert_eq!(MathFn1::from_name("cos"), Some(MathFn1::Cos));
assert_eq!(MathFn1::from_name("nope"), None);
assert_eq!(MathFn2::from_name("atan2"), Some(MathFn2::Atan2));
assert_eq!(MathFn3::from_name("if"), Some(MathFn3::If));
}
}