use crate::Result;
use crate::func1::Func1;
pub type Line1 = super::polynomial::Polynomial<2>;
impl Line1 {
pub fn new_mxb(m: f64, b: f64) -> Self {
Self::new([b, m])
}
pub fn try_from_points(x0: f64, y0: f64, x1: f64, y1: f64) -> Result<Self> {
if (x1 - x0).abs() < 1e-12 {
return Err(
"x1 and x0 are too close together and will create an infinite slope".into(),
);
}
let m = (y1 - y0) / (x1 - x0);
let b = y0 - m * x0;
Ok(Self::new_mxb(m, b))
}
pub fn m(&self) -> f64 {
self.c[1]
}
pub fn b(&self) -> f64 {
self.c[0]
}
}
pub struct Gaussian1 {
pub mean: f64,
pub sigma: f64,
}
impl Gaussian1 {
pub fn new(mean: f64, sigma: f64) -> Self {
Self { mean, sigma }
}
}
impl Func1 for Gaussian1 {
fn f(&self, x: f64) -> f64 {
(-0.5 * ((x - self.mean).powi(2) / self.sigma.powi(2))).exp()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::func1::Func1;
use approx::assert_relative_eq;
#[test]
fn test_line1() {
let line = Line1::new_mxb(2.0, 1.0);
assert_relative_eq!(line.f(0.0), 1.0);
assert_relative_eq!(line.f(1.0), 3.0);
assert_relative_eq!(line.f(2.0), 5.0);
}
}