FRsūtil̲s
A fuzzy-rough reasoning utilities library
 
Loading...
Searching...
No Matches
tnorms.py
Go to the documentation of this file.
1"""
2T-norms
3Provides an extensible and optimized framework to compute T-norms
4"""
5
6import numpy as np
7from abc import ABC, abstractmethod
8
9class TNorm(ABC):
10 """
11 Abstract base class for all T-norms.
12
13 Provides a standard interface for pairwise and reduction operations.
14
15 """
16
17 @abstractmethod
18 def __call__(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
19 """
20 Apply the T-norm to two arrays element-wise.
21
22 @param a: First input array.
23 @param b: Second input array.
24 @return: Element-wise result of the T-norm.
25 """
26 pass
27
28 @abstractmethod
29 def reduce(self, arr: np.ndarray) -> np.ndarray:
30 """
31 Reduce a single array using the T-norm.
32
33 @param arr: Array of shape (n_samples, n_samples).
34 @return: Reduced value for each row/column on axis=1.
35 """
36 pass
37
38
40 """
41 Minimum T-norm: min(a, b)
42 """
43
44 def __call__(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
45 return np.minimum(a, b)
46
47 def reduce(self, arr: np.ndarray) -> np.ndarray:
48 return np.min(arr, axis=0)
49
50
52 """
53 Product T-norm: a * b
54 """
55
56 def __call__(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
57 return a * b
58
59 def reduce(self, arr: np.ndarray) -> np.ndarray:
60 return np.prod(arr, axis=0)
61
62
63
65 """
66 Łukasiewicz T-norm: max(0, a + b - 1)
67 """
68
69 def __call__(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
70 return np.maximum(0.0, a + b - 1.0)
71
72 def reduce(self, arr: np.ndarray) -> np.ndarray:
73 result = arr[0]
74 for x in arr[1:]:
75 result = max(0.0, result + x - 1.0)
76 return result
77
78
79# TODO: uncomment and test this TNorm
80# class YagerTNorm(TNorm):
81# """
82# Yager T-norm: 1 - min(1, [(1 - a)^p + (1 - b)^p]^(1/p))
83
84# @param p: The exponent parameter (default: 2.0)
85# """
86
87# def __init__(self, p: float = 2.0):
88# self.p = p
89
90# def __call__(self, a: np.ndarray, b: np.ndarray) -> np.ndarray:
91# return 1.0 - np.minimum(
92# 1.0, ((1.0 - a) ** self.p + (1.0 - b) ** self.p) ** (1.0 / self.p)
93# )
94
95# def reduce(self, arr: np.ndarray) -> np.ndarray:
96# return 1.0 - np.minimum(
97# 1.0, np.sum((1.0 - arr) ** self.p, axis=0) ** (1.0 / self.p)
98# )
99
100# example usage
101# tnorm = YagerTNorm(p=3.0)
102# tnorm = ProductTNorm()
103# result_pairwise = tnorm(arr1, arr2)
104# result_reduced = tnorm.reduce(arr2d)
np.ndarray reduce(self, np.ndarray arr)
Definition tnorms.py:72
np.ndarray __call__(self, np.ndarray a, np.ndarray b)
Definition tnorms.py:69
np.ndarray reduce(self, np.ndarray arr)
Definition tnorms.py:47
np.ndarray __call__(self, np.ndarray a, np.ndarray b)
Definition tnorms.py:44
np.ndarray reduce(self, np.ndarray arr)
Definition tnorms.py:59
np.ndarray __call__(self, np.ndarray a, np.ndarray b)
Definition tnorms.py:56
np.ndarray reduce(self, np.ndarray arr)
Definition tnorms.py:29
np.ndarray __call__(self, np.ndarray a, np.ndarray b)
Definition tnorms.py:18