Ask Your Question

symbolically solve for unknown matrix

asked 2017-04-11 18:51:26 -0500

How do you solve a matrix equation for an unknown symbolic matrix? I have read the documentation on symbolic matrix calcs, and the documentation of linear algebra / systems of linear equations, but can't work out how to do what I want.

For example given the matrix equation y = Ax, where y and x are column vectors with (say) 2 rows, and A is a 2x2 matrix, I can symbolically solve this equation as follows:

A = matrix(SR, 2, 2, [[var('a'),var('b')],[var('c'),var('d')]])
y = vector([var('y1'), var('y2')])
# Solve for x such thay Ax = y:
x = A.solve_right(y)

Which will give me an expression for each of the elements of x in terms of [a, b, c, d] and the elements of y.

Question: how to I get an expression for the elements of A (i.e. [a, b, c, d], for a given (symbolic) x and y. Below is a concrete trivial example where the answer is obviously [a=1, b=1, c=1, d=-1], but how could I solve for these elements in sagemath?

A = matrix(SR, 2, 2, [[var('a'), var('b')],[var('c'), var('d')]])
x = vector(SR, [var('x1'), var('x2')])
y = vector(SR, [x1 + x2, x1-x2])
#solve(A*x == y, A) # <= This doesn't work
#solve(A*x == y, ['a', 'b', 'c', 'd']) # <= Nor does this

I think I could probably do it by looping through each row (equation) in the system of equations, and generating a list of equations and then solving those for the unknowns, but was wondering if there's a better way?

edit retag flag offensive close merge delete


can you apply vectorization? in particular $\text{vec}~(Ax) = (x^T \otimes I_n) \text{vec}~A$ if $A$ is a square matrix of order $n$. i didn't find by tab completion this functionality out of the box in Sage (as with Matlab's vec command); for the kronecker product it exists in Sage, but it is called differently (the method is named tensor_product).

mforets gravatar imagemforets ( 2017-04-12 03:17:12 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-04-12 03:11:23 -0500

ndomes gravatar image

I recommend list comprehension to generate the list of equations.

A = matrix(SR,2,2,var('a b c d')) 
x = vector(SR, var('x1 x2'))
y = vector(SR, [x1 + x2, x1-x2])
eqns = [(A*x)[k] == y[k] for k in [0,1]]
sol = solve(eqns, [a,b,c,d]) 
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

1 follower


Asked: 2017-04-11 18:51:26 -0500

Seen: 51 times

Last updated: Apr 12