# fast factorization of ratios of polynomials

I consider polynomials of the form $P=\prod_i (1-1/y_i)$, where each $y_i$ is a Laurent monomial with unit coefficient in variables $x_1,\ldots,x_k, q_1,q_2,q_3,m$.

I'm interested in taking ratios of the form $P_1 / (P_2 \prod_{n=1}^k x_n)$ of such polynomials, and would like to know what the fastest way is to cancel common factors using sagemath.

The final goal is to take residues at simple poles, so factorizations will happen.

Let me write a full code example:

```
def br(x):
return 1-1/x
q1,q2,q3,m = var('q1,q2,q3,m')
def mex(q1,q2,q3,m):
q4 = var('q4')
N.<q1,q2,q3,q4> = PolynomialRing(ZZ, 4, order='neglex')
k = 3
K = 1 + q1 + q2
rho = [p.substitute({q4:(q1*q2*q3)^-1}) for p in K.monomials()]
X = [var("x%d" % i) for i in range(k)]
chim = prod([ br(X[j]/m) for j in range(k)])
chix = prod([ br(X[j]) for j in range(k)])
chiup = prod([ prod([ br(q1*q2*X[i]/X[j])*br(q1*q3*X[i]/X[j])*br(q2*q3*X[i]/X[j])*(br(X[i]/X[j]))^2 for i in range(k) if i > j]) for j in range(k)])
chiups = prod([ prod([ br(X[i]/(X[j]*q1*q2))*br(X[i]/(X[j]*q1*q3))*br(X[i]/(X[j]*q2*q3)) for i in range(k) if i > j]) for j in range(k)])
chido = prod([ prod([ br(q1*X[i]/X[j])*br(q2*X[i]/X[j])*br(q3*X[i]/X[j])*br(q1*q2*q3*X[i]/X[j]) for i in range(k) if i > j]) for j in range (k)])
chidos = prod([ prod([ br(X[i]/(X[j]*q1))*br(X[i]/(X[j]*q2))*br(X[i]/(X[j]*q3))*br(X[i]/(X[j]*q1*q2*q3)) for i in range(k) if i > j]) for j in range (k)])
dx = prod([ X[j] for j in range(k)])
chinum = (chim*chiup*chiups)
chiden = (chix*chido*chidos*dx)
chi = chinum/chiden
# return chi.factor()
for xi,rhoi in zip(X,rho):
chi = (chi*(xi-rhoi)).factor().subs({xi: rhoi})
return chi
```

This currently works in the symbolic ring, but gets slow as soon as k,K grow a bit. I was hoping that using singular (Laurent polyn ring, e.g.) would make it faster, but cannot make it such that everything happens there yet.

It's unclear what is your problem. Your polynomials are already given in the factored form - why do you want to factor them?

Well, numerator and denominator are factored separately, but I want to cancel common factors when taking the ratio, namely I want to factor the ratio. Is that more clear?

Could you please provide your full code ? In particular, how are

`q1,q2,q3,x0,x1,x2,x3`

defined ?@tmonteil I added a code example, does this clarify my question?

Couple of issues in your code: 1 -

`q1,q2,q3,m = var('q1,q2,q3,m')`

and`q4 = var('q4')`

are redundant and can be safely removed 2 - defining`X`

as symbolic variables slow down things, you'd better define them as generators of Laurent polynomial ring.