Coverage for src/susi/utils/polynomials.py: 16%
31 statements
« prev ^ index » next coverage.py v7.5.0, created at 2025-06-13 14:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2025-06-13 14:15 +0000
1from typing import Optional
3EPS = 1e-5
6def __locally_extreme_values(poly, interval: tuple = (None, None)) -> tuple:
7 points = []
8 if interval[0] is not None:
9 points.append(interval[0])
10 for x in poly.deriv().roots:
11 if interval[0] is not None and x < interval[0]:
12 continue
13 if interval[1] is not None and x > interval[1]:
14 continue
15 points.append(x)
16 if interval[1] is not None:
17 points.append(interval[1])
18 points.sort()
19 values = [poly(x) for x in points]
20 return points, values
23def min_value(poly, interval: tuple = (None, None)) -> Optional[float]:
24 """
25 Return the minimum value of the polynomial within the given range
26 (or globally, if no range is given)
27 """
28 xes, yes = __locally_extreme_values(poly, interval)
29 if len(yes) > 1:
30 return min(yes)
31 if poly(xes[0] - EPS) > yes[0] and poly(xes[0] + EPS) > yes[0]:
32 return yes[0]
33 return None
36def max_value(poly, interval: tuple = (None, None)) -> Optional[float]:
37 """
38 Return the maximum value of the polynomial within the given range
39 (or globally, if no range is given)
40 """
41 xes, yes = __locally_extreme_values(poly, interval)
42 if len(yes) > 1:
43 return max(yes)
44 if poly(xes[0] - EPS) < yes[0] and poly(xes[0] + EPS) < yes[0]:
45 return yes[0]
46 return None