Michael Bowen's VC Course Pages

# Math V22 Maple Scripts for Manipulating Matrices and Vectors (Page 1)

### Introduction

This page lists some Maple scripts that may help you with computationally-intensive portions of the course, focusing on Chapter 1 of the textbook (Anton 10th). Some of the matrix handling capabilities used below are also available on high-end graphing calculators. However, the high speed of a computer, and its extended viewing screen, often make Maple a better choice for visualization. Maple software is available in the computer lab ("BEACH") located on the first floor of the LRC; ask at the main desk for an appropriate pod and reservation. The software may also have been bundled with your textbook; the student license is valid for a year from the date it is first installed on your home computer. For my courses, use of this software is optional.

The following scripts may be cut and pasted directly from your browser window into a blank Maple file. Then click anywhere on the red script and press the ENTER button on your computer's keyboard to activate the script. User-adjustable parameters described at the beginning of each listing tell you how to edit the script in order to customize it to fit the problem you are investigating. If you "break" a script by accidental editing, you can copy and paste a new copy of the original script from the browser window into Maple. In Windows, the ALT+TAB key combination allows you to toggle quickly back and forth between your browser and Maple. This page should be considered a reference; if you want more detailed information, please visit the Maplesoft online help site.

In the examples below, black text is documentation, and red text is the actual script. The documentation should not be copied into Maple; please be careful to select an entire section of red text, from the beginning of the first line to the end of the last line. If you seek more information concerning the syntax of Maple commands, click on the command, then press the help (F1) button from Maple (not from the browser). Extensive information concerning Maple commands is also available on line via search engine.

### "Classic Maple" Scripts for Chapter 1

• Creating an Arbitrary Matrix.
Maple syntax and function notes:
Ending a statement with a colon (:) computes internally without displaying results;
Ending a statement with a semicolon (;) displays the computational results;
Note the various types of numbers (decimals, imaginaries, transcendentals, fractions, irrationals, roots) and their syntax;
Any or all of the numerical values may be replaced by variables;
The "evalf" (or "evaluate as floating") function converts symbolic expressions to corresponding decimal approximations;
These examples create the same 4 x 3 matrix using two different methods, and display both exact and decimal forms;

Objective: Creating a matrix using Maple's "Matrix" function (requires data entry row by row):
Each inner set of rectangular brackets [ ] encompasses one row of the matrix;
The outer set of rectangular brackets encompasses the matrix;
Insert additional rows by including more sets of comma-separated inner rectangular brackets;

A := matrix created from the sequences of row entries;

A:=Matrix([[1,0.3,-2],[2*I,6,-Pi],[exp(-4),2/7,sqrt(5)],[2,6^(1/3),infinity]]):

A;
evalf(A);

Objective: Creating the same matrix without using Maple's "Matrix" function (requires data entry column by column):
The stile (|) concatenates matrix columns side-by-side;
Each inner set of angle brackets < > encompasses one column of the matrix;
The outer set of angle brackets encompasses the matrix;
Insert additional columns by including more sets of stile-separated inner angle brackets;

AltA := matrix created from the sequences of column entries;

AltA:=<<1,2*I,exp(-4),2>|<0.3,6,2/7,6^(1/3)>|<-2,-Pi,sqrt(5),infinity>>:

AltA;
evalf(AltA);

• Creating Identity and Zero Matrices.
Objective: Creating an identity matrix quickly using Maple's "Matrix" function, and demonstrating an alternative method using the "IdentityMatrix" function:

size := size of identity matrix (since identity matrices are square, "size" indicates both row and column size);

The parameter "size" should be a whole number;

with(LinearAlgebra):
size:=4:

Ident1:=Matrix(size,shape=identity);
Ident2:=IdentityMatrix(size);

Objective: Creating a zero matrix quickly using Maple's "Matrix" function:

ZRows := number of rows in zero matrix;
ZColumns := number of columns in zero matrix;

The parameters "ZRows" and "ZColumns" should be whole numbers (they may be equal or unequal);

ZRows:=4:
ZColumns:=5:

