Skip to content

Commit cf22ef7

Browse files
committed
rules.scale added and tested.
1 parent b72d10e commit cf22ef7

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

fuzzy/rules.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
11

2-
def scale(x, *, IN_min=0, IN_max=1, OUT_min, OUT_max):
3-
return (OUT_max - OUT_min)*(x - IN_min) / (IN_max - IN_min) + OUT_min
2+
from math import isinf
3+
4+
def scale(OUT_min, OUT_max, *, IN_min=0, IN_max=1):
5+
"""Scale from one domain to another.
6+
7+
Works best for [0,1] -> R. For R -> R additional testing is required.
8+
"""
9+
assert IN_min < IN_max
10+
# this is not arbitrary. lim x -> inf x*0+x -> inf
11+
if isinf(OUT_max - OUT_min):
12+
return lambda x: OUT_max
13+
14+
def f(x):
15+
return (OUT_max - OUT_min)*(x - IN_min) / (IN_max - IN_min) + OUT_min
16+
return f

test_fuzzy_units.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
from unittest import TestCase, skip
55
import numpy as np
66

7-
from fuzzy.classes import Domain, Set, Rule
7+
from fuzzy.classes import Domain, Set
88
from fuzzy import functions as fun
99
from fuzzy import hedges
1010
from fuzzy import combinators as combi
11+
import fuzzy.rules as ru
1112

1213
class Test_Functions(TestCase):
1314
@given(st.floats(allow_nan=False))
@@ -343,4 +344,18 @@ def test_complement(self):
343344
D = Domain("d", 0, 10, res=0.1)
344345
D.s1 = Set(fun.bounded_linear(3, 12))
345346
D.s2 = ~~D.s1
346-
assert all(np.isclose(D.s1.array(), D.s2.array()))
347+
assert all(np.isclose(D.s1.array(), D.s2.array()))
348+
349+
350+
class Test_Rules(TestCase):
351+
@given(st.floats(min_value=0, max_value=1),
352+
st.floats(min_value=0, max_value=1),
353+
st.floats(min_value=0, max_value=1),
354+
st.floats(allow_infinity=False, allow_nan=False),
355+
st.floats(allow_infinity=False, allow_nan=False))
356+
def test_scaling(self, x, IN_min, IN_max, OUT_min, OUT_max):
357+
assume(IN_min < IN_max)
358+
assume(IN_min <= x <= IN_max)
359+
assume(OUT_min < OUT_max)
360+
f = ru.scale(OUT_min, OUT_max, IN_min=IN_min, IN_max=IN_max)
361+
assert (OUT_min <= f(x) <= OUT_max)

0 commit comments

Comments
 (0)