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.Mon, 30 Apr 2012 07:16:36 +0200Using the solution of a linear system, and splitting a matrixhttps://ask.sagemath.org/question/8925/using-the-solution-of-a-linear-system-and-splitting-a-matrix/Hi there,
I'm fairly new to Sage and Python, so I'm getting into basic problems here that I'd be happy if you could help me out.<br>
Here it is, actually, here they are:
I'm generating a set of equalities and solving them with solve. A simple example:
<blockquote> import numpy as n;<br>m=4;<br>
s = list(var('s_%d' % int(i)) for i in range(m));<br>
eqns=[s_0+s_1==1,s_2-s_3==0];<br>
sol=solve(eqns,s,solution_dict=True)[0]
</blockquote>
This gives the solutions:
<blockquote>
{s_1: -r2 + 1, s_0: r2, s_3: r1, s_2: r1}
</blockquote>
My first question is, how do I create a matrix with the solutions? Say, something like:
<blockquote>M=m.zeros((2,2));<br>
for i in range(2):<br>
for j in range(2):<br>
M[int(i),int(j)] = sol[s[i]]+ sol[s[j+2]]
</blockquote>
This is giving me the error: "TypeError: unable to simplify to float approximation"
My second question, would be, given the array M, how do I split it as r1 times a matrix, plus r2 times another matrix, plus a constant matrix? In the above example,
<blockquote> M= [[1,r1+r2],[r1-r2+1,r1-r2+1]]= r1 [[0,1],[1,1]] + r2 [[0,1],[-1,-1]]+ [[1,0],[0,0]]
</blockquote>
I'm interested in the matrices multiplying the still unknown coefficients. Maybe I should add that the number of equations in the problem I'm solving is much bigger than in this simple example, and therefore I cannot find this matrix decomposition by simply looking at it.<br>
Thanks for the help!
Wed, 25 Apr 2012 09:56:14 +0200https://ask.sagemath.org/question/8925/using-the-solution-of-a-linear-system-and-splitting-a-matrix/Answer by ndomes for <p>Hi there,
I'm fairly new to Sage and Python, so I'm getting into basic problems here that I'd be happy if you could help me out.<br/>
Here it is, actually, here they are:
I'm generating a set of equalities and solving them with solve. A simple example:</p>
<blockquote> import numpy as n;<br/>m=4;<br/>
s = list(var('s_%d' % int(i)) for i in range(m));<br/>
eqns=[s_0+s_1==1,s_2-s_3==0];<br/>
sol=solve(eqns,s,solution_dict=True)[0]
</blockquote>
<p>This gives the solutions:</p>
<blockquote>
{s_1: -r2 + 1, s_0: r2, s_3: r1, s_2: r1}
</blockquote>
<p>My first question is, how do I create a matrix with the solutions? Say, something like:</p>
<blockquote>M=m.zeros((2,2));<br/>
for i in range(2):<br/>
for j in range(2):<br/>
M[int(i),int(j)] = sol[s[i]]+ sol[s[j+2]]
</blockquote>
<p>This is giving me the error: "TypeError: unable to simplify to float approximation"</p>
<p>My second question, would be, given the array M, how do I split it as r1 times a matrix, plus r2 times another matrix, plus a constant matrix? In the above example, </p>
<blockquote> M= [[1,r1+r2],[r1-r2+1,r1-r2+1]]= r1 [[0,1],[1,1]] + r2 [[0,1],[-1,-1]]+ [[1,0],[0,0]]
</blockquote>
<p>I'm interested in the matrices multiplying the still unknown coefficients. Maybe I should add that the number of equations in the problem I'm solving is much bigger than in this simple example, and therefore I cannot find this matrix decomposition by simply looking at it.<br/></p>
<p>Thanks for the help!</p>
https://ask.sagemath.org/question/8925/using-the-solution-of-a-linear-system-and-splitting-a-matrix/?answer=13510#post-id-13510You need a matrix that can handle symbolic expressions
M = matrix(SR,2,2) #m.zeros((2,2));
for i in range(2):
for j in range(2):
M[int(i),int(j)] = sol[s[i]]+ sol[s[j+2]]
show(M)Mon, 30 Apr 2012 07:16:36 +0200https://ask.sagemath.org/question/8925/using-the-solution-of-a-linear-system-and-splitting-a-matrix/?answer=13510#post-id-13510