Skip to content

Commit b2aed6e

Browse files
committed
testing bounded_linear, R, S..
1 parent 517bc5c commit b2aed6e

File tree

2 files changed

+48
-12
lines changed

2 files changed

+48
-12
lines changed

fuzzy/functions.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"""
3030

3131

32-
from math import exp, log, sqrt
32+
from math import exp, log, sqrt, isinf
3333

3434
#####################
3535
# SPECIAL FUNCTIONS #
@@ -178,17 +178,37 @@ def bounded_linear(low_bound, high_bound, core_m=1, unsupported_m=0):
178178
1.0
179179
"""
180180
assert high_bound > low_bound, 'high must not be less than low.'
181-
assert core_m != unsupported_m, "core_m must differ from unsupported_m"
181+
assert core_m > unsupported_m, "core_m must differ from unsupported_m"
182182

183183
gradient = (core_m - unsupported_m) / (high_bound - low_bound)
184-
184+
185+
# special cases found by hypothesis
186+
187+
def g_0(x):
188+
return (core_m + unsupported_m) / 2
189+
190+
if gradient == 0:
191+
return g_0
192+
193+
def g_inf(x):
194+
asympt = (high_bound + low_bound) / 2
195+
if x < asympt:
196+
return unsupported_m
197+
elif x > asympt:
198+
return core_m
199+
else:
200+
return (core_m + unsupported_m) / 2
201+
202+
if isinf(gradient):
203+
return g_inf
204+
185205
def f(x):
186-
m = gradient * (x - low_bound) + unsupported_m
187-
if m < 0:
206+
y = gradient * (x - low_bound) + unsupported_m
207+
if y < 0:
188208
return 0.
189-
if m > 1:
209+
if y > 1:
190210
return 1.
191-
return m
211+
return y
192212
return f
193213

194214

@@ -198,8 +218,7 @@ def R(left, right):
198218
USE THE S() FUNCTION FOR NEGATIVE SLOPE.
199219
"""
200220

201-
if left >= right:
202-
raise ValueError("left must be less than right.")
221+
assert left < right, "left must be less than right."
203222

204223
def f(x):
205224
if x < left:
@@ -247,8 +266,7 @@ def rectangular(left, right, core_m=1, unsupported_m=0):
247266
"height" of the rectangle
248267
"""
249268

250-
if left > right:
251-
raise ValueError('left must not be greater than right.')
269+
assert left < right, 'left must not be greater than right.'
252270

253271
def f(x):
254272
if x < left:

test_fuzzy_units.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@ def test_linear(self, x, m, b):
6363
st.floats(min_value=0, max_value=1))
6464
def test_bounded_linear(self, x, low_bound, high_bound, core_m, unsupported_m):
6565
assume(low_bound < high_bound)
66-
assume(core_m != unsupported_m)
66+
assume(core_m > unsupported_m)
6767
f = fun.bounded_linear(low_bound, high_bound, core_m, unsupported_m)
68+
assert (0 <= f(x) <= 1)
69+
70+
@given(st.floats(allow_nan=False),
71+
st.floats(allow_nan=False, allow_infinity=False),
72+
st.floats(allow_nan=False, allow_infinity=False),
73+
)
74+
def test_R(self, x, left, right):
75+
assume(left < right)
76+
f = fun.R(left, right)
77+
assert (0 <= f(x) <= 1)
78+
79+
@given(st.floats(allow_nan=False),
80+
st.floats(allow_nan=False, allow_infinity=False),
81+
st.floats(allow_nan=False, allow_infinity=False),
82+
)
83+
def test_S(self, x, left, right):
84+
assume(left < right)
85+
f = fun.S(left, right)
6886
assert (0 <= f(x) <= 1)

0 commit comments

Comments
 (0)