Particular polynomial multiplication

Dear friends,

Please, I would like to ask a question that it is not exactly about fortran but about algorithms, hoping you can still offer to me some advice on the subject.

Let’s say that I have a two polynomials multiplication, for example:

p = x^12 + x^7 + x^5
q = x^18 + x^13 + x^4

p.q = x^30 + 2x^25 + x^23 + x^20 + x^18 + x^16 + x^11 + x^9

and I want to discard those monomials larger than x^23. This is easy, I just have to do (p.q) mod x^25. But now I need discard those monomials larger than x^23 and, at the same time, those smaller than x^16. Are you aware of an efficient procedure to accomplish that?

As always, many thanks for your help.

I’m not sure what do you mean by discarding monomials. If, as I guess, p.q after discarding should read

p.q.discarded = x^23 + x^20 + x^18 + x^16 + x^11 + x^9

(i.e. discarding monomials with the exponent greater than 23),
then your algorithm (p.q) mod x^25 is not right. Even in trivial case of x=1, the value of p.q is 9, 9 mod 1 is 0 while p.q.discarded is 6.
In a non-integer case of x=1.2 you get 590.455, 18.078 and 162.287, respectively