# Apply modulus to polynomial coefficients

I want to apply mod operator to a polynomial and get the expanded result. For example

$$(x+1)^4 \bmod 6$$ should give $$x^4 + 4 x^3 + 4 x + 1$$

What I've tried:

sage: ZP.<x> = ZZ[]
sage: ( (x+1)^4 ).factor_mod(6)
ValueError: p must be prime

sage: Mod( (x+1)^4 , 6)
TypeError: not a constant polynomial


and

a,b,c=var('a,b,c')
sage: ((a+b+c)^3).expand()
a^3 + 3*a^2*b + 3*a*b^2 + b^3 + 3*a^2*c + 6*a*b*c + 3*b^2*c + 3*a*c^2 + 3*b*c^2 + c^3
sage: (a+b+c)^3 % 3
TypeError: unsupported operand parent(s) for %: 'Symbolic Ring' and 'Symbolic Ring'

edit retag close merge delete

Sort by ยป oldest newest most voted

There are several ways to go depending whether the result should be a polynomial with coefficients in $\mathbb{Z}$ or a polynomial with coefficients in $\mathbb{Z}/6\mathbb{Z}$.

Here is an illustration for both options.

Define a polynomial ring and a polynomial:

sage: ZP.<x> = ZZ[]
sage: p = (x+1)^4
sage: p
x^4 + 4*x^3 + 6*x^2 + 4*x + 1
sage: p.parent()
Univariate Polynomial Ring in x over Integer Ring


Mod out by 6, keeping coefficients in the integers:

sage: q = p % 6
sage: q
x^4 + 4*x^3 + 4*x + 1
sage: q.parent()
Univariate Polynomial Ring in x over Integer Ring


Move to coefficients in modular integers:

sage: r = p.change_ring(Zmod(6))
sage: r
x^4 + 4*x^3 + 4*x + 1
sage: r.parent()
Univariate Polynomial Ring in x over Ring of integers modulo 6


The difference is apparent when you keep working with the modded out polynomial. Compare:

sage: 3 * q
3*x^4 + 12*x^3 + 12*x + 3

sage: 3 * r
3*x^4 + 3

more