ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 29 Aug 2015 22:15:56 -0500Inverses of matrices of Laurent polynomialshttp://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/ This question is rather similar in spirit to [my previous question](/question/28844/eigenvalues-of-matrices-of-laurent-polynomials/) in that it's about figuring out how to "correctly" manipulate matrices of polynomials, ensuring that everything stays in the right rings.
In this case, I have a matrix, all of whose elements are of the form <code>Univariate Laurent Polynomial Ring in t over Integer Ring</code>. The matrix has an inverse over this ring, yet sage insists that the entries of the inverse belong to <code>Fraction Field of Univariate Polynomial Ring in t over Integer Ring</code>. Here's a minimal example:
sage: R.<t> = LaurentPolynomialRing(ZZ)
sage: f = t^2
sage: mat = matrix([[f]])
sage: mat[0,0].parent()
Univariate Laurent Polynomial Ring in t over Integer Ring
sage: mat.inverse()
[1/t^2]
sage: mat.inverse()[0,0].parent()
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
Desired output:
sage: mat.inverse()
[t^-2]
sage: mat.inverse()[0,0].parent()
Univariate Laurent Polynomial Ring in t over Integer Ring
What would have been the correct way to obtain what I want?
Using <code>apply_map</code> to force each entry back into the right ring does not immediately do the job:
sage: R(1/f)
t^-2
sage: mat.inverse().apply_map(lambda f: R(f))
...
TypeError: denominator must be a unit
sage: mat.inverse().apply_map(R)
...
TypeError: denominator must be a unitTue, 25 Aug 2015 05:16:00 -0500http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/Answer by slelievre for <p>This question is rather similar in spirit to <a href="/question/28844/eigenvalues-of-matrices-of-laurent-polynomials/">my previous question</a> in that it's about figuring out how to "correctly" manipulate matrices of polynomials, ensuring that everything stays in the right rings.</p>
<p>In this case, I have a matrix, all of whose elements are of the form <code>Univariate Laurent Polynomial Ring in t over Integer Ring</code>. The matrix has an inverse over this ring, yet sage insists that the entries of the inverse belong to <code>Fraction Field of Univariate Polynomial Ring in t over Integer Ring</code>. Here's a minimal example:</p>
<pre><code>sage: R.<t> = LaurentPolynomialRing(ZZ)
sage: f = t^2
sage: mat = matrix([[f]])
sage: mat[0,0].parent()
Univariate Laurent Polynomial Ring in t over Integer Ring
sage: mat.inverse()
[1/t^2]
sage: mat.inverse()[0,0].parent()
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
</code></pre>
<p>Desired output:</p>
<pre><code>sage: mat.inverse()
[t^-2]
sage: mat.inverse()[0,0].parent()
Univariate Laurent Polynomial Ring in t over Integer Ring
</code></pre>
<p>What would have been the correct way to obtain what I want?</p>
<p>Using <code>apply_map</code> to force each entry back into the right ring does not immediately do the job:</p>
<pre><code>sage: R(1/f)
t^-2
sage: mat.inverse().apply_map(lambda f: R(f))
...
TypeError: denominator must be a unit
sage: mat.inverse().apply_map(R)
...
TypeError: denominator must be a unit
</code></pre>
http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/?answer=29371#post-id-29371The problem is that Sage is unable to convert `1/t^2`, seen as
an element in the fraction field of the univariate polynomial ring in `t`,
to the ring of Laurent polynomials in `t`.
The following function should fit your needs.
def inv(m):
r"""
Return the inverse of this matrix over a ring of Laurent polynomials
Raises ``ZeroDivisionError`` if matrix has zero determinant,
and ``ArithmeticError`` if matrix is nonsquare.
Uses the meth:`__invert__` method in `src/sage/matrix/matrix0.pyx`,
which returns a matrix over the fraction field. This is then
converted back to the ring of Laurent polynomials, with the trick
that the fraction field of a ring of Laurent polynomials is really
the fraction field of the corresponding ring of polynomials.
EXAMPLE::
sage: R.<t> = LaurentPolynomialRing(ZZ)
sage: m = matrix([[t^2]])
sage: m.parent()
Full MatrixSpace of 1 by 1 dense matrices over
Univariate Laurent Polynomial Ring in t over Integer Ring
sage: mi = inv(m)
sage: mi
[1/t^2]
sage: mi.parent()
Full MatrixSpace of 1 by 1 dense matrices over
Univariate Laurent Polynomial Ring in t over Integer Ring
"""
M = m.parent()
R = m.base_ring()
l = m.inverse().list()
return M([R(x.numerator()) * R(x.denominator()).inverse_of_unit() for x in l])
Then, as in the example,
sage: R.<t> = LaurentPolynomialRing(ZZ)
sage: m = matrix([[t^2]])
sage: inv(m)
[1/t^2]
Sat, 29 Aug 2015 22:15:56 -0500http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/?answer=29371#post-id-29371Answer by rws for <p>This question is rather similar in spirit to <a href="/question/28844/eigenvalues-of-matrices-of-laurent-polynomials/">my previous question</a> in that it's about figuring out how to "correctly" manipulate matrices of polynomials, ensuring that everything stays in the right rings.</p>
<p>In this case, I have a matrix, all of whose elements are of the form <code>Univariate Laurent Polynomial Ring in t over Integer Ring</code>. The matrix has an inverse over this ring, yet sage insists that the entries of the inverse belong to <code>Fraction Field of Univariate Polynomial Ring in t over Integer Ring</code>. Here's a minimal example:</p>
<pre><code>sage: R.<t> = LaurentPolynomialRing(ZZ)
sage: f = t^2
sage: mat = matrix([[f]])
sage: mat[0,0].parent()
Univariate Laurent Polynomial Ring in t over Integer Ring
sage: mat.inverse()
[1/t^2]
sage: mat.inverse()[0,0].parent()
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
</code></pre>
<p>Desired output:</p>
<pre><code>sage: mat.inverse()
[t^-2]
sage: mat.inverse()[0,0].parent()
Univariate Laurent Polynomial Ring in t over Integer Ring
</code></pre>
<p>What would have been the correct way to obtain what I want?</p>
<p>Using <code>apply_map</code> to force each entry back into the right ring does not immediately do the job:</p>
<pre><code>sage: R(1/f)
t^-2
sage: mat.inverse().apply_map(lambda f: R(f))
...
TypeError: denominator must be a unit
sage: mat.inverse().apply_map(R)
...
TypeError: denominator must be a unit
</code></pre>
http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/?answer=29368#post-id-29368This is defined behaviour of the general inverse, as far as I understand from the code. For example,
sage: parent(~1)
Rational Field
The definition of `Matrix_generic_dense.__invert__()` explicitly states
Return this inverse of this matrix, as a matrix over the fraction field.
sage: R.fraction_field()
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
I'm not an algebraist so no comment on that but, with symbolics you would stay in the ring, so symbolics seems to be good for something, contrary to many a belief.
sage: mat = matrix([[x^2]])
sage: mati = mat.inverse(); mati
[x^(-2)]
sage: mati[0,0].parent()
Symbolic Ring
Thu, 27 Aug 2015 02:01:40 -0500http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/?answer=29368#post-id-29368Comment by fuglede for <p>This is defined behaviour of the general inverse, as far as I understand from the code. For example,</p>
<pre><code> sage: parent(~1)
Rational Field
</code></pre>
<p>The definition of <code>Matrix_generic_dense.__invert__()</code> explicitly states </p>
<pre><code>Return this inverse of this matrix, as a matrix over the fraction field.
sage: R.fraction_field()
Fraction Field of Univariate Polynomial Ring in t over Integer Ring
</code></pre>
<p>I'm not an algebraist so no comment on that but, with symbolics you would stay in the ring, so symbolics seems to be good for something, contrary to many a belief.</p>
<pre><code>sage: mat = matrix([[x^2]])
sage: mati = mat.inverse(); mati
[x^(-2)]
sage: mati[0,0].parent()
Symbolic Ring
</code></pre>
http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/?comment=29370#post-id-29370Thanks for the answer. I don't think it entirely answers my question. In my case, there is a way to obtain a matrix over the desired ring as one could try to coerce all entries (even though apply_map doesn't work for whatever reason). Clearly though, that's a very inelegant way of doing it, and so I was looking for the "correct" way to do it.Thu, 27 Aug 2015 08:40:13 -0500http://ask.sagemath.org/question/29355/inverses-of-matrices-of-laurent-polynomials/?comment=29370#post-id-29370