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 (lp). However, it is possible that the constant part of the polynomial is zero. This causes an issue because I need to have exactly (lp) coefficients. I tried to resolve this by using .coefficients(sparse=False), as was recommended in the question https://ask.sagemath.org/question/26907/get-the-coefficients-from-the-polynomial/, 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.