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, 09 Jul 2015 17:57:51 +0200Condensing variables of a matrixhttps://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/I'm working with matrices such as
M = matrix([
[a, b, 0],
[c, 0, d],
[0, e, 0]])
where a, b, c, d, and e are variables. Then I loop through some numbers for each of a, b, c, d, and e, looking at the eigenvalues of the resulting matrix. $$ char(M) = x^3 - ax^2 - (bc + de)x + ade $$
So, I'm doing more work than necessary, and ideally, I'd reduce M down to
M = matrix([
[a, b, 0],
[1, 0, d],
[0, 1, 0]])
Is there a simple way of using Sage to do so?Tue, 07 Jul 2015 19:16:55 +0200https://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/Answer by tmonteil for <p>I'm working with matrices such as </p>
<pre><code>M = matrix([
[a, b, 0],
[c, 0, d],
[0, e, 0]])
</code></pre>
<p>where a, b, c, d, and e are variables. Then I loop through some numbers for each of a, b, c, d, and e, looking at the eigenvalues of the resulting matrix. $$ char(M) = x^3 - ax^2 - (bc + de)x + ade $$
So, I'm doing more work than necessary, and ideally, I'd reduce M down to</p>
<pre><code> M = matrix([
[a, b, 0],
[1, 0, d],
[0, 1, 0]])
</code></pre>
<p>Is there a simple way of using Sage to do so?</p>
https://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/?answer=27278#post-id-27278You can define a function `my_matrix`, which transforms Python variables to your matrix:
sage: my_matrix = lambda a,b,c,d,e : matrix([[a, b, 0], [c, 0, d], [0, e, 0]])
sage: my_matrix(pi,0,1,1/2,42)
[ pi 0 0]
[ 1 0 1/2]
[ 0 42 0]
Then you can do something like
sage: var('a,b,c,d,e') # those letters are symbols (like undeterminates), not Python variables.
(a, b, c, d, e)
sage: my_matrix(a,b,1,d,1)
[a b 0]
[1 0 d]
[0 1 0]
sage: my_matrix(a,b,1,d,1).charpoly()
x^3 - a*x^2 + (-b - d)*x + a*d
sage: my_matrix(a,a,a,a,a)
[a a 0]
[a 0 a]
[0 a 0]
Wed, 08 Jul 2015 15:32:15 +0200https://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/?answer=27278#post-id-27278Comment by Iceman for <p>You can define a function <code>my_matrix</code>, which transforms Python variables to your matrix:</p>
<pre><code>sage: my_matrix = lambda a,b,c,d,e : matrix([[a, b, 0], [c, 0, d], [0, e, 0]])
sage: my_matrix(pi,0,1,1/2,42)
[ pi 0 0]
[ 1 0 1/2]
[ 0 42 0]
</code></pre>
<p>Then you can do something like</p>
<pre><code>sage: var('a,b,c,d,e') # those letters are symbols (like undeterminates), not Python variables.
(a, b, c, d, e)
sage: my_matrix(a,b,1,d,1)
[a b 0]
[1 0 d]
[0 1 0]
sage: my_matrix(a,b,1,d,1).charpoly()
x^3 - a*x^2 + (-b - d)*x + a*d
sage: my_matrix(a,a,a,a,a)
[a a 0]
[a 0 a]
[0 a 0]
</code></pre>
https://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/?comment=27297#post-id-27297Maybe I should have been a little more explicit. I want a method for determining when a matrix can have the number of variables reduced. Implementing the reduction is the easy part.Thu, 09 Jul 2015 17:57:51 +0200https://ask.sagemath.org/question/27271/condensing-variables-of-a-matrix/?comment=27297#post-id-27297