5.4 Eigenvectors and eigenvalues

How do you compute eigenvalues and eigenvectors using Sage?

Sage included both in the eigenspaces command, the output of which has several components, corresponding to the different eigenvalues.

sage: MS = MatrixSpace(QQ, 3, 3)
sage: A = MS([[1,1,0],[0,2,0],[0,0,3]])
sage: A
[1 1 0]
[0 2 0]
[0 0 3]
sage: A.eigenspaces()
[
(3, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[0 0 1]),
(2, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[0 1 0]),
(1, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[ 1 -1  0])
]
sage: A = MS([[1,1,0],[0,1,0],[0,0, 2]])
sage: A
[1 1 0]
[0 1 0]
[0 0 2]
sage: A.eigenspaces()
[
(2, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[0 0 1]),
(1, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[0 1 0])
]

A word of caution - if the eigenvalues are not in the base ring of the matrix space (the eigenvalues below are $ \pm \sqrt{3}$ ) then the output is incomplete:

sage: MS = MatrixSpace(QQ, 2, 2)
sage: A = MS([1,-4,1, -1])
sage: A.eigenspaces()
[
(a0, Vector space of degree 2 and dimension 1 over Number Field in a0 with defining polynomial x^2 + 3
User basis matrix:
[     1 a0 - 1])
]
sage: MS = MatrixSpace(CC, 2, 2)
sage: A = MS([1,-4,1, -1])
sage: A.eigenspaces(even_if_inexact=True) # random output
[                              
(1.73205080756888*I, [                
]),
(-1.73205080756888*I, [
])
]

Another approach is to use the interface with Maxima:

sage: A = maxima("matrix ([1, -4], [1, -1])")
sage: eig = A.eigenvectors()
sage: eig
[[[-sqrt(3)*%i,sqrt(3)*%i],[1,1]],[1,(sqrt(3)*%i+1)/4],[1,-(sqrt(3)*%i-1)/4]]
This tells us that $ \vec{v}_1 = [1,(\sqrt{3}i + 1)/4]$ is an eigenvector of $ \lambda_1 = - \sqrt{3}i$ (which occurs with multiplicity one) and $ \vec{v}_2 = [1,(-\sqrt{3}i + 1)/4]$ is an eigenvector of $ \lambda_2 = \sqrt{3}i$ (which also occurs with multiplicity one).

Here are two more examples:

sage: A = maxima("matrix ([11, 0, 0], [1, 11, 0], [1, 3, 2])")
sage: A.eigenvectors()
[[[2,11],[1,2]],[0,0,1],[0,1,1/3]]
sage: A = maxima("matrix ([-1, 0, 0], [1, -1, 0], [1, 3, 2])")
sage: A.eigenvectors()
 [[[-1,2],[2,1]],[0,1,-1],[0,0,1]]
Warning: Notice how the ordering of the output is reversed, though the matrices are almost the same.

Finally, you can use Sage's GAP interface as well to compute ``rational'' eigenvalues and eigenvectors:

sage: print gap.eval("A := [[1,2,3],[4,5,6],[7,8,9]]")
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
sage: print gap.eval("v := Eigenvectors( Rationals,A)")
[ [ 1, -2, 1 ] ]
sage: print gap.eval("lambda := Eigenvalues( Rationals,A)")
[ 0 ]

See About this document... for information on suggesting changes.