# How to truncate a polynomial to a certain power [closed]

I've been trying to find a way to truncate a polynomial to a certain set of powers. For example, creating a function that will reduce 1+q+2q^2+3q^3+4q^4+5q^5 to 1+q+2q^2. What I have for this so far is:

R.<q> = PowerSeriesRing(ZZ)
def ordred(a,p):
ordred_a=a.coefficients()
ordred_coeff=list('ordred_coeff_%d' % s for s in range(0,(l*p)))
for i in range(0,(l*p)):
ordred_coeff[i]=Mod(ordred_a[i],p^mp)
return R(ordred_coeff)


Note: the reason I am using the power series ring is that the polynomials I am getting as inputs come as the q-expansions of modular forms, which are elements of the ring of power series. Also, p,l and mp are defined earlier in the program. Here, they are p=5,l=4,mp=12.

Now, I know for a fact that I have powers of q from 1 to (l*p). However, it is possible that the constant part of the polynomial is zero. This causes an issue because I need to have exactly (l*p)coefficients. I tried to resolve this by using .coefficients(sparse=False), as was recommended in the question https://ask.sagemath.org/question/269..., but this simply gives me the error message coefficients() takes no keyword arguments. I assume this is because the .coefficients for PowerSeriesRing is different from the .coefficients for PolynomialRing, but I don't know how to resolve this issue.

Should I convert my polynomial to PolynomialRing somehow before applying .coefficients? If so, how would I go about doing that? Or is there some other solution?

If you have a better way to do the truncation in the first place, I am happy to hear it, but note that the reason my method is like this is because my initial equations are of the form 1+q+q^2+.....+O(q^20), so I have to deal with the Big O notation.

edit retag reopen merge delete

### Closed for the following reason the question is answered, right answer was accepted by Rune close date 2022-01-30 19:57:55.902757

Sort by ยป oldest newest most voted

Just add a suitable big-O - like

f = 1+q+2*q^2+3*q^3+4*q^4+5*q^5
f += O(q^3)   # truncating as a power series
f = f.polynomial()   # converting back to polynomial


truncates f at 3rd degree.

more

I am not sure about your ultimate goal, but if you want to do operations between your polynomials, then you will have to truncate again and again. Let me suggest another way : instead of truncating by hand, why not just work in a quotient ring, where you declare that $q^3=0$ (hence every power above 3 will vanish as well). It can be as follows:

sage: R.<q> = PolynomialRing(ZZ)
sage: P = 1+q+2*q^2+3*q^3+4*q^4+5*q^5
sage: S = R.quotient(q^3)
sage: S
Univariate Quotient Polynomial Ring in qbar over Integer Ring with modulus q^3
sage: Q = S(P)
sage: Q
2*qbar^2 + qbar + 1


The good point here is that if you compute, say, the square of Q, you do not have to truncate again:

sage: Q^2
5*qbar^2 + 2*qbar + 1

more