Z:=Matrix(ZRows,ZColumns);

• Creating an Arbitrary Column Matrix ($n \times 1$ Matrix) or Column Vector.
Maple syntax and function notes:
The "whattype" function reveals the internal data type Maple assigns to a data object (possibly useful for debugging);
The first two examples create the same 3-element column matrix, or (equivalently) a 3 x 1 matrix, using two different methods;
The last example creates a 3-element column vector;
Maple treats column vectors (internally) as a different data type from one-column matrices, even though they display identically;

Objective: Creating a column matrix using Maple's "Matrix" function:
Insert additional rows by including more sets of comma-separated inner rectangular brackets;

ColMat := column matrix created from the sequence of row entries;

ColMat:=Matrix([[9],[1],[0]]); whattype(ColMat);

Objective: Creating the same column matrix without using Maple's "Matrix" function:
Insert additional rows by including more sets of comma-separated values;

AltCol := column matrix created from the sequence of column entries;

AltCol:=<<9,1,0>>; whattype(AltCol);

Objective: Creating a column vector with the same entries:
Insert additional rows by including more sets of comma-separated values;

ColVec := column vector created from the sequence of column entries;

ColVec:=<9,1,0>; whattype(ColVec);

• Creating an Arbitrary Row Matrix ($1 \times n$ Matrix) or Row Vector.
Maple syntax and function notes:
The first two examples create the same 3-element row matrix, or (equivalently) a 1 x 3 matrix, using two different methods;
The last example creates a 3-element row vector;
Maple treats row vectors (internally) as a different data type from one-row matrices, even though they display identically;

Objective: Creating a row matrix using Maple's "Matrix" function:
Insert additional columns by including more sets of comma-separated values;

RowMat := row matrix created from the sequence of column entries;

RowMat:=Matrix([[1,-3,2]]); whattype(RowMat);

Objective: Creating the same row matrix without using Maple's "Matrix" function:
The stile (|) concatenates matrix columns side-by-side;
Insert additional columns by including more sets of stile-separated inner angle brackets;

AltRow := row matrix created from the sequence of column entries;

AltRow:=<<1>|<-3>|<2>>; whattype(AltRow);

Objective: Creating a row vector with the same entries:
Insert additional columns by including more sets of stile-separated values;

RowVec := row vector created from the sequence of column entries;

RowVec:=<1|-3|2>; whattype(RowVec);

• Extracting a Row or Column Vector from a Matrix and Converting a Vector to a Matrix.
Maple syntax and function notes:
Double-dot (..) notation (when used with array indices) indicates a range of numbers; for example, [1..4] is equivalent to [1, 2, 3, 4];
When double-dot notation is used without starting/ending values, it refers to the entire legal range of a set of indices;
In Maple, matrix row and column indices begin with 1 (unlike, for example, C++, in which array indices begin with 0);

Objective: Extracting a row vector from a matrix using index notation, and converting it into a row matrix using angle brackets:

A := an arbitrary matrix;
RowNum := the number of the row to extract as a row vector (top row of the matrix is row 1);

The parameter "RowNum" should be a positive integer;

A:=Matrix([[1,0.3,-2],[2*I,6,-Pi],[exp(-4),2/7,sqrt(5)],[2,6^(1/3),infinity]]);
RowNum:=3:

RowVec:=A[RowNum,..]; whattype(RowVec);
RowMat:=<RowVec>; whattype(RowMat);

Objective: Extracting a column vector from a matrix using index notation, and converting it into a column matrix using angle brackets:

A := an arbitrary matrix;
ColNum := the number of the column to extract as a column vector (leftmost column of the matrix is column 1);

The parameter "ColNum" should be a positive integer;

A:=Matrix([[1,0.3,-2],[2*I,6,-Pi],[exp(-4),2/7,sqrt(5)],[2,6^(1/3),infinity]]);
ColNum:=2:

ColVec:=A[..,ColNum]; whattype(ColVec);
ColMat:=<ColVec>; whattype(ColMat);

