Linear Algebra Sandbox

Click the "Execute Code" button below to run the Sage code below. You can edit the code and execute again!

You can copy the code (or pieces of the code) below and paste into the sandbox above. This code runs through a bunch of standard linear algebra stuff like computing RREF, transpose, determinant, eigenvalues/eigenvectors, and Gram-Schmidt.

Keep in mind that matrices are being constructed row by row (you input a list of rows). "#" starts a comment and won't be executed. Several matrices are defined over "SR" which allows for symbolic computations. You can use "pi", "e", "sqrt(2)", and even variables in matrices. Just keep in mind that Sage might need you to declare a variable like "a = var('a')". Most commands are called as attributes of an object. So applying a command to A looks like "A.command()". Finally, the sandbox only prints out what you tell it to plus the last thing computed. That is why I have the "pretty_print" commands - to show output!

# Define a matrix with SR = "Symbolic Ring" entries.
# This allows for entries like pi and e and square roots like sqrt(2).
A = matrix(SR,[[1,2,3],[3,2,1],[1,1,1]])

# We use pretty_print to show output.
# This prints "A = " and then the matrix stored in variable A.
pretty_print("A = ", A)

# A.transpose() computes A's transpose.
pretty_print("A^T = ", A.transpose())

# A "vector" can be treated like a column vector,
# but here it prints out like an ordered triple.
w = vector(SR,[1,1,-4])
pretty_print("vector: w = ", w)
pretty_print("Aw = ", A*w)

# A.echelon_form() computes the RREF of A.
pretty_print("RREF of A: ", A.echelon_form())

# If you want to augment a matrix (slap one matrix next to another),
# you can use augment. Here I'm putting the 3x3 identity next to A:
pretty_print("[A:I] = ", A.augment(matrix.identity(3)))

# To work with unknown quantities we declare variable names
# first. Then we define our arbitrary 2x2 matrix.
# Then SAGE can compute a general 2x2 determinant.
a = var('a')
b = var('b')
c = var('c')
d = var('d')
B = matrix(SR,[[a,b],[c,d]])
pretty_print("B = ", B)
pretty_print("det(B) = ", B.determinant())

# SAGE has several simplification commands.
# Unfortunately the one we want "full_simplify" cannot
# just be applied to a matrix. Instead we have to use a
# little programming magic to apply this command to every
# entry of our matrix.
# Here we compute the inverse of B (i.e., B^(-1)) and
# then simplify the result.
pretty_print("B^(-1) = ", (B^(-1)).apply_map(lambda x: x.full_simplify()))

# A.charpoly() computes the characteristic polynomial of A (with indeterminant x)
# as defined by det(xI-A). To get the char. poly. as defined in Lay (i.e., det(A-xI))
# we may need a sign adjustment: (-1)^(# of rows/cols of A). 
# Note: A.nrows() is the number of rows in A.
n = A.nrows()
pretty_print("The characteristic polynomial of A: det(A-xI) = ", (-1)^n*A.charpoly())

# Eigenvalues/eigenvectors
# A.eigenvectors_right() produces a list. Each entry is:
# (eigenvalue,[basis of eigenvectors for the eigenspace], algebraic multiplicity) 
pretty_print(A.eigenvalues())
pretty_print(A.eigenvectors_right())

# Diagonalization. 
# The "Jordan Form" is the closest a matrix can get to being
# diagonal. If a matrix is diagonalizable, its Jordan form 
# will be a diagonal matrix.
# A.jordan_form(transformation=True) spits out (J,P) where
# J is the Jordan form of A and P^(-1)AP = J.
(J,P) = A.jordan_form(transformation=True)
pretty_print("P = ",P)
pretty_print("J = ",J)
pretty_print("PJP^(-1) = A = ",P*J*P^(-1))
pretty_print("P^(-1)AP = J = ",P^(-1)*A*P)

# SAGE doesn't allow Gram-Schmidt run with entries in the Symbolic Ring (SR).
# Our matrix needs to have either (decimal) approximate entries or rational
# number entries (QQ = rational numbers like "-3/2").
# C.gram_schmidt spits out matrices (Q,R). Q's rows are the result of running
# Gram-Schmidt on the rows of C. 
C = matrix(QQ,[[1,0,-1,1],[1,1,0,1],[1,-1,0,2]])
pretty_print("Run Gram-Schmidt on the ROWS of C... ", C)
(Q,R) = C.gram_schmidt()
pretty_print("The rows are an orthogonal basis for Row(C)... ",Q)