rust_fuzzylogic/membership/
mod.rs

1use crate::error::*;
2use crate::*;
3
4pub mod gaussian;
5pub mod trapezoidal;
6pub mod triangular;
7
8pub use gaussian::Gaussian;
9pub use triangular::Triangular;
10
11pub trait MembershipFn {
12    fn eval(&self, x: crate::Float) -> crate::Float;
13}
14
15///validation function to check that the order in the tiangular or trapezoidal apexes are correct.
16fn validate_order(vals: &[Float]) -> Result<()> {
17    for i in 0..vals.len() - 1 {
18        if vals[i + 1] <= vals[i] {
19            return Err(FuzzyError::BadArity);
20        }
21    }
22    Ok(())
23}
24
25///Calculate the slope. delta is the change amount.(Either 1.0 or -1.0 by definition.)
26fn slope(value: Float, left: Float, right: Float, delta: Float) -> Float {
27    (delta * (value - left) / (right - left) + ((-1.0 * delta + 1.0) / 2.0)).clamp(0.0, 1.0)
28}
29
30//simple unit testing for validation
31#[cfg(test)]
32mod tests {
33    use crate::membership::validate_order;
34
35    #[test]
36    fn test_validation() {
37        assert_eq!(
38            validate_order(&[0.0, 1.1, 0.5]),
39            Err(crate::error::FuzzyError::BadArity)
40        );
41    }
42}