-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
73 lines (57 loc) · 1.82 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def round_or_decimal(coeff, tolerance=1e-10):
"""Round to integer or keep 9 decimal."""
if abs(coeff - round(coeff)) < tolerance:
return round(coeff)
else:
return round(coeff, 10)
def get_sign(coeff, is_first_term):
if is_first_term:
return "- " if coeff < 0 else ""
return " - " if coeff < 0 else " + "
def get_format_coeff(coeff, power):
abs_coeff = abs(coeff)
if abs_coeff == 1 and power != 0:
coeff_str = ""
else:
coeff_str = f"{abs_coeff}"
return coeff_str
def get_format_power(coeff_str, power):
if power == 0:
return f"{coeff_str}"
elif power == 1:
return f"{coeff_str} * x" if coeff_str else "x"
else:
return f"{coeff_str} * x**{power}" if coeff_str else f"x**{power}"
def ensemble_polynomial(an, smart_round=True, keep_all_zeros=False, keep_first_zeros=True):
"""
Ensemble px by input coefficient an, make it clean and beauty.
"""
terms = []
n = len(an) - 1
skip_zero = False if keep_first_zeros else True
if smart_round:
an = [round_or_decimal(a) for a in an]
for i in range(n, -1, -1):
coeff = an[i]
is_first_term = i == n
if keep_all_zeros:
pass
elif skip_zero == bool(coeff):
pass
elif coeff == 0:
continue
else:
skip_zero = True
sign = get_sign(coeff, is_first_term)
coeff_str = get_format_coeff(coeff, i)
term = get_format_power(coeff_str, i)
terms.append(f"{sign}{term}") # if sign else f"{term}")
px = "".join(terms)
return px
if __name__ == "__main__":
an = [1.9999999999999425, -2.4999999999999996, 1]
px = ensemble_polynomial(an)
print(px)
an = [1, 0.0, 2.5, -1, 0, 1, 0, 0]
px = ensemble_polynomial(an)
print(px)