Session 07
Matrix Manipulations and Some Linear Algebra
We begin with some basic arithmetic of matrices.
> with(linalg):
> A:=matrix([[a$3],[b$3],[c$3]]);
> A[3,2]; # Note the use of brackets to get the (3,2) entry.
> C:=diag(d,d,d);
> v:=vector([d$3]);
> augment(A,v);
> stackmatrix(A,v);
> 3*A;
> evalm(%); # use evalm to evaluate a matrix expression
> id:=array(identity,1..3,1..3);
> evalm(%);
> id:=Matrix(3,3,shape=identity);
> id + A;
> evalm(%);
> matadd(A,id);
> det(A);
> C:=diag(d,d,d);
> evalm(C &* A);
> multiply(C,A);
> evalm(C^2);
> M:=matrix([[2,1,4],[3,3/2,34/5],[0,2/3,5]]);
> b:=vector([41/4,41/12,35/4]);
The following sequence of commands allow one to perform elementary row operations. Here we solve a system by Gaussian elimination as we ‘step’ through the elementary row operations.
> Mb:=augment(M,b);
> Mb1:=mulrow(Mb,1,1/2); # mulrow(A, r, expr): row r multiplied by expr
> Mb2:=addrow(Mb1,1,2,-3); # addrow(A,r1,r2,m): row r2 is replaced by (r1*m + r2)
> Mb3:=mulrow(Mb2,3,3);
> Mb4:=swaprow(Mb3,2,3); # swaprow(A,r1,r2): row r1 swapped with r2
> Mb5:=mulrow(Mb4,2,1/2);
> Mb6:=mulrow(Mb5,3,5/4);
> soln:=backsub(Mb5); # backsub(U,b) : backsub generates a solution vector x to the equation U*x=b
# alternatively try this: gaussjord(Mb);
The “gausselim” command gives you a quick way to reduce the augmented linear system to upper triangular form. Note however that the gausselim command will not necessarily give you the same upper triangular matrix that you obtained by use of the mulrow, swaprow, etc commands. Maple may employ a different pivoting strategy.
> gausselim(Mb);
> linsolve(M,b);
Here are some more ways to create a matrix.
> A:=matrix([[0,3,-4,5],[2,7,4/3,9],[1/2,-3,2,13],[4/3,7,-2/3,11/31]]);
> type(A,matrix);
> M:=matrix(4,2,[1,2,3,4,2,3,4,5]);
> S:=A &* M;
> evalm(S);
> F1:=A^2;
> evalm(F1);
> F:=F1&*(2*M-S-M);
> evalm(F);
> evalm(A+3);
> C:=evalm(M&*transpose(M));
> C1:=diag(A,C);
> C2:=delrows(C1,1..3);
> C3:=delcols(C2,1..3);
> C4:=extend(C3,1,1,2);
The call extend(A, m, n, x) returns a new matrix which is a copy of the matrix A with m additional rows and n additional columns. The new entries are initialized to x.
This is a useful method for creating a matrix where the entries are defined by a rule of correspondence.
> A1:=matrix(5,5,(i,j)->max(i,j));
> A2:=matrix(4,4,(i,j)->i^(j-1));
Here and some ‘built-in’ matrices.
> V:=vandermonde([seq(a[j],j=1..3)]);
> det(V); # Note to find the determinant just use det
> factor(%);
> H:=hilbert(3,x); # Hilbert matrix is symmetric and has 1/(i + j - x) as its (i, j)th entry. If x is not specified, x = 1 is used.
> H1:=scalarmul(H,x); # used to multiply each entry by a scalar
> det(H1);
> A:=randmatrix(5,3);
> A:=randmatrix(5,5,sparse); # note you can only use one option at a time
# sparse matrix has a high proportion of its entries zero
> A:=randmatrix(3,3,symmetric);
> band([-1,2,-1],5);
Now lets build a complicated matrix by putting matrices and vectors together.
> c:=vector(6,1); # create a one dimensional array, with 6 rows, each element value 1
> a:=vector(5); # create a one dimensional array, with 5 rows, with unspecified elements
> A1:=multiply(c,transpose(a));
> A2:=concat(c,A1);
> b:='b';
> bb:=seq(b[j],j=1..5);
> db:=diag(0,bb);
> matadd(A2,db);
> det(%);
Lets do the above in the general (n+1) time (n+1) case.
> with(linalg);
> D1:=proc(n) local c,a,A1,A2,bb,j,db;
c:=vector(n+1,1);
a:=vector(n);
A1:=multiply(c,transpose(a));
A2:=augment(c,A1);
bb:=seq(b[j],j=1..n);
db:=diag(0,bb);
matadd(A2,db);
det(%);
end;
Lets try a few. What do you think the determinant is in general?
> D1(5);
> D1(7);
> D1(10);
> D1(2);
Looks like the answer is always the product of the elements of b.
Exercises
1. Given the matrices A and B,
A:=matrix(3,3,[1,-1,0,-1,2,0,0,0,1]);
B:=matrix([[1,0,1],[1,1,1],[1,0,1]]);
Find:
a. 2 times A squared minus 3 times B
b. Find (A+B) times (A-B) and (A^2 - B^2)
c. Find A times B and B times A. How do they compare?
d. Find (A+B) squared and A^2 plus 2 times A times B plus B^2
2. Use the swaprow, multrow, etc command to reduce the following system to upper triangular form. Then use backsub to solve the system.
2*x1 + x2 + 4*x3 = 41/4
3*x1 + (3/2)*x2 + (34/5)*x3 = 41/12
(2/3)*x2 + 5*x3 = 35/4
3. Find the values of a and b for which the system of equations has
1. unique solution
2. an infinite number of solutions
3. no solutions
x1+x2+x3=6
x1+2*x2+3*x3=10
x1+2*x2+a*x3=b
4. Find the values of b for which the system of equations has a solution. Then solve the system for each value of b.
x1+x2+x3=1
x1+2*x2+4*x3=b
x1+4*x2+10*x3=b^2
5. Write a procedure depending on an integer n that will build an n time n matrix A whose (i,j)th entry is (i+j-2)^2 and computes the determinant.
Make a conjecture for the determinant.
6. Write a procedure that depends on an integer n (an n times n matrix) with main diagonal all ones and the first sub-diagonal all ones, i.e., if n = 4, A=matrix([[1,0,0,0],[1,1,0,0],[0,1,1,0],[0,0,1,1]]);
For several values of n find the inverse of the matrix.
<< Back - Next >>
|