Skip to main content

Membership functions

A membership function (MF) maps a crisp value x to a degree μ(x)∈[0,1]\mu(x) \in [0, 1]. The shapes and their parameters are explained in Concepts → Membership functions. This page covers the Rust types.

use ferrifuzz::prelude::*;

let warm = mf::triangle(20.0, 50.0, 80.0)?;
assert_eq!(warm.membership(35.0), 0.5);
assert_eq!(warm.center(), 50.0);
assert_eq!(warm.shape(), "Triangular");
println!("{warm}"); // triangle(20, 50, 80)

The mf constructors​

Use these in normal code. They return the unified Type1MF enum, which is what Variable::with_term expects.

FunctionReturnsValidation
mf::triangle(a, b, c)Result<Type1MF>a ≤ b ≤ c
mf::trapezoid(a, b, c, d)Result<Type1MF>a ≤ b ≤ c ≤ d
mf::gaussian(center, sigma)Result<Type1MF>sigma > 0
mf::bell(center, width, slope)Result<Type1MF>width > 0, slope > 0
mf::singleton(x)Type1MFnone
mf::sigmoid(center, slope)Type1MFnone (slope > 0 rises, < 0 falls)
mf::it2(upper, lower)Result<IT2MF>lower ≤ upper everywhere
mf::evenly_spaced(lo, hi, n, shape)Result<Vec<(f64, Type1MF)>>n ≥ 2, lo < hi

Invalid parameters give Error::InvalidParameter, for example "triangle(5, 0, 10): needs a ≤ b ≤ c (feet a, c; peak b)" or "gaussian(0, -1): sigma must be > 0". NaN parameters are rejected the same way.

Shoulders are degenerate triangles and trapezoids. triangle(0.0, 0.0, 20.0) is a left shoulder that is 1.0 at x = 0. trapezoid(60.0, 80.0, 100.0, 100.0) is a right shoulder.

mf::evenly_spaced​

pub fn evenly_spaced(lo: f64, hi: f64, n: usize, shape: Shape) -> Result<Vec<(f64, Type1MF)>>

Returns n overlapping terms whose centers are evenly spaced from lo to hi, as (center, mf) pairs. Triangles peak at each center and reach zero at the neighbouring centers, so inside [lo, hi] the memberships sum to 1. Gaussians use sigma = step / 2. Variable::automf is built on top of this function.

ferrifuzz::Shape has two variants. Shape::Triangle is the default and parses from "triangle", "triangular" or "trimf". Shape::Gaussian parses from "gaussian", "gauss" or "gaussmf".

MembershipFunction trait​

pub trait MembershipFunction {
fn membership(&self, x: f64) -> f64;
}

All six shape structs and Type1MF implement it. Implement it yourself if you want a custom shape in your own code. The inference engine itself works on Type1MF and IT2MF.

Shape structs​

Each shape is also a standalone Copy struct. Its constructor validates the parameters, and params() returns them. Convert a struct to Type1MF with .into().

StructConstructorFormula / behaviourAccessor
TriangularMFnew(a, b, c) -> Resultlinear up from a to the peak at b, down to cparams() -> (a, b, c)
TrapezoidalMFnew(a, b, c, d) -> Resultrises a→b, flat b..=c, falls c→dparams() -> (a, b, c, d)
GaussianMFnew(center, sigma) -> Resulte−12(x−cσ)2e^{-\frac{1}{2}\left(\frac{x-c}{\sigma}\right)^2}params() -> (center, sigma)
BellMFnew(center, width, slope) -> Result11+∣(x−c)/w∣2s\dfrac{1}{1 + \lvert (x-c)/w \rvert^{2s}}params() -> (center, width, slope)
SigmoidMFnew(center, slope)11+e−s(x−c)\dfrac{1}{1 + e^{-s(x-c)}}params() -> (center, slope)
SingletonMFnew(x)1 at exactly x (within f64::EPSILON), else 0value() -> f64
use ferrifuzz::{GaussianMF, MembershipFunction, Type1MF};

let g = GaussianMF::new(50.0, 10.0)?;
assert!((g.membership(60.0) - 0.6065).abs() < 1e-4); // one sigma away
let as_enum: Type1MF = g.into();

Type1MF​

pub enum Type1MF {
Triangular(TriangularMF),
Trapezoidal(TrapezoidalMF),
Gaussian(GaussianMF),
Singleton(SingletonMF),
Bell(BellMF),
Sigmoid(SigmoidMF),
}

Type1MF is Copy and implements PartialEq and Debug.

MethodReturnsNotes
membership(x)f64degree at x
center()f64exact analytic center: triangle → b, trapezoid → (b + c) / 2, gaussian/bell/sigmoid → center, singleton → x. This is the z used by Sugeno inference.
shape()&'static str"Triangular", "Trapezoidal", "Gaussian", "Singleton", "Bell", "Sigmoid"
key_points()Vec<f64>points where the shape changes (feet, peaks, ±3σ, …); useful for plotting
Displaythe constructor call, e.g. triangle(0, 5, 10)

IT2MF: interval type-2​

An interval type-2 MF is a band between an upper MF (UMF) and a lower MF (LMF). The band is the footprint of uncertainty (FOU). At each x it returns an interval (lower, upper) instead of a single number. See Concepts → Interval type-2.

use ferrifuzz::prelude::*;

let fou = mf::it2(
mf::triangle(0.0, 5.0, 10.0)?, // upper (wider)
mf::triangle(2.0, 5.0, 8.0)?, // lower (narrower)
)?;
let (lo, up) = fou.membership(3.0); // (0.333…, 0.6)
assert!(lo <= up);
ItemDescription
IT2MF::new(upper, lower) -> Result<IT2MF>Same as mf::it2.
IT2MF::with_lower_height(upper, lower, h) -> Result<IT2MF>Scales the lower MF by h ∈ (0, 1], which gives the textbook "shorter inner triangle".
membership(x) -> (f64, f64)(h · lower(x), upper(x)). The order is (lower, upper).
upper(), lower() -> Type1MFThe two bounding MFs.
lower_height() -> f641.0 unless built with with_lower_height.
center() -> f64Midpoint of the upper and lower centers.

Validation. Both constructors check that the lower MF never rises above the upper one. They test both shapes' key points plus a 129-point grid between them, and return Error::InvalidParameter if the check fails. Swapped arguments are the usual cause:

IT2 lower MF triangle(0, 5, 10) is above upper MF triangle(2, 5, 8) at x=2 (0.4000 > 0.0000); the order is it2(upper, lower) — are they swapped?

with_lower_height also rejects h outside (0, 1].

Argument order

The constructor takes (upper, lower), but membership returns (lower, upper). That way the returned interval reads left to right like [a, b] with a ≤ b.

See also​