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.Tue, 04 Apr 2017 10:43:02 -0500Transforming a matrix into a system of equationshttp://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/ I have a $n \times n$ matrix that I would like to transform into a system of $n$ equations (for $n$ disctinct variables). I am not sure how to proceed (the end goal being to solve the system eventually). The number of variables is not fixed. Should I consider every row of the lattice and map each coefficient $c_{ij}$ to a string that I write as $"c_{11}x_1 + .....+ c_{1n-1}x_n + c_{1n}$. I am hoping there exists a more direct solution. Thanks!Mon, 03 Apr 2017 22:21:45 -0500http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/Answer by nbruin for <p>I have a $n \times n$ matrix that I would like to transform into a system of $n$ equations (for $n$ disctinct variables). I am not sure how to proceed (the end goal being to solve the system eventually). The number of variables is not fixed. Should I consider every row of the lattice and map each coefficient $c_{ij}$ to a string that I write as $"c_{11}x_1 + .....+ c_{1n-1}x_n + c_{1n}$. I am hoping there exists a more direct solution. Thanks!</p>
http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/?answer=37160#post-id-37160Something like:
sage: M=matrix(3,3,[1,2,3,4,5,6,7,8,9])
sage: P=PolynomialRing(QQ,M.nrows(),names="x")
sage: (vector(P.gen(i) for i in range(3))*M).list()
[x0 + 4*x1 + 7*x2, 2*x0 + 5*x1 + 8*x2, 3*x0 + 6*x1 + 9*x2]
perhaps? It will depend on your application a bit whether you need to convert to/from lists and what kind of polynomial ring you need. A good rule of thumb is avoiding going through strings. It will be much more robust if the system knows the meaning of the objects throughout.Tue, 04 Apr 2017 02:10:50 -0500http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/?answer=37160#post-id-37160Comment by nbruin for <p>Something like:</p>
<pre><code>sage: M=matrix(3,3,[1,2,3,4,5,6,7,8,9])
sage: P=PolynomialRing(QQ,M.nrows(),names="x")
sage: (vector(P.gen(i) for i in range(3))*M).list()
[x0 + 4*x1 + 7*x2, 2*x0 + 5*x1 + 8*x2, 3*x0 + 6*x1 + 9*x2]
</code></pre>
<p>perhaps? It will depend on your application a bit whether you need to convert to/from lists and what kind of polynomial ring you need. A good rule of thumb is avoiding going through strings. It will be much more robust if the system knows the meaning of the objects throughout.</p>
http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/?comment=37169#post-id-37169If you multiply on the other side: `M*(vector(P.gen(i) for i in range(3)))` sage considers the vector as a column rather than a row. The right length is `M.ncols()` in that case. You can build your vector any way you like. You can just append a "1": `vector([P.gen(i) for i in range(2)] + [1])`Tue, 04 Apr 2017 10:43:02 -0500http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/?comment=37169#post-id-37169Comment by Sasha-dpt for <p>Something like:</p>
<pre><code>sage: M=matrix(3,3,[1,2,3,4,5,6,7,8,9])
sage: P=PolynomialRing(QQ,M.nrows(),names="x")
sage: (vector(P.gen(i) for i in range(3))*M).list()
[x0 + 4*x1 + 7*x2, 2*x0 + 5*x1 + 8*x2, 3*x0 + 6*x1 + 9*x2]
</code></pre>
<p>perhaps? It will depend on your application a bit whether you need to convert to/from lists and what kind of polynomial ring you need. A good rule of thumb is avoiding going through strings. It will be much more robust if the system knows the meaning of the objects throughout.</p>
http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/?comment=37162#post-id-37162thank you ! I have two additional questions. It seems that your solution gives the column coefficient to a polynomial (and not the rows). Would it be possible to have the row coefficients? One way would be to simple transpose the matrix beforehand. I have tried using M.ncolumns() but it seems to generate an error. Also, is there anyway the last coefficient could be a constant variable (1) (in order to have $ax+by+c$ for example) ?Tue, 04 Apr 2017 03:58:58 -0500http://ask.sagemath.org/question/37157/transforming-a-matrix-into-a-system-of-equations/?comment=37162#post-id-37162