<script>
$(function () {sagecell.makeSagecell({inputLocation: '.sage-MD', linked: true});});
</script><h5 class="sage">
<span class="type">Sage</span> <span class="acro">MD</span> <span class="titletext">Matrix Diagonalization</span>
</h5>


<p>The third way to get eigenvectors is the matrix method <span class="code">.eigenmatrix_right()</span> (and the analogous <span class="code">.eigenmatrix_left()</span>).  It always returns two square matrices of the same size as the original matrix.  The first matrix of the output is a diagonal matrix with the eigenvalues of the matrix filling the diagonal entries of the matrix.  The second matrix has eigenvectors in the columns, in the same order as the corresponding eigenvalues.  For a single eigenvalue, these columns/eigenvectors form a linearly independent set.</p>

<p>A careful reading of the previous paragraph suggests the question:  what if we do not have enough eigenvectors to fill the columns of the second square matrix?  When the geometric multiplicity does not equal the algebraic multiplicity, the deficit is met by inserting zero columns in the matrix of eigenvectors.  Conversely, when the matrix is diagonalizable, by <a class="knowl" acro="DMFE" type="Theorem" title="Diagonalizable Matrices have Full Eigenspaces" knowl="./knowls/theorem.DMFE.knowl">Theorem DMFE</a> the geometric and algebraic multiplicities of each eigenvalue are equal, and the union of the bases of the eigenspaces provides a complete set of linearly independent vectors.  So for a matrix $A$, Sage will output two matrices, $D$ and $S$ such that $\inverse{S}AS=D$.</p>

<p>We can rewrite the relation above as $AS=SD$.  In the case of a non-diagonalizable matrix, the matrix of eigenvectors is singular (it has zero columns), but the relationship $AS=SD$ still holds.  Here are examples of the two scenarios, along with demonstrations of the matrix method <span class="code">is_diagonalizable()</span>.</p>

<br><br><div class="sage-MD"><script type="text/x-sage">A = matrix(QQ, [[ 2, -18, -68,  64, -99, -87,  83],
                [ 4, -10, -41,  34, -58, -57,  46],
                [ 4,  16,  59, -60,  86,  70, -72],
                [ 2, -15, -65,  57, -92, -81,  78],
                [-4,  -7, -32,  31, -45, -31,  41],
                [ 2,  -6, -22,  20, -32, -31,  26],
                [ 0,   7,  30, -27,  42,  37, -36]])
D, S = A.eigenmatrix_right()
D
</script></div>

<br><br><div class="sage-MD"><script type="text/x-sage">S
</script></div>

<br><br><div class="sage-MD"><script type="text/x-sage">S.inverse()*A*S == D
</script></div>

<br><br><div class="sage-MD"><script type="text/x-sage">A.is_diagonalizable()
</script></div>

<p>Now for a matrix that is far from diagonalizable.</p>

<br><br><div class="sage-MD"><script type="text/x-sage">B = matrix(QQ, [[ 37, -13,  30,  81, -74, -13,  18],
                [  6,  26,  21, -11, -46, -48,  19],
                [ 16,  10,  29,  16, -42, -39,  26],
                [-24,   8, -24, -53,  54,  13, -15],
                [ -8,   3,  -8, -20,  24,   4,  -5],
                [ 31,  12,  46,  48, -97, -56,  35],
                [  8,   5,  16,  12, -34, -20,  11]])
D, S = B.eigenmatrix_right()
D
</script></div>

<br><br><div class="sage-MD"><script type="text/x-sage">S
</script></div>

<br><br><div class="sage-MD"><script type="text/x-sage">B*S == S*D
</script></div>

<br><br><div class="sage-MD"><script type="text/x-sage">B.is_diagonalizable()
</script></div>
<br><br><div class="sage-MD sagecell-practice"><script type="text/x-sage"># Sage practice area
</script></div>
