Accessing or Modifying Entries

Indexing

The following functions and operators enable one to access individual entries or rows of matrices or vectors.

A[i]: Mtrx, RngIntElt -> ModTupRngElt

Given a matrix \(A\) over the ring \(R\) having \(m\) rows and \(n\) columns, and an integer \(i\) such that \(1 \leq i \leq m\), return the \(i\)-th row of \(A\), as a vector of length \(n\).

A[i, j]: Mtrx, RngIntElt, RngIntElt -> RngElt

Given a matrix \(A\) over the ring \(R\) having \(m\) rows and \(n\) columns, integers \(i\) and \(j\) such that \(1 \leq i \leq m\) and \(1 \leq j \leq n\), return the \((i,j)\)-th entry of \(A\), as an element of the ring \(R\).

A[Q]: Mtrx, [ RngIntElt ] -> RngElt
A[i .. j]: Mtrx, RngIntElt, RngIntElt -> RngElt

Given a matrix \(A\) over the ring \(R\) having \(m\) rows and \(n\) columns, and a sequence \(Q\) of integers in the range \([1..m]\), return the sequence consisting of the rows of \(A\) specified by \(Q\). This is equivalent to [A[i]: i in Q]]. If \(Q\) is a range, then the second form A[i .. j] may be used to specify the range directly.

A[i] := v: Mtrx, RngIntElt, Mtrx

Given a matrix \(A\) over the ring \(R\) having \(m\) rows and \(n\) columns, an integer \(i\) such that \(1 \leq i \leq m\), and a vector \(v\) over \(R\) of length \(n\), modify the \(i\)-th row of \(A\) to be \(v\). The integer \(0\) may also be given for \(v\), indicating the zero vector.

A[i, j] := x: Mtrx, RngIntElt, RngIntElt, RngElt

Given a matrix \(A\) over the ring \(R\) having \(m\) rows and \(n\) columns, integers \(i\) and \(j\) such that \(1 \leq i \leq m\) and \(1 \leq j \leq n\), and a ring element \(x\) coercible into \(R\), modify the \((i,j)\)-th entry of \(A\) to be \(x\).

Example: Accessing Matrix Entries (ex-e9c35f)

Run in calculator (opens in a new tab)

This example demonstrates simple ways of accessing the entries of matrices.

> X := Matrix(4, [1,2,3,4, 5,4,3,2, 1,2,3,4]);
> X;
[1 2 3 4]
[5 4 3 2]
[1 2 3 4]
> X[1];
(1 2 3 4)
> X[1, 2];
2
> X[1, 2] := 23;
> X;
[ 1 23  3  4]
[ 5  4  3  2]
[ 1  2  3  4]
> X[3] := Vector([9,8,7,6]);
> X[2] := 0;
> X;
[ 1 23  3  4]
[ 0  0  0  0]
[ 9  8  7  6]

Run in calculator (opens in a new tab)

Extracting and Inserting Blocks

The following functions enable the extraction of certain rows, columns, or general submatrices, or the replacement of a block by another matrix.

Submatrix(A, i, j, p, q): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx
ExtractBlock(A, i, j, p, q): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and integers \(i\), \(j\), \(p\) and \(q\) such that \(1\leq i \leq i + p \leq m+1\) and \(1 \leq j \leq j + q \leq n+1\), return the \(p \times q\) submatrix of \(A\) rooted at \((i, j)\). Either or both of \(p\) and \(q\) may be zero, while \(i\) may be \(m+1\) if \(p\) is zero and \(j\) may be \(n+1\) if \(q\) is zero.

SubmatrixRange(A, i, j, r, s): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx
ExtractBlockRange(A, i, j, r, s): Mtrx, RngIntElt, RngIntElt, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and integers \(i\), \(j\), \(r\), and \(s\) such that \(1\leq i\), \(i-1 \leq r \leq m\), \(1 \leq j\), and \(j-1 \leq s \leq n\), return the \(r-i+1\times s-j+1\) submatrix of \(A\) rooted at the \((i, j)\)-th entry and extending to the \((r, s)\)-th entry, inclusive. \(r\) may equal \(i-1\) or \(s\) may equal \(j-1\), in which case a matrix with zero rows or zero columns, respectively, will be returned.

Submatrix(A, I, J): Mtrx, [RngIntElt], [RngIntElt] -> Mtrx

Given an \(m \times n\) matrix \(A\) and integer sequences \(I\) and \(J\), return the submatrix of \(A\) given by the row indices in \(I\) and the column indices in \(J\).

InsertBlock(A, B, i, j): Mtrx, Mtrx, RngIntElt, RngIntElt -> Mtrx
InsertBlock(~A, B, i, j): Mtrx, Mtrx, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) over a ring \(R\), a \(p \times q\) matrix \(B\) over \(R\), and integers \(i\) and \(j\) such that \(1\leq i \leq i + p \leq m+1\) and \(1 \leq j \leq j + q \leq n+1\), insert \(B\) at position \((i,j)\) in \(A\). In the functional version (\(A\) is a value argument), this function returns the new matrix and leaves \(A\) untouched, while in the procedural version (\(\sim A\) is a reference argument), \(A\) is modified in place so that the \(p \times q\) submatrix of \(A\) rooted at \((i, j)\) is now equal to \(B\).

