Linear Algebra Syntax Shortcuts
The examples in this worksheet demonstrate shortcuts for some Linear Algebra commands. They illustrate how you can save keystrokes when using Maple interactively.
>
|
|
>
|
|
|
Creating Matrices and Vectors
|
|
The Matrix and Vector functions give you great control over the type of Matrix or Vector you create. For example, you can specify shape, storage mode, and datatype. If you are creating a Matrix or Vector interactively and do not need to specify special features, you may find it quicker to use some shorthand notations:
Instead of using
>
|
|
| (1.1) |
you can enter:
>
|
|
| (1.2) |
If you prefer to enter your data in column-major order, use commas to separate entries, and vertical bars to separate rows:
>
|
|
| (1.3) |
Instead of using
>
|
|
| (1.4) |
you can enter:
>
|
|
| (1.5) |
Instead of using
>
|
|
| (1.6) |
you can enter:
>
|
|
| (1.7) |
You can also enter Matrices and Vectors using the Matrix palette.
|
|
Addition
|
|
|
Adding a Scalar to a Matrix, Vector, or Array
|
|
When you try to add a scalar to a Vector, you get an error because there is no mathematical meaning for this operation.
>
|
|
When you add a scalar to a Matrix, the scalar is interpreted as the scalar multiplied by an appropriately sized Identity Matrix. Another way to think of this is that the scalar is added to the entries on the main diagonal.
>
|
|
| (2.1.1) |
>
|
|
| (2.1.2) |
When you add a scalar to an Array, the scalar is added to every entry in the Array.
>
|
|
| (2.1.3) |
|
|
Adding Rectangular Objects
|
|
The MatrixAdd and VectorAdd functions let you add a multiple of one Matrix (or Vector) to another. This functionality can also be obtained by using the + and * operators:
Adding (multiples of) Vectors:
>
|
|
| (2.2.1) |
Adding two 2x3 Matrices (one is constructed in row-major order and the other in column-major order, but the addition still works since the resulting Matrices are the same size):
>
|
|
| (2.2.2) |
You can also add Arrays:
>
|
|
| (2.2.3) |
|
|
|
Multiplication
|
|
The method by which multiplication is carried out varies with the type of objects involved, and the operators used to request multiplication.
|
Commutative Multiplication
|
|
The '*' operator is used to perform commutative multiplication. When you use the '*' operator, Maple assumes it can safely reorder the operands.
>
|
|
| (3.1.1) |
Since multiplying a Vector, Matrix, or Array by a scalar is commutative, you can use the * operator to perform that operation:
| (3.1.2) |
Matrix and Vector multiplication is not commutative, so attempting to use the '*' operator when both operands are Matrices or Vectors results in an error.
>
|
|
There is a form of commutative multiplication for Arrays. The '*' operator carries out pointwise multiplication (each entry in the first Array is multiplied by the corresponding entry in the second Array):
>
|
|
| (3.1.3) |
|
|
Noncommutative Multiplication
|
|
The '.' operator is used to perform noncommutative multiplication. When you use the '.' operator, Maple knows it must not reorder the operands.
>
|
|
| (3.2.1) |
You can also use the '.' operator to perform scalar multiplication. In 2-D math, you must put the scalar in parentheses so that Maple knows you intend to use the . as an operator, not a decimal point. Scalar multiplication is the one situation in which '*' and '.' can be used interchangeably. You may find it easier to use '*' or implicit multiplication, since it does not require parentheses.
(Note: in 1-D math, there is no need to use parentheses, but you must put spaces on either side of the dot so that Maple knows you intend to use the . as an operator.)
>
|
|
| (3.2.2) |
If one of the operands is an unassigned variable, Maple does not know whether it represents a scalar, and so does not automatically carry out the multiplication.
| (3.2.3) |
To tell Maple to treat the variable as a scalar, use the simplify command with option symbolic or assume=scalar:
| (3.2.4) |
You can carry out Matrix and Vector multiplication by using the '.' operator.
| (3.2.5) |
On Arrays, the '.' operator performs component-wise multiplication.
>
|
|
| (3.2.6) |
To carry out Matrix multiplication repeatedly (that is, to get a power of a Matrix), use the '^' operator:
| (3.2.7) |
|
|
|
Working with Subsections of Data
|
|
|
Extracting Data
|
|
The examples in this section use the following Matrix. Note that (with one exception, indicated below) the same techniques can be used on Vectors and Arrays.
>
|
|
| (4.1.1) |
To extract a single entry, type its row and column coordinates in square brackets after the name of the Matrix:
>
|
|
| (4.1.2) |
The simplest way to extract a row is by using a single index.
>
|
|
| (4.1.3) |
To extract part of a row (or column) as a row Vector (or column Vector), use a range instead of a single index for the row (or column) coordinate:
>
|
|
| (4.1.4) |
To extract a sub-Matrix, replace both coordinates with ranges:
>
|
|
| (4.1.5) |
Notice that you can control whether the result is the entry, a 1-element row Vector, a 1-element column Vector, or a 1x1 Matrix based on whether you use single indices or ranges:
| (4.1.6) |
If you do not want a contiguous set of entries, or if you wish to reorder rows or columns, use a list (which can also include ranges):
>
|
|
| (4.1.7) |
Note that an index list containing a single item (for example, [3]) yields the same results as a range with the same beginning and end points.
>
|
|
| (4.1.8) |
You can use negative indices to count backwards from the end of a Vector or Matrix. Use -1 to indicate the last row (or column), -2 to indicate the second last row (or column), and so on. (This technique does not apply to Arrays since they can have negative indices.)
>
|
|
| (4.1.9) |
You can also use implicit range endpoints. For example, means from the third row (or column) to the last row (or column), and means from the first row (or column) to the third row (or column). Thus, the following syntax is equivalent to for the 5x5 Matrix .
>
|
|
| (4.1.10) |
|
|
Updating Data
|
|
You can overwrite data using the same notation as in the previous section. This works as long as the right-hand side of your expression matches the size and shape of the left-hand side (it can be smaller than the left-hand side, but not larger).
>
|
|
| (4.2.1) |
>
|
|
| (4.2.2) |
So far, we have seen how to index entries in a Matrix or Vector using square brackets, []. There is a second way to index entries using round brackets, (). This is called programmer indexing. One use of programmer indexing is to grow a Matrix or Vector past its initially declared bounds.
The following attempts to assign to column 6 of the 5x5 Matrix . Maple returns an error.
>
|
|
Using round brackets, however, the Matrix is enlarged to include a sixth column.
>
|
|
For more information on programmer indexing, see rtable_indexing.
| (4.2.3) |
|
|
Elementary Row Operations
|
|
You can use the techniques from the previous two sections to carry out elementary row operations in place. Note that these techniques are intended for convenient interactive Maple use. For programs carrying out extensive operations it will be more efficient to call the appropriate LinearAlgebra routine directly.
>
|
|
| (4.3.1) |
Swap two rows:
>
|
|
| (4.3.2) |
Multiply a row by a scalar:
>
|
|
| (4.3.3) |
Add a multiple of one row to another:
>
|
|
| (4.3.4) |
>
|
|
|
|
Return to Index for Example Worksheets
|