• Finding the Transpose of a Matrix.
Maple syntax and function notes:
The transpose of a matrix may be created using either the "Transpose" function or superscript notation;
The following examples illustrate both methods;

Objective: Finding the transpose of a matrix using the "Transpose" function:

A := an arbitrary matrix;

If matrix "A" is its own transpose, then the effects of transposition will not be readily visible;

with(LinearAlgebra):
A:=Matrix([[1,0.3,-2],[2*I,6,-Pi],[exp(-4),2/7,sqrt(5)],[2,6^(1/3),infinity]]);

ATrans:=Transpose(A);

Objective: Finding the transpose of a matrix using superscript notation (whose syntax is similar to power notation):

A := an arbitrary matrix;

If matrix "A" is its own transpose, then the effects of transposition will not be readily visible;

with(LinearAlgebra):
A:=Matrix([[1,0.3,-2],[2*I,6,-Pi],[exp(-4),2/7,sqrt(5)],[2,6^(1/3),infinity]]);

AltATrans:=A^%T;

• Finding the Trace of a Square Matrix.
Maple syntax and function notes:
The trace of a square matrix is the sum of the entries on its main diagonal;

Objective: Finding the trance of a square matrix using the "Trace" function:

SqMtrx := a square matrix;

Matrix "SqMtrx" may be any size, but it should be kept square;

with(LinearAlgebra):
SqMtrx:=<<1,2,1,1>|<3,5,3,3>|<1,2,8,2>|<1,2,9,2>>;

TraceVal:=Trace(SqMtrx);

• Performing Gaussian Elimination and Gauss-Jordan Elimination on an Augmented Matrix.
Maple syntax and function notes:
The stile (|) concatenates matrix columns (or entire matrices) side-by-side;
The stile effectively produces an augmented matrix if the second item is a column vector or column matrix;
Using the stile does not generate a vertical dashed separator line in the output, however;

Objective: Gaussian elimination using the "GaussianElimination" function, and learning what happens if the corresponding system is inconsistent:
Gaussian elimination produces a matrix in triangular form, but not a complete solution to the corresponding system;

A := square (n x n) matrix representing a consistent system;
InCons := square (n x n) matrix representing an inconsistent system;
b := column (n x 1) matrix (column vector of constants from the corresponding system of linear equations);

Function "GaussianElimination" also works on non-square matrices, but the corresponding system is likely to be either overdetermind or underdetermined;
Changing matrix "A" carelessly may result in an inconsistent system;
Changing matrix "InCons" carelessly may result in a consistent system;
Matrix "b" should have the same number of rows as matrices "A" and "InCons," otherwise augmentation with the stile will fail;

with(Student[LinearAlgebra]):
A:=<<8,3,-1,-5>|<4,-5,0,-2>|<-5,8,3,-1>|<-5,5,-4,-9>>;
InCons:=<<8,3,-1,17>|<4,-5,0,-6>|<-5,8,3,2>|<-5,5,-4,17>>;
b:=<<4,0,-8,-5>>;

GaussElim:=GaussianElimination(<A|b>);
GaussErr:=GaussianElimination(<InCons|b>);

Objective: Gauss-Jordan elimination using the "ReducedRowEchelonForm" function:
Gauss-Jordan elimination produces a matrix in row-reduced echelon form;
For matrix "A," this effectively solves the corresponding linear system;
Matrix "InCons" was specifically created so as to have no solution; what do you think will happen?

A := square (n x n) matrix representing a consistent system;
InCons := square (n x n) matrix representing an inconsistent system;
b := column (n x 1) matrix (column vector of constants from the corresponding system of linear equations);

"ReducedRowEchelonForm" also works on non-square matrices, but the corresponding system is likely to be either overdetermind or underdetermined;
Changing matrix "A" carelessly may result in an inconsistent system;
Changing matrix "InCons" carelessly may result in a consistent system;
Matrix "b" should have the same number of rows as matrices "A" and "InCons," otherwise augmentation with the stile will fail;

