ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sat, 16 Jun 2012 00:33:35 -0500Substitution 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.
Thu, 14 Jun 2012 10:58:48 -0500http://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/Answer by burcin for <p>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:</p>
<pre><code>sage: aMatrix = matrix(SR,1,1)
sage: var('aVariable')
aVariable
sage: aDict = {}
sage: aDict[aVariable] = aMatrix
sage: aDict[aVariable]
[0]
</code></pre>
<p>but:</p>
<pre><code>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
</code></pre>
<p>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. </p>
http://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/?answer=13714#post-id-13714Symbolic expressions do not support working with Sage matrices. There are different approaches to do "linear algebra" in this context though.
- Indexed expressions
This is [GiNaC](http://www.ginac.de/)'s solution. Our symbolics library [pynac](https://bitbucket.org/burcin/pynac) is based on GiNaC, so we just inherit this functionality. Although it hasn't been wrapped for easy use from Python.
There is an experimental patch at [#11576](http://trac.sagemath.org/sage_trac/ticket/11576), but the [GiNaC tutorial](http://www.ginac.de/tutorial/Indexed-objects.html) is the best reference.
- SymbolicMatrixAlgebra as Nicolas Thiery implemented in [this thread](https://groups.google.com/d/msg/sage-devel/yvtIbHmi6zw/6vjY-HGW0eEJ).
Any help to get these things in a polished state so they can be included in Sage is welcome.Sat, 16 Jun 2012 00:33:35 -0500http://ask.sagemath.org/question/9075/substitution-using-dictionary-with-matrix-as-value/?answer=13714#post-id-13714