Ask Your Question

Using the solution of a linear system, and splitting a matrix

asked 2012-04-25 02:56:14 -0500

fmelo gravatar image

updated 2012-04-25 03:21:04 -0500

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.
Here it is, actually, here they are: I'm generating a set of equalities and solving them with solve. A simple example:

import numpy as n;
s = list(var('s_%d' % int(i)) for i in range(m));

This gives the solutions:

{s_1: -r2 + 1, s_0: r2, s_3: r1, s_2: r1}

My first question is, how do I create a matrix with the solutions? Say, something like:

for i in range(2):
for j in range(2):
M[int(i),int(j)] = sol[s[i]]+ sol[s[j+2]]

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,

M= [[1,r1+r2],[r1-r2+1,r1-r2+1]]= r1 [[0,1],[1,1]] + r2 [[0,1],[-1,-1]]+ [[1,0],[0,0]]

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.

Thanks for the help!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-04-30 00:16:36 -0500

ndomes gravatar image

You 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]] 
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools


Asked: 2012-04-25 02:56:14 -0500

Seen: 249 times

Last updated: Apr 30 '12