Linear Algebra – First Matrix Computations with Sage

Here is a short overview for doing matrix manipulations online using the Sage framework. For more information, see my Sage information page.

For short computations, I recommend using the SageCellServer. Here you can type in a few lines of code, hit “evaluate”, and see the result.

To get started, try this code:


When you hit “evaluate” you should see the result: 5.

Now try this code:

A = matrix([[1,2],[3,4]])

The first line tells the computer what A is. The second line tells the computer to show you what A is. What should happen if you only put in the first line? Try it out?

Now let’s compute the inverse of matrix A. We use the following code.

A = matrix([[1,2],[3,4]])
B = A.inverse()

Line 1 defines the matrix A.
Line 2 defines the matrix B as being equal to the inverse of A.
The way we do this is by attaching the inverse function to the matrix A. The dot (period) is the “attaching” operation in Sage.
Line 3 shows the matrix B.

Here is a more “efficient” way to do the same thing:

A = matrix([[1,2],[3,4]])
show( A.inverse() )

Here is a more complicated piece of code:

S = matrix([[1,-1],[2,2]])
D = matrix([[2,0],[0,3]])
A = S*D*S.inverse()

Let’s unpack what this is doing:
– Line 1 defines a change of coordinate matrix S
– Line 2 defines a diagonal matrix D that is the matrix of our transformation in the basis determined by the columns of S.
– Line 3 defines A = SDS^{-1}
– Line 4 shows us the matrix A.
If you wanted Sage to show the matrix S, what would you do?

Finally, let me mention the .eigenvectors_right() command. This command takes in a matrix and gives out a list of:
– eigenvalues, eigenvectors, and the multiplicity
For example:

S = matrix([[1,-1],[2,2]])
D = matrix([[2,0],[0,3]])
A = S*D*S.inverse()
show( A.eigenvectors_right() )

Does the result make sense, based on the way that we constructed the matrix A?

Important note: While it is useful to be able to have a computer do these computations, it is also important to be able to do the computations by hand!

This entry was posted in Linear Algebra, Linear Algebra 2020, Uncategorized. Bookmark the permalink.

Leave a comment here

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s