1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """
19 Abstract base class for any kind of fuzzy norm.
20 """
21
22 __revision__ = "$Id: Norm.py,v 1.16 2010-02-17 19:45:00 rliebscher Exp $"
23
24 from fuzzy.Exception import FuzzyException
26 """Base class for any exception in norm calculations."""
27 pass
28
29
31 """Abstract Base class of any fuzzy norm"""
32
33
34 UNKNOWN = 0
35 T_NORM = 1
36 S_NORM = 2
37
39 """Initialize type of norm"""
40 self._type = type
41
43 """
44 Calculate result of norm(arg1,arg2,...)
45
46 @param args: list of floats as arguments for norm.
47 @type args: list of float
48 @return: result of norm calulation
49 @rtype: float
50 @raise NormException: any problem in calculation (wrong number of arguments, numerical problems)
51 """
52 raise NotImplementedError("abstract class %s can't be called" % self.__class__.__name__)
53
55 """
56 Return type of norm:
57 0 = not defined or not classified
58 1 = t-norm ( = Norm.T_NORM)
59 2 = s-norm ( = Norm.S_NORM)
60
61 """
62 return self._type
63
65 """Checks args to be 2 float values.
66
67 @param args: list of arguments
68 @type args: list of float?
69 @return: first two args as float values
70 @rtype: (float,float)
71 """
72 if len(args) != 2:
73 raise NormException("%s is supported only for 2 arguments" % self.__class__.__name__ )
74 return float(args[0]), float(args[1])
75
77 """Checks args to be at least 2 float values.
78
79 @param args: list of arguments
80 @type args: list of float?
81 @return: arguments as float values
82 @rtype: list of float
83 """
84 if len(args) < 2:
85 raise NormException("%s is supported only for more the 2 arguments" % self.__class__.__name__ )
86 return [float(x) for x in args]
87
89 """Return representation of instance.
90
91 @return: representation of instance
92 @rtype: string
93 """
94 return "%s.%s()" % (self.__class__.__module__, self.__class__.__name__)
95
97 """Calculate product of args.
98
99 @param args: list of floats to multiply
100 @type args: list of float
101 @return: product of args
102 @rtype: float
103 """
104 r = args[0]
105 for x in args[1:]:
106 r *= x
107 return r
108
109
111 """Calculate sum of args.
112
113 If using numpy the builtin sum doesn't work always!
114
115 @param args: list of floats to sum
116 @type args: list of float
117 @return: sum of args
118 @rtype: float
119 """
120 r = args[0]
121 for x in args[1:]:
122 r += x
123 return r
124