ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 13 Oct 2016 16:41:36 +0200assign variable valuehttps://ask.sagemath.org/question/35110/assign-variable-value/ Hi!
I'm just getting started with Sage and therefore my question may sound really stupid (and nevertheless I couldn't find an answer here).
I have a matrix with both symbolic entries and numerical values, let's say:
a,b=var('a,b')
M=matrix([[1,a], [0,b]])
Now I want do assign a value to a:
a=0
However if I print M to the screen it still says a in the matrix and not 0.
You can clearly see that I'm really not used to Sage, so what's the problem here?
What should I do if I want to assign a fixed value to a variable which is also used in the calculations where this variable appears?
Thank's for the answers, I know this will sound highly trivial for you!
PhilippMon, 10 Oct 2016 20:28:44 +0200https://ask.sagemath.org/question/35110/assign-variable-value/Answer by tmonteil for <p>Hi!</p>
<p>I'm just getting started with Sage and therefore my question may sound really stupid (and nevertheless I couldn't find an answer here).
I have a matrix with both symbolic entries and numerical values, let's say:</p>
<pre><code>a,b=var('a,b')
M=matrix([[1,a], [0,b]])
</code></pre>
<p>Now I want do assign a value to a:</p>
<pre><code>a=0
</code></pre>
<p>However if I print M to the screen it still says a in the matrix and not 0.</p>
<p>You can clearly see that I'm really not used to Sage, so what's the problem here?
What should I do if I want to assign a fixed value to a variable which is also used in the calculations where this variable appears?</p>
<p>Thank's for the answers, I know this will sound highly trivial for you!</p>
<p>Philipp</p>
https://ask.sagemath.org/question/35110/assign-variable-value/?answer=35127#post-id-35127You can just use substitution:
sage: M.substitute(a=0)
[1 0]
[0 b]
Tue, 11 Oct 2016 19:42:29 +0200https://ask.sagemath.org/question/35110/assign-variable-value/?answer=35127#post-id-35127Comment by philipp7 for <p>You can just use substitution:</p>
<pre><code>sage: M.substitute(a=0)
[1 0]
[0 b]
</code></pre>
https://ask.sagemath.org/question/35110/assign-variable-value/?comment=35144#post-id-35144Thanks you!Thu, 13 Oct 2016 16:41:36 +0200https://ask.sagemath.org/question/35110/assign-variable-value/?comment=35144#post-id-35144Answer by ndomes for <p>Hi!</p>
<p>I'm just getting started with Sage and therefore my question may sound really stupid (and nevertheless I couldn't find an answer here).
I have a matrix with both symbolic entries and numerical values, let's say:</p>
<pre><code>a,b=var('a,b')
M=matrix([[1,a], [0,b]])
</code></pre>
<p>Now I want do assign a value to a:</p>
<pre><code>a=0
</code></pre>
<p>However if I print M to the screen it still says a in the matrix and not 0.</p>
<p>You can clearly see that I'm really not used to Sage, so what's the problem here?
What should I do if I want to assign a fixed value to a variable which is also used in the calculations where this variable appears?</p>
<p>Thank's for the answers, I know this will sound highly trivial for you!</p>
<p>Philipp</p>
https://ask.sagemath.org/question/35110/assign-variable-value/?answer=35126#post-id-35126May be you can achieve what you want via an anonymous function.
a,b=var('a,b')
M = lambda a,b : matrix([[1,a], [0,b]])
A = M(a,b)
show(A)
B = M(a=0,b=b)
show(B)Tue, 11 Oct 2016 18:00:45 +0200https://ask.sagemath.org/question/35110/assign-variable-value/?answer=35126#post-id-35126Comment by philipp7 for <p>May be you can achieve what you want via an anonymous function.</p>
<pre><code>a,b=var('a,b')
M = lambda a,b : matrix([[1,a], [0,b]])
A = M(a,b)
show(A)
B = M(a=0,b=b)
show(B)
</code></pre>
https://ask.sagemath.org/question/35110/assign-variable-value/?comment=35143#post-id-35143Thank you! Not such a nice intuitive solution as I hoped to get, I admit. However, this just seems to be quite different in Sage compared to e.g. Mathematica.Thu, 13 Oct 2016 16:40:46 +0200https://ask.sagemath.org/question/35110/assign-variable-value/?comment=35143#post-id-35143