mirl 9.2.0

Miners Rust Lib - A massive collection of ever growing and changing functions, structs, and enums. Check the description for compatibility and toggleable features! (Most of the lib is controlled by flags/features so the lib can continue to be lightweight despite its size)
use crate::math::expressions::d2::GetY;

#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
#[cfg_attr(
    feature = "wincode",
    derive(wincode::SchemaWrite, wincode::SchemaRead)
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Hash)]
/// x * m + n
#[cfg_attr(feature = "c_compatible", repr(C))]
pub struct LinearExpression<T> {
    /// The offset of x
    pub offset: T,
    /// The multiplier of x
    pub multiplier: T,
}

impl<T> LinearExpression<T> {
    #[must_use]
    /// Create a mew [`LinearExpression`] using a offset and multiplier
    pub const fn new(offset: T, multiplier: T) -> Self {
        Self {
            offset,
            multiplier,
        }
    }
}

impl<T: crate::math::NumberWithMonotoneOps + Copy> LinearExpression<T> {
    #[must_use]
    /// Get the linear expression from 2 known points
    pub fn new_from_2_points(point1: (T, T), point2: (T, T)) -> Self {
        let multiplier = (point2.1 - point1.1) / (point2.0 - point1.0);
        let offset = point1.1 - (multiplier * point1.0);
        Self {
            offset,
            multiplier,
        }
    }
}

impl<T: crate::math::NumberWithMonotoneOps + Copy> GetY<T>
    for LinearExpression<T>
{
    fn get_y(&self, x: T) -> T {
        x * self.multiplier + self.offset
    }
}