11
22import matplotlib .pyplot as plt
3- from numpy import arange , fromiter , array_equal
3+ from numpy import arange , fromiter , array_equal , less_equal , greater_equal , less , greater
4+ import numpy as np
45from logging import warn
56import pickle
67
7- from fuzzy .functions import inv
8+ from fuzzy .functions import inv , normalize
89from fuzzy .combinators import MAX , MIN , product , bounded_sum
910
1011class FuzzyWarning (UserWarning ):
@@ -179,7 +180,7 @@ def __pow__(self, power):
179180 return Set (lambda x : pow (self .func (x ), power ))
180181
181182 def __eq__ (self , other ):
182- """A set is equal with another if it returns exactly the same values."""
183+ """A set is equal with another if both return the same values over the same range ."""
183184 if self .domain is None or other .domain is None :
184185 # It would require complete AST analysis to check whether both Sets
185186 # represent the same recursive functions -
@@ -190,7 +191,37 @@ def __eq__(self, other):
190191 # however, if domains ARE assigned (whether or not it's the same domain),
191192 # we simply can check if they map to the same values
192193 return array_equal (self .array (), other .array ())
194+
195+ def __le__ (self , other ):
196+ """If this is less than other, it means this is a subset of the other."""
197+ if self .domain is None or other .domain is None :
198+ raise FuzzyWarning ("Can't compare without Domains." )
199+ return all (less_equal (self .array (), other .array ()))
200+
201+ def __lt__ (self , other ):
202+ """If this is less than other, it means this is a subset of the other."""
203+ if self .domain is None or other .domain is None :
204+ raise FuzzyWarning ("Can't compare without Domains." )
205+ return all (less (self .array (), other .array ()))
206+
207+ def __ge__ (self , other ):
208+ """If this is greater than other, it means this is a superset of the other."""
209+ if self .domain is None or other .domain is None :
210+ raise FuzzyWarning ("Can't compare without Domains." )
211+ return all (greater_equal (self .array (), other .array ()))
193212
213+ def __gt__ (self , other ):
214+ """If this is less than other, it means this is a subset of the other."""
215+ if self .domain is None or other .domain is None :
216+ raise FuzzyWarning ("Can't compare without Domains." )
217+ return all (greater (self .array (), other .array ()))
218+
219+ def __len__ (self ):
220+ if self .domain is None :
221+ raise FuzzyWarning ("No domain, can't determine length." )
222+ return len (self .array ())
223+
224+
194225 def plot (self , low = None , high = None , res = None ):
195226 """Graph the set.
196227 Use the bounds and resolution of the domain to display the set
@@ -219,7 +250,9 @@ def __repr__(self):
219250 Return a string representation of the Set that reconstructs the set with eval().
220251
221252 *******
222- this is harder than expected since all functions are (recursive!) closures which
253+ Current implementation does NOT work correctly.
254+
255+ This is harder than expected since all functions are (recursive!) closures which
223256 can't simply be pickled. If this functionality really is needed, all functions
224257 would have to be peppered with closure-returning overhead such as
225258
@@ -232,15 +265,21 @@ def create_function_closure():
232265 func = types.FunctionType(*args[:-1] + [closure])
233266 return func
234267 """
235- return NotImplemented
236- #return f"Set({}, domain={self.domain}, name={self.name})"
268+ return f"Set({ self .func } , domain={ self .domain } , name={ self .name } )"
237269
238270 def __str__ (self ):
239271 if self .name is None and self .domain is None :
240272 return f"dangling Set({ self .func } )"
241273 else :
242274 return f"{ self .domain } .{ self .name } "
243-
275+
276+ def normalized (self ):
277+ if self .domain is None :
278+ raise FuzzyWarning ("Can't normalize without domain." )
279+ else :
280+ return Set (normalize (max (self .array ()), self .func ),
281+ domain = self .domain ,
282+ name = f"normalized_{ self .name } " )
244283
245284class Rule :
246285 """
0 commit comments