Matrices

A matrix is also created using angled brackets. As you may recall, matrix-vector multiplication $A\textbf{u}$ is simply a linear combination of the columns of $A$ with the coefficients being the corresponding entries of $\textbf{u}$. Consequently, the most useful constructor defines a matrix as a sequence of vectors:

[> A := <<1, 2, 3>|<4, 5, 6>|<7, 8, 10>>;

$A := \begin{pmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 10 \end{pmatrix}$

The pipe (|) separates columns, and this can subsequently be used to create an augmented matrix. Suppose you are trying to solve a system of linear equations where the target vector is as follows:

[> u := <9, 0, -4>;

$z := \begin{pmatrix} 9 \\ 0 \\ -4 \end{pmatrix}$

Suppose we want to create an augmented matrix:

[> Aaug := < A | u >;

$Aaug := \begin{pmatrix} 1 & 4 & 7 & 9 \\ 2 & 5 & 8 & 0 \\ 3 & 6 & 10 & -4 \end{pmatrix}$

Like vectors, there is also a matrix constructor, and you can either create a zero matrix, provide a fill value, or provide a symbol. In general, if the matrix is being assigned to a capital letter, the entry symbol should be the corresponding lower-case letter prefixed with an %.

[> B := Matrix( 3, 4 )

$B := \begin{pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end{pmatrix}$

[> C := Matrix( 5, 3, 'fill' = 1 )

$C := \begin{pmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{pmatrix}$

[> E := Matrix( 2, 6, 'sybmol' = %e )

$E := \begin{pmatrix} e_{1,1} & e_{1, 2} & e_{1, 3} & e_{1,4} & e_{1, 5} & e_{1, 6} \\ e_{2,1} & e_{2, 2} & e_{2, 3} & e_{2,4} & e_{2, 5} & e_{2, 6} \end{pmatrix}$

Indexing into a matrix

Recall that a vector may be an integer from 1 to n, where $n$ is the dimension, or -1 to -n, or a range with either side being either empty or an integer as just described, or a list of integers or ranges as just described.

Indexing into a matrix requires two indices:

  1. If both indices are integers, the result is the value at that location.
  2. If the second index is an integer, but the first is a list or range, the result is a (column) vector consisting of those entries specified in the list or range.
  3. If the first is an integer, and the second is a list or range, the result is a row vector consisting of those entries specified in the list or range.
  4. If both are lists or ranges, then the result is a matrix comprised of the entries in the Cartesian product of the entries specified.

For example:

[> Aaug;

$\begin{pmatrix} 1 & 4 & 7 & 9 \\ 2 & 5 & 8 & 0 \\ 3 & 6 & 10 & -4 \end{pmatrix}$

[> Aaug[3, 4];

$-4$

[> Aaug[.., 3];

$\begin{pmatrix} 7 \\ 8 \\ 10 \end{pmatrix}$

[> # The second last to the last entry
[> Aaug[-2.., 3];

$\begin{pmatrix} 8 \\ 10 \end{pmatrix}$

[> Aaug[[1, 3, 2, 1, 3, 3], 2];

$\begin{pmatrix} 7 \\ 10 \\ 8 \\ 7 \\ 10 \\ 10 \end{pmatrix}$

[> Aaug[1..2, 1..2];

$\begin{pmatrix} 1 & 4 \\ 2 & 5 \end{pmatrix}$

[> Aaug[[1, 3], [1, 3]] := Matrix( 2, 2, 'fill' = 42 );

$A_{[1,3], [1,3]} := \begin{pmatrix} 42 & 42 \\ 42 & 42 \end{pmatrix}$

[> Aaug;

$\begin{pmatrix} 42 & 4 & 42 & 9 \\ 2 & 5 & 8 & 0 \\ 42 & 6 & 42 & -4 \end{pmatrix}$