Accessing or Modifying Entries

Contents

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 ≤i ≤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 ≤i ≤m and 1 ≤j ≤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 ≤i ≤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 ≤i ≤m and 1 ≤j ≤n, and a ring element x coercible into R, modify the (i, j)-th entry of A to be x.

Example Mat_Indexing (H27E4)

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]

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 x n matrix A and integers i, j, p and q such that 1≤i ≤i + p ≤m + 1 and 1 ≤j ≤j + q ≤n + 1, return the p x 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 x n matrix A and integers i, j, r and s such that 1≤i, i - 1 ≤r ≤m, 1 ≤j, and j - 1 ≤s ≤n, return the r - i + 1 x 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 x 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 x n matrix A over a ring R, a p x q matrix B over R, and integers i and j such that 1≤i ≤i + p ≤m + 1 and 1 ≤j ≤j + q ≤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 (~A is a reference argument), A is modified in place so that the p x q submatrix of A rooted at (i, j) is now equal to B.
RowSubmatrix(A, i, k) : Mtrx, RngIntElt, RngIntElt -> Mtrx
Given an m x n matrix A and integers i and k such that 1 ≤i ≤i + k ≤m + 1, return the k x n submatrix of X consisting of rows [i ... 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 x n matrix A and an integer i such that 0 ≤i ≤m, return the i x 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 x n matrix A and integers i and j such that 1 ≤i and i - 1 ≤j ≤m, return the j - i + 1 x n submatrix of X consisting of rows [i ... 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 x n matrix A and integers i and k such that 1 ≤i ≤i + k ≤n + 1, return the m x k submatrix of X consisting of columns [i ... 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 x n matrix A and an integer i such that 0 ≤i ≤n, return the m x 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 x n matrix A and integers i and j such that 1 ≤i and i - 1 ≤j ≤n, return the m x j - i + 1 submatrix of X consisting of columns [i ... j] inclusive. The integer j may equal i - 1, in which case a matrix with zero columns and n rows will be returned.

Example Mat_Submatrix (H27E5)

The use of the submatrix operations is illustrated by applying them to a 6 x 6 matrix over the ring of integers 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

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 x n matrix A and integers i and j such that 1 ≤i≤m and 1 ≤j ≤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 x n matrix A and integers i and j such that 1 ≤i≤n and 1 ≤j ≤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 x n matrix A over a ring R, a ring element c coercible into R, and integers i and j such that 1 ≤i≤m and 1 ≤j ≤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 x n matrix A over a ring R, a ring element c coercible into R, and integers i and j such that 1 ≤i≤n and 1 ≤j ≤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 x n matrix A over a ring R, a ring element c coercible into R, and an integer i such that 1 ≤i≤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 x n matrix A over a ring R, a ring element c coercible into R, and an integer i such that 1 ≤i≤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 x n matrix A and an integer i such that 1 ≤i≤m, remove row i from A (leaving an (m - 1) x n matrix).
RemoveColumn(A, j) : Mtrx, RngIntElt -> Mtrx
RemoveColumn(~A, j) : Mtrx, RngIntElt ->
Given an m x n matrix A and an integer j such that 1 ≤j≤n, remove column j from A (leaving an m x (n - 1) matrix).
RemoveRowColumn(A, i, j) : Mtrx, RngIntElt -> Mtrx
RemoveRowColumn(~A, i, j) : Mtrx, RngIntElt ->
Given an m x n matrix A and integers i and j such that 1 ≤i≤m and 1 ≤j≤n, remove row i and column j from A (leaving an (m - 1) x (n - 1) matrix).
RemoveZeroRows(A) : Mtrx -> Mtrx
RemoveZeroRows(~A) : Mtrx ->
Given a matrix A, remove all the zero rows of A.

Example Mat_RowColumnOps (H27E6)

The use of row and column operations is illustrated by applying them to a 5 x 6 matrix over the ring of integers 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]
V2.28, 13 July 2023