Linear Algebra Computation - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


LinearAlgebra Computation 

The computation component of the Student[LinearAlgebra] subpackage contains commands that can be used to study the various matrix and vector computations presented in an introductory linear algebra course. There are many commands, and this worksheet demonstrates the basics of a selection of them.  

 

Most of the computation commands in this package are front-ends to more powerful (and often more complicated) commands in the top-level LinearAlgebra package. In all such cases, the help page for the command in the Student[LinearAlgebra] subpackage documents only those features which are considered relevant in a linear algebra course context. Each page includes a link to the help page for the main LinearAlgebra command, should you need more details. 

 

For further information about the commands in the Student[LinearAlgebra] package, see the help page for that command. For a general overview, see Student[LinearAlgebra]. 

Getting Started 

Commands in the package can be referred to using the long form, that is, Student[LinearAlgebra][Eigenvalues]. 

It is recommended to load the package first and then use the shorter command names. 

> restart;
 

> with(Student[LinearAlgebra]); -1
 

The following examples show how the various routines work.  

Note: If you set infolevel[Student] := 1 or infolevel[Student[LinearAlgebra]] := 1, many of the routines in this package provide additional information about the objects they are passed in the form of userinfo messages. This setting is recommended. 

> infolevel[Student[LinearAlgebra]] := 1; -1
 

Basic Matrices and Vectors 

Syntax 

Most matrices and vectors can be constructed using the angle bracket (< >) syntax. To build a column vector, for example, use this form: 

> v := `<,>`(3, 2, 1);
 

v := rtable(1 .. 3, [3, 2, 1], subtype = Vector[column]); (1.1.1)
 

A row vector is similarly constructed using vertical bars instead of commas as the separators. 

> w := `<|>`(a, b, c);
 

w := rtable(1 .. 3, [a, b, c], subtype = Vector[row]); (1.1.2)
 

A matrix is constructed out of vectors, either as rows (separated by commas) or as columns (separated by vertical bars).  

> A := `<|>`(`<,>`(1, 2, 3), `<,>`(4, 5, 6));
 

A := rtable(1 .. 3, 1 .. 2, [[1, 4], [2, 5], [3, 6]], subtype = Matrix); (1.1.3)
 

> B := `<,>`(`<|>`(a, b, c), `<|>`(d, e, f));
 

B := rtable(1 .. 2, 1 .. 3, [[a, b, c], [d, e, f]], subtype = Matrix); (1.1.4)
 

The rows or columns of a matrix can be pre-existing vectors or matrices, so long as the dimensions are compatible. 

> C := `<|>`(v, `<,>`(4, 5, 6));
 

C := rtable(1 .. 3, 1 .. 2, [[3, 4], [2, 5], [1, 6]], subtype = Matrix); (1.1.5)
 

> B := `<,>`(w, w);
 

B := rtable(1 .. 2, 1 .. 3, [[a, b, c], [a, b, c]], subtype = Matrix); (1.1.6)
 

> `<|>`(A, `<,>`(x, y, z));
 

Matrix(%id = 36893628030309723404) (1.1.7)
 

These syntactic constructions result in calls to the Maple commands Vector and Matrix. These are very powerful constructor commands, taking a wide variety of options, which control the precise form and properties of the result. The constructed objects are of type Vector and Matrix. 

> type(v, Vector);
 

true (1.1.8)
 

> type(A, Matrix);
 

true (1.1.9)
 

This distinguishes these objects from the older vector and matrix objects, which were used in earlier versions of Maple to represent vectors and matrices.  Because these older objects continue to exist in Maple, in the remainder of this worksheet the capitalized names, Vector and Matrix, are used whenever discussing the corresponding Maple object, as distinct from the represented mathematical object.  

> type(v, vector);
 

false (1.1.10)
 

> type(B, matrix);
 

false (1.1.11)
 

Standard Operations 

Arithmetic with Matrices and Vectors can be carried out using the standard + operator for addition and the . (dot) operator for non-commutative multiplication. 

> Typesetting:-delayDotProduct(B, A);
 

Matrix(%id = 36893628030309723556) (1.2.1)
 

> Typesetting:-delayDotProduct(w, A);
 

