Ask Your Question

Transforming a matrix into a system of equations

asked 2017-04-03 22:21:45 -0500

anonymous user


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!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-04-04 02:10:50 -0500

nbruin gravatar image

Something 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.

edit flag offensive delete link more


thank 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) ?

Sasha-dpt gravatar imageSasha-dpt ( 2017-04-04 03:58:58 -0500 )edit

If 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])

nbruin gravatar imagenbruin ( 2017-04-04 10:43:02 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-04-03 22:21:45 -0500

Seen: 31 times

Last updated: Apr 04