RowSubmatrix(A, i, k): Mtrx, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(k\) such that \(1 \le i \leq i + k \leq m+1\), return the \(k \times n\) submatrix of \(X\) consisting of rows \([i \ldots i + k - 1]\) inclusive. The integer \(k\) may be zero and \(i\) may also be \(m+1\) if \(k\) is zero, but the result will always have \(n\) columns.

RowSubmatrix(A, i): Mtrx, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and an integer \(i\) such that \(0 \le i \leq m\), return the \(i\times n\) submatrix of \(X\) consisting of the first \(i\) rows. The integer \(i\) may be \(0\), but the result will always have \(n\) columns.

RowSubmatrixRange(A, i, j): Mtrx, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(j\) such that \(1 \le i\) and \(i-1 \leq j \leq m\), return the \(j-i+1 \times n\) submatrix of \(X\) consisting of rows \([i \ldots j]\) inclusive. The integer \(j\) may equal \(i-1\), in which case a matrix with zero rows and \(n\) columns will be returned.

ColumnSubmatrix(A, i, k): Mtrx, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(k\) such that \(1 \le i \leq i + k \leq n+1\), return the \(m \times k\) submatrix of \(X\) consisting of columns \([i \ldots i + k - 1]\) inclusive. The integer \(k\) may be zero and \(i\) may also be \(n+1\) if \(k\) is zero, but the result will always have \(m\) rows.

ColumnSubmatrix(A, i): Mtrx, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and an integer \(i\) such that \(0 \le i \leq n\), return the \(m\times i\) submatrix of \(X\) consisting of the first \(i\) columns. The integer \(i\) may be \(0\), but the result will always have \(m\) rows.

ColumnSubmatrixRange(A, i, j): Mtrx, RngIntElt, RngIntElt -> Mtrx

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(j\) such that \(1 \le i\) and \(i-1 \leq j \leq n\), return the \(m\times j-i+1\) submatrix of \(X\) consisting of columns \([i \ldots j]\) inclusive. The integer \(j\) may equal \(i-1\), in which case a matrix with zero columns and \(n\) rows will be returned.

Example: Extracting Submatrices (ex-0ed08c)

Run in calculator (opens in a new tab)

The use of the submatrix operations is illustrated by applying them to a \(6 \times 6\) matrix over the ring of integers \({\mathbb{Z}}\).

> A := Matrix(6,
>     [ 9, 1, 7, -3, 2, -1,
>       3, -4, -5, 9, 2, 7,
>       7, 1, 0, 1, 8, 22,
>       -3, 3, 3, 8, 8, 37,
>       -9, 0, 7, -1, 2, 3,
>       7, 2, -2, 4, 3, 47 ]);
> A;
[ 9  1  7 -3  2 -1]
[ 3 -4 -5  9  2  7]
[ 7  1  0  1  8 22]
[-3  3  3  8  8 37]
[-9  0  7 -1  2  3]
[ 7  2 -2  4  3 47]
> Submatrix(A, 2,2, 3,3);
[-4 -5  9]
[ 1  0  1]
[ 3  3  8]
> SubmatrixRange(A, 2,2, 3,3);
[-4 -5]
[ 1  0]
> S := $1;
> InsertBlock(~A, S, 5,5);
> A;
[ 9  1  7 -3  2 -1]
[ 3 -4 -5  9  2  7]
[ 7  1  0  1  8 22]
[-3  3  3  8  8 37]
[-9  0  7 -1 -4 -5]
[ 7  2 -2  4  1  0]
> RowSubmatrix(A, 5, 2);
[-9  0  7 -1 -4 -5]
[ 7  2 -2  4  1  0]
> RowSubmatrixRange(A, 2, 3);
[ 3 -4 -5  9  2  7]
[ 7  1  0  1  8 22]
> RowSubmatrix(A, 2, 0);
Matrix with 0 rows and 6 columns

Run in calculator (opens in a new tab)

Row and Column Operations

The following functions and procedures provide elementary row or column operations on matrices. For each operation, there is a corresponding function which creates a new matrix for the result (leaving the input matrix unchanged), and a corresponding procedure which modifies the input matrix in place.

SwapRows(A, i, j): Mtrx, RngIntElt, RngIntElt -> Mtrx
SwapRows(~A, i, j): Mtrx, RngIntElt, RngIntElt

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(j\) such that \(1 \le i\le m\) and \(1 \leq j \leq m\), swap the \(i\)-th and \(j\)-th rows of \(A\).

SwapColumns(A, i, j): Mtrx, RngIntElt, RngIntElt -> Mtrx
SwapColumns(~A, i, j): Mtrx, RngIntElt, RngIntElt

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(j\) such that \(1 \le i\le n\) and \(1 \leq j \leq n\), swap the \(i\)-th and \(j\)-th columns of \(A\).

ReverseRows(A): Mtrx -> Mtrx
ReverseRows(~A): Mtrx

Given a matrix \(A\), reverse all the rows of \(A\).

