# Revision history [back]

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.