use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
pub struct DomainMap {
pub x0: f64,
pub m: f64,
}
impl DomainMap {
pub fn new(x0: f64, m: f64) -> Self {
Self { x0, m }
}
pub fn to(&self, a: f64) -> f64 {
self.x0 + self.m * a
}
pub fn from(&self, b: f64) -> f64 {
(b - self.x0) / self.m
}
}
#[cfg(test)]
mod tests {
use super::*;
use approx::assert_relative_eq;
#[test]
fn test_domain_map_to_fwd() {
let dm = DomainMap::new(0.5, 1.0);
assert_relative_eq!(dm.to(0.1), 0.6);
}
#[test]
fn test_domain_map_from_fwd() {
let dm = DomainMap::new(0.5, 1.0);
assert_relative_eq!(dm.from(0.6), 0.1);
}
#[test]
fn test_domain_map_to_rev() {
let dm = DomainMap::new(0.5, -1.0);
assert_relative_eq!(dm.to(0.1), 0.4);
}
#[test]
fn test_domain_map_from_rev() {
let dm = DomainMap::new(0.5, -1.0);
assert_relative_eq!(dm.from(0.4), 0.1);
}
}