Syntax Shortcuts - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


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.

restart

withLinearAlgebra:

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:

Matrix6,3,4,8,9,14

you can enter:

6,3,4;8,9,14

or,

6|3|4,8|9|14

If you prefer to enter your data in column-major order, use commas to separate entries, and vertical bars to separate rows:

6,8|3,9|4,14

or,

6,8|3,9|4,14

Instead of using,

Vector7,3,2

you can enter:

7,3,2

Instead of using,

Vectorrow11,27,6

you can enter:

11|27|6

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.

3+7,8,9

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 = 7IdentityMatrix3 + 1,2,3;4,5,6;7,8,9

10+2,5,11;4,6,7=10IdentityMatrix2,3+2,5,11;4,6,7

When you add a scalar to an Array, the scalar is added to every entry in the Array.

7+Array1..3,1..3,1,2,3,4,5,6,7,8,9

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:

3,4+37,5

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):

27,2,3;8,9,634,3|6,5|12,19

You can also add Arrays:

3Array1,7+Array8,23

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.

xyz;zyx

xyz

xyz

(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,3;4 7,8;1,6t;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:

1,2,3;4,5,6~a,b,c;d,e,f

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):

Array1,2,3,4 Arrayw,x,y,z

Noncommutative Multiplication

The '.' operator is used to perform noncommutative multiplication.  When you use the '.' operator, Maple knows it must not reorder the operands.

x.y.z;z.y.x

x.y.z

z.y.x

(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, 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:

simplifyx . 1,2,3;simplify7,8;1,6  . x;simplifyx . 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

22

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:

M:=0.2,0.8|0.35,0.65;M.M.M.M.M.M.M.M.M.M=M10

Transposition

To transpose a Vector or Matrix, use the powering operator with exponent '+':

1&comma;2&comma;3&plus;&semi; <1&comma;2&comma;3&semi;4&comma;5&comma;6&gt;&plus;

To obtain the Hermitian transpose of a Vector or Matrix, use the powering operator with exponent '*':

1&plus;I&comma;2&plus;2 I&comma;3I&ast;&semi; 1&plus;I&comma;2&plus;2 I&comma;3&plus;3 I&semi;4&plus;4 I&comma;5&plus;5 I&comma;6&plus;6 I&ast;

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.

M:=Matrix5&comma;i&comma;j&rarr;3ij


To extract a single entry, type its row and column coordinates in square brackets after the name of the Matrix:

M5&comma;4

11

(5.1.1)


The simplest way to extract a row is by using a single index.

M5

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:

M5&comma;1..3&comma;M3..5&comma;2

 


To extract a sub-Matrix, replace both coordinates with ranges:

M3..5&comma;1..3


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&comma;2&semi;                        &num; the value of the entry M3&comma; 2..2&semi;                    &num; a row VectorM3..3&comma;2&semi;                    &num; a column VectorM3..3&comma;2..2                &num; a 1x1 Matrix

7


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&comma;3&comma;2&comma;4&semi;           &num; 2nd and 4th entries of 1st and 3rd rowsM1..3&comma;5&comma;4&comma;1..5      &num; swap 4th and 5th rows


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.  

M3&comma; 2&semi;               &num; a row VectorM3&comma; 2&semi;                &num; a column VectorM3&comma; 2             &num; 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.)

M1&comma;1&semi;          &num; entry in last row, last columnM2&comma;1..1        &num; all of the second row

10

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&comma;2 for the 5x5 Matrix M.

M3..&comma;2

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_rowsselecti  typeMi&comma; 1&comma;even&comma; seq1 .. upperboundM&comma; 1&colon;

Meven_rows

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_columnsselecti M2&comma; i&gt;2&comma; seq1 .. upperboundM&comma; 1&colon;

M..&comma;filtered_columns

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).

M:=Matrix5&comma;5&comma;i&comma;j&rarr;7ji

M1&comma;3  1&semi;                &num; update an entryM4&comma;1..2  2&comma;3&semi;   &num; replace part of a rowM4..5&comma;4  4&verbar;5&semi;   &num; replace part of a columnM2..2&comma;5..5  6&semi; &num; replace a sub-MatrixM&semi;

M1&comma;3:=1

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.

M1..5&comma;60&verbar;1&verbar;2&verbar;3&verbar;4&verbar;5

Error, Matrix index out of range

Using round brackets, however, the Matrix is enlarged to include a sixth column.

M1..5&comma;60&verbar;1&verbar;2&verbar;3&verbar;4&verbar;5

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.

M:=Matrix5&comma;i&comma;j&rarr;i5&plus;j5

Swap two rows:

M1&comma;2  M2&comma;1&semi; M&semi;

Multiply a row by a scalar:

M3  3  M3&semi;M&semi;

Add a multiple of one row to another:

M4  M4 16 M2&semi;M&semi;

See Also

dot, Matrix and Vector Construction Shortcuts, Matrix and Vector Entry Extraction, Matrix palette, rtable_indexing, simplify

 

Return to Index for Example Worksheets