Book Image

Sage Beginner's Guide

By : Craig Finch
1 (1)
Book Image

Sage Beginner's Guide

1 (1)
By: Craig Finch

Overview of this book

Table of Contents (17 chapters)
Sage Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – computing eigenvalues and eigenvectors


Let's see how to compute the eigenvalues and eigenvectors for a 3x3 matrix. Evaluate the following code:

 A = Matrix(QQ, [[2, -3, 1], [1, -2, 1], [1, -3, 2]])
print("Matrix A:")
show(A)

print("Eigenvalues:")
print(A.eigenvalues())
ev = A.eigenvectors_right()
for v in ev:
    print("Eigenvalue: {0}".format(v[0]))
    print("   Multiplicity: {0}".format(v[2]))
    print("   Eigenvectors:")
    for e in v[1]:
        print("   " + str(e))
    
print("Eigenmatrices:")
D, P = A.eigenmatrix_right()
print("D:")
show(D)
print("P:")
show(P)
print(A*P == P*D)

The output should look like this:

What just happened?

We defined a 3x3 matrix of rational numbers, and used the eigenvalues method to return a list of eigenvalues. We then used the eigenvectors_right method to return a list of tuples that contain data about the eigenvectors. We used a for loop to iterate through the list and print the information in a more readable format. Each element in...