Python math.nan() Examples

The following are 30 code examples of math.nan(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module math , or try the search function .
Example #1
Source File: defuzzifier.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def defuzzify(self, term: Term, minimum: float, maximum: float) -> float:
        if not math.isfinite(minimum + maximum):
            return nan
        resolution = self.resolution
        dx = (maximum - minimum) / resolution
        counter = resolution
        left = right = 0
        x_left, x_right = (minimum, maximum)
        left_area = right_area = 0.0

        # TODO: Improve?
        while counter > 0:
            counter = counter - 1
            if left_area <= right_area:
                x_left = minimum + (left + 0.5) * dx
                left_area += term.membership(x_left)
                left += 1
            else:
                x_right = maximum - (right + 0.5) * dx
                right_area += term.membership(x_right)
                right += 1

        # Inverse weighted average to compensate
        return (left_area * x_right + right_area * x_left) / (left_area + right_area) 
Example #2
Source File: test_variable.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_fuzzy_value(self) -> None:
        InputVariableAssert(self, fl.InputVariable(name="name",
                                                   description="description",
                                                   minimum=-1.0,
                                                   maximum=1.0,
                                                   terms=[
                                                       fl.Triangle('Low', -1.0, -1.0, 0.0),
                                                       fl.Triangle('Medium', -0.5, 0.0, 0.5),
                                                       fl.Triangle('High', 0.0, 1.0, 1.0)
                                                   ])) \
            .fuzzy_values({-1.00: "1.000/Low + 0.000/Medium + 0.000/High",
                           -0.50: "0.500/Low + 0.000/Medium + 0.000/High",
                           -0.25: "0.250/Low + 0.500/Medium + 0.000/High",
                           0.00: "0.000/Low + 1.000/Medium + 0.000/High",
                           0.25: "0.000/Low + 0.500/Medium + 0.250/High",
                           0.50: "0.000/Low + 0.000/Medium + 0.500/High",
                           0.75: "0.000/Low + 0.000/Medium + 0.750/High",
                           1.00: "0.000/Low + 0.000/Medium + 1.000/High",
                           math.nan: "nan/Low + nan/Medium + nan/High",
                           math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
                           -math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
                           }) 
Example #3
Source File: test_variable.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_highest_membership(self) -> None:
        low, medium, high = (fl.Triangle('Low', -1.0, -.5, 0.0),
                             fl.Triangle('Medium', -0.5, 0.0, 0.5),
                             fl.Triangle('High', 0.0, .5, 1.0))
        VariableAssert(self, fl.Variable(name="name",
                                         description="description",
                                         minimum=-1.0,
                                         maximum=1.0,
                                         terms=[low, medium, high])) \
            .highest_memberships(
            {-1.00: (0.0, None),
             -0.75: (0.5, low),
             -0.50: (1.0, low),
             -0.25: (0.5, low),
             0.00: (1.0, medium),
             0.25: (0.5, medium),
             0.50: (1.0, high),
             0.75: (0.5, high),
             1.00: (0.0, None),
             math.nan: (0.0, None),
             math.inf: (0.0, None),
             -math.inf: (0.0, None),
             }) 
Example #4
Source File: test_variable.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_fuzzify(self) -> None:
        VariableAssert(self, fl.Variable(name="name",
                                         description="description",
                                         minimum=-1.0,
                                         maximum=1.0,
                                         terms=[
                                             fl.Triangle('Low', -1.0, -1.0, 0.0),
                                             fl.Triangle('Medium', -0.5, 0.0, 0.5),
                                             fl.Triangle('High', 0.0, 1.0, 1.0)
                                         ])) \
            .fuzzy_values(
            {-1.00: "1.000/Low + 0.000/Medium + 0.000/High",
             -0.50: "0.500/Low + 0.000/Medium + 0.000/High",
             -0.25: "0.250/Low + 0.500/Medium + 0.000/High",
             0.00: "0.000/Low + 1.000/Medium + 0.000/High",
             0.25: "0.000/Low + 0.500/Medium + 0.250/High",
             0.50: "0.000/Low + 0.000/Medium + 0.500/High",
             0.75: "0.000/Low + 0.000/Medium + 0.750/High",
             1.00: "0.000/Low + 0.000/Medium + 1.000/High",
             math.nan: "nan/Low + nan/Medium + nan/High",
             math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
             -math.inf: "0.000/Low + 0.000/Medium + 0.000/High",
             }) 
Example #5
Source File: test_hedge.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_lambda(self) -> None:
        HedgeAssert(self, fl.HedgeLambda("my_hedge", lambda x: (
            2.0 * x * x if x <= 0.5 else (1.0 - 2.0 * (1.0 - x) * (1.0 - x))))) \
            .has_name("my_hedge") \
            .evaluates({-1.0: 2.0,
                        -0.5: 0.5,
                        0.00: 0.0,
                        0.25: 0.125,
                        0.50: 0.5,
                        0.75: 0.875,
                        1.00: 1.0,
                        inf: -inf,
                        -inf: inf,
                        nan: nan})

        if __name__ == '__main__':
            unittest.main() 
Example #6
Source File: test_hedge.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_somewhat(self) -> None:
        with self.assertRaisesRegex(ValueError,
                                    r"math domain error"):
            HedgeAssert(self, fl.Somewhat()).evaluates({-1.0: nan})
            HedgeAssert(self, fl.Somewhat()).evaluates({-0.5: nan})
            HedgeAssert(self, fl.Somewhat()).evaluates({-inf: nan})

        HedgeAssert(self, fl.Somewhat()) \
            .has_name("somewhat") \
            .evaluates({0.00: 0.0,
                        0.25: 0.5,
                        0.50: 0.7071067811865476,
                        0.75: 0.8660254037844386,
                        1.00: 1.0,
                        inf: inf,
                        nan: nan}) 
Example #7
Source File: test_hedge.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_seldom(self) -> None:
        with self.assertRaisesRegex(ValueError,
                                    r"math domain error"):
            HedgeAssert(self, fl.Seldom()).evaluates({-1.0: nan})
            HedgeAssert(self, fl.Seldom()).evaluates({-0.5: nan})
            HedgeAssert(self, fl.Seldom()).evaluates({inf: nan})
            HedgeAssert(self, fl.Seldom()).evaluates({-inf: nan})

        HedgeAssert(self, fl.Seldom()) \
            .has_name("seldom") \
            .evaluates({0.00: 0.0,
                        0.25: 0.3535533905932738,
                        0.50: 0.5,
                        0.75: 0.6464466094067263,
                        1.00: 1.0,
                        nan: nan}) 
Example #8
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan

        if self.start == self.end:
            return self.height * 0.0

        if self.start < self.end:
            if x <= self.start:
                return self.height * 0.0
            if x >= self.end:
                return self.height * 1.0
            return self.height * (x - self.start) / (self.end - self.start)

        else:
            if x >= self.start:
                return self.height * 0.0
            if x <= self.end:
                return self.height * 1.0
            return self.height * (self.start - x) / (self.start - self.end) 
Example #9
Source File: doc_utils.py    From combine-FEVER-NSMN with MIT License 6 votes vote down vote up
def doc_f1(cls, d_list):

        def single_f1(item):
            docid_predicted = item['predicted_docids']
            docid_predicted = set(docid_predicted)
            docid_gt =  [iii for i in item['evidence'] \
                             for ii in i \
                             for iii in ii \
                             if type(iii) == str]
            docid_gt = set(docid_gt)
            docid_intersect = docid_predicted & docid_gt

            if len(docid_gt) == 0:
                return math.nan
            f1 = 2*len(docid_intersect) / (len(docid_gt) + len(docid_predicted))
            return f1

        score_list = map(single_f1, d_list)
        score_list = [s for s in score_list if not math.isnan(s)]
        return sum(score_list) / len(score_list) 
Example #10
Source File: test_term.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_division_by_zero_fails_with_float(self) -> None:
        self.assertEqual(fl.lib.floating_point_type, float)

        TermAssert(self, fl.Function.create("dbz", "0.0/x")) \
            .membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
            .has_memberships({fl.inf: 0.0, -fl.inf: -0.0, fl.nan: fl.nan})

        TermAssert(self, fl.Function.create("dbz", "inf/x")) \
            .membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
            .has_memberships({fl.inf: fl.nan, -fl.inf: fl.nan, fl.nan: fl.nan})

        TermAssert(self, fl.Function.create("dbz", ".-inf/x")) \
            .membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
            .has_memberships({fl.inf: fl.nan, -fl.inf: fl.nan, -fl.nan: fl.nan})

        TermAssert(self, fl.Function.create("dbz", "nan/x")) \
            .membership_fails(0.0, ZeroDivisionError, re.escape("float division by zero")) \
            .has_memberships({fl.inf: fl.nan, -fl.inf: fl.nan, -fl.nan: fl.nan}) 
Example #11
Source File: test_operation.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def test_str(self) -> None:
        fl.lib.decimals = 3
        self.assertEqual(fl.Op.str(0.3), "0.300")
        self.assertEqual(fl.Op.str(-0.3), "-0.300")
        self.assertEqual(fl.Op.str(3), "3")
        self.assertEqual(fl.Op.str(3.0001), "3.000")

        self.assertEqual(fl.Op.str(math.inf), "inf")
        self.assertEqual(fl.Op.str(-math.inf), "-inf")
        self.assertEqual(fl.Op.str(math.nan), "nan")

        fl.lib.decimals = 5
        self.assertEqual(fl.Op.str(0.3), "0.30000")

        fl.lib.decimals = 0
        self.assertEqual(fl.Op.str(0.3), "0")

        fl.lib.decimals = 3 
Example #12
Source File: defuzzifier.py    From pyfuzzylite with GNU Affero General Public License v3.0 6 votes vote down vote up
def defuzzify(self, term: Term, minimum: float, maximum: float) -> float:
        if not math.isfinite(minimum + maximum):
            return nan
        resolution = self.resolution
        dx = (maximum - minimum) / resolution
        y_max = -math.inf
        x_smallest = minimum
        x_largest = maximum
        find_x_largest = False
        for i in range(0, resolution):
            x = minimum + (i + 0.5) * dx
            y = term.membership(x)
            if Op.gt(y, y_max):
                y_max = y
                x_smallest = x
                x_largest = x
                find_x_largest = True
            elif find_x_largest and Op.eq(y, y_max):
                x_largest = x
            elif Op.lt(y, y_max):
                find_x_largest = False
        return (x_largest + x_smallest) / 2.0 
Example #13
Source File: pattern_analysis.py    From OctoBot-Tentacles with GNU Lesser General Public License v3.0 6 votes vote down vote up
def get_pattern(data):
        if len(data) > 0:
            mean_value = np.mean(data) * 0.7
        else:
            mean_value = math.nan
        if math.isnan(mean_value):
            return PatternAnalyser.UNKNOWN_PATTERN
        indexes_under_mean_value = np.where(data > mean_value)[0] \
            if mean_value < 0 \
            else np.where(data < mean_value)[0]

        nb_gaps = 0
        for i in range(len(indexes_under_mean_value)-1):
            if indexes_under_mean_value[i+1]-indexes_under_mean_value[i] > 3:
                nb_gaps += 1

        if nb_gaps > 1:
            return "W" if mean_value < 0 else "M"
        else:
            return "V" if mean_value < 0 else "N"

    # returns a value 0 < value < 1: the higher the stronger is the pattern 
Example #14
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name: str = "", left: float = nan, rising: float = nan,
                 falling: float = nan, right: float = nan, height: float = 1.0) -> None:
        super().__init__(name, height)
        self.left = left
        self.rising = rising
        self.falling = falling
        self.right = right 
