Syntax Shortcuts - Maple Help

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.

 > $\mathrm{restart}$
 > $\mathrm{with}\left(\mathrm{LinearAlgebra}\right):$

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:

 > $\mathrm{Matrix}\left(\left[\left[6,-3,4\right],\left[8,9,-14\right]\right]\right)$
 $\left[\begin{array}{rrr}{6}& {-}{3}& {4}\\ {8}& {9}& {-}{14}\end{array}\right]$ (1.1)

you can enter:

 > $⟨6,-3,4;8,9,14⟩$
 $\left[\begin{array}{rrr}{6}& {-}{3}& {4}\\ {8}& {9}& {14}\end{array}\right]$ (1.2)

or,

 > $\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}⟨⟨6|-3|4⟩,⟨8|9|14⟩⟩$
 $\left[\begin{array}{rrr}{6}& {-}{3}& {4}\\ {8}& {9}& {14}\end{array}\right]$ (1.3)

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⟩$
 $\left[\begin{array}{rrr}{6}& {-}{3}& {4}\\ {8}& {9}& {14}\end{array}\right]$ (1.4)

or,

 > $⟨⟨6,8⟩|⟨-3,9⟩|⟨4,14⟩⟩$
 $\left[\begin{array}{rrr}{6}& {-}{3}& {4}\\ {8}& {9}& {14}\end{array}\right]$ (1.5)

 > $\mathrm{Vector}\left(\left[7,-3,2\right]\right)$
 $\left[\begin{array}{r}{7}\\ {-}{3}\\ {2}\end{array}\right]$ (1.6)

you can enter:

 > $⟨7,-3,2⟩$
 $\left[\begin{array}{r}{7}\\ {-}{3}\\ {2}\end{array}\right]$ (1.7)

 > $\mathrm{Vector}\left[\mathrm{row}\right]\left(\left[11,27,6\right]\right)$
 $\left[\begin{array}{ccc}{11}& {27}& {6}\end{array}\right]$ (1.8)

