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 faster to use some shorthand notations:
Instead of using:
you can enter:
If you prefer to enter your data in column-major order, use commas to separate entries, and vertical bars to separate rows:
Instead of using,
You can also enter Matrices and Vectors using the Matrix palette.
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.
Error, a constant cannot be added to a Vector; use +~ for elementwise addition instead of +
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.
7+1,2,3;4,5,6;7,8,9 = 7⋅IdentityMatrix3 + 1,2,3;4,5,6;7,8,9
When you add a scalar to an Array, the scalar is added to every entry in the Array.
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:
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):
You can also add Arrays:
The method by which multiplication is carried out varies with the type of objects involved, and the operators used to request multiplication.
The '*' operator is used to perform commutative multiplication. When you use the '*' operator, Maple assumes it can safely reorder the operands.
Since multiplying a Vector, Matrix, or Array by a scalar is commutative, you can use the * operator to perform that operation:
−3 ⋅ 1,2,3;4 ⋅⁢7,8;1,6⋅t;3 ⋅⁢Arrayw,y,y,z
Matrix and Vector multiplication is not commutative, so attempting to use the '*' operator when both operands are Matrices or Vectors results in an error.
1,3 ⋅ 4|6
Error, (in rtable/Product) use *~ for elementwise multiplication of Vectors or Matrices; use . (dot) for Vector/Matrix multiplication
Pointwise (or elementwise) multiplication of Matrices or Vectors can be performed by using the '*~' ("elementwise") operator:
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):
The '.' operator is used to perform noncommutative multiplication. When you use the '.' operator, Maple knows it must not reorder the operands.
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, however, you must type spaces on either side of the dot so that Maple knows you intend to use the . as an operator.)
−3 . 1,2,3;4.7,8;1,6;3 Arrayw,y,y,z
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.
x . 1,2,3;7,8;1,6 . x;x . Arrayw,y,y,z
To tell Maple to treat the variable as a scalar, use the simplify command:
simplify⁡x . 1,2,3;simplify⁡7,8;1,6 . x;simplify⁡x . Arrayw,y,y,z
You can carry out Matrix and Vector multiplication by using the '.' operator.
1,3 . 4|6;4|6 . 1,3;3,−1;−8,15 . 1,x;4,−7
On Arrays, the '.' operator performs component-wise multiplication.
Array1,2,3,4 . Arrayw,x,y,z
To carry out Matrix multiplication repeatedly (that is, to get a power of a Matrix), use the '^' operator:
To transpose a Vector or Matrix, use the powering operator with exponent '+':
To obtain the Hermitian transpose of a Vector or Matrix, use the powering operator with exponent '*':
1+I,2+2 I,3−I*; 1+I,2+2 I,3+3 I;4+4 I,5+5 I,6+6 I*
Working with Subsections of 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.
To extract a single entry, type its row and column coordinates in square brackets after the name of the Matrix:
The simplest way to extract a row is by using a single index.
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:
To extract a sub-Matrix, replace both coordinates with ranges:
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:
M3,2; # the value of the entry M3, 2..2; # a row VectorM3..3,2; # a column VectorM3..3,2..2 # a 1x1 Matrix
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):
M1,3,2,4; # 2nd and 4th entries of 1st and 3rd rowsM1..3,5,4,1..5 # swap 4th and 5th rows
Note that an index list containing a single item (for example, ) yields the same results as a range, with the same beginning and end points.
M3, 2; # a row VectorM3, 2; # a column VectorM3, 2 # a 1x1 Matrix
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.)
M−1,−1; # entry in last row, last columnM2,1..−1 # all of the second row
You can also use implicit range endpoints. For example, 3.. means from the third row (or column) to the last row (or column), and ..3 means from the first row (or column) to the third row (or column). Thus, the following syntax is equivalent to M3..5,2 for the 5x5 Matrix M.
You can also use other commands such as select to remove columns or rows of a matrix that do not match a given criteria. For example, say we wanted to remove any row where the value in the first column is not even:
even_rows≔selecti → typeMi, 1,even, seq1 .. upperboundM, 1:
We can also filter out columns in a similar manner. Say we wanted to filter out any columns where the value in the second row is less than 3.
filtered_columns≔selecti →M2, i>2, seq1 .. upperboundM, 1:
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).
M1,3 ≔ −1; # update an entryM4,1..2 ≔ −2,−3; # replace part of a rowM4..5,4 ≔ −4|−5; # replace part of a columnM2..2,5..5 ≔ −6; # replace a sub-MatrixM;
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 M. Maple returns an error.
Error, Matrix index out of range
Using round brackets, however, the Matrix is enlarged to include a sixth column.
For more information on programmer indexing, see rtable_indexing.
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.
Swap two rows:
M1,2 ≔ M2,1; M;
Multiply a row by a scalar:
M3 ≔ 3 ⋅ M3;M;
Add a multiple of one row to another:
M4 ≔ M4 −16⋅ M2;M;
dot, Matrix and Vector Construction Shortcuts, Matrix and Vector Entry Extraction, Matrix palette, rtable_indexing, simplify
Return to Index for Example Worksheets
Download Help Document
What kind of issue would you like to report? (Optional)