Example #15
Source File: defuzzifier.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def defuzzify(self, fuzzy_output: Term,
                  unused_minimum: float = nan, unused_maximum: float = nan) -> float:
        if not isinstance(fuzzy_output, Aggregated):
            raise ValueError(f"expected an Aggregated term, but found {type(fuzzy_output)}")

        if not self.type:
            raise ValueError("expected a type of defuzzifier, but found none")

        if not fuzzy_output.terms:
            return nan

        this_type = self.type
        if self.type == WeightedDefuzzifier.Type.Automatic:
            this_type = self.infer_type(fuzzy_output.terms[0])

        weighted_sum = 0.0
        if this_type == WeightedDefuzzifier.Type.TakagiSugeno:
            # Provides Takagi-Sugeno and Inverse Tsukamoto of Functions
            for activated in fuzzy_output.terms:
                w = activated.degree
                z = activated.term.membership(w)
                weighted_sum += w * z
        else:
            for activated in fuzzy_output.terms:
                w = activated.degree
                z = activated.term.tsukamoto(w, fuzzy_output.minimum, fuzzy_output.maximum)
                weighted_sum += w * z

        return weighted_sum 
Example #16
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name: str = "", left: float = nan, rising: float = nan,
                 falling: float = nan, right: float = nan, height: float = 1.0) -> None:
        super().__init__(name, height)
        self.left = left
        self.rising = rising
        self.falling = falling
        self.right = right 
