Basic Syntax of Matrix Algebra
>
The linear algebra package in Maple is loaded by the command
> with(linalg):
>
To see the content of the package, replace the colon by a semicolon and re-execute the command.
There are several ways to enter a matrix in Maple. One of them is to give Maple a list of rows of your matrix, that is, a list of lists. Recall that a list is entered between brackets [...] . Note the use of brackets below that denotes a list of lists. We shall label our matrices by A, B, C and so on.
> A:=matrix([[a,b,0,0],[-b,a,b,0],[0,-b,a,b],[0,0,-b,a]]);
>
Alternatively, you can tell Maple the size of your matrix and then specify the list of its entries, as follows:
> B:=matrix(4,4,[1,2,1,0, 1,1,1,2, 3,1/2,2,4, 4,4,5,5]);
>
You do not have to put spaces between row entries in the latter command. It just makes entering a matrix easier. Maple ignores the spaces. Let's define one more matrix
> C:=matrix(2,4,[2,3,2,3,4,6,4,6]);
>
There are many shortcuts in Maple for entering special kinds of matrices. Consult the Help menu if you want to learn about them. Basic algebraic operations are accomplished using the syntax illustrated below. Note that to add two matrices it is not sufficient to write A+B. You have to preceed your command by evalm . The latter tells Maple to actually perform the specified operation. Matrix multiplication is denoted by &* and not *. In the linear algebra context, * denotes multiplication of a matrix by a scalar. Again, the matrix multiplication has to be preceeded by evalm . To obtain the rank of a matrix, the inverse matrix, the transpose or the det erminant, we use the obvious syntax illustrated below. Observe that Maple easily handles symbolic inputs and simplifies answers. Also, whenever possible, Maple gives exact values of entries in the form of fractions, unlike Matlab , which uses decimal approximations.
> evalm(A+3*B);
> evalm(A&*B);
> transpose(C);
> rank(C); rank(B);
> evalm(A&*transpose(C)+(1/2)*transpose(C));
> det(B);
> det(A);
>
As you see, Maple calculated and simplified the determinant of A. We see that the determinant is not zero except when a=b=0. The inverse matrices can be easily obtained, too.
> inverse(B);
> inverse(A);
>
Note: In the above example, Maple provides a formula for the inverse of A . It does not tell us, however, that in the case a=b=0 the inverse does not exist. It is clear that the latter formula for the inverse is not valid for a=b=0 as the denominators are zero. What happens if a=b=0? We know already that when a=b=0, and only then, A has no inverse as det(A)=0. In general, when Maple works with matrices involving parameters, it assumes that all parametric expressions are not zero. Hence, you have to be somewhat critical evaluating outputs of commands involving matrices with parameters, and make sure you understand what happens when entries happen to be equal to zero.
>
There is an alternative syntax for calculating the inverse matrix or multiplying two matrices. Namely, evalm(A^(-1)); instead of inverse(A); and multiply(A,B); instead of evalm(A&*B); . You can experiment with these commands on your own.
>
For multiplying a matrix, say A, by itself several times, say 2 times, you can use the shortcut evalm(A^2);
> evalm(A^2);
>
As you know, very often in linear algebra we want to multiply a vector by a matrix. Recall that vectors are defined in Maple as follows
> v:=vector([1,1,0,0]);
>
Note: Maple displays all vectors horizontally but, for the purpose of matrix multiplication, if a vector is multiplied by a matrix from the left, say Bv, it acts as a column vector. If it is multiplied from the right, vB, it acts as a row vector. Shortly speaking, v behaves the way you would want it to behave. The result of multiplication by a vector is displayed again as a vector, horizontally.
> evalm(B&*v);
> evalm(v&*B);
If you ever try to do programming in Maple or perform complex tasks, you should know that rules of evaluation for matrices are somewhat different than for other objects. For matrices, Maple does what is called "the last name evaluation" . If you assign a name to a matrix, say C, and ask Maple what C is, it will tell you that C is C. If you want Maple to display the matrix C, you have to use the evaluation command eval or evalm . For example:
> C;
> evalm(C);
>
Another very useful command is the command augment that concatenates two matrices horizontally.
> augment(A,B);
>
You can also augment a matrix by a vector which becomes the last column of the matrix. We do this often for systems of equations. Let's augment the matrix A by the vector v:
> augment(A,v);
>
Of course, one of the main reasons for using matrices is solving systems of linear equations. Maple has a simple command linsolve that solves systems with or without parameters.
>