ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 19 Sep 2017 20:57:47 -0500symbolically solve for unknown matrixhttps://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/ 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?
Tue, 11 Apr 2017 18:51:26 -0500https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/Comment by aijony for <p>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.</p>
<p>For example given the matrix equation <code>y = Ax</code>, where <code>y</code> and <code>x</code> are column vectors with (say) 2 rows, and A is a 2x2 matrix, I can symbolically solve this equation as follows:</p>
<pre><code>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)
</code></pre>
<p>Which will give me an expression for each of the elements of <code>x</code> in terms of <code>[a, b, c, d]</code> and the elements of <code>y</code>.</p>
<p>Question: how to I get an expression for the elements of <code>A</code> (i.e. <code>[a, b, c, d]</code>, for a given (symbolic) <code>x</code> and <code>y</code>. Below is a concrete trivial example where the answer is obviously <code>[a=1, b=1, c=1, d=-1]</code>, but how could I solve for these elements in sagemath?</p>
<pre><code>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
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/?comment=38863#post-id-38863[Here](https://ask.sagemath.org/question/25895/solve-symbolic-matrix-equations/) is something similarTue, 19 Sep 2017 20:57:47 -0500https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/?comment=38863#post-id-38863Comment by mforets for <p>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.</p>
<p>For example given the matrix equation <code>y = Ax</code>, where <code>y</code> and <code>x</code> are column vectors with (say) 2 rows, and A is a 2x2 matrix, I can symbolically solve this equation as follows:</p>
<pre><code>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)
</code></pre>
<p>Which will give me an expression for each of the elements of <code>x</code> in terms of <code>[a, b, c, d]</code> and the elements of <code>y</code>.</p>
<p>Question: how to I get an expression for the elements of <code>A</code> (i.e. <code>[a, b, c, d]</code>, for a given (symbolic) <code>x</code> and <code>y</code>. Below is a concrete trivial example where the answer is obviously <code>[a=1, b=1, c=1, d=-1]</code>, but how could I solve for these elements in sagemath?</p>
<pre><code>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
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/?comment=37268#post-id-37268can you apply [vectorization](https://en.wikipedia.org/wiki/Vectorization_(mathematics))? 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](https://en.wikipedia.org/wiki/Kronecker_product) it exists in Sage, but it is called differently (the method is named `tensor_product`).Wed, 12 Apr 2017 03:17:12 -0500https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/?comment=37268#post-id-37268Answer by ndomes for <p>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.</p>
<p>For example given the matrix equation <code>y = Ax</code>, where <code>y</code> and <code>x</code> are column vectors with (say) 2 rows, and A is a 2x2 matrix, I can symbolically solve this equation as follows:</p>
<pre><code>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)
</code></pre>
<p>Which will give me an expression for each of the elements of <code>x</code> in terms of <code>[a, b, c, d]</code> and the elements of <code>y</code>.</p>
<p>Question: how to I get an expression for the elements of <code>A</code> (i.e. <code>[a, b, c, d]</code>, for a given (symbolic) <code>x</code> and <code>y</code>. Below is a concrete trivial example where the answer is obviously <code>[a=1, b=1, c=1, d=-1]</code>, but how could I solve for these elements in sagemath?</p>
<pre><code>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
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/?answer=37267#post-id-37267I 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]]
show(eqns)
sol = solve(eqns, [a,b,c,d])
solWed, 12 Apr 2017 03:11:23 -0500https://ask.sagemath.org/question/37263/symbolically-solve-for-unknown-matrix/?answer=37267#post-id-37267