Vector[row](%id = 36893628030253683364) (1.2.2)
 

> `+`(A, `<|>`(`<,>`(a, b, c), `<,>`(d, e, f)));
 

Matrix(%id = 36893628030309723708) (1.2.3)
 

The dot product of two Vectors is also computed using the . operator. 

> Typesetting:-delayDotProduct(w, w);
 

`+`(`*`(`^`(a, 2)), `*`(`^`(b, 2)), `*`(`^`(c, 2))) (1.2.4)
 

The transpose of a Matrix or Vector is computed using the ^ (exponentiation) operator with the special exponent value %T. 

> `^`(B, %T);
 

Matrix(%id = 36893628030309723860) (1.2.5)
 

> Typesetting:-delayDotProduct(`^`(A, %T), v);
 

Vector[column](%id = 36893628030253679268) (1.2.6)
 

To consider Matrices and Vectors with complex-valued components, the Hermitian transpose of a Matrix or Vector is similarly represented using the ^ operator with exponent %H. 

> `^`(`<,>`(`+`(1, I), `*`(2, `*`(I))), %H);
 

Vector[row](%id = 36893628030253679948) (1.2.7)
 

The cross product of two 3-D Vectors is computed using the &x operator. 

> `&x`(`<,>`(1, 2, 3), `<,>`(a, b, c));
 

Vector[column](%id = 36893628030253681308) (1.2.8)
 

Notes:  

  • Most commands in the Student[LinearAlgebra] subpackage consider symbolic quantities to represent real variables. For example, a dot product of Vectors whose entries are symbolic expressions does not use complex conjugates when computing the result. This assumption is specific to the Student[LinearAlgebra] subpackage and does not hold in the main LinearAlgebra package. To allow variables to represent complex-valued quantities, use the package command, SetDefault( conjugate = true ).
 

  • All syntaxes, except for the cross product (&x), are available throughout Maple, whether or not the Student[LinearAlgebra] subpackage has been loaded. The definition of the . (dot) operator is, however, different if the Student[LinearAlgebra] subpackage has been loaded, in that it then treats symbolic quantities as being real-valued by default, as discussed earlier.
 

Other Useful Matrices and Vectors 

The basic syntax described in the preceding sections suffices to construct most of the Matrices and Vectors you need. However, there are some commands in the package that build more specialized Matrices and Vectors. 

 

The n x n identity Matrix is represented as Id(n). Note that n must be a non-negative integer, not a symbol. 

> Id(2);
 

Matrix(%id = 36893628030309715964) (1.3.1)
 

Other useful Matrices and Vectors include: 

> RandomMatrix(4);
 

Matrix(%id = 36893628030309716116) (1.3.2)
 

> DiagonalMatrix(v);
 

Matrix(%id = 36893628030309716268) (1.3.3)
 

> JordanBlockMatrix([[lambda[1], 3], [lambda[2], 2]]);
 

Matrix(%id = 36893628030309716420) (1.3.4)
 

> UnitVector[row](2, 5);
 

Vector[row](%id = 36893628030253663836) (1.3.5)
 

> CharacteristicMatrix(`<|>`(`<,>`(1, 2, 3), `<,>`(4, 5, 6), `<,>`(7, 8, 9)), t);
 

Matrix(%id = 36893628030309716572) (1.3.6)
 

> RotationMatrix(`+`(`*`(`/`(1, 6), `*`(Pi))));
 

Matrix(%id = 36893628030309716724) (1.3.7)
 

Queries 

There are several commands to handle basic queries about Matrices and Vectors, such as dimensionality, rank, and other properties. 

> B := `<|>`(`<,>`(1, 2, 3), `<,>`(4, 5, 6), `<,>`(7, 8, 9));
 

B := rtable(1 .. 3, 1 .. 3, [[1, 4, 7], [2, 5, 8], [3, 6, 9]], subtype = Matrix); (1.4.1)
 

> ColumnDimension(B), RowDimension(B), Rank(B);
 

3, 3, 2 (1.4.2)
 

Both dimensions of a Matrix can be obtained at once, using the same command as is used for the dimension of a Vector. 

> Dimension(B); 1Dimension(v);
 

 

3, 3
3 (1.4.3)
 

