ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 11 Apr 2017 18:51:26 -0500symbolically solve for unknown matrixhttp://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?
khalidaTue, 11 Apr 2017 18:51:26 -0500http://ask.sagemath.org/question/37263/Substitution using Dictionary with Matrix as Valuehttp://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/As a newcomer to SAGE, trying to use it to do symbolic linear algebra, I am wondering why substitution of a variable using a dictionary doesn't work in this case:
sage: aMatrix = matrix(SR,1,1)
sage: var('aVariable')
aVariable
sage: aDict = {}
sage: aDict[aVariable] = aMatrix
sage: aDict[aVariable]
[0]
but:
sage: aVariable.subs(aDict)
....
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.substitute (sage/symbolic/expression.cpp:16025)()
/Applications/sage/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression.coerce_in (sage/symbolic/expression.cpp:11265)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent_old.so in sage.structure.parent_old.Parent._coerce_ (sage/structure/parent_old.c:3369)()
/Applications/sage/local/lib/python2.7/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.coerce (sage/structure/parent.c:8912)()
TypeError: no canonical coercion from Full MatrixSpace of 1 by 1 dense matrices over Symbolic Ring to Symbolic Ring
Functionality to substitute matrices for variables seems to be indispensable to doing symbolic linear algebra, so I am sure there is a proper way to do this.
heatkernelThu, 14 Jun 2012 10:58:48 -0500http://ask.sagemath.org/question/9075/