Example #17
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan

        if x < self.center - 0.5 * self.width or x > self.center + 0.5 * self.width:
            return self.height * 0.0

        return self.height * 0.5 * (1.0 + cos(2.0 / self.width * pi * (x - self.center))) 
Example #18
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name: str = "", center: float = nan, width: float = nan,
                 height: float = 1.0) -> None:
        super().__init__(name, height)
        self.center = center
        self.width = width 
Example #19
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name: str = "", value: float = nan) -> None:
        super().__init__(name)
        self.value = value 
Example #20
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan

        if self.inflection <= self.end:  # Concave increasing
            if x < self.end:
                return (self.height * (self.end - self.inflection)
                        / (2.0 * self.end - self.inflection - x))

        else:  # Concave decreasing
            if x > self.end:
                return (self.height * (self.inflection - self.end)
                        / (self.inflection - 2.0 * self.end + x))

        return self.height * 1.0 
Example #21
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan

        if self.direction > self.start and x >= self.start:
            return self.height * 1.0

        if self.direction < self.start and x <= self.start:
            return self.height * 1.0

        return self.height * 0.0 
Example #22
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name: str = "", start: float = nan, direction: float = nan,
                 height: float = 1.0) -> None:
        super().__init__(name, height)
        self.start = start
        self.direction = direction 
