Skip to content

Section documentation for STACK-Maxima /linearalgebra

Matrix operations, predicates and equation-display functions provided by STACK. They are available in question variables, answer tests and feedback without loading matrix.mac. Existing questions can continue to include that file; it retains the declaration of the short c and r vector notation.

The reference below covers extracting, replacing and scaling matrix entries. The subdirectories document matrix predicates and the display of linear systems.


columnscale(M,i,alpha)

Scales column i of matrix A by alpha. A companion to columnop and columnswap. C_i <- alpha*C_i

Argument name type description
M matrix The matrix whose column you are scaling
i integer The column you are scaling
alpha number The amount you are scaling the column.
Return type description
matrix C_i <- alpha*C_i

diag_entries(M)

Extracts the diagonal of a matrix as a list. For a rectangular matrix, the list has length min(m,n). Non-matrix input is returned in a singleton list.

Argument name type description
M matrix An mxn matrix
Return type description
list The diagonal entries of M as a list

diagmatrix_like(d,m,n)

Construct a diagonal matrix of size m by n with diagonal given as a list Similar to native function diag which builds a block diagonal matrix, but instead is intended for numerical diagonals of rectangular matrices. Intended use case is to extend a reduced SVD into a full SVD If the whole diagonal does not fit in an mxn matrix, then it truncates d. If d is not long enough to fill an mxn matrix, remaining diagonal entries are set to 0.

Argument name type description
d list the list of diagonal entries, which may be symbolic.
m positive integer The number of rows in the desired matrix
n positive integer The number of columns in the desired matrix
Return type description
matrix A mxn matrix with d as the diagonal

diagonal(M)

Take the diagonal of a matrix, leaving the remaining entries = 0. The matrix may be rectangular. Non-matrix input is returned unchanged. This uses genmatrix, which requires positive row and column counts.

Argument name type description
M matrix An mxn matrix
Return type description
matrix The same matrix with all off-diagonal entries set to 0

rowscale(M,i,alpha)

Scales row i of matrix A by alpha. A companion to rowop and rowswap. R_i <- alpha*R_i

Argument name type description
M matrix The matrix whose row you are scaling
i integer The row you are scaling
alpha number The amount you are scaling the row.
Return type description
matrix R_i <- alpha*R_i

setcolmx(c,i,M)

Replace column i of matrix M with c. If c is a list or a matrix, replace the column exactly. Nothing will happen if c does not conform. If c is a number, fill column i with the value c. Companion to setelmx

Argument name type description
c number, list or matrix the replacement column (or value that will be repeated in the column)
i integer the index of the column that will be replaced
M matrix the matrix whose column is being replaced
Return type description
matrix The matrix M with column i replaced by c.

setdiagmx(L,M,[_k])

Replace diagonal k of matrix M with L. If L is a list, replace the diagonal exactly. If L is too short to fill the diagonal, then any remaining diagonal entries are untouched If L is too long for the diagonal, then only the first l entries of L are used, where l is the length of the diagonal. If L is a number, fill the diagonal with the value L. If the optional argument k is given, then choose the kth diagonal. For example, setdiagmx(L,M,1) will adjust the values one entry above the primary diagonal. Companion to setelmx

Argument name type description
L number or list the replacement diagonal (or value that will be repeated in the diagonal)
M matrix the matrix whose diagonal is being replaced
_k integer optional diagonal offset; defaults to zero.
Return type description
matrix The matrix M with diagonal k replaced by L.

setrowmx(r,i,M)

Replace row i of matrix M with r. If r is a list or a matrix, replace the row exactly. Nothing will happen if r does not conform. If r is a number, fill row i with the value r. Companion to setelmx

Argument name type description
r number, list or matrix the replacement row (or value that will be repeated in the row)
i integer the index of the row that will be replaced
M matrix the matrix whose row is being replaced
Return type description
matrix The matrix M with row i replaced by r.

tril(M)

Take the lower triangular part of a matrix, leaving the remaining entries = 0. The matrix may be rectangular. Non-matrix input is returned unchanged. This uses genmatrix, which requires positive row and column counts.

Argument name type description
M matrix An mxn matrix
Return type description
matrix The same matrix with all entries above the diagonal set to 0

triu(M)

Take the upper triangular part of a matrix, leaving the remaining entries = 0. The matrix may be rectangular. Non-matrix input is returned unchanged. This uses genmatrix, which requires positive row and column counts.

Argument name type description
M matrix An mxn matrix
Return type description
matrix The same matrix with all entries below the diagonal set to 0