Skip to content

Commit f26e7fd

Browse files
committed
little more testing..
1 parent a0d18a7 commit f26e7fd

File tree

4 files changed

+67
-40
lines changed

4 files changed

+67
-40
lines changed

.cache/v/cache/lastfailed

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
{}
1+
{
2+
"test_fuzzy_units.py::test_alpha": true
3+
}

Showcase.ipynb

Lines changed: 22 additions & 24 deletions
Large diffs are not rendered by default.

fuzzy/functions.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ def f(x):
3838
return x
3939
return f
4040

41-
def inv(func):
42-
"""Invert the given function within the unit-interval.
41+
def inv(func=None):
42+
"""Invert the given function or value within the unit-interval.
4343
>>> f = inv(constant(0))
4444
>>> f(0.7)
4545
0.3
4646
"""
4747

48-
def f(x):
48+
def f_func(x):
4949
return 1 - func(x)
50-
return f
50+
51+
def f_value(x):
52+
return 1 - x
53+
54+
return f_func if func is not None else f_value
5155

5256

5357
def constant(c):
@@ -57,16 +61,13 @@ def constant(c):
5761
1
5862
"""
5963

60-
if not(0 <= c <= 1):
61-
raise ValueError
62-
6364
def f(x):
6465
return c
6566
return f
6667

6768

68-
def alpha(func, ceiling=1, floor=0):
69-
"""Function to clip other functions.
69+
def alpha(func=None, floor=0, ceiling=1):
70+
"""Function to clip functions or values.
7071
This is used to either cut off the upper or lower part of a graph.
7172
7273
>>> s = singleton(2)
@@ -83,12 +84,19 @@ def alpha(func, ceiling=1, floor=0):
8384
if ceiling < floor:
8485
raise ValueError('ceiling must not be less than floor.')
8586

86-
def f(x):
87+
def f_func(x):
8788
if func(x) >= ceiling:
8889
return ceiling
8990
if func(x) <= floor:
9091
return floor
91-
return f
92+
93+
def f_value(x):
94+
if x >= ceiling:
95+
return ceiling
96+
if x <= floor:
97+
return floor
98+
99+
return f_value if func is None else f_func
92100

93101

94102
def singleton(p, non_p_m=0, p_m=1):

test_fuzzy_units.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,34 @@ def test_noop(x):
99
f = fun.noop()
1010
assert f(x) == x
1111

12-
@given(st.floats(min_value=0, max_value=1))
12+
@given(st.floats(allow_nan=False, allow_infinity=False))
1313
def test_invert(x):
14+
f = fun.inv()
15+
assert f(x) == 1 - x
16+
1417
n = fun.noop()
1518
f = fun.inv(n)
1619
assert f(x) == 1 - x
20+
1721

1822

19-
@given(st.floats(min_value=0, max_value=1),
20-
st.floats(min_value=0, max_value=1))
23+
@given(st.floats(allow_nan=False, allow_infinity=False),
24+
st.floats(allow_nan=False, allow_infinity=False))
2125
def test_constant(c, r):
2226
f = fun.constant(c)
23-
assert f(r) == c
27+
assert f(r) == c
28+
29+
30+
@given(st.floats(min_value=0, max_value=1),
31+
st.floats(min_value=0, max_value=1),
32+
st.floats(allow_nan=False))
33+
def test_alpha(lower, upper, x):
34+
if lower >= upper:
35+
return
36+
f = fun.alpha(floor=lower, ceiling=upper)
37+
if x <= lower:
38+
assert f(x) == lower
39+
if x >= upper:
40+
assert f(x) == upper
41+
else:
42+
assert f(x) == x

0 commit comments

Comments
 (0)