you can enter:

 > $⟨11|27|6⟩$
 $\left[\begin{array}{ccc}{11}& {27}& {6}\end{array}\right]$ (1.9)

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.

 > $3+⟨7,8,9⟩$

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.

 >
 $\left[\begin{array}{rrr}{8}& {2}& {3}\\ {4}& {12}& {6}\\ {7}& {8}& {16}\end{array}\right]{=}\left[\begin{array}{rrr}{8}& {2}& {3}\\ {4}& {12}& {6}\\ {7}& {8}& {16}\end{array}\right]$ (2.1.1)
 > $10+⟨2,5,11;4,6,7⟩=10\cdot \mathrm{IdentityMatrix}\left(2,3\right)+⟨2,5,11;4,6,7⟩$
 $\left[\begin{array}{rrr}{12}& {5}& {11}\\ {4}& {16}& {7}\end{array}\right]{=}\left[\begin{array}{rrr}{12}& {5}& {11}\\ {4}& {16}& {7}\end{array}\right]$ (2.1.2)

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

 > $7+\mathrm{Array}\left(1..3,1..3,\left[\left[1,2,3\right],\left[4,5,6\right],\left[7,8,9\right]\right]\right)$
 $\left[\begin{array}{rrr}{8}& {9}& {10}\\ {11}& {12}& {13}\\ {14}& {15}& {16}\end{array}\right]$ (2.1.3)

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⟩+3\cdot ⟨7,5⟩$
 $\left[\begin{array}{r}{24}\\ {19}\end{array}\right]$ (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\cdot ⟨7,-2,3;8,9,6⟩-3\cdot ⟨4,3|-6,-5|12,19⟩$
 $\left[\begin{array}{rrr}{2}& {14}& {-}{30}\\ {7}& {33}& {-}{45}\end{array}\right]$ (2.2.2)

You can also add Arrays:

 > $3\cdot \mathrm{Array}\left(\left[\left[1,7\right]\right]\right)+\mathrm{Array}\left(\left[\left[-8,23\right]\right]\right)$
 $\left[\begin{array}{rr}{-}{5}& {44}\end{array}\right]$ (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.

 > $x\cdot y\cdot z;\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}$$z\cdot y\cdot x$
 ${x}{}{y}{}{z}$
 ${x}{}{y}{}{z}$ (3.1.1)

Since multiplying a Vector, Matrix, or Array by a scalar is commutative, you can use the * operator to perform that operation:

 >
 $\left[\begin{array}{r}{-}{3}\\ {-}{6}\\ {-}{9}\end{array}\right]$
 $\left[\begin{array}{cc}{28}& {32}\\ {4}& {24}{}{t}\end{array}\right]$
 $\left[\begin{array}{cc}{3}{}{w}& {3}{}{y}\\ {3}{}{y}& {3}{}{z}\end{array}\right]$ (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.

 >

Pointwise (or elementwise) multiplication of Matrices or Vectors can be performed by using the '*~' ("elementwise") operator:

 > $⟨1,2,3;4,5,6⟩\cdot ~⟨a,b,c;d,e,f⟩$
 $\left[\begin{array}{ccc}{a}& {2}{}{b}& {3}{}{c}\\ {4}{}{d}& {5}{}{e}& {6}{}{f}\end{array}\right]$ (3.1.3)

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

 >
 $\left[\begin{array}{cc}{w}& {2}{}{x}\\ {3}{}{y}& {4}{}{z}\end{array}\right]$ (3.1.4)

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;$$\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}z.y.x$
 ${x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{y}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{z}$
 ${z}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{y}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{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.)

 >
 $\left[\begin{array}{r}{-}{3}\\ {-}{6}\\ {-}{9}\end{array}\right]$
 $\left[\begin{array}{cc}{28.}& {32.}\\ {4.}& {24.}\end{array}\right]$
 $\left[\begin{array}{cc}{3}{}{w}& {3}{}{y}\\ {3}{}{y}& {3}{}{z}\end{array}\right]$ (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.

 >
 ${x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left[\begin{array}{r}{1}\\ {2}\\ {3}\end{array}\right]$
 $\left[\begin{array}{rr}{7}& {8}\\ {1}& {6}\end{array}\right]\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}$
 ${x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}\left[\begin{array}{cc}{w}& {y}\\ {y}& {z}\end{array}\right]$ (3.2.3)

To tell Maple to treat the variable as a scalar, use the simplify command:

 >
 $\left[\begin{array}{c}{x}\\ {2}{}{x}\\ {3}{}{x}\end{array}\right]$
 $\left[\begin{array}{cc}{7}{}{x}& {8}{}{x}\\ {x}& {6}{}{x}\end{array}\right]$
 $\left[\begin{array}{cc}{x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{w}& {x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{y}\\ {x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{y}& {x}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{.}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{z}\end{array}\right]$ (3.2.4)

You can carry out Matrix and Vector multiplication by using the '.' operator.

 >
 $\left[\begin{array}{rr}{4}& {6}\\ {12}& {18}\end{array}\right]$
 ${22}$
 $\left[\begin{array}{cc}{-}{1}& {3}{}{x}{+}{7}\\ {52}& {-}{8}{}{x}{-}{105}\end{array}\right]$ (3.2.5)

On Arrays, the '.' operator performs component-wise multiplication.

 >
 $\left[\begin{array}{cc}{w}& {2}{}{x}\\ {3}{}{y}& {4}{}{z}\end{array}\right]$ (3.2.6)

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⟩;\phantom{\rule[-0.0ex]{0.0em}{0.0ex}}M.M.M.M.M.M.M.M.M.M={M}^{10}$
 ${M}{:=}\left[\begin{array}{cc}{0.2}& {0.35}\\ {0.8}& {0.65}\end{array}\right]$
 $\left[\begin{array}{cc}{0.304347830098437}& {0.304347824331934}\\ {0.695652169901563}& {0.695652175668067}\end{array}\right]{=}\left[\begin{array}{cc}{0.304347830098437}& {0.304347824331934}\\ {0.695652169901563}& {0.695652175668067}\end{array}\right]$ (3.2.7)

Transposition

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

 >
 $\left[\begin{array}{ccc}{1}& {2}& {3}\end{array}\right]$
 $\left[\begin{array}{rr}{1}& {4}\\ {2}& {5}\\ {3}& {6}\end{array}\right]$ (4.1)

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

 >
 $\left[\begin{array}{ccc}{1}{-}{I}& {2}{-}{2}{}{I}& {3}{+}{I}\end{array}\right]$
 $\left[\begin{array}{cc}{1}{-}{I}& {4}{-}{4}{}{I}\\ {2}{-}{2}{}{I}& {5}{-}{5}{}{I}\\ {3}{-}{3}{}{I}& {6}{-}{6}{}{I}\end{array}\right]$ (4.2)

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:=\mathrm{Matrix}\left(5,\left(i,j\right)→3i-j\right)$
 ${M}{:=}\left[\begin{array}{rrrrr}{2}& {1}& {0}& {-}{1}& {-}{2}\\ {5}& {4}& {3}& {2}& {1}\\ {8}& {7}& {6}& {5}& {4}\\ {11}& {10}& {9}& {8}& {7}\\ {14}& {13}& {12}& {11}& {10}\end{array}\right]$ (5.1.1)

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

 > $M\left[5,4\right]$
 ${11}$ (5.1.2)

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

 > $M\left[5\right]$
 $\left[\begin{array}{ccccc}{14}& {13}& {12}& {11}& {10}\end{array}\right]$ (5.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:

 > $M\left[5,1..3\right],M\left[3..5,2\right]$
 $\left[\begin{array}{ccc}{14}& {13}& {12}\end{array}\right]{,}\left[\begin{array}{r}{7}\\ {10}\\ {13}\end{array}\right]$ (5.1.4)

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

 > $M\left[3..5,1..3\right]$
 $\left[\begin{array}{rrr}{8}& {7}& {6}\\ {11}& {10}& {9}\\ {14}& {13}& {12}\end{array}\right]$ (5.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:

 >
 ${7}$
 $\left[\begin{array}{c}{7}\end{array}\right]$
 $\left[\begin{array}{r}{7}\end{array}\right]$
 $\left[\begin{array}{r}{7}\end{array}\right]$ (5.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):

 >
 $\left[\begin{array}{rr}{1}& {-}{1}\\ {7}& {5}\end{array}\right]$
 $\left[\begin{array}{rrrrr}{2}& {1}& {0}& {-}{1}& {-}{2}\\ {5}& {4}& {3}& {2}& {1}\\ {8}& {7}& {6}& {5}& {4}\\ {14}& {13}& {12}& {11}& {10}\\ {11}& {10}& {9}& {8}& {7}\end{array}\right]$ (5.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.

 >
 $\left[\begin{array}{c}{7}\end{array}\right]$
 $\left[\begin{array}{r}{7}\end{array}\right]$
 $\left[\begin{array}{r}{7}\end{array}\right]$ (5.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.)

 >
 ${10}$
 $\left[\begin{array}{ccccc}{5}& {4}& {3}& {2}& {1}\end{array}\right]$ (5.1.9)

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 $M\left[3..5,2\right]$ for the 5x5 Matrix $M$.

 > $M\left[3..,2\right]$

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:

 $\left[\begin{array}{r}{7}\\ {10}\\ {13}\end{array}\right]$ (5.1.10)
 >
 > $M\left[\mathrm{even_rows}\right]$
 $\left[\begin{array}{rrrrr}{2}& {1}& {0}& {-}{1}& {-}{2}\\ {8}& {7}& {6}& {5}& {4}\\ {14}& {13}& {12}& {11}& {10}\end{array}\right]$ (5.1.11)

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.

 >
 > $M\left[..,\mathrm{filtered_columns}\right]$
 $\left[\begin{array}{rrr}{2}& {1}& {0}\\ {5}& {4}& {3}\\ {8}& {7}& {6}\\ {11}& {10}& {9}\\ {14}& {13}& {12}\end{array}\right]$ (5.1.12)

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:=\mathrm{Matrix}\left(5,5,\left(i,j\right)→7j-i\right)$
 ${M}{:=}\left[\begin{array}{rrrrr}{6}& {13}& {20}& {27}& {34}\\ {5}& {12}& {19}& {26}& {33}\\ {4}& {11}& {18}& {25}& {32}\\ {3}& {10}& {17}& {24}& {31}\\ {2}& {9}& {16}& {23}& {30}\end{array}\right]$ (5.2.1)
 >
 ${{M}}_{{1}{,}{3}}{:=}{-}{1}$
 ${{M}}_{{4}{,}{1}{..}{2}}{:=}\left[\begin{array}{r}{-}{2}\\ {-}{3}\end{array}\right]$
 ${{M}}_{{4}{..}{5}{,}{4}}{:=}\left[\begin{array}{cc}{-}{4}& {-}{5}\end{array}\right]$
 ${{M}}_{{2}{..}{2}{,}{5}{..}{5}}{:=}\left[\begin{array}{r}{-}{6}\end{array}\right]$
 $\left[\begin{array}{rrrrr}{6}& {13}& {-}{1}& {27}& {34}\\ {5}& {12}& {19}& {26}& {-}{6}\\ {4}& {11}& {18}& {25}& {32}\\ {-}{2}& {-}{3}& {17}& {-}{4}& {31}\\ {2}& {9}& {16}& {-}{5}& {30}\end{array}\right]$ (5.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 $M$. Maple returns an error.

 > $M\left[1..5,6\right]≔⟨0|1|2|3|4|5⟩$

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

 > $M\left(1..5,6\right)≔⟨0|1|2|3|4|5⟩$

For more information on programmer indexing, see rtable_indexing.

 ${M}{:=}\left[\begin{array}{rrrrrr}{6}& {13}& {-}{1}& {27}& {34}& {0}\\ {5}& {12}& {19}& {26}& {-}{6}& {1}\\ {4}& {11}& {18}& {25}& {32}& {2}\\ {-}{2}& {-}{3}& {17}& {-}{4}& {31}& {3}\\ {2}& {9}& {16}& {-}{5}& {30}& {4}\end{array}\right]$ (5.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.

 > $M:=\mathrm{Matrix}\left(5,\left(i,j\right)→i\cdot 5+j-5\right)$
 ${M}{:=}\left[\begin{array}{rrrrr}{1}& {2}& {3}& {4}& {5}\\ {6}& {7}& {8}& {9}& {10}\\ {11}& {12}& {13}& {14}& {15}\\ {16}& {17}& {18}& {19}& {20}\\ {21}& {22}& {23}& {24}& {25}\end{array}\right]$ (5.3.1)

Swap two rows:

 >
 ${{M}}_{\left[{1}{,}{2}\right]}{:=}\left[\begin{array}{rrrrr}{6}& {7}& {8}& {9}& {10}\\ {1}& {2}& {3}& {4}& {5}\end{array}\right]$
 $\left[\begin{array}{rrrrr}{6}& {7}& {8}& {9}& {10}\\ {1}& {2}& {3}& {4}& {5}\\ {11}& {12}& {13}& {14}& {15}\\ {16}& {17}& {18}& {19}& {20}\\ {21}& {22}& {23}& {24}& {25}\end{array}\right]$ (5.3.2)

Multiply a row by a scalar:

 >
 ${{M}}_{{3}}{:=}\left[\begin{array}{ccccc}{33}& {36}& {39}& {42}& {45}\end{array}\right]$
 $\left[\begin{array}{rrrrr}{6}& {7}& {8}& {9}& {10}\\ {1}& {2}& {3}& {4}& {5}\\ {33}& {36}& {39}& {42}& {45}\\ {16}& {17}& {18}& {19}& {20}\\ {21}& {22}& {23}& {24}& {25}\end{array}\right]$ (5.3.3)

Add a multiple of one row to another:

 >
 ${{M}}_{{4}}{:=}\left[\begin{array}{ccccc}{0}& {-}{15}& {-}{30}& {-}{45}& {-}{60}\end{array}\right]$
 $\left[\begin{array}{rrrrr}{6}& {7}& {8}& {9}& {10}\\ {1}& {2}& {3}& {4}& {5}\\ {33}& {36}& {39}& {42}& {45}\\ {0}& {-}{15}& {-}{30}& {-}{45}& {-}{60}\\ {21}& {22}& {23}& {24}& {25}\end{array}\right]$ (5.3.4)
 >