use crate::utilities;
use impl_ops::*;
use std::ops;
#[derive(Debug, PartialEq, Clone)]
pub struct LinearFunction {
slope: f64,
intercept: f64,
}
impl LinearFunction {
pub fn new(slope: f64, intercept: f64) -> Self {
Self {
slope: utilities::math::round_f64(slope, 5),
intercept: utilities::math::round_f64(intercept, 5),
}
}
pub fn slope(&self) -> f64 {
self.slope
}
pub fn intercept(&self) -> f64 {
self.intercept
}
pub fn sum(&self, other: &LinearFunction) -> Self {
LinearFunction::new(self.slope + other.slope, self.intercept + other.intercept)
}
}
impl_op!(+ |a: &LinearFunction, b: &LinearFunction| -> LinearFunction { a.sum(b) });
impl_op!(+ |a: LinearFunction, b: &LinearFunction| -> LinearFunction { a.sum(b) });
impl_op!(+ |a: &LinearFunction, b: LinearFunction| -> LinearFunction { a.sum(&b) });
impl_op!(+ |a: LinearFunction, b: LinearFunction| -> LinearFunction { a.sum(&b) });
#[cfg(test)]
mod tests {
use crate::fuzzy::membership::piecewise::LinearFunction;
#[test]
fn sum_references() {
let a = LinearFunction::new(2.1, 3.1);
let b = LinearFunction::new(2.3, 3.7);
let sum = &a + &b;
assert_eq!(sum.slope(), 4.4);
assert_eq!(sum.intercept(), 6.8);
}
#[test]
fn sum_ownerships() {
let a = LinearFunction::new(2.1, 3.1);
let b = LinearFunction::new(2.3, 3.7);
let sum = a + b;
assert_eq!(sum.slope(), 4.4);
assert_eq!(sum.intercept(), 6.8);
}
#[test]
fn sum_ownership_reference() {
let a = LinearFunction::new(2.1, 3.1);
let b = LinearFunction::new(2.3, 3.7);
let sum = a + &b;
assert_eq!(sum.slope(), 4.4);
assert_eq!(sum.intercept(), 6.8);
}
#[test]
fn sum_reference_ownership() {
let a = LinearFunction::new(2.1, 3.1);
let b = LinearFunction::new(2.3, 3.7);
let sum = &a + b;
assert_eq!(sum.slope(), 4.4);
assert_eq!(sum.intercept(), 6.8);
}
}