Linear Algebra

with SAGE at UMBC


Contents

  1. Linear Algebra Basics
  2. Numerical Linear Algebra

[Prev] [Intro] [Next]

SAGE has basic commands and subroutines which implement a number of functions in linear algebra (and many other things). A general introduction to SAGE use and how SAGE can be accessed at UMBC can be found in a separate document.

Refs:

Linear Algebra Basics

SAGE represents matrices with matrix() objects and vectors as vector() objects. Multiplication and exponentiation are represented by the usual * and ^ (optionally **) operators:

Some examples of these basic operators: sage: A = matrix([[1,2,3],[4,5,6],[7,7,1]]) sage: A [1 2 3] [4 5 6] [7 7 1] sage: v = vector([1,1,4]) sage: v (1, 1, 4) sage: A*A # matrix-matrix product [30 33 18] [66 75 48] [42 56 64] sage: A^4 [ 3834 4473 3276] [ 8946 10491 7860] [ 7644 9170 7540] sage: A*v # matrix-vector product (15, 33, 18)

Solving linear systems can be done with the solve_right() method, which can also be written as A\v (backslash). There is also a corresponding solve_left() method. When a matrix is invertible, its inverse can be computed with either the inverse() method or by using the exponential function. Naively working with a non-invertible matrix has some problems though, as the solve_right() method only returns a single solution, while the correct answer would be a vector plus an element of the null space. sage: A.inverse() [-37/18 19/18 -1/6] [ 19/9 -10/9 1/3] [ -7/18 7/18 -1/6] sage: A^(-1) [-37/18 19/18 -1/6] [ 19/9 -10/9 1/3] [ -7/18 7/18 -1/6] sage: A\v (-5/3, 7/3, -2/3)

The null space of a matrix can be computed, but it takes a little work, as the methods available were written from the perspective of a mathematician, not a student. Rather than null space, the term kernel is used, as this is the more popular term in more abstract and general contexts. Slightly more confusing is the fact that this is defined in SAGE based on left multiplication, so if v*A=0 then v is defined to be in the kernel, rather than the more commonly used requirement that A*v=0. (This use of left multiplication of matrices is a perfectly valid convention commonly used in some fields of mathematics, but differs from that commonly used in linear algebra.) The kernel then returns the entire null space, which is a vector space, rather than a set of basis vectors for it. We then may want to explicitly ask for a basis. So to translate from the convention used in SAGE to the usual linear algebra convention we need to transpose our matrices, compute the kernel and then ask for a basis of it. sage: B = matrix([[1,2,3,4,5],[7,5,3,2,1],[1,1,1,1,1]]) sage: B [1 2 3 4 5] [7 5 3 2 1] [1 1 1 1 1] sage: B\vector([1,1,1]) (-11, 18, 0, -6, 0) sage: transpose(B).nullity() 2 sage: transpose(B).kernel().basis() [ (1, -2, 0, 2, -1), (0, 0, 1, -2, 1) ]

Thus, the full set of solutions to the equation B*x=v is any vector of the form (-11,18,0,-6,0) + a(1, -2, 0, 2, -1) + b(0, 0, 1, -2, 1).

Determinant, eigenvalues and eigenvectors are computed with the determinant() and eigenspaces() methods. In this example the matrix C has a determinant of 0, eigenvalues of λ∈{12, 0, -1} and an eigenvector correponding to λ=-1 of (1,1/2,-1). Note again that, when computing the eigenvectors you need to transpose the matrix if you are looking for the relation C*v = λv. sage: C = matrix([[1,2,3],[2,3,4],[5,6,7]]) sage: C.determinant() 0 sage: transpose(C).eigenspaces() [ (12, Vector space of degree 3 and dimension 1 over Rational Field User basis matrix: [ 1 10/7 19/7]), (0, Vector space of degree 3 and dimension 1 over Rational Field User basis matrix: [ 1 -2 1]), (-1, Vector space of degree 3 and dimension 1 over Rational Field User basis matrix: [ 1 1/2 -1]) ] sage: C*vector([1,1/2,-1]) (-1, -1/2, 1)

Finally, it is worth pointing out that, as SAGE still has a bias towards being useful for the professional rather than the student, it often produces eigenvalues and eigenvectors in a form which is harder to understand. In particular, if the eigenvalues are not rational (fractions) it will present them in exact form as roots of the characteristic polynomial, and then present the eigenvectors in terms of these roots. sage: D = matrix([[1,2,3],[2,3,-1],[1,3,-2]]) sate: D.transpose().eigenspaces() [ (a0, Vector space of degree 3 and dimension 1 over Number Field in a0 with defining polynomial x^3 - 2*x^2 - 9*x - 12 User basis matrix: [ 1 3/61*a0^2 + 5/61*a0 - 29/61 -2/61*a0^2 + 17/61*a0 - 1/61]) ] sage: D.transpose().characteristic_polynomial() x^3 - 2*x^2 - 9*x - 12 sage: evals = D.transpose().characteristic_polynomial().roots(CC) # force computation over complex numbers [(4.55453727907156, 1), (-1.27726863953578 - 1.00165866979120*I, 1), (-1.27726863953578 + 1.00165866979120*I, 1)] sage: e1 = evals[0][0] # the first eigenvalue sage: evect1 = [ 1, 3/61*e1^2 + 5/61*e1 - 29/61, -2/61*r1^2 + 17/61*r1 - 1/61] # its eigenvector [1, 0.918100260241238, 0.572778919529695] # Confirm that this is an eigenvector corresponding to the eigenvalue e1 = 4.55453727907156 sage: e1*vector(evect1) (4.55453727907156, 4.18152186119402, 2.60874294166432) sage: D*vector(evect1) (4.55453727907156, 4.18152186119402, 2.60874294166433)

Numerical Linear Algebra

A weak point of the native SAGE implementation is its numerical functions. The best way to work around this is to call one of several packages which do have strong numerical linear algebra capabilities from within SAGE. Some options are:

Linear Algebra Examples

sage: GL3Q = MatrixSpace(QQ,3,3) # 3x3 matrices over the rationals
sage: A = GL3Q([1,2,3, 4,5,6, 7,8,9])
sage: A.kernel()

Vector space of degree 3 and dimension 1 over Rational Field
Basis matrix:
[ 1 -2  1]
sage: A.eigenspaces()

[
(0, Vector space of degree 3 and dimension 1 over Rational Field
User basis matrix:
[ 1 -2  1]),
(a1, Vector space of degree 3 and dimension 1 over Number Field in a1 with defining polynomial x^2 - 15*x - 18
User basis matrix:
[            1 1/18*a1 + 1/3  1/9*a1 - 1/3])
]

[Prev] [Intro] [Next]
Robert Campbell
10 Nov, 2009