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.Tue, 19 Aug 2014 13:16:25 -0500Newbie question: introducing symbols (variables) inside vectors and matriceshttp://ask.sagemath.org/question/23833/newbie-question-introducing-symbols-variables-inside-vectors-and-matrices/Hi,
I would like to have linear algebra operations to be evaluated with symbols instead of numerically.
For example, having the following matrix:
A = matrix(QQ,[
[2,1,2,-6],
[-1,2,1,7],
[3,-1,-3,-1],
[1,5,6,0],
[2,2,1,1]
])
I would like to multiply for a vector with symbolic variables as follows:
t = 'real'
var('x1')
assume(x1,t)
var('x2')
assume(x2,t)
var('x3')
assume(x3,t)
var('x4')
assume(x4,t)
xx = vector(QQ,[x1,x2,x3,x4])
A * xx.transpose()
Unfortunately building the `xx` vector is unsuccessful, producing this error message:
TypeError: Cannot evaluate symbolic expression to a numeric value.
This does not work, so how can I use symbols in sage's linear algebra framework?
thank you very much,
-FrancescoTue, 19 Aug 2014 11:18:57 -0500http://ask.sagemath.org/question/23833/newbie-question-introducing-symbols-variables-inside-vectors-and-matrices/Answer by kcrisman for <p>Hi,</p>
<p>I would like to have linear algebra operations to be evaluated with symbols instead of numerically.
For example, having the following matrix:</p>
<pre><code>A = matrix(QQ,[
[2,1,2,-6],
[-1,2,1,7],
[3,-1,-3,-1],
[1,5,6,0],
[2,2,1,1]
])
</code></pre>
<p>I would like to multiply for a vector with symbolic variables as follows:</p>
<pre><code>t = 'real'
var('x1')
assume(x1,t)
var('x2')
assume(x2,t)
var('x3')
assume(x3,t)
var('x4')
assume(x4,t)
xx = vector(QQ,[x1,x2,x3,x4])
A * xx.transpose()
</code></pre>
<p>Unfortunately building the <code>xx</code> vector is unsuccessful, producing this error message:</p>
<pre><code>TypeError: Cannot evaluate symbolic expression to a numeric value.
</code></pre>
<p>This does not work, so how can I use symbols in sage's linear algebra framework?</p>
<p>thank you very much,
-Francesco</p>
http://ask.sagemath.org/question/23833/newbie-question-introducing-symbols-variables-inside-vectors-and-matrices/?answer=23834#post-id-23834Why don't you try not specifying a ring for the vector?
xx = vector([x1,x2,x3,x4])
It should then put things in the "symbolic ring" `SR` which should do what you need it to. Tue, 19 Aug 2014 12:19:43 -0500http://ask.sagemath.org/question/23833/newbie-question-introducing-symbols-variables-inside-vectors-and-matrices/?answer=23834#post-id-23834Comment by stablum for <p>Why don't you try not specifying a ring for the vector? </p>
<pre><code>xx = vector([x1,x2,x3,x4])
</code></pre>
<p>It should then put things in the "symbolic ring" <code>SR</code> which should do what you need it to. </p>
http://ask.sagemath.org/question/23833/newbie-question-introducing-symbols-variables-inside-vectors-and-matrices/?comment=23835#post-id-23835Thanks! It worked! :)Tue, 19 Aug 2014 13:16:25 -0500http://ask.sagemath.org/question/23833/newbie-question-introducing-symbols-variables-inside-vectors-and-matrices/?comment=23835#post-id-23835