modelization + pretty print
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
### print
|
||||
|
||||
def monomial_to_str(exp, names):
|
||||
factors = []
|
||||
for i, e in enumerate(exp):
|
||||
if e == 0:
|
||||
continue
|
||||
name = names[i]
|
||||
if e == 1:
|
||||
factors.append(name)
|
||||
else:
|
||||
factors.append(f"{name}^{e}")
|
||||
return "*".join(factors) if factors else "1"
|
||||
|
||||
|
||||
def pretty_print(poly):
|
||||
ring = poly.parent()
|
||||
names = ring.variable_names()
|
||||
|
||||
R_terms = []
|
||||
r_terms = []
|
||||
x_terms = []
|
||||
mixed_terms = []
|
||||
has_const = False
|
||||
|
||||
for exp, coeff in poly.dict().items():
|
||||
if coeff == 0:
|
||||
continue
|
||||
|
||||
if sum(exp) == 0:
|
||||
has_const = True
|
||||
continue
|
||||
|
||||
term = monomial_to_str(exp, names)
|
||||
vars_in_term = [names[i] for i, e in enumerate(exp) if e != 0]
|
||||
families = {v[0] for v in vars_in_term}
|
||||
|
||||
if families == {"R"}:
|
||||
R_terms.append(term)
|
||||
elif families == {"r"}:
|
||||
r_terms.append(term)
|
||||
elif families == {"x"}:
|
||||
x_terms.append(term)
|
||||
else:
|
||||
mixed_terms.append(term)
|
||||
|
||||
parts = []
|
||||
|
||||
if R_terms or (has_const and not r_terms):
|
||||
parts.append("f(Ri)")
|
||||
elif has_const:
|
||||
parts.append("1")
|
||||
|
||||
if r_terms:
|
||||
parts.append("f(ri)")
|
||||
|
||||
parts.extend(x_terms)
|
||||
parts.extend(mixed_terms)
|
||||
|
||||
return " + ".join(parts) if parts else "0"
|
||||
|
||||
|
||||
def pretty_print_vec(vec):
|
||||
return "[" + ",\n ".join(pretty_print(p) for p in vec) + "]"
|
||||
Reference in New Issue
Block a user