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);

[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...
[Add, Adjoint, BackwardSubstitute, BandMatrix, Basi...

Entering Matrices and Vectors

Enter a 2 by 3 matrix:

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

A1 := _rtable[15591516]

Or Simply

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

A1 := _rtable[17346708]

Vectors are entered similarly

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

v1 := _rtable[17331040]

v2 := _rtable[17331120]

Matrix Multiplication, Evalm, Transpose, Determinant, Inverses

'.' is the matrix multiplication operator.

> (A1 . v1) + v2;

_rtable[17331200]

> A2 := A1 . Transpose(A1);

A2 := _rtable[17362308]

Ordinary scalar multiplication is indicated with a *.

> 2*A2;

_rtable[14970828]

> Determinant(A2);

54

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

> A2_inv := MatrixInverse(A2);

A2_inv := _rtable[15173424]

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

Convert to floating point.

> map(evalf, A2_inv);

_rtable[17367380]

See the worksheet "Entering Matrices" for more information on constructing matrices.

Bases, Rank and Subspaces Associated to a Matrix


Compute a basis for the range of A1.

> ColumnSpace(Transpose(A1));

[_rtable[17331400], _rtable[17331480]]

Basis for the kernel.

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

{_rtable[17331560], _rtable[17331600]}

> Rank(A2);

2

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])});

{_rtable[17331640], _rtable[17331720]}

> JordanForm(A2);

_rtable[17385704]

Solution of a Linear System

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

_rtable[17331800]

Dot Product and Cross Product

Dot product.

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

25

Cross product in R^3.

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

_rtable[17331880]

Rows, Columns, and Submatrices

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

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

B := _rtable[17394824]

Or

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

B := _rtable[14979244]

B[2 .. 3,1 .. 3] := _rtable[17346708]

_rtable[14979244]

Row 1 of A1.

> Row(A1,1);

_rtable[17332000]

Column 1 of A1.

> Column(A1,1);

_rtable[17332080]

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

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

_rtable[16034808]

Norms

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

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

sqrt(abs(x)^2+abs(y)^2)

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

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

vnorm := proc (u) options operator, arrow; sqrt(Lin...

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

sqrt(conjugate(x)*x+conjugate(y)*y)

The default norm is the infinity one.

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

max(abs(x),abs(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]]);

B := _rtable[16089860]

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

mat_with_unknowns := _rtable[17411400]

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});

soln1 := {d = d, c = c, a = -63/32*c+d, b = c}

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

> assign(soln1);

> print(B);

_rtable[16089860]

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])]);

[_rtable[17332200], _rtable[17332240], _rtable[1733...

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

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

[x+2*y = 3, 4*x+5*y = 6]

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

[x+2*y+3*z = 0, 4*x+5*y+6*z = 0]

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

[x+2*y+3*z = 7, 4*x+5*y+6*z = 13]

LinearSolve solves the equation A.x = b for x

> LinearSolve(A1);

_rtable[17332400]

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

_rtable[17332480]

Eigenvalues and Eigenvectors

A list of symbolic eigenvalues.

> evals_of_A2 := Eigenvalues(A2);

evals_of_A2 := _rtable[17332520]

> evals_of_A2[1];

91/2+1/2*sqrt(8065)

Extract the first eigenvalue from the list.

Convert the first eigenvalue to a floating point number.

> evalf(evals_of_A2[1]);

90.40267252

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

> map(evalf, evals_of_A2);

_rtable[17332600]

Directly calculate numerical eigenvalues.

> evalf(Eigenvalues(A2));

_rtable[17332640]

Eigenvectors outputs both values and vectors.

> evects_of_A2 := Eigenvectors(A2);

evects_of_A2 := _rtable[17332720], _rtable[15358452...

Check the eigenvectors and eigenvalues.

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

c_poly := 54*lambda-91*lambda^2+lambda^3

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

0

0

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

evector1 := _rtable[17332840]

evector2 := _rtable[17332920]

Maple doesn't always simplify when it should

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

_rtable[17332960]

_rtable[17333040]

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

_rtable[17333080]

_rtable[17333160]