@@ -39,8 +39,8 @@ class FuzzyWarning(UserWarning):
39
39
pass
40
40
41
41
42
- NO_DOMAIN_TO_COMPARE = "No domains to compare to ."
43
- CANT_COMPARE_DOMAINS = "Can't compare different domains."
42
+ NO_DOMAIN_TO_COMPARE = "Domain can't work with no domain ."
43
+ CANT_COMPARE_DOMAINS = "Can't work with different domains."
44
44
NO_DOMAIN = "No domain defined."
45
45
46
46
@@ -253,14 +253,17 @@ def __call__(
253
253
254
254
def __invert__ (self ) -> Set :
255
255
"""Return a new set with 1 - function."""
256
+ assert self .domain is not None , NO_DOMAIN
256
257
return Set (inv (self .func ), domain = self .domain )
257
258
258
259
def __neg__ (self ) -> Set :
259
260
"""Synonyme for invert."""
261
+ assert self .domain is not None , NO_DOMAIN
260
262
return Set (inv (self .func ), domain = self .domain )
261
263
262
264
def __and__ (self , other : Set ) -> Set :
263
265
"""Return a new set with modified function."""
266
+ assert self .domain is not None and other .domain is not None , NO_DOMAIN_TO_COMPARE
264
267
assert self .domain == other .domain
265
268
return Set (MIN (self .func , other .func ), domain = self .domain )
266
269
@@ -380,8 +383,7 @@ def multiplied(self, n: float) -> Set:
380
383
381
384
def plot (self ) -> None :
382
385
"""Graph the set in the given domain."""
383
- if self .domain is None :
384
- raise FuzzyWarning ("No domain assigned, cannot plot." )
386
+ assert self .domain is not None , NO_DOMAIN
385
387
R = self .domain .range
386
388
V = [self .func (x ) for x in R ]
387
389
if plt :
@@ -393,21 +395,19 @@ def plot(self) -> None:
393
395
394
396
def array (self ) -> Array :
395
397
"""Return an array of all values for this set within the given domain."""
396
- if self .domain is None :
397
- raise FuzzyWarning ("No domain assigned." )
398
+ assert self .domain is not None , NO_DOMAIN
398
399
return np .fromiter ((self .func (x ) for x in self .domain .range ), float )
399
400
400
401
def range (self ) -> Array :
401
402
"""Return the range of the domain."""
402
- if self .domain is None :
403
- raise FuzzyWarning ("No domain assigned." )
403
+ assert self .domain is not None , NO_DOMAIN
404
404
return self .domain .range
405
405
406
406
def center_of_gravity (self ) -> float :
407
407
"""Return the center of gravity for this distribution, within the given domain."""
408
408
if self .__center_of_gravity is not None :
409
409
return self .__center_of_gravity
410
- assert self .domain is not None , "No center of gravity with no domain."
410
+ assert self .domain is not None , NO_DOMAIN
411
411
weights = self .array ()
412
412
if sum (weights ) == 0 :
413
413
return 0
@@ -431,8 +431,7 @@ def __str__(self) -> str:
431
431
432
432
def normalized (self ) -> Set :
433
433
"""Return a set that is normalized *for this domain* with 1 as max."""
434
- if self .domain is None :
435
- raise FuzzyWarning ("Can't normalize without domain." )
434
+ assert self .domain is not None , NO_DOMAIN
436
435
return Set (normalize (max (self .array ()), self .func ), domain = self .domain )
437
436
438
437
def __hash__ (self ) -> int :
0 commit comments