1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//! The functionality and parameters around a linear scaling normaliser.
use super::traits::Normaliser;
/// A linear scaling normaliser.
///
/// # Fields
/// * `min` - The minimum value to scale to.
/// * `max` - The maximum value to scale to.
#[derive(Debug, PartialEq)]
pub struct LinearScaling {
pub min: f32,
pub max: f32,
}
impl Normaliser for LinearScaling {
/// Normalises a value.
///
/// # Arguments
/// * `input` - The value to normalise.
///
/// # Returns
/// The normalised value.
fn normalise(&self, input: f32)-> f32 {
let range = self.max - self.min;
let normalised = (input - self.min) / range;
normalised
}
/// Applies the inverse of the value for the normaliser.
///
/// # Arguments
/// * `input` - The value to inverse normalise.
///
/// # Returns
/// The inverse normalised value.
fn inverse_normalise(&self, input: f32) -> f32 {
let range = self.max - self.min;
let denormalised = (input * range) + self.min;
denormalised
}
/// The key of the normaliser.
///
/// # Returns
/// The key of the normaliser.
fn key() -> String {
"linear_scaling".to_string()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_normalise_with_both_bounds() {
let normaliser = LinearScaling {
min: 0.0,
max: 100.0,
};
let input = 50.0;
let expected = 0.5;
let actual = normaliser.normalise(input);
assert_eq!(expected, actual);
}
}