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.Sat, 04 Jul 2020 11:20:20 +0200Right choice for a dictionary of matrices with non constant coefficients.https://ask.sagemath.org/question/52312/right-choice-for-a-dictionary-of-matrices-with-non-constant-coefficients/Hi,
K is any field.
I need to store matrices in a dictionary.
Some of them have constant coefficients in K (let M1 be such a matrix),
and some have non constant coefficients, say these coeffcients are functions from t to K (let M2 be such a matrix).
I need to get M2 from the dictionary, and then be able to manipulate it as if it was a function of t.
(T) In particular, I need to be able to manipulate the matrices obtained from M2 by setting t to be equal to some specific values.
Since my functions are polynomials for now, I have tried doing something like this:
```
var('t')
sample = {}
sample[('M1')] = matrix(QQ, 2, [7, 5, 2, 4])
sample[('M2')] = matrix(QQ['t'], 2, [t^2, 2, 3, 1 - t])
```
But then I don't know how to proceed to perform the task (T).
And I have the feeling that there are much better options.
What should I do?
Thanks.Fri, 03 Jul 2020 14:32:53 +0200https://ask.sagemath.org/question/52312/right-choice-for-a-dictionary-of-matrices-with-non-constant-coefficients/Answer by mwageringel for <p>Hi,</p>
<p>K is any field.</p>
<p>I need to store matrices in a dictionary.</p>
<p>Some of them have constant coefficients in K (let M1 be such a matrix),
and some have non constant coefficients, say these coeffcients are functions from t to K (let M2 be such a matrix).</p>
<p>I need to get M2 from the dictionary, and then be able to manipulate it as if it was a function of t.</p>
<p>(T) In particular, I need to be able to manipulate the matrices obtained from M2 by setting t to be equal to some specific values.</p>
<p>Since my functions are polynomials for now, I have tried doing something like this:</p>
<p><code>
var('t')
sample = {}
sample[('M1')] = matrix(QQ, 2, [7, 5, 2, 4])
sample[('M2')] = matrix(QQ['t'], 2, [t^2, 2, 3, 1 - t])
</code></p>
<p>But then I don't know how to proceed to perform the task (T).
And I have the feeling that there are much better options.</p>
<p>What should I do?</p>
<p>Thanks.</p>
https://ask.sagemath.org/question/52312/right-choice-for-a-dictionary-of-matrices-with-non-constant-coefficients/?answer=52318#post-id-52318You can use `subs` on a matrix to substitute variables by values:
sage: sample = {}
sage: sample['M1'] = matrix(QQ, 2, [7, 5, 2, 4])
sage: R.<t> = QQ[]
sage: sample['M2'] = matrix(R, 2, [t^2, 2, 3, 1 - t])
sage: sample['M2'].subs({t: 10})
[100 2]
[ 3 -9]
sage: sample['M2'].subs({t: 20})
[400 2]
[ 3 -19]
Sat, 04 Jul 2020 11:20:20 +0200https://ask.sagemath.org/question/52312/right-choice-for-a-dictionary-of-matrices-with-non-constant-coefficients/?answer=52318#post-id-52318