ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 11 Apr 2012 15:49:47 +0200Using polynomial over matriceshttps://ask.sagemath.org/question/8872/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 ?Wed, 11 Apr 2012 14:05:12 +0200https://ask.sagemath.org/question/8872/using-polynomial-over-matrices/Answer by niles for <p>Things goes wrong when I try to define polynomial ring over a matrix algebra.
When I type</p>
<pre><code>A = MatrixSpace(QQ, 2)
PA.<x> = PolynomialRing(A)
x
</code></pre>
<p>I obtain the following error :</p>
<pre><code>AttributeError: 'MatrixSpace_generic' object has no attribute 'is_atomic_repr'
</code></pre>
<p>Of course, I could use matrices over polynomial rings, but I would loss generality of the code...
Do you have any idea ?</p>
https://ask.sagemath.org/question/8872/using-polynomial-over-matrices/?answer=13446#post-id-13446The 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[0]
[-1/2 0]
[ -1 0]
sage: f[1]
[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]}
sage: ((x^2 + 1)^2).padded_list()
[
[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)[0]
[8 0]
[0 8]
sage: ((f^2 - f)^3)[4]
[33 0]
[ 0 33]
Wed, 11 Apr 2012 15:00:02 +0200https://ask.sagemath.org/question/8872/using-polynomial-over-matrices/?answer=13446#post-id-13446Comment by Pierre L for <p>The problem is just with <em>printing</em> elements of the ring; the function <code>is_atomic_repr</code> is just used to determine whether or not to write parentheses around the coefficients of a polynomial (e.g. the elements of <code>ZZ</code> are atomic, but elements of a polynomial ring are not).</p>
<p>Matrix_space doesn't have one, but you can give it one easily:</p>
<pre><code>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
</code></pre>
<p>Note that things will still print in silly ways, because matrices always print a linebreak after printing the top row:</p>
<pre><code>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[0]
[-1/2 0]
[ -1 0]
sage: f[1]
[1/2 1/2]
[ 0 -2]
</code></pre>
<p>You can still work with polynomials and print their coefficients without defining <code>is_atomic_repr</code>:</p>
<pre><code>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]}
sage: ((x^2 + 1)^2).padded_list()
[
[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)[0]
[8 0]
[0 8]
sage: ((f^2 - f)^3)[4]
[33 0]
[ 0 33]
</code></pre>
https://ask.sagemath.org/question/8872/using-polynomial-over-matrices/?comment=19955#post-id-19955Thanks ! That was quite tricky...Wed, 11 Apr 2012 15:49:47 +0200https://ask.sagemath.org/question/8872/using-polynomial-over-matrices/?comment=19955#post-id-19955