Skip to content

Commit 88b3c80

Browse files
committed
simple_disjoint_sum
1 parent 4d443fc commit 88b3c80

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

Showcase.ipynb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,18 @@
134134
},
135135
{
136136
"cell_type": "code",
137-
"execution_count": 28,
137+
"execution_count": 29,
138138
"metadata": {
139139
"collapsed": false
140140
},
141141
"outputs": [
142142
{
143143
"data": {
144144
"text/plain": [
145-
"<function all>"
145+
"False"
146146
]
147147
},
148-
"execution_count": 28,
148+
"execution_count": 29,
149149
"metadata": {},
150150
"output_type": "execute_result"
151151
}

fuzzy/classes.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import pickle
77

88
from fuzzy.functions import inv, normalize
9-
from fuzzy.combinators import MAX, MIN, product, bounded_sum
9+
from fuzzy.combinators import MAX, MIN, product, bounded_sum, simple_disjoint_sum
1010

1111
class FuzzyWarning(UserWarning):
1212
pass
@@ -174,6 +174,9 @@ def __mul__(self, other):
174174

175175
def __add__(self, other):
176176
return Set(bounded_sum(self.func, other.func))
177+
178+
def __xor__(self, other):
179+
return Set(simple_disjoint_sum(self.func, other.func))
177180

178181
def __pow__(self, power):
179182
"""pow is used with hedges"""
@@ -218,9 +221,18 @@ def __gt__(self, other):
218221

219222
def __len__(self):
220223
if self.domain is None:
221-
raise FuzzyWarning("No domain, can't determine length.")
224+
raise FuzzyWarning("No domain.")
222225
return len(self.array())
223-
226+
227+
def cardinality(self):
228+
if self.domain is None:
229+
raise FuzzyWarning("No domain.")
230+
return sum(self.array())
231+
232+
def relative_cardinality(self):
233+
if self.domain is None:
234+
raise FuzzyWarning("No domain.")
235+
return self.cardinality() / len(self)
224236

225237
def plot(self, low=None, high=None, res=None):
226238
"""Graph the set.

fuzzy/combinators.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,13 @@ def f(z):
120120
x, y = a(z), b(z)
121121
return (x * y) ** (1 - g) * ((1 - x) * (1 - y)) ** g
122122
return f
123-
return e
123+
return e
124+
125+
def simple_disjoint_sum(a, b):
126+
"""Implements a simple fuzzy XOR operation.
127+
(A AND ~B) OR (~A AND B)
128+
"""
129+
def f(z):
130+
x, y = a(z), b(z)
131+
return max(min(x, 1-y), min(1-x, y))
132+
return f

test_fuzzy_units.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,12 @@ def test_gamma_op(self, x, g):
278278
f = g(a, b)
279279
assert (0 <= f(x) <= 1)
280280

281+
@given(st.floats(min_value=0, max_value=1))
282+
def test_hamacher_sum(self, x):
283+
a = fun.noop()
284+
b = fun.noop()
285+
f = combi.simple_disjoint_sum(a, b)
286+
assert (0 <= f(x) <= 1)
281287

282288
class Test_Set(TestCase):
283289
@skip("repr is complicated")

0 commit comments

Comments
 (0)