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.Tue, 07 Nov 2017 18:37:45 +0100how to use variables in a matrix ?https://ask.sagemath.org/question/39413/how-to-use-variables-in-a-matrix/ Hello,
this code below doesn't do what I expect.
var('x');
M(x) = matrix(2, 2, [x,x,x,x]);
M(3)
Can someone tell me what to do please ?Tue, 07 Nov 2017 11:40:07 +0100https://ask.sagemath.org/question/39413/how-to-use-variables-in-a-matrix/Answer by tmonteil for <p>Hello,</p>
<p>this code below doesn't do what I expect.</p>
<p>var('x');
M(x) = matrix(2, 2, [x,x,x,x]);
M(3)</p>
<p>Can someone tell me what to do please ?</p>
https://ask.sagemath.org/question/39413/how-to-use-variables-in-a-matrix/?answer=39420#post-id-39420Well, you should perhaps tell what you would expect... Let me suggest two possibilities:
- use a Python function:
sage: M = lambda x : matrix(2, 2, [x,x,x,x])
sage: M(3)
[3 3]
[3 3]
- if you want to consider your matrix as a symbolic object:
sage: var('x'); M = matrix(2, 2, [x,x,x,x])
x
sage: M
[x x]
[x x]
sage: M.substitute(x=3)
[3 3]
[3 3]Tue, 07 Nov 2017 13:47:56 +0100https://ask.sagemath.org/question/39413/how-to-use-variables-in-a-matrix/?answer=39420#post-id-39420Comment by Kamel for <p>Well, you should perhaps tell what you would expect... Let me suggest two possibilities:</p>
<ul>
<li><p>use a Python function:</p>
<pre><code>sage: M = lambda x : matrix(2, 2, [x,x,x,x])
sage: M(3)
[3 3]
[3 3]
</code></pre></li>
<li><p>if you want to consider your matrix as a symbolic object:</p>
<pre><code>sage: var('x'); M = matrix(2, 2, [x,x,x,x])
x
sage: M
[x x]
[x x]
sage: M.substitute(x=3)
[3 3]
[3 3]
</code></pre></li>
</ul>
https://ask.sagemath.org/question/39413/how-to-use-variables-in-a-matrix/?comment=39424#post-id-39424ok, thanks.Tue, 07 Nov 2017 18:37:45 +0100https://ask.sagemath.org/question/39413/how-to-use-variables-in-a-matrix/?comment=39424#post-id-39424