Vector and Matrix Operations
# Maple Engine Operations
# No packages are required for basic maple engine operations.
# Define and display a vector like this:
v := <1, -1>
v[1]; v[2]; v;
# Maple treats vectors as columns. Now define another vector w and do
# some simple computations:
w := <1, 1>;
v+w; # vector or matrix addition
v.w; # dot product for vectors, matrix multiply otherwise
# Define two matrices by rows and by columns.
A := Matrix([ [2,3], [1,2] ]);# Enter a matrix by rows
B := <<1,1> | <0,1>>; # Enter a matrix by columns
C := <<2|3>,<1|2>>; # Enter a matrix by rows, C == A.
F := <2,3|1,2>^+; # Ver 12+ shortcut, ^+ is transpose. F == A.
# Make an augmented matrix and an augmented vector from previous definitions.
G := < A| v| w >; # Augment two columns to matrix A
u:=<v,w>; # Create a 4-dimensional vector
# Algebraic computations with matrices A and B and vectors v and w:
A+B;
2*(A+B);
A.B;
A.(v+w);
# Change the individual entries of a matrix. Beware, it changes symbols A
# and B everywhere in the worksheet!
A[1,1] := 5; B[2,1] := 1;
A,B; # check
# Maple Engine Linear Algebra, since maple Ver 7 (2001), no packages.
A:=Matrix([[1,2],[3,4]]); # Enter A by rows
R:=<<1|2>,<3|4>>; # By row.
R:=<<1,3>|<2,4>>; # By column
A.R-3*R; # Kernel matrix algebra, like matlab
B:=<-1,1>; # Column vector
A.B; # Matrix times vector
1/A; A^(-1); # Matrix inverse
# Solve linear systems, since maple Ver 7 (2001). No packages.
A:=Matrix([[1,2],[3,4]]);X:=<x,y>;B:=<-1,1>;
Q:=A.X-B; # Represent A.X=B as Q=<0,0>
sys:=convert(Q,set); # Convert A.X=B to scalar equations.
solve(sys); # Linear system solve for unique x,y.
<x,y> = A^(-1).B; # Inversion solve of AX=B for unique x,y
C:=<<1,2>|<1,2>>; # C has no inverse, infinitely many or no solution.
sys:=convert(C.X,set);solve(sys); # Solve CX=0, free variables
sys:=convert(C.X-B,set);solve(sys); # No solution to CX=B
# Shortcuts in maple Ver 9 and higher
A:=Matrix([[1,2],[3,4]]);
C:=A^%T; # Transpose, since Ver 9 (2003)
K:=<1,3|2,4>; # K==Matrix([[1,2],[3,4]]), since Ver 12 (2008)
J:=<1,2|3,4>^+; # By columns, then transpose, J==K, since Ver 12
# LinearAlgebra package.
# This package is used to extend the basic operations illustrated above.
# The long forms avoid loading a package, giving bulletproof code.
LinearAlgebra[Determinant](A); LinearAlgebra[Determinant](B);
with(LinearAlgebra): # load the package to shorten commands
Determinant(A-B); Determinant(A+B); Eigenvectors(A);
# Shortcut: To complete a long command, use the ESC key. Prevents typing
# errors.
# Symbolic matrices are as legitimate as numerical ones:
A := Matrix([ [a,b], [c,d] ]);
Determinant(A);
# Reduced Echelon Form of a matrix
C := Matrix([ [3,2,2], [3,1,2], [1,1,1] ]);
LinearAlgebra[ReducedRowEchelonForm](C);
linalg[rref](C);
linalg[gaussjord](C);
# Elimination Steps
# The hand work on paper can be done error free by defining operations in
# terms of a linear algebra package, which do the three basic toolkit
# operations combination, swap, multiply. Here's the definitions which
# makes this process identical to what is done with paper and pencil.
# A set of Elimination Toolkit definitions for deprecated linalg package.
combo:=(A,s,t,m)->linalg[addrow](A,s,t,m);
swap:=(A,s,t)->linalg[swaprow](A,s,t);
mult:=(A,t,m)->linalg[mulrow](A,t,m);
C1 := Matrix([ [3,2,2], [3,1,2], [1,1,1] ]);
C2:=combo(C1,1,2,-1);
C3:=combo(C2,1,3,-1/3);
C4:=combo(C3,2,3,1/3);
C5:=mult(C4,2,-1);
C6:=mult(C5,3,3);
C7:=combo(C6,2,1,-2);
C8:=combo(C7,3,1,-2);
C9:=mult(C8,1,1/3);
linalg[rref](C1);
LinearAlgebra[ReducedRowEchelonForm](C1);
# A set of Elimination Toolkit definitions for the
# LinearAlgebra package.
# Reversible operations. a=matrix, s=source, t=target, c=constant
# combo: Replace row t by c times row s added to row t.
# swap: Swap rows s and t
# mult: Replace row t by c times row t. Illegal: c=0
combo:=(a,s,t,c)->RowOperation(a,[t,s],c);
swap:=(a,s,t)->RowOperation(a,[t,s]);
mult:=(a,t,c)->RowOperation(a,t,c);
C1 := Matrix([ [3,2,2], [3,1,2], [1,1,1] ]);
C2:=combo(C1,1,2,-1);
C3:=combo(C2,1,3,-1/3);
C4:=combo(C3,2,3,1/3);
C5:=mult(C4,2,-1);
C6:=mult(C5,3,3);
C7:=combo(C6,2,1,-2);
C8:=combo(C7,3,1,-2);
C9:=mult(C8,1,1/3);
LinearAlgebra[ReducedRowEchelonForm](C1);
#
# Lay Linalg Package
# Functions: swap, replace, scale. Same result as swap, combo, mult.
Q:=<<1,2,1>|<2,3,4>|<1,1,1>>;
b:=<1,2,3>;
with(laylinalg):
A1:=; # Do 9-10 steps with swap, replace, scale
A2:=replace(A1,2,-2,1); # Means replace row 2 by row 2 plus (-2) times row 1
A3:=swap(A2,1,2); # Swap rows 1 and 2 of matrix A2
A4:=scale(A3,2,c); # Multiply row 2 of A3 by scalar = c
#
# Elimination tutor
# On maple menus, Tools --> Tutors --> Linear Algebra --> Gaussian
# Elimination produces a tutor for learning combo, swap, multiply ideas.
# It is too clumsy for daily use (use the macros above), but useful on
# the first days with the topic.
Student[LinearAlgebra][GaussianEliminationTutor]();