# Using polynomial over matrices

Things goes wrong when I try to define polynomial ring over a matrix algebra. When I type

A = MatrixSpace(QQ, 2)
PA.<x> = PolynomialRing(A)
x


I obtain the following error :

AttributeError: 'MatrixSpace_generic' object has no attribute 'is_atomic_repr'


Of course, I could use matrices over polynomial rings, but I would loss generality of the code... Do you have any idea ?

edit retag close merge delete

Sort by » oldest newest most voted

The problem is just with printing elements of the ring; the function is_atomic_repr is just used to determine whether or not to write parentheses around the coefficients of a polynomial (e.g. the elements of ZZ are atomic, but elements of a polynomial ring are not).

Matrix_space doesn't have one, but you can give it one easily:

sage: A = MatrixSpace(QQ, 2)
sage: PA.<x> = PolynomialRing(A)
sage: def iar():
...    return True
...
sage: A.is_atomic_repr = iar
sage: x
[1 0]
[0 1]*x


Note that things will still print in silly ways, because matrices always print a linebreak after printing the top row:

sage: f = PA.random_element()
sage: f
[   1    1]
[-1/2    0]*x^2 + [1/2 1/2]
[  0  -2]*x + [-1/2    0]
[  -1    0]
sage: f
[-1/2    0]
[  -1    0]
sage: f
[1/2 1/2]
[  0  -2]


You can still work with polynomials and print their coefficients without defining is_atomic_repr:

sage: B = MatrixSpace(ZZ, 2)
sage: PB.<x> = PolynomialRing(B)
sage: f = x - 1
sage: (f^2).coeffs()
[
[1 0]  [-2  0]  [1 0]
[0 1], [ 0 -2], [0 1]
]
sage: (f^2+1).dict()
{0: [2 0]
[0 2], 1: [-2  0]
[ 0 -2], 2: [1 0]
[0 1]}

[
[1 0]  [0 0]  [2 0]  [0 0]  [1 0]
[0 1], [0 0], [0 2], [0 0], [0 1]
]

sage: ((f^2 - f)^3)
[8 0]
[0 8]
sage: ((f^2 - f)^3)
[33  0]
[ 0 33]

more