BasicLinearAlgebra.mws

Some Basic Linear Algebra

Draft Version .8 for Maple VI

(Thanks to Alex Smith for much work on this.)

This worksheet documents the newer more numerically efficient LinearAlgebra package.

The traditional Maple linear algebra package is called linalg. The older linalg may have greater

symbolic capability but LinearAlgebra has some serious speed advantages.

Load the linear algebra package. Below is the list of routines. You can get
help on any of them by typing ?
command_name and then hitting the <ENTER> key.

> with(LinearAlgebra);

Entering Matrices and Vectors

Enter a 2 by 3 matrix:

> A1 := Matrix(2,3,[[1,2,3],[4,5,6]]);

Or Simply

> A1 := Matrix([[1,2,3],[4,5,6]]);

Vectors are entered similarly

> v1 := Vector([5,3,1]); v2 := Vector([3,4]);

Matrix Multiplication, Evalm, Transpose, Determinant, Inverses

'.' is the matrix multiplication operator.

> (A1 . v1) + v2;

> A2 := A1 . Transpose(A1);

Ordinary scalar multiplication is indicated with a *.

> 2*A2;

> Determinant(A2);

Calculate the inverse of a matrix.
A2^(-1) would also work here.

> A2_inv := MatrixInverse(A2);

The Map Function - Applying a function to every entry of a matrix.

Convert to floating point.

> map(evalf, A2_inv);

Bases, Rank and Subspaces Associated to a Matrix

Compute a basis for the range of A1.

> ColumnSpace(Transpose(A1));

Basis for the kernel.

> NullSpace(Matrix(3,3,[[1,2,3],[2,4,6],[3,6,9]]));

> Rank(A2);

Find a basis for the subspace spanned by a set of vectors.

> Basis({Vector([1,0,1]),Vector([1,0,5]),Vector([3,0,1])});

> JordanForm(A2);

Solution of a Linear System

> LinearSolve(Matrix([[a,b],[c,d]]),Vector([e,f]));

Dot Product and Cross Product

Dot product.

> DotProduct(v1, Vector([1,4,8]));

Cross product in R^3.

> CrossProduct(v1,Vector([1,0,0]));

Rows, Columns, and Submatrices

Copy the all the entries of A1 into the matrix B.

> B:= Matrix(3,3, A1);

Or

> B:= Matrix(3,3); B[2..3,1..3] := A1; B;

Row 1 of A1.

> Row(A1,1);

Column 1 of A1.

> Column(A1,1);

Construct a submatrix of A using rows 1..2 and column 1.

> SubMatrix(A1,1..2,1..1);

Norms

The norm function uses absolute value signs which is somewhat inconvenient for algebraic
simplification.

> Norm(Vector([x,y]),2);

So it may be better top define your own vector norm function:

> vnorm := u -> sqrt(DotProduct(u,u));

> vnorm(Vector([x,y]),2);

The default norm is the infinity one.

> Norm(Vector([x,y]));

Using Maple's Solve Command to find the set of matrices which
commute with a given one.

Matrix equations can be converted into sets of equations and then solved.
For example consider the problem of finding all matrices B which commute with the matrix A2 above (i.e. A2 B - B A2 = 0) :

> B := Matrix(2,2,[[a,b],[c,d]]);

> mat_with_unknowns := A2 . B - B . A2;

Now we solve for the unknown entries a,b,c, and d.
(This relies on the fact that Maple's solve command ordinarily wants a set of equations in its
first argument. But if given a first argument which is a set of algebraic equations,
it interprets these as a set of equations by viewing each expression as equal to 0.)

> soln1 := solve(convert(mat_with_unknowns,set),{a,b,c,d});

This globally assigns b and a to have the values mentioned in soln1.

> assign(soln1);

> print(B);

Gram Schmidt, GenerateEquations, LinearSolve

Convert a list of vectors into a list of orthogonal vectors. The first k vectors in
each list (for any k) span the same subspace.

> GramSchmidt([Vector([1,1,1]),Vector([1,2,3]),Vector([1,4,5])]);

GenerateEquations can be used to convert a matrix into linear equations in a number of ways:

> GenerateEquations(A1, ['x','y']);

> GenerateEquations(A1, ['x','y','z']);

> GenerateEquations(A1, ['x','y','z'], <7,13>);

LinearSolve solves the equation A.x = b for x

> LinearSolve(A1);

> LinearSolve(A2, <3, 16>);

Eigenvalues and Eigenvectors

A list of symbolic eigenvalues.

> evals_of_A2 := Eigenvalues(A2);

> evals_of_A2[1];

Extract the first eigenvalue from the list.

Convert the first eigenvalue to a floating point number.

> evalf(evals_of_A2[1]);

Convert evals_of_A2 to a list, and change each entry to floating point.

> map(evalf, evals_of_A2);

Directly calculate numerical eigenvalues.

> evalf(Eigenvalues(A2));

Eigenvectors outputs both values and vectors.

> evects_of_A2 := Eigenvectors(A2);

Check the eigenvectors and eigenvalues.

> c_poly := CharacteristicPolynomial(Transpose(A1).A1, lambda);

> simplify(subs('lambda' = evals_of_A2[1], c_poly));
simplify(subs('lambda' = evals_of_A2[2], c_poly));

> evector1 := Column(evects_of_A2[2],1); evector2 := Column(evects_of_A2[2],2);

Maple doesn't always simplify when it should

> (A2 . evector1) - (evals_of_A2[1] . evector1);
(A2 . evector2) - (evals_of_A2[2] . evector2);

> map(simplify, (A2 . evector1) - (evals_of_A2[1] . evector1));
map(simplify, (A2 . evector2) - (evals_of_A2[2] . evector2));