Functions
This modules enables applying functions to Fuzzy Sets.
real_to_real uses the simple method of apply a function element-wise to the set.
Apply uses the grid method to apply a function to a set. This enables functions with more than one fuzzy set as an input.
- fuzzy_sets.functions.apply(func: Callable[[float], float], *input_sets: FuzzySet) FuzzySet
Applies a function to any number of fuzzy sets using the alpha cut method
Note
You must provide as many fuzzy sets as the function expects inputs.
- Parameters:
func (Callable[[float], float]) – The function to the provided fuzzy sets
*input_sets (FuzzySet) – The fuzzy sets to apply the function to
- Raises:
TypeError – If any fuzzy set does not contain real number values (e.g. strings)
ValueError – If the number of fuzzy sets does not match the number of inputs the function expects
- Returns:
The resulting fuzzy set
- Return type:
Examples
>>> die_high_scores = FuzzySet( { FuzzySetMember(1, 0.1), FuzzySetMember(2, 0.2), FuzzySetMember(3, 0.4), FuzzySetMember(4, 0.6), FuzzySetMember(5, 0.8), FuzzySetMember(6, 1.0), } ) >>> print("Die high scores:", die_high_scores) Die high scores: 1/0.1 + 2/0.2 + 3/0.4 + 4/0.6 + 5/0.8 + 6/1.0 >>> squared = fuzzy_sets.functions.apply(lambda x: x**2, die_high_scores) >>> print("Squared:", squared) Squared: 1/0.1 + 4/0.2 + 9/0.4 + 16/0.6 + 25/0.8 + 36/1.0
>>> die_low_scores = FuzzySet( { FuzzySetMember(1, 1.0), FuzzySetMember(2, 0.8), FuzzySetMember(3, 0.6), FuzzySetMember(4, 0.4), FuzzySetMember(5, 0.2), FuzzySetMember(6, 0.1), } ) >>> print("Die low scores:", die_low_scores) Die low scores: 1/1.0 + 2/0.8 + 3/0.6 + 4/0.4 + 5/0.2 + 6/0.1 >>> added = fuzzy_sets.functions.apply(lambda x, y: x + y, die_high_scores, die_low_scores) >>> print("Added:", added) 2/0.1 + 12/0.1 + 3/0.2 + 11/0.2 + 4/0.4 + 10/0.4 + 5/0.6 + 9/0.6 + 6/0.8 + 8/0.8 + 7/1.0
- fuzzy_sets.functions.get_alpha_overlap(alpha_ranges: list[tuple[float, float]]) tuple[float, float]
- Takes in a list of alpha ranges and returns the overlapping alpha range
This is used by the apply function to find if a given combination of alpha ranges overlap.
- Parameters:
*alpha_ranges (tuple[float, float]) – A list of alpha ranges
- Returns:
The overlapping alpha range
- Return type:
tuple[float, float]
Examples
>>> get_alpha_overlap((0, 0.5), (0.25, 0.75)) (0.25, 0.5)
- fuzzy_sets.functions.real_to_real(func: Callable[[float], float], fuzzy_set: FuzzySet) FuzzySet
Takes in a function and a fuzzy set and returns a new fuzzy set with the function applied to each member’s value
The function must take in a single real number and return a single real number.
- Parameters:
func (Callable[[float], float]) – The function to apply to each member’s value
fuzzy_set (FuzzySet) – The fuzzy set to apply the function to
- Raises:
TypeError – If the fuzzy set does not contain real values
- Returns:
The new fuzzy set with the function applied to each member’s value
- Return type:
Examples
>>> fuzzy_set = FuzzySet({FuzzySetMember(1, 1), FuzzySetMember(2, 0.5), FuzzySetMember(3, 0.25)}) >>> print(fuzzy_set) 1/1 + 2/0.5 + 3/0.25 >>> squared = real_to_real(lambda x: x ** 2, fuzzy_set) >>> print(squared) 1/1 + 4/0.5 + 9/0.25