I am new to sympy. I am working with sympy matrices. Is anybody knows about making a matrix as subject from a matrix equation? for examble if the equation is like following A+2B=C, here A,B and C are matrices. I want to make subject as B. So that the final answer must be looks like B=(C-A)/2. Is there any straight way in sympy to do this?
Asked
Active
Viewed 213 times
1 Answers
1
The approach offered by asmeurer seems to be applicable: see How to solve matrix equation with sympy?.
First, declare A, B and C to be non-commutative variables and obtain a solution to the equation. Second, re-define C and A as the desired arrays and then apply the formula to these arrays.
>>> from sympy import *
>>> A,B,C = symbols('A B C', commutative=False)
>>> solve(A+2*B-C,B)
[(-A + C)/2]
>>> A = Matrix([2,2,1,5])
>>> C = Matrix([1,1,1,1])
>>> A = A.reshape(2,2)
>>> C = C.reshape(2,2)
>>> (-A + C)/2
Matrix([
[-1/2, -1/2],
[ 0, -2]])
To answer the question in the comments: Define matrix C to be the zero matrix on the right of the equation and proceed as above.
>>> A,B,C = symbols('A B C', commutative=False)
>>> solve(2*A+B-C,A)
[(-B + C)/2]
>>> B = Matrix([1,4,3,5])
>>> B = B.reshape(2,2)
>>> C = Matrix([0,0,0,0])
>>> C = C.reshape(2,2)
>>> (-B + C)/2
Matrix([
[-1/2, -2],
[-3/2, -5/2]])
-
thank you for your answer. It has solved my problem. Now I have another problem that I couldn't mixedup matrix variables with Matrix values like 2*A+B=Matrix([[0,0],[0,0]]). Here I want make A as the subject. Is there any way to do with sympy? – Nilasini Dec 10 '16 at 19:40
-
Thanks. Sorry for the inconvenience basically what I want is my input for sympy contains matrix values mixedup with variables. so I can't change it as all matrices as variables first, like what you said above bcoz in my i/p there are multiple values and matrix variables, so I can't change each matrix values to variables and then send it to sympy. Is there any way to do this in sympy?. If not I planned to implement this for sympy. So please help me to find out whether this functionality already exist or not.i/p can be like this also 2*A+Matrix[] = Matrix[]. Please let me know if there is a way. – Nilasini Dec 10 '16 at 20:45
-
If you want to implement it then the best thing to do is to ask about this on the google group for sympy. Or on their gitter chat. You'll get answers from the most knowledgeable people in those places. (I'm a second or third stringer.) To get the addresses of those go to the sympy website. – Bill Bell Dec 10 '16 at 20:54