Matrix Operations and Linear Algebra

Maple can handle a wide variety of matrix and linear algebra operations. The first step is letting Maple know you will be doing some linear algebra. Do so with the with(linalg) command. Maple will list all of the available commands (No, we will not be going through all of them in this exercise!) on the screen:

> with(linalg);
Warning: new definition for   norm
Warning: new definition for   trace
    [BlockDiagonal, GramSchmidt, JordanBlock,Wronskian, add, addcol, addrow, adj,
    adjoint, angle, augment, backsub, band, basis, bezout, blockmatrix,
    charmat, charpoly, col, coldim, colspace,colspan, companion, concat,
    cond, copyinto, crossprod, curl, definite,delcols, delrows, det, diag,
    diverge, dotprod, eigenvals, eigenvects,entermatrix, equal, exponential,
    extend, ffgausselim, fibonacci, frobenius,gausselim, gaussjord,
    genmatrix, grad, hadamard, hermite,hessian, hilbert, htranspose,
    ihermite, indexfunc, innerprod, intbasis,inverse, ismith, iszero,
    jacobian, jordan, kernel, laplacian,leastsqrs, linsolve, matrix, minor,
    minpoly, mulcol, mulrow, multiply, norm,normalize, nullspace, orthog,
    permanent, pivot, potential, randmatrix,randvector, rank, ratform, row,
    rowdim, rowspace, rowspan, rref,scalarmul, singularvals, smith, stack,
    submatrix, subvector, sumbasis, swapcol,swaprow, sylvester, toeplitz,
    trace, transpose, vandermonde, vecpotent,vectdim, vector]

(Ignore the two Warnings. They are there to let us know of new definitions of norm and trace since the last version of Maple)
Go ahead and define a matrix using the array variable:

> A := array([ [1,2,3], [x,x/2,3-x],[2/3, 0, -5/x] ]);

                                [  1     2     3   ]
                                [                  ]
                           A := [  x   1/2 x 3 - x ]
                                [                  ]
                                [ 2/3    0   - 5/x ]

Notice that we placed each row in square brackets, []. We also blocked off the entire array with square brackets, [].
Now, using evalm, compute the matrix inverse of A:

> Ainv := evalm(A^(-1));

                 [       15                60           - 12 + 7 x  ]
                 [  -----------   ----------------   3 -----------  ]
                 [  - 69 + 14 x     x (- 69 +14 x)     - 69 + 14 x  ]
                 [                                                  ]
                 [    - 21 + 2 x        5 + 2x           - 3 + 4 x  ]
         Ainv := [ 2 -----------  6---------------  - 6 ----------- ]
                 [   - 69 + 14 x    x (- 69 +14 x)      - 69 + 14 x ]
                 [                                                  ]
                 [        x                8                x       ]
                 [ 2 -----------    ------------     9 -----------  ]
                 [   - 69 + 14 x      - 69 +14 x       - 69 + 14 x  ]

We know that [A][1/A] should yield identity. Let us confirm that:

> evalm( A &* Ainv );

                                  [ 1  0  0 ]
                                  [         ]
                                  [ 0  1  0 ]
                                  [         ]
                                  [ 0  0  1 ]

What is the determinant of A?

> det(A);

                                  23/2 - 7/3 x

Now go ahead an define a vector, B:

> B:= array(1..3): B[1]:= 9:  B[2]:= x;

                                   B[2] := x

> print(B);

                                 [ 9, x, B[3] ]

We can multiply the array and the vector using the &* to symbolize this operation:

> A &* B;

                                     A &* B

Remember, Maple stores the answer symbolically until called for with the evalm command:

> evalm(");

                                      2                   6 x - 5 B[3]
       [ 9 + 2 x + 3 B[3], 9 x + 1/2 x  + 3B[3] - B[3] x, ------------ ]
                                                                x

We will now define another vector, C:

> C:= array(1..3): C[1]:=-x:  C[2]:= 12;

                                   C[2] := 12

> print(C);

                               [ - x, 12, C[3]]

And when we multiply B.C :

> evalm(B &* C);
Error, (in linalg[multiply]) vector dimensions incompatible

Uh Oh! A common mistake occurs when &* is used to represent the dot product. The dotprod command fixes this:

> evalm( dotprod(B,C) );

                                3 x + B[3] C[3]

Much better!
This is just a start. Maple allows for eigenvalue calculations, exponentials of matrices and vectors, Hermitian operations, and a whole host of other linear algebra tools. Look at one of the resources for a more detailed explanation of linear algebra tools available with Maple.


This page Maintained by Dale H. Leschnitzer
Last Modified Monday, November 4, 1996

L O S   A L A M O S   N A T I O N A L   L A B O R A T O R Y
Operated by the University of California for the U.S. Department of Energy

Copyright © 1998 UC - Disclaimer