The following functions and operators enable one to access individual entries or rows of matrices or vectors.
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.
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.
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.
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.
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.
> 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]
The following functions enable the extraction of certain rows, columns or general submatrices, or the replacement of a block by another matrix.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
> 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
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.
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.
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.
Given a matrix A, reverse all the rows of A.
Given a matrix A, reverse all the columns of A.
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.
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.
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).
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).
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).
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).
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).
Given a matrix A, remove all the zero rows of A.
> 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]