> IsDefinite(`<|>`(`<,>`(3, 1, 1), `<,>`(2, 4, 2), `<,>`(1, 2, 5)));
 

true (1.4.4)
 

> IsOrthogonal(`<|>`(`<,>`(3, 1, 1), `<,>`(2, 4, 2), `<,>`(1, 2, 5)));
 

false (1.4.5)
 

Linear Systems 

The simplest way to solve a linear system of equations is to represent the problem as a (Matrix, Vector) pair and call LinearSolve. 

> A, v, LinearSolve(A, v);
 

rtable(1 .. 3, 1 .. 2, [[1, 4], [2, 5], [3, 6]], subtype = Matrix), rtable(1 .. 3, [3, 2, 1], subtype = Vector[column]), rtable(1 .. 2, [-`/`(7, 3), `/`(4, 3)], subtype = Vector[column]); (2.1)
 

Alternatively, LinearSolve accepts a Matrix, which it interprets as an augmented matrix with the last column representing the right-hand side of the system. 

> LinearSolve(`<|>`(`<,>`(1, 2), `<,>`(3, 4), `<,>`(-1, 2)));
 

Vector[column](%id = 36893628030253655916) (2.2)
 

The GenerateMatrix command can be used to transform a set of linear equations into an augmented Matrix suitable for use in LinearSolve. 

> eqns := {`+`(y, `*`(4, `*`(z))) = 2, `+`(x, `*`(2, `*`(y)), `-`(`*`(3, `*`(z)))) = 5, `+`(`*`(2, `*`(x)), `-`(y), z) = 1};
 

{`+`(y, `*`(4, `*`(z))) = 2, `+`(x, `*`(2, `*`(y)), `-`(`*`(3, `*`(z)))) = 5, `+`(`*`(2, `*`(x)), `-`(y), z) = 1} (2.3)
 

> GenerateMatrix(eqns, [x, y, z]);
 

Matrix(%id = 36893628030309717028) (2.4)
 

> LinearSolve(Matrix(%id = 36893628030309717028));
 

Vector[column](%id = 36893628030253650596) (2.5)
 

LinearSolve also handles underdetermined systems. 

> LinearSolve(`<|>`(`<,>`(1, 2), `<,>`(3, 4), `<,>`(5, 6)), `<,>`(-1, 1), free = t);
 

Vector[column](%id = 36893628030253651956) (2.6)
 

There are many commands available to study the process of solving a linear system in more detail. There are factorization commands that are equivalent to the elementary row operations needed to reduce a system to a triangular or diagonal one, which can then be easily solved. For example: 

> GaussianElimination(`<|>`(A, v));
 

Matrix(%id = 36893628030309717180) (2.7)
 

To obtain the components of this reduction, use: 

> P, L, U := LUDecomposition(A);
 

