Package fuzzy :: Package norm :: Module Norm
[hide private]
[frames] | no frames]

Source Code for Module fuzzy.norm.Norm

  1  # -*- coding: utf-8 -*- 
  2  # 
  3  # Copyright (C) 2009  Rene Liebscher 
  4  # 
  5  # This program is free software; you can redistribute it and/or modify it under 
  6  # the terms of the GNU Lesser General Public License as published by the Free  
  7  # Software Foundation; either version 3 of the License, or (at your option) any 
  8  # later version. 
  9  # 
 10  # This program is distributed in the hope that it will be useful, but WITHOUT  
 11  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 12  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 
 13  # details. 
 14  #  
 15  # You should have received a copy of the GNU Lesser General Public License 
 16  # along with this program; if not, see <http://www.gnu.org/licenses/>.  
 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 
25 -class NormException(FuzzyException):
26 """Base class for any exception in norm calculations.""" 27 pass
28 29
30 -class Norm(object):
31 """Abstract Base class of any fuzzy norm""" 32 33 # types of norm 34 UNKNOWN = 0 #: type of norm unknown 35 T_NORM = 1 #: norm is t-norm 36 S_NORM = 2 #: norm is s-norm 37
38 - def __init__(self, type=UNKNOWN):
39 """Initialize type of norm""" 40 self._type = type
41
42 - def __call__(self, *args):
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
54 - def getType(self):
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
64 - def checkArgs2(self, args):
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
76 - def checkArgsN(self, args):
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
88 - def __repr__(self):
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
96 -def product(*args):
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
110 -def sum(*args):
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