ReverseColumns(A): Mtrx -> Mtrx
ReverseColumns(~A): Mtrx

Given a matrix \(A\), reverse all the columns of \(A\).

AddRow(A, c, i, j): Mtrx, RngElt, RngIntElt, RngIntElt -> Mtrx
AddRow(~A, c, i, j): Mtrx, RngElt, RngIntElt, RngIntElt

Given an \(m \times n\) matrix \(A\) over a ring \(R\), a ring element \(c\) coercible into \(R\), and integers \(i\) and \(j\) such that \(1 \le i\le m\) and \(1 \leq j \leq m\), add \(c\) times row \(i\) of \(A\) to row \(j\) of \(A\).

AddColumn(A, c, i, j): Mtrx, RngElt, RngIntElt, RngIntElt -> Mtrx
AddColumn(~A, c, i, j): Mtrx, RngElt, RngIntElt, RngIntElt

Given an \(m \times n\) matrix \(A\) over a ring \(R\), a ring element \(c\) coercible into \(R\), and integers \(i\) and \(j\) such that \(1 \le i\le n\) and \(1 \leq j \leq n\), add \(c\) times column \(i\) of \(A\) to column \(j\).

MultiplyRow(A, c, i): Mtrx, RngElt, RngIntElt -> Mtrx
MultiplyRow(~A, c, i): Mtrx, RngElt, RngIntElt

Given an \(m \times n\) matrix \(A\) over a ring \(R\), a ring element \(c\) coercible into \(R\), and an integer \(i\) such that \(1 \le i\le m\), multiply row \(i\) of \(A\) by \(c\) (on the left).

MultiplyColumn(A, c, i): Mtrx, RngElt, RngIntElt -> Mtrx
MultiplyColumn(~A, c, i): Mtrx, RngElt, RngIntElt

Given an \(m \times n\) matrix \(A\) over a ring \(R\), a ring element \(c\) coercible into \(R\), and an integer \(i\) such that \(1 \le i\le n\), multiply column \(i\) of \(A\) by \(c\) (on the left).

RemoveRow(A, i): Mtrx, RngIntElt -> Mtrx
RemoveRow(~A, i): Mtrx, RngIntElt

Given an \(m \times n\) matrix \(A\) and an integer \(i\) such that \(1 \le i\le m\), remove row \(i\) from \(A\) (leaving an \((m-1) \times n\) matrix).

RemoveColumn(A, j): Mtrx, RngIntElt -> Mtrx
RemoveColumn(~A, j): Mtrx, RngIntElt

Given an \(m \times n\) matrix \(A\) and an integer \(j\) such that \(1 \le j\le n\), remove column \(j\) from \(A\) (leaving an \(m \times (n-1)\) matrix).

RemoveRowColumn(A, i, j): Mtrx, RngIntElt -> Mtrx
RemoveRowColumn(~A, i, j): Mtrx, RngIntElt

Given an \(m \times n\) matrix \(A\) and integers \(i\) and \(j\) such that \(1 \le i\le m\) and \(1 \le j\le n\), remove row \(i\) and column \(j\) from \(A\) (leaving an \((m-1) \times (n-1)\) matrix).

RemoveZeroRows(A): Mtrx -> Mtrx
RemoveZeroRows(~A): Mtrx

Given a matrix \(A\), remove all the zero rows of \(A\).

Example: Row and Column Operations (ex-80d37c)

Run in calculator (opens in a new tab)

The use of row and column operations is illustrated by applying them to a \(5 \times 6\) matrix over the ring of integers \({\mathbb{Z}}\).

> A := Matrix(5, 6,
>     [ 3, 1, 0, -4, 2, -12,
>       2, -4, -5, 5, 23, 6,
>       8, 0, 0, 1, 5, 12,
>       -2, -6, 3, 8, 9, 17,
>       11, 12, -6, 4, 2, 27 ]);
> A;
[  3   1   0  -4   2 -12]
[  2  -4  -5   5  23   6]
[  8   0   0   1   5  12]
[ -2  -6   3   8   9  17]
[ 11  12  -6   4   2  27]
> SwapColumns(~A, 1, 2);
> A;
[  1   3   0  -4   2 -12]
[ -4   2  -5   5  23   6]
[  0   8   0   1   5  12]
[ -6  -2   3   8   9  17]
[ 12  11  -6   4   2  27]
> AddRow(~A, 4, 1, 2);
> AddRow(~A, 6, 1, 4);
> AddRow(~A, -12, 1, 5);
> A;
[  1   3   0  -4   2 -12]
[  0  14  -5 -11  31 -42]
[  0   8   0   1   5  12]
[  0  16   3 -16  21 -55]
[  0 -25  -6  52 -22 171]
> RemoveRow(~A, 1);
> A;
[ 2 -4 -5  5 23  6]
[ 8  0  0  1  5 12]
[-2 -6  3  8  9 17]
[11 12 -6  4  2 27]
> RemoveRowColumn(~A, 4, 6);
> A;
[ 2 -4 -5  5 23]
[ 8  0  0  1  5]
[-2 -6  3  8  9]

Run in calculator (opens in a new tab)