Typesetting:-mi( (2.8)
 

To obtain a triangular system, multiply the inverses of P and L. 

> u := Typesetting:-delayDotProduct(`/`(1, `*`(Typesetting:-delayDotProduct(P, L))), v);
 

u := rtable(1 .. 3, [3, -4, 0], subtype = Vector[column]); (2.9)
 

The system can then be solved using BackwardSubstitute. 

> BackwardSubstitute(U, u);
 

Vector[column](%id = 36893628030253642948) (2.10)
 

The reduced row echelon form of a Matrix is also easily obtained. 

> ReducedRowEchelonForm(`<|>`(`<,>`(1, 2, 3), `<,>`(4, 5, 6), `<,>`(7, 8, 9), `<,>`(10, 11, 12)));
 

Matrix(%id = 36893628030309717788) (2.11)
 

At an even more detailed level, commands for the elementary row operations are available. 

> A, SwapRows(A, 2, 3);
 

rtable(1 .. 3, 1 .. 2, [[1, 4], [2, 5], [3, 6]], subtype = Matrix), rtable(1 .. 3, 1 .. 2, [[1, 4], [3, 6], [2, 5]], subtype = Matrix); (2.12)
 

> MultiplyRow(A, 2, `/`(1, 2));
 

Matrix(%id = 36893628030309718092) (2.13)
 

> AddRow(A, 2, 1, -2);
 

Matrix(%id = 36893628030309718244) (2.14)
 

Vector Spaces 

The technology of solving linear systems can be used to obtain bases for various vector spaces. For example, the null space, row space, and column space of a Matrix can all be computed. 

> A1 := `^`(A, %T);
 

A1 := rtable(1 .. 2, 1 .. 3, [[1, 2, 3], [4, 5, 6]], subtype = Matrix); (3.1)
 

> NullSpace(A1), RowSpace(A1), ColumnSpace(A1);
 

{rtable(1 .. 3, [1, -2, 1], subtype = Vector[column])}, [rtable(1 .. 3, [1, 0, -1], subtype = Vector[row]), rtable(1 .. 3, [0, 1, 2], subtype = Vector[row])], [rtable(1 .. 2, [1, 0], subtype = Vector[... (3.2)
 

You can also determine a basis for the span of a set of Vectors, for the sum of two subspaces, or for the intersection of two subspaces. 

> Basis({`<,>`(-3, 2, 4, -2), `<,>`(-2, -3, -1, 4), `<,>`(0, 1, -3, 7), `<,>`(1, 4, 7, 3), `<,>`(3, 1, 1, 0), `<,>`(4, 2, 5, 3)});
 

{rtable(1 .. 4, [-3, 2, 4, -2], subtype = Vector[column]), rtable(1 .. 4, [-2, -3, -1, 4], subtype = Vector[column]), rtable(1 .. 4, [0, 1, -3, 7], subtype = Vector[column]), rtable(1 .. 4, [1, 4, 7, ... (3.3)
 

> IntersectionBasis([{`<,>`(-1, 0, 1), `<,>`(1, 2, 3)}, {`<,>`(2, 0, -1), `<,>`(4, 2, 5)}]);
 

{rtable(1 .. 3, [-6, 2, 10], subtype = Vector[column])}; (3.4)
 

> SumBasis([{`<,>`(-1, 0, -1, 0), `<,>`(1, 2, 0, -3)}, {`<,>`(1, 1, 0, -1), `<,>`(2, 0, 1, 1)}]);
 

{rtable(1 .. 4, [-1, 0, -1, 0], subtype = Vector[column]), rtable(1 .. 4, [1, 1, 0, -1], subtype = Vector[column]), rtable(1 .. 4, [1, 2, 0, -3], subtype = Vector[column])}; (3.5)
 

Eigenvalues and Eigenvectors 

You can compute eigenvalues and eigenvectors with a single command, Eigenvectors. 

> B := `<|>`(`<,>`(-1, 2, 3), `<,>`(0, 1, 3), `<,>`(-1, 1, 1));
 

B := rtable(1 .. 3, 1 .. 3, [[-1, 0, -1], [2, 1, 1], [3, 3, 1]], subtype = Matrix); (4.1)
 

> e, E := Eigenvectors(B);
 

Typesetting:-mi( (4.2)
 

If only the eigenvalues are of interest, you can use the Eigenvalues command. 

> Eigenvalues(B);
 

Vector[column](%id = 36893628030243704132) (4.3)
 

You can also study the basic computation of eigenvalues and eigenvectors. 

> p := CharacteristicPolynomial(B, t);
 

`+`(`*`(`^`(t, 3)), `-`(`*`(`^`(t, 2))), `-`(t), 1) (4.4)
 

This polynomial is the determinant of the characteristic matrix of B. 

> p = Determinant(`+`(B, `-`(`*`(t, `*`(Id(3))))));
 

`+`(`*`(`^`(t, 3)), `-`(`*`(`^`(t, 2))), `-`(t), 1) = `+`(`-`(1), t, `*`(`^`(t, 2)), `-`(`*`(`^`(t, 3)))) (4.5)
 

> e := [solve(p)];
 

[-1, 1, 1] (4.6)
 

> NullSpace(`+`(B, `-`(`*`(e[1], `*`(Id(3))))));
 

{rtable(1 .. 3, [-1, 1, 0], subtype = Vector[column])}; (4.7)
 

You can also obtain the eigenvalues from the Jordan canonical form. 

> JordanForm(B);
 

Matrix(%id = 36893628030309718852) (4.8)
 

>
 

 

Return to Index of Example Worksheets