# how to best simplify/factor symbolic expressions

Define symbolic expressions T3 and T3s.

q1,q2,q3 = var('q1,q2,q3')
T3 = (q1^2*q2^2 + 1)*(q1^2*q3^2 + 1)*(q2^2*q3^2 + 1)*(q1*q2 + 1)*(q1*q2 - 1)*(q1*q3 + 1)*(q1*q3 - 1)*(q2*q3 + 1)*(q2*q3 - 1)/((q1^2*q2^2*q3^2 + 1)*(q1*q2*q3 + 1)*(q1*q2*q3 - 1)*(q1^2 + 1)*(q2^2 + 1)*(q3^2 + 1)*(q1 + 1)*(q1 - 1)*(q2 + 1)*(q2 - 1)*(q3 + 1)*(q3 - 1))

T3s = (q1^4*q2^4 - 1)*(q1^4*q3^4 - 1)*(q2^4*q3^4 - 1)/((q1^4*q2^4*q3^4 - 1)*(q1^4 - 1)*(q2^4 - 1)*(q3^4 - 1))


Is there any method to reduce T3 to its simpler (at least for a human) form T3s in Sage?

edit retag close merge delete

Sort by ยป oldest newest most voted

I prefer to work with polynomial rings and their fields of fractions:

R.<q1,q2,q3> = PolynomialRing(QQ)
T3 = (q1^2*q2^2 + 1)*(q1^2*q3^2 + 1)*(q2^2*q3^2 + 1)*(q1*q2 + 1)*(q1*q2 - 1)*(q1*q3 + 1)*(q1*q3 - 1)*(q2*q3 + 1)*(q2*q3 - 1)/((q1^2*q2^2*q3^2 + 1)*(q1*q2*q3 + 1)*(q1*q2*q3 - 1)*(q1^2 + 1)*(q2^2 + 1)*(q3^2 + 1)*(q1 + 1)*(q1 - 1)*(q2 + 1)*(q2 - 1)*(q3 + 1)*(q3 - 1))


Here T3 automatically belongs to the field of fractions of R.

One way to characterize the factorization you like is that all factors which contain exactly the same variables are multiplied out. We can make a function that returns such a factorization:

def my_factor(f):
from collections import defaultdict
factors = defaultdict(lambda: f.parent().one())
for (g,e) in f.factor():
factors[g.variables()] *= g^e
return Factorization([(g,1) for g in factors.values()])


We can use it e.g. as follows:

show(LatexExpr('\\frac{' + latex(my_factor(T3.numerator())) + '}{' + latex(my_factor(T3.denominator())) + '}'))


$$\frac{ (q_{2}^{4} q_{3}^{4} - 1) \cdot (q_{1}^{4} q_{3}^{4} - 1) \cdot (q_{1}^{4} q_{2}^{4} - 1) }{ (q_{3}^{4} - 1) \cdot (q_{2}^{4} - 1) \cdot (q_{1}^{4} - 1) \cdot (q_{1}^{4} q_{2}^{4} q_{3}^{4} - 1) }$$

If you really prefer symbolic expressions, then you can do the conversions back and forth, or adapt the function.

more