with(Student[LinearAlgebra]):
A:=<<8,3,-1,-5>|<4,-5,0,-2>|<-5,8,3,-1>|<-5,5,-4,-9>>;
InCons:=<<8,3,-1,17>|<4,-5,0,-6>|<-5,8,3,2>|<-5,5,-4,17>>;
b:=<<4,0,-8,-5>>;

GJSolution:=ReducedRowEchelonForm(<A|b>);
GJErr:=ReducedRowEchelonForm(<InCons|b>);

• Matrix Algebra.
Maple syntax and function notes:
Use a dot (.) not an asterisk (*) for matrix multiplication (dot also works with numbers if they are parenthesized);

Objective: Performing matrix addition and subtraction, scalar multiplication, and matrix multiplication:
One matrix multiplication is purposely chosen using incompatible matrices so you can see what happens;
Different syntax forms for scalar multiplication are also shown;

A := (m x n) matrix;
B := (m x n) matrix;
C := (n x p) matrix;

Changing the size of matrices "A," "B," or "C" carelessly may break these examples;

with(Student[LinearAlgebra]):
A:=<<8,3,-1>|<4,-5,0>|<-5,8,3>|<-5,5,-4>>;
B:=<<3,1,4>|<-1,-5,-9>|<2,6,5>|<-3,-5,-8>>;
C:=<<0,2,4,6>|<1,3,5,7>>;

MatrixSum:=A+B; MatrixDifference:=A-B;
ScalarProduct:=5*A; AltScalarProduct:=(5).A;
LegalMatrixProduct:=B.C; IllegalMatrixProduct:=C.B;

Objective: Finding the inverse of a matrix using the "MatrixInverse" function and verifying its correctness:

SqMtrx := invertible (non-singular) square (n x n) matrix;

Matrix "SqMtrx" may be any size, but it should be kept square;
Changing matrix "SqMtrx" carelessly (even without a size change) may result in a non-invertible (singular) matrix;

with(LinearAlgebra):
SqMtrx:=<<1,2,1,1>|<3,5,3,3>|<1,2,8,2>|<1,2,9,2>>;

InvMtrx:=MatrixInverse(SqMtrx);
Ident1:=SqMtrx.InvMtrx;
Ident2:=InvMtrx.SqMtrx;

Objective: Learning what happens when "MatrixInverse" is used on a non-invertible (singular) matrix:

SingMtrx := non-invertible (singular) square (n x n) matrix;

Matrix "SingMtrx" may be any size, but it should be kept square;
Changing matrix "SingMtrx" carelessly (even without a size change) may result in an invertible (non-singular) matrix;

with(LinearAlgebra):
SingMtrx:=<<8,3,-1,17>|<4,-5,0,-6>|<-5,8,3,2>|<-5,5,-4,17>>;

InvMtrx:=MatrixInverse(SingMtrx);

Objective: Multiplying a matrix by itself repeatedly (raising a matrix to a power); raising the identity matrix to a power:

SqMtrx := square (n x n) matrix;

Matrix "SqMtrx" may be any size, but it should be kept square;

with(LinearAlgebra):
SqMtrx:=<<1,2,1,1>|<3,5,3,3>|<1,2,8,2>|<1,2,9,2>>;

PowerMtrx:=SqMtrx^3;

Ident:=Matrix(5,shape=identity);
PowerIdent:=Ident^4;

• Polynomial Interpolation.
Objective: Fitting a polynomial function of degree n to pass through (n + 1) given ordered pairs:

ListOfPoints := list of (n + 1) ordered pairs (enclosed in rectangular brackets, not parentheses);
x := the independent variable of the polynomial function (any legal Maple variable name may be used);

If only a single ordered pair is provided, the resulting "curve" is a horizontal line through the ordered pair;
The x-coordinates in "ListOfPoints" must be distinct (no duplicates allowed);

with(CurveFitting):
ListOfPoints := [[0,0],[1,3],[2,1],[3,3]];

PolynomialInterpolation(ListOfPoints,x);


Michael Bowen's VC Course Pages: Math V22 Maple Scripts for Manipulating Matrices and Vectors (Page 1)