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.Sun, 22 Dec 2013 15:41:45 +0100solving a matrix equation modulo mhttps://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/Hello,
I am quite new to sage an have troubles with the following problem:
I'm given a matrix 'A' and a vector 'b' and a positiv interger 'm' (m does not have to be prime). 'A' is a matrix with more rows than collums, so it is not quadratic. I would like to find the solution 'x' of the equation: A*x = b (mod m).
I have tried to manage it with e.g.:
A.solve_right(Integers(m),p),
but this works only if m is prime. I also was able to solve my problem if I explicitly insert the equations like this:
gp('matsolvemod([1,2;1,3],6,[1,0]~,1)')
But I need something, where I just have to specify 'A' and 'b' (and of course m). Can somebody help me?
best regardsSun, 22 Dec 2013 07:12:57 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/Answer by tmonteil for <p>Hello,</p>
<p>I am quite new to sage an have troubles with the following problem:</p>
<p>I'm given a matrix 'A' and a vector 'b' and a positiv interger 'm' (m does not have to be prime). 'A' is a matrix with more rows than collums, so it is not quadratic. I would like to find the solution 'x' of the equation: A*x = b (mod m).</p>
<p>I have tried to manage it with e.g.: </p>
<p>A.solve_right(Integers(m),p),</p>
<p>but this works only if m is prime. I also was able to solve my problem if I explicitly insert the equations like this: </p>
<p>gp('matsolvemod([1,2;1,3],6,[1,0]~,1)')</p>
<p>But I need something, where I just have to specify 'A' and 'b' (and of course m). Can somebody help me?</p>
<p>best regards</p>
https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?answer=15853#post-id-15853You should directly work on the $\mathbb{Z}/m\mathbb{Z}$ module as follows:
sage: R = IntegerModRing(15)
sage: M = Matrix(R, [[1,2],[1,4],[2,4]])
sage: b = vector(R, [3,5,6])
sage: M.solve_right(b)
(1, 1)
Sun, 22 Dec 2013 08:28:33 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?answer=15853#post-id-15853Comment by tmonteil for <p>You should directly work on the $\mathbb{Z}/m\mathbb{Z}$ module as follows:</p>
<pre><code>sage: R = IntegerModRing(15)
sage: M = Matrix(R, [[1,2],[1,4],[2,4]])
sage: b = vector(R, [3,5,6])
sage: M.solve_right(b)
(1, 1)
</code></pre>
https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16512#post-id-16512I was using Sage 5.12, what is the result of the `version()` function ?
Sun, 22 Dec 2013 10:15:21 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16512#post-id-16512Comment by tedmos for <p>You should directly work on the $\mathbb{Z}/m\mathbb{Z}$ module as follows:</p>
<pre><code>sage: R = IntegerModRing(15)
sage: M = Matrix(R, [[1,2],[1,4],[2,4]])
sage: b = vector(R, [3,5,6])
sage: M.solve_right(b)
(1, 1)
</code></pre>
https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16509#post-id-16509Ok thank you, with 6.0 everything works fine now!Sun, 22 Dec 2013 15:41:45 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16509#post-id-16509Comment by tmonteil for <p>You should directly work on the $\mathbb{Z}/m\mathbb{Z}$ module as follows:</p>
<pre><code>sage: R = IntegerModRing(15)
sage: M = Matrix(R, [[1,2],[1,4],[2,4]])
sage: b = vector(R, [3,5,6])
sage: M.solve_right(b)
(1, 1)
</code></pre>
https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16510#post-id-16510It also works on the latest 6.0, so perhaps should you upgrade.Sun, 22 Dec 2013 11:52:38 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16510#post-id-16510Comment by tedmos for <p>You should directly work on the $\mathbb{Z}/m\mathbb{Z}$ module as follows:</p>
<pre><code>sage: R = IntegerModRing(15)
sage: M = Matrix(R, [[1,2],[1,4],[2,4]])
sage: b = vector(R, [3,5,6])
sage: M.solve_right(b)
(1, 1)
</code></pre>
https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16513#post-id-16513Hello and thanks for the answer. If I try your example above I get the following error:
TypeError: base ring must be an integral domain
I get this error if and only if m is not prime (in this case m=15 is not prime). I tried your example with the free sage provided online at [sagemath](http://www.sagemath.org/) and there it works fine. So could it be, that my version is not up to date, although I downloaded it last week?Sun, 22 Dec 2013 09:17:33 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16513#post-id-16513Comment by tedmos for <p>You should directly work on the $\mathbb{Z}/m\mathbb{Z}$ module as follows:</p>
<pre><code>sage: R = IntegerModRing(15)
sage: M = Matrix(R, [[1,2],[1,4],[2,4]])
sage: b = vector(R, [3,5,6])
sage: M.solve_right(b)
(1, 1)
</code></pre>
https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16511#post-id-16511This is my version: 'Sage Version 5.8, Release Date: 2013-03-15'
Sun, 22 Dec 2013 11:48:04 +0100https://ask.sagemath.org/question/10852/solving-a-matrix-equation-modulo-m/?comment=16511#post-id-16511