Example #23
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan

        a = b = 1.0

        if x < self.mean_a:
            a = exp((-(x - self.mean_a) * (x - self.mean_a))
                    / (2.0 * self.standard_deviation_a * self.standard_deviation_a))

        if x > self.mean_b:
            b = exp((-(x - self.mean_b) * (x - self.mean_b))
                    / (2.0 * self.standard_deviation_b * self.standard_deviation_b))

        return self.height * a * b 
Example #24
Source File: defuzzifier.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def defuzzify(self, term: Term, minimum: float, maximum: float) -> float:
        if not math.isfinite(minimum + maximum):
            return nan
        resolution = self.resolution
        dx = (maximum - minimum) / resolution
        area = x_centroid = 0.0
        for i in range(0, resolution):
            x = minimum + (i + 0.5) * dx
            y = term.membership(x)
            x_centroid += y * x
            area += y
        return x_centroid / area 
Example #25
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan
        return self.height * (1.0 / (1.0 + (fabs((x - self.center) / self.width)
                                            ** (2.0 * self.slope)))) 
Example #26
Source File: defuzzifier.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def defuzzify(self, term: Term, minimum: float, maximum: float) -> float:
        if not math.isfinite(minimum + maximum):
            return nan
        resolution = self.resolution
        dx = (maximum - minimum) / resolution
        y_max = -math.inf
        x_smallest = minimum
        for i in range(0, resolution):
            x = minimum + (i + 0.5) * dx
            y = term.membership(x)
            if Op.gt(y, y_max):
                y_max = y
                x_smallest = x
        return x_smallest 
Example #27
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, name: str = "", center: float = nan, width: float = nan, slope: float = nan,
                 height: float = 1.0) -> None:
        super().__init__(name, height)
        self.center = center
        self.width = width
        self.slope = slope 
Example #28
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan
        if self.terms and not self.aggregation:
            raise ValueError("expected an aggregation operator, but none found")

        result = 0.0
        for term in self.terms:
            result = self.aggregation.compute(result, term.membership(x))  # type: ignore
        logging.debug(f"{Op.str(result)}: {str(self)}")
        return result 
Example #29
Source File: defuzzifier.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def defuzzify(self, fuzzy_output: Term,
                  unused_minimum: float = nan, unused_maximum: float = nan) -> float:
        if not isinstance(fuzzy_output, Aggregated):
            raise ValueError(f"expected an Aggregated term, but found {type(fuzzy_output)}")

        if not self.type:
            raise ValueError("expected a type of defuzzifier, but found none")

        if not fuzzy_output.terms:
            return nan

        this_type = self.type
        if self.type == WeightedDefuzzifier.Type.Automatic:
            this_type = self.infer_type(fuzzy_output.terms[0])

        weighted_sum = weights = 0.0
        if this_type == WeightedDefuzzifier.Type.TakagiSugeno:
            # Provides Takagi-Sugeno and Inverse Tsukamoto of Functions
            for activated in fuzzy_output.terms:
                w = activated.degree
                z = activated.term.membership(w)
                weighted_sum += w * z
                weights += w
        else:
            for activated in fuzzy_output.terms:
                w = activated.degree
                z = activated.term.tsukamoto(w, fuzzy_output.minimum, fuzzy_output.maximum)
                weighted_sum += w * z
                weights += w

        return weighted_sum / weights 
Example #30
Source File: term.py    From pyfuzzylite with GNU Affero General Public License v3.0 5 votes vote down vote up
def membership(self, x: float) -> float:
        if isnan(x):
            return nan

        if not self.term:
            raise ValueError("expected a term to activate, but none found")
        if not self.implication:
            raise ValueError("expected an implication operator, but none found")
        result = self.implication.compute(self.term.membership(x), self.degree)
        logging.debug(f"{Op.str(result)}: {str(self)}")
        return result