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.Tue, 23 Dec 2014 12:04:50 +0100transform list of equations to coefficientmatrix?https://ask.sagemath.org/question/25308/transform-list-of-equations-to-coefficientmatrix/ I have a list of equations in multiple variables and therefor need to know if this list has a unique solution, if I had the coefficientmatrix this would be easily done by calculating the rank. So the question is, is there a fast way to transform the list of equations to a matrix?Sat, 20 Dec 2014 18:17:19 +0100https://ask.sagemath.org/question/25308/transform-list-of-equations-to-coefficientmatrix/Answer by calc314 for <p>I have a list of equations in multiple variables and therefor need to know if this list has a unique solution, if I had the coefficientmatrix this would be easily done by calculating the rank. So the question is, is there a fast way to transform the list of equations to a matrix?</p>
https://ask.sagemath.org/question/25308/transform-list-of-equations-to-coefficientmatrix/?answer=25309#post-id-25309I'm not aware of anything easy, although someone might suggest a shorter method using a polynomial ring. Here is what I've cooked up:
var('x y z')
eqs = [3*x+2*y+z == -4, 2*x-3*y+5*z==2,7*x+2*y-3*z==0]
lhs = map(lambda a: a.lhs(),eqs)
m=[]
for leftside in lhs:
m.append(map(lambda w: w.subs(x=1,y=1,z=1),leftside.operands()))
M=matrix(m)
Sat, 20 Dec 2014 19:44:29 +0100https://ask.sagemath.org/question/25308/transform-list-of-equations-to-coefficientmatrix/?answer=25309#post-id-25309Comment by Emiel Ergo for <p>I'm not aware of anything easy, although someone might suggest a shorter method using a polynomial ring. Here is what I've cooked up:</p>
<pre><code>var('x y z')
eqs = [3*x+2*y+z == -4, 2*x-3*y+5*z==2,7*x+2*y-3*z==0]
lhs = map(lambda a: a.lhs(),eqs)
m=[]
for leftside in lhs:
m.append(map(lambda w: w.subs(x=1,y=1,z=1),leftside.operands()))
M=matrix(m)
</code></pre>
https://ask.sagemath.org/question/25308/transform-list-of-equations-to-coefficientmatrix/?comment=25334#post-id-25334Thank you! this is already very helpfull!
although this does not work if not all equations are explicitly in all the variables. For example [x+y = 0, x+z = 5]
this code does work even with coefficients that are 0 but doesn't look pretty, can someone help me make it simpler?
s = list(var('x y z'))
eqs = [3*x+z == -4, 2*x-3*y+5*z==2,7*x+2*y-3*z==0]
lhs = map(lambda a: a.lhs(),eqs)
m = []
for leftside in lhs:
for i in range(3):
if leftside.coeff(s[i],1 ) == 0:
m.append(0)
else:
m.append(leftside.coeff(s[i],1 ))
M=matrix(3,m)
MTue, 23 Dec 2014 12:04:50 +0100https://ask.sagemath.org/question/25308/transform-list-of-equations-to-coefficientmatrix/?comment=25334#post-id-25334