Up | Next | Prev | PrevTail | Tail |
This package provides a selection of functions that are useful in the world of linear algebra.
This package provides a selection of functions that are useful in the world of linear algebra. These functions are described alphabetically in subsection 20.33.3 and are labelled 20.33.3.1 to 20.33.3.53. They can be classified into four sections(n.b: the numbers after the dots signify the function label in section 20.33.3).
Contributions to this package have been made by Walter Tietze (ZIB).
add_columns | … | 20.33.3.1 | add_rows | … | 20.33.3.2 |
add_to_columns | … | 20.33.3.3 | add_to_rows | … | 20.33.3.4 |
augment_columns | … | 20.33.3.5 | char_poly | … | 20.33.3.9 |
column_dim | … | 20.33.3.12 | copy_into | … | 20.33.3.14 |
diagonal | … | 20.33.3.15 | extend | … | 20.33.3.16 |
find_companion | … | 20.33.3.17 | get_columns | … | 20.33.3.18 |
get_rows | … | 20.33.3.19 | hermitian_tp | … | 20.33.3.21 |
matrix_augment | … | 20.33.3.28 | matrix_stack | … | 20.33.3.30 |
minor | … | 20.33.3.31 | mult_columns | … | 20.33.3.32 |
mult_rows | … | 20.33.3.33 | pivot | … | 20.33.3.34 |
remove_columns | … | 20.33.3.37 | remove_rows | … | 20.33.3.38 |
row_dim | … | 20.33.3.39 | rows_pivot | … | 20.33.3.40 |
stack_rows | … | 20.33.3.43 | sub_matrix | … | 20.33.3.44 |
swap_columns | … | 20.33.3.46 | swap_entries | … | 20.33.3.47 |
swap_rows | … | 20.33.3.48 |
Functions that create matrices.
band_matrix | … | 20.33.3.6 | block_matrix | … | 20.33.3.7 |
char_matrix | … | 20.33.3.8 | coeff_matrix | … | 20.33.3.11 |
companion | … | 20.33.3.13 | hessian | … | 20.33.3.22 |
hilbert | … | 20.33.3.23 | mat_jacobian | … | 20.33.3.24 |
jordan_block | … | 20.33.3.25 | make_identity | … | 20.33.3.27 |
random_matrix | … | 20.33.3.36 | toeplitz | … | 20.33.3.50 |
Vandermonde | … | 20.33.3.52 | Kronecker_Product | … | 20.33.3.53 |
char_poly | … | 20.33.3.9 | cholesky | … | 20.33.3.10 |
gram_schmidt | … | 20.33.3.20 | lu_decom | … | 20.33.3.26 |
pseudo_inverse | … | 20.33.3.35 | simplex | … | 20.33.3.41 |
svd | … | 20.33.3.45 | triang_adjoint | … | 20.33.3.51 |
There is a separate NORMFORM package described in section 20.40 for computing the following matrix normal forms in REDUCE:
smithex, smithex_int, frobenius, ratjordan, jordansymbolic, jordan.
matrixp | … | 20.33.3.29 | squarep | … | 20.33.3.42 |
symmetricp | … | 20.33.3.49 |
In the examples the matrix \(\mathcal {A}\) will be
Throughout \(\mathcal {I}\) is used to indicate the identity matrix and \(\mathcal {A}^T\) to indicate the transpose of the matrix \(\mathcal {A}\).
If you have not used matrices within REDUCE before then the following may be helpful.
Initialisation of matrices takes the following syntax:
mat1 := mat((a,b,c),(d,e,f),(g,h,i));
will produce
\( mat1 := \begin {pmatrix} a & b & c \\ d & e & f \\ g & h & i \end {pmatrix} \)
The \((i,j)\)th entry can be accessed by:
mat1(i,j);
The package is loaded by:
load_package linalg;
add_columns(\(\mathcal {A}\),c1,c2,expr);
\(\mathcal {A}\) | :- | a matrix. |
\(c1,c2\) | :- | positive integers. |
expr | :- | a scalar expression. |
add_columnsreplaces column \(c\)2 of \(\mathcal {A}\) by
\(\texttt {expr} * \texttt {column($\mathcal {A}$,c1)} + \texttt {column($\mathcal {A}$,c2)}\).
add_rowsperforms the equivalent task on the rows of \(\mathcal {A}\).
add_columns\((\mathcal {A},1,2,x) = \begin {pmatrix} 1 & x+2 & 3 \\ 4 & 4*x+5 & 6 \\ 7 & 7*x+8 & 9 \end {pmatrix}\)
add_rows\((\mathcal {A},2,3,5) = \begin {pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 27 & 33 & 39 \end {pmatrix}\)
add_to_columns, add_to_rows, mult_columns, mult_rows.
See: add_columns.
add_to_columns(\(\mathcal {A}\),column_list,expr);
\(\mathcal {A}\) | :- | a matrix. |
column_list | :- | a positive integer or a list of positive integers. |
expr | :- | a scalar expression. |
add_to_columnsadds expr to each column specified in column_list of \(\mathcal {A}\).
add_to_rowsperforms the equivalent task on the rows of \(\mathcal {A}\).
add_to_columns\((\mathcal {A},\{1,2\},10) = \begin {pmatrix} 11 & 12 & 3 \\ 14 & 15 & 6 \\ 17 & 18 & 9 \end {pmatrix}\)
add_to_rows\((\mathcal {A},2,-x) = \begin {pmatrix} 1 & 2 & 3 \\ -x+4 & -x+5 & -x+6 \\ 7 & 8 & 9 \end {pmatrix}\)
add_columns, add_rows, mult_rows, mult_columns.
See: add_to_columns.
augment_columns(\(\mathcal {A}\),column_list);
\(\mathcal {A}\) | :- | a matrix. |
column_list | :- | either a positive integer or a list of positive integers. |
augment_columnsgets hold of the columns of \(\mathcal {A}\) specified in column_list and sticks
them together.
stack_rowsperforms the same task on rows of \(\mathcal {A}\).
augment_columns\((\mathcal {A},\{1,2\}) = \begin {pmatrix}{cc} 1 & 2 \\ 4 & 5 \\ 7 & 8 \end {pmatrix}\)
stack_rows\((\mathcal {A},\{1,3\}) = \begin {pmatrix} 1 & 2 & 3 \\ 7 & 8 & 9 \end {pmatrix}\)
get_columns, get_rows, sub_matrix.
band_matrix(expr_list,square_size);
expr_list | :- | either a single scalar expression or a list of an odd number of scalar expressions. |
square_size | :- | a positive integer. |
band_matrixcreates a square matrix of dimension square_size. The diagonal
consists of the middle expr of the expr_list. The expressions to the left of this
fill the required number of sub-diagonals and the expressions to the right the
super-diagonals.
band_matrix\((\{x,y,z\},6) = \begin {pmatrix} y & z & 0 & 0 & 0 & 0 \\ x & y & z & 0 & 0 & 0 \\ 0 & x & y & z & 0 & 0 \\ 0 & 0 & x & y & z & 0 \\ 0 & 0 & 0 & x & y & z \\ 0 & 0 & 0 & 0 & x & y \end {pmatrix}\)
diagonal.
block_matrix(r,c,matrix_list);
\(r,c\) | :- | positive integers. |
matrix_list | :- | a list of matrices. |
block_matrixcreates a matrix that consists of \(r\times c\) matrices filled from the
matrix_listrow-wise.
\(\mathcal {B} = \begin {pmatrix} 1 & 0 \\ 0 & 1 \end {pmatrix}, \,\, \mathcal {C} = \begin {pmatrix} 5 \\ 5 \end {pmatrix}, \,\, \mathcal {D} = \begin {pmatrix} 22 & 33 \\ 44 & 55 \end {pmatrix}\)
block_matrix\((2,3,\{\mathcal {B,C,D,D,C,B}\}) = \begin {pmatrix} 1 & 0 & 5 & 22 & 33 \\ 0 & 1 & 5 & 44 & 55 \\ 22 & 33 & 5 & 1 & 0 \\ 44 & 55 & 5 & 0 & 1 \end {pmatrix}\)
char_matrix(\(\mathcal {A},\lambda \));
\(\mathcal {A}\) | :- | a square matrix. |
\(\lambda \) | :- | a symbol or algebraic expression. |
char_matrixcreates the characteristic matrix \(\mathcal {C}\) of \(\mathcal {A}\). This is \(\mathcal {C} = \lambda \mathcal {I} - \mathcal {A}\).
char_matrix\((\mathcal {A},x) = \begin {pmatrix} x-1 & -2 & -3 \\ -4 & x-5 & -6 \\ -7 & -8 & x-9 \end {pmatrix}\)
char_poly.
char_poly(\(\mathcal {A},\lambda \));
\(\mathcal {A}\) | :- | a square matrix. |
\(\lambda \) | :- | a symbol or algebraic expression. |
char_polyfinds the characteristic polynomial of \(\mathcal {A}\).
This is the determinant of \(\lambda \mathcal {I} - \mathcal {A}\).
char_poly(\(\mathcal {A},x\)) \(= x^3-15*x^2-18*x\)
char_matrix.
cholesky(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a positive definite matrix containing numeric entries. |
choleskycomputes the cholesky decomposition of \(\mathcal {A}\).
It returns {\(\mathcal {L},\mathcal {U}\)} where \(\mathcal {L}\) is a lower matrix, \(\mathcal {U}\) is an upper matrix,
\(\mathcal {A} = \mathcal {LU}\), and \(\mathcal {U} = \mathcal {L}^T\).
\(\mathcal {F} = \begin {pmatrix} 1 & 1 & 0 \\ 1 & 3 & 1 \\ 0 & 1 & 1 \end {pmatrix}\)
\(\texttt {cholesky}(\mathcal {F}) = \left \{ \begin {pmatrix} 1 & 0 & 0 \\ 1 & \sqrt {2} & 0 \\ 0 & \frac {1}{\sqrt {2}} & \frac {1}{\sqrt {2}} \end {pmatrix}, \begin {pmatrix} 1 & 1 & 0 \\ 0 & \sqrt {2} & \frac {1}{\sqrt {2}} \\ 0 & 0 & \frac {1}{\sqrt {2}} \end {pmatrix} \right \}\)
lu_decom.
coeff_matrix({lin_eqn\(_{1}\),lin_eqn\(_{2}\), …,lin_eqn\(_{n}\)});
27
lin_eqn\(_{1}\),lin_eqn\(_{2}\), …,lin_eqn\(_{n}\) | :- | linear equations. Can be of the form equation \(=\) number or just equation which is equivalent to equation \(=\) 0. |
coeff_matrixcreates the coefficient matrix \(\mathcal {C}\) of the linear equations. It returns {\(\mathcal {C,X,B}\)} such
that \(\mathcal {CX} = \mathcal {B}\).
coeff_matrix\((\{x+y+4*z=10,y+x-z=20,x+y+4\}) =\)
\(\left \{ \begin {pmatrix} 4 & 1 & 1 \\ -1 & 1 & 1 \\ 0 & 1 & 1 \end {pmatrix}, \begin {pmatrix} z \\ y \\ x \end {pmatrix}, \begin {pmatrix} 10 \\ 20 \\ -4 \end {pmatrix} \right \}\)
column_dim(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix. |
column_dimfinds the column dimension of \(\mathcal {A}\).
row_dimfinds the row dimension of \(\mathcal {A}\).
column_dim(\(\mathcal {A}\)) = 3
companion(poly,x);
poly | :- | a monic univariate polynomial in \(x\). |
\(x\) | :- | the variable. |
companioncreates the companion matrix \(\mathcal {C}\) of poly.
This is the square matrix of dimension \(n\), where \(n\) is the degree of polyw.r.t. \(x\). The entries of \(\mathcal {C}\) are: \(\mathcal {C}(i,n) = -\texttt {coeffn}(\texttt {poly},x,i-1)\) for \(i = 1,\ldots , n\), \(\mathcal {C}(i,i-1) = 1\) for \(i=2,\ldots ,n\) and the rest are \(0\).
\( \texttt {companion}(x^4+17*x^3-9*x^2+11,x) = \begin {pmatrix} 0 & 0 & 0 & -11 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 9 \\ 0 & 0 & 1 & -17 \end {pmatrix} \)
find_companion.
copy_into(\(\mathcal {A,B}\),r,c);
\(\mathcal {A,B}\) | :- | matrices. |
\(r,c\) | :- | positive integers. |
copy_intocopies matrix \(\mathcal {A}\) into \(\mathcal {B}\) with \(\mathcal {A}(1,1)\) at \(\mathcal {B}(r,c)\).
\(\mathcal {G} = \begin {pmatrix} 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 \end {pmatrix}\)
copy_into\((\mathcal {A,G},1,2) = \begin {pmatrix} 0 & 1 & 2 & 3 \\ 0 & 4 & 5 & 6 \\ 0 & 7 & 8 & 9 \\ 0 & 0 & 0 & 0 \end {pmatrix}\)
augment_columns, extend, matrix_augment, matrix_stack, stack_rows,
sub_matrix.
diagonal({mat\(_{1}\),mat\(_{2}\),
…,mat\(_{n}\)});28
mat\(_{1}\),mat\(_{2}\), …,mat\(_{n}\) | :- | each can be either a scalar expr or a square matrix. |
diagonalcreates a matrix that contains the input on the diagonal.
\(\mathcal {H} = \begin {pmatrix} 66 & 77 \\ 88 & 99 \end {pmatrix}\)
\(\texttt {diagonal}(\{\mathcal {A},x,\mathcal {H}\}) = \begin {pmatrix} 1 & 2 & 3 & 0 & 0 & 0 \\ 4 & 5 & 6 & 0 & 0 & 0 \\ 7 & 8 & 9 & 0 & 0 & 0 \\ 0 & 0 & 0 & x & 0 & 0 \\ 0 & 0 & 0 & 0 & 66 & 77 \\ 0 & 0 & 0 & 0 & 88 & 99 \end {pmatrix}\)
jordan_block.
extend(\(\mathcal {A}\),r,c,expr);
\(\mathcal {A}\) | :- | a matrix. |
\(r,c\) | :- | positive integers. |
expr | :- | algebraic expression or symbol. |
extendreturns a copy of \(\mathcal {A}\) that has been extended by \(r\) rows and \(c\) columns. The new entries
are made equal to expr.
\( \texttt {extend}(\mathcal {A},1,2,x) = \begin {pmatrix} 1 & 2 & 3 & x & x \\ 4 & 5 & 6 & x & x \\ 7 & 8 & 9 & x & x \\ x & x & x & x & x \end {pmatrix} \)
copy_into, matrix_augment, matrix_stack, remove_columns,
remove_rows.
find_companion(\(\mathcal {A}\),x);
\(\mathcal {A}\) | :- | a matrix. |
\(x\) | :- | the variable. |
Given a companion matrix, find_companionfinds the polynomial from which it was
made.
\(\mathcal {C} = \begin {pmatrix} 0 & 0 & 0 & -11 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 9 \\ 0 & 0 & 1 & -17 \end {pmatrix}\)
find_companion\((\mathcal {C},x) = x^4+17*x^3-9*x^2+11\)
companion.
get_columns(\(\mathcal {A}\),column_list);
\(\mathcal {A}\) | :- | a matrix. |
\(c\) | :- | either a positive integer or a list of positive integers. |
get_columnsremoves the columns of \(\mathcal {A}\) specified in column_listand returns them as
a list of column matrices.
get_rowsperforms the same task on the rows of \(\mathcal {A}\).
get_columns\((\mathcal {A},\{1,3\}) = \left \{ \begin {pmatrix} 1 \\ 4 \\ 7 \end {pmatrix}, \begin {pmatrix} 3 \\ 6 \\ 9 \end {pmatrix} \right \}\)
get_rows\((\mathcal {A},2) = \left \{ \begin {pmatrix} 4 & 5 & 6 \end {pmatrix} \right \}\)
augment_columns, stack_rows, sub_matrix.
gram_schmidt({vec\(_{1}\),vec\(_{2}\), …,vec\(_{n}\)});
29
vec\(_{1}\),vec\(_{2}\), …,vec\(_{n}\) | :- | linearly-independent vectors. Each vector must be written as a list, eg:{1,0,0}. |
gram_schmidtperforms the Gram-Schmidt orthonormalisation on the input vectors. It
returns a list of orthogonal normalised vectors.
gram_schmidt({{1,0,0},{1,1,0},{1,1,1}})=
{{1,0,0},{0,1,0},{0,0,1}}
gram_schmidt({{1,2},{3,4}})\(\displaystyle = \{\{ \frac {1}{{\sqrt {5}}} , \frac {2}{\sqrt {5}} \}, \{ \frac {2*\sqrt {5}}{5} , \frac {-\sqrt {5}}{5} \}\}\)
hermitian_tp(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix. |
hermitian_tpcomputes the hermitian transpose of \(\mathcal {A}\).
This is a matrix in which the \((i,j)\)th entry is the conjugate of the \((j,i)\)th entry of \(\mathcal {A}\).
\(\mathcal {J} = \begin {pmatrix} i+1 & i+2 & i+3 \\ 4 & 5 & 2 \\ 1 & i & 0 \end {pmatrix}\)
hermitian_tp\((\mathcal {J}) = \begin {pmatrix} -i+1 & 4 & 1 \\ -i+2 & 5 & -i \\-i+3 & 2 & 0 \end {pmatrix}\)
tp30 .
hessian(expr,variable_list);
expr | :- | a scalar expression. |
variable_list | :- | either a single variable or a list of variables. |
hessiancomputes the hessian matrix of expr w.r.t. the varibles in variable_list.
This is an \(n\times n\) matrix where \(n\) is the number of variables and the \((i,j)\)th entry is df(expr,variable_list(i),variable_list(j)).
\( \texttt {hessian}(x*y*z+x^2,\{w,x,y,z\}) = \begin {pmatrix} 0 & 0 & 0 & 0 \\ 0 & 2 & z & y \\ 0 & z & 0 & x \\ 0 & y & x & 0 \end {pmatrix} \)
df31 .
hilbert(square_size,expr);
square_size | :- | a positive integer. |
expr | :- | an algebraic expression. |
hilbertcomputes the square hilbert matrix of dimension square_size.
This is the symmetric matrix in which the \((i,j)\)th entry is \(1/(i+j-\texttt {expr})\).
\( \texttt {hilbert}(3,y+x) = \begin {pmatrix} \frac {-1}{x+y-2} & \frac {-1}{x+y-3} & \frac {-1}{x+y-4} \\ \frac {-1}{x+y-3} & \frac {-1}{x+y-4} & \frac {-1}{x+y-5} \\ \frac {-1}{x+y-4} & \frac {-1}{x+y-5} & \frac {-1}{x+y-6} \end {pmatrix} \)
mat_jacobian(expr_list,variable_list);
expr_list | :- | either a single algebraic expression or a list of algebraic expressions. |
variable_list | :- | either a single variable or a list of variables. |
mat_jacobiancomputes the jacobian matrix of expr_listw.r.t. variable_list.
This is a matrix whose entry at position \((i,j)\) is df(expr_list(i),variable_list(j)).
The matrix is \(n\times m\) where \(n\) is the number of variables and \(m\) the number of expressions.
mat_jacobian\((\{x^4,x*y^2,x*y*z^3\},\{w,x,y,z\}) =\)
\(\begin {pmatrix} 0 & 4*x^3 & 0 & 0 \\ 0 & y^2 & 2*x*y & 0 \\ 0 & y*z^3 & x*z^3 & 3*x*y*z^2 \end {pmatrix}\)
hessian, df32 .
NOTE: The function mat_jacobianused to be called just "jacobian" however us of that name was in conflict with another Reduce package.
jordan_block(expr,square_size);
expr | :- | an algebraic expression or symbol. |
square_size | :- | a positive integer. |
jordan_blockcomputes the square jordan block matrix \(\mathcal {J}\) of dimension
square_size.
The entries of \(\mathcal {J}\) are: \(\mathcal {J}(i,i) = \texttt {expr}\) for \(i=1,\ldots ,n\), \(\mathcal {J}(i,i+1) = 1\) for \(i=1,\ldots ,n-1\), and all other entries are 0.
jordan_block(x,5)\(= \begin {pmatrix} x & 1 & 0 & 0 & 0 \\ 0 & x & 1 & 0 & 0 \\ 0 & 0 & x & 1 & 0 \\ 0 & 0 & 0 & x & 1 \\ 0 & 0 & 0 & 0 & x \end {pmatrix}\)
diagonal, companion.
lu_decom(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix containing either numeric entries or imaginary entries with numeric coefficients. |
lu_decomperforms LU decomposition on \(\mathcal {A}\), ie: it returns {\(\mathcal {L,U}\)} where \(\mathcal {L}\) is a lower diagonal
matrix, \(\mathcal {U}\) an upper diagonal matrix and \(\mathcal {A} = \mathcal {LU}\).
Caution: The algorithm used can swap the rows of \(\mathcal {A}\) during the calculation. This means that \(\mathcal {LU}\) does not equal \(\mathcal {A}\) but a row equivalent of it. Due to this, lu_decomreturns {\(\mathcal {L,U}\),vec}. The call convert(\(\mathcal {A}\),vec)will return the matrix that has been decomposed, ie: \(\mathcal {LU} = \) convert(\(\mathcal {A}\),vec).
\( \mathcal {K} = \begin {pmatrix} 1 & 3 & 5 \\ -4 & 3 & 7 \\ 8 & 6 & 4 \end {pmatrix} \)
\(\texttt {first lu * second lu} = \begin {pmatrix} 8 & 6 & 4 \\ -4 & 3 & 7 \\ 1 & 3 & 5 \end {pmatrix}\)
\(\texttt {convert($\mathcal {K}$,third lu)} = \begin {pmatrix} 8 & 6 & 4 \\ -4 & 3 & 7 \\ 1 & 3 & 5 \end {pmatrix}\)
\( \mathcal {P} = \begin {pmatrix} i+1 & i+2 & i+3 \\ 4 & 5 & 2 \\ 1 & i & 0 \end {pmatrix} \)
\(\texttt {first lu * second lu} = \begin {pmatrix} 1 & i & 0 \\ 4 & 5 & 2 \\ i+1 & i+2 & i+3 \end {pmatrix}\)
\(\mathtt {convert(\mathcal {P},third lu}) = \begin {pmatrix} 1 & i & 0 \\ 4 & 5 & 2 \\ i+1 & i+2 & i+3 \end {pmatrix}\)
cholesky.
make_identity(square_size);
square_size | :- | a positive integer. |
make_identitycreates the identity matrix of dimension square_size.
make_identity(4) \(= \begin {pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end {pmatrix}\)
diagonal.
matrix_augment({mat\(_{1}\),mat\(_{2}\),
…,mat\(_{n}\)});33
mat\(_{1}\),mat\(_{2}\), …,mat\(_{n}\) | :- | matrices. |
matrix_augmentsticks the matrices in matrix_listtogether horizontally.
matrix_stacksticks the matrices in matrix_listtogether vertically.
matrix_augment\((\{\mathcal {A,A}\}) = \begin {pmatrix} 1 & 2 & 3 & 1 & 2 & 3 \\ 4 & 4 & 6 & 4 & 5 & 6 \\ 7 & 8 & 9 & 7 & 8 & 9 \end {pmatrix}\)
matrix_stack\((\{\mathcal {A,A}\}) = \begin {pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end {pmatrix}\)
augment_columns, stack_rows, sub_matrix.
matrixp(test_input);
test_input | :- | anything you like. |
matrixpis a boolean function that returns t if the input is a matrix and nil
otherwise.
matrixp(\(\mathcal {A}\)) = t
matrixp(doodlesackbanana) = nil
squarep, symmetricp.
minor(\(\mathcal {A}\),r,c);
\(\mathcal {A}\) | :- | a matrix. |
\(r,c\) | :- | positive integers. |
minorcomputes the \((r,c)\)th minor of \(\mathcal {A}\).
This is created by removing the \(r\)th row and the \(c\)th column from \(\mathcal {A}\).
\( \texttt {minor}(\mathcal {A},1,3) = \begin {pmatrix} 4 & 5 \\ 7 & 8 \end {pmatrix} \)
remove_columns, remove_rows.
mult_columns(\(\mathcal {A}\),column_list,expr);
\(\mathcal {A}\) | :- | a matrix. |
column_list | :- | a positive integer or a list of positive integers. |
expr | :- | an algebraic expression. |
mult_columnsreturns a copy of \(\mathcal {A}\) in which the columns specified in column_list have
been multiplied by expr.
mult_rowsperforms the same task on the rows of \(\mathcal {A}\).
mult_columns\((\mathcal {A},\{1,3\},x) = \begin {pmatrix} x & 2 & 3*x \\ 4*x & 5 & 6*x \\ 7*x & 8 & 9*x \end {pmatrix}\)
mult_rows\((\mathcal {A},2,10) = \begin {pmatrix} 1 & 2 & 3 \\ 40 & 50 & 60 \\ 7 & 8 & 9 \end {pmatrix}\)
add_to_columns, add_to_rows.
pivot(\(\mathcal {A}\),r,c);
\(\mathcal {A}\) | :- | a matrix. |
\(r,c\) | :- | positive integers such that \(\mathcal {A}(r,c) \neq 0\). |
pivotpivots \(\mathcal {A}\) about its \((r,c)\)th entry.
To do this, multiples of the r’th row are added to every other row in the matrix.
This means that the c’th column will be 0 except for the (r,c)’th entry.
\( \texttt {pivot}(\mathcal {A},2,3) = \begin {pmatrix} -1 & -0.5 & 0 \\ 4 & 5 & 6 \\ 1 & 0.5 & 0 \end {pmatrix} \)
rows_pivot.
pseudo_inverse(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix containing only real numeric entries. |
pseudo_inverse, also known as the Moore-Penrose inverse, computes the pseudo
inverse of \(\mathcal {A}\).
Given the singular value decomposition of \(\mathcal {A}\), i.e: \(\mathcal {A} = \mathcal {U} \Sigma \mathcal {V}^T\), then the pseudo inverse \(\mathcal {A}^{\dagger }\) is defined by \(\mathcal {A}^{\dagger } = \mathcal {V} \Sigma ^{\dagger } \mathcal {U}^{T}\). For the diagonal matrix \(\Sigma \), the pseudoinverse \(\Sigma ^{\dagger }\) is computed by taking the reciprocal of only the nonzero diagonal elements.
If \(\mathcal {A}\) is square and non-singular, then \(\mathcal {A}^{\dagger } = \mathcal {A}\). In general, however, \(\mathcal {A} \mathcal {A}^{\dagger } \mathcal {A} = \mathcal {A}\), and \(\mathcal {A}^{\dagger } \mathcal {A} \mathcal {A}^{\dagger } = \mathcal {A}^{\dagger }\).
Perhaps more importantly, \(\mathcal {A}^{\dagger }\) solves the following least-squares problem: given a rectangular matrix \(\mathcal {A}\) and a vector \(b\), find the \(x\) minimizing \(\|\mathcal {A}x - b\|_2\), and which, in addition, has minimum \(\ell _{2}\) (euclidean) Norm, \(\|x\|_2\). This \(x\) is \(\mathcal {A}^{\dagger } b\).
svd.
random_matrix(r,c,limit);
\(r,c\), limit | :- | positive integers. |
random_matrixcreates an \(r\times c\) matrix with random entries in the range \(-\text {limit} < \text {entry} < \text {limit}\).
imaginary | :- | if on, then matrix entries are \(x+iy\) where \(-\text {limit} < x,y < \text {limit}\). |
not_negative | :- | if on then \(0 < \text {entry} < \text {limit}\). In the imaginary case we have \(0<x,y<\text {limit}\). |
only_integer | :- | if on then each entry is an integer. In the imaginary case \(x,y\) are integers. |
symmetric | :- | if on then the matrix is symmetric. |
upper_matrix | :- | if on then the matrix is upper triangular. |
lower_matrix | :- | if on then the matrix is lower triangular. |
random_matrix\((3,3,10) = \begin {pmatrix} -4.729721 & 6.987047 & 7.521383 \\ - 5.224177 & 5.797709 & - 4.321952 \\ - 9.418455 & - 9.94318 & - 0.730980 \end {pmatrix}\)
on only_integer, not_negative, upper_matrix, imaginary;
remove_columns(\(\mathcal {A}\),column_list);
\(\mathcal {A}\) | :- | a matrix. |
column_list | :- | either a positive integer or a list of positive integers. |
remove_columnsremoves the columns specified in column_list from \(\mathcal {A}\).
remove_rowsperforms the same task on the rows of \(\mathcal {A}\).
remove_columns\((\mathcal {A},2) = \begin {pmatrix} 1 & 3 \\ 4 & 6 \\ 7 & 9 \end {pmatrix}\)
remove_rows\((\mathcal {A},\{1,3\}) = \begin {pmatrix} 4 & 5 & 6 \end {pmatrix}\)
minor.
See: column_dim.
rows_pivot(\(\mathcal {A}\),r,c,{row_list});
\(\mathcal {A}\) | :- | a matrix. |
r,c | :- | positive integers such that \(\mathcal {A}\)(r,c) neq 0. |
row_list | :- | positive integer or a list of positive integers. |
rows_pivotperforms the same task as pivotbut applies the pivot only to the rows
specified in row_list.
\(\mathcal {N} = \begin {pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\1 & 2 & 3 \\ 4 & 5 & 6 \end {pmatrix}\)
rows_pivot\((\mathcal {N},2,3,\{4,5\}) = \begin {pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \\ -0.75 & 0 & 0.75 \\ -0.375 & 0 & 0.375 \end {pmatrix}\)
pivot.
\(\langle \)max/min\(\rangle \) | :- | either max or min (signifying maximise and minimise). |
\(\langle \)objective function\(\rangle \) | :- | the function you are maximising or minimising. |
\(\langle \)linear inequalities\(\rangle \) | :- | the constraint inequalities. Each one must be of the form \(\langle \)linear combination of variables\(\rangle \) \(\langle \)compop\(\rangle \) \(\langle \)number\(\rangle \) where \(\langle \)compop\(\rangle \) is one of <=, =, >=. |
\(\langle \)bounds\(\rangle \) | :- | bounds on the variables as specified for the LP file format. Each bound has one of the forms \(l\leq v\), \(v\leq u\), or \(l\leq v\leq u\), where \(v\) is a variable and \(l\), \(u\) are numbers or infinity or -infinity. |
simplexapplies the revised simplex algorithm to find the optimal (either maximum or
minimum) value of the objective function under the linear inequality constraints.
It returns {optimal value, {values of variables at this optimum}}.
The {bounds}argument is optional and admissible only when the switch fastsimplexis on, which is the default. Without a {bounds}argument, the algorithm assumes that all the variables are non-negative.
By default, simplexthrows an error if a problem has no feasible solution. However, if the switch noerrsimplexis turned on (it is off by default) then simplexreturns the empty list {}, which facilitates use of simplexas a subroutine within other code.
(assuming on rounded)
simplex(max, 10x+5y+5.5z, {5x+3z<=200,0.2x+0.1y+0.5z<=12, 0.1x+0.2y+0.3z<=9,30x+10y+50z<=1500}); {525.0,{x = 40.0,y = 25.0,z = 0}} simplex(max, x+y, {x>=10,y>=20,x+y<=25}); ***** Error in simplex: Problem has no feasible solution. on noerrsimplex; simplex(max, x+y, {x>=10,y>=20,x+y<=25}); {}
squarep(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix. |
squarepis a boolean function that returns t if the matrix is square and nil
otherwise.
\(\mathcal {L} = \begin {pmatrix} 1 & 3 & 5 \end {pmatrix}\)
\(\texttt {squarep}(\mathcal {A}) = \texttt {t}\)
\(\texttt {squarep}(\mathcal {L}) = \texttt {nil}\)
matrixp, symmetricp.
See: augment_columns.
sub_matrix(\(\mathcal {A}\),row_list,column_list);
\(\mathcal {A}\) | :- | a matrix. |
row_list, column_list | :- | either a positive integer or a list of positive integers. |
sub_matrixproduces the matrix consisting of the intersection of the rows specified in
row_list and the columns specified in column_list.
sub_matrix\((\mathcal {A},\{1,3\},\{2,3\}) = \begin {pmatrix} 2 & 3 \\ 8 & 9 \end {pmatrix}\)
augment_columns, stack_rows.
svd(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix containing only real numeric entries. |
svdcomputes the singular value decomposition of \(\mathcal {A}\). If \(A\) is an \(m\times n\) real matrix of (column) rank
\(r\), svdreturns the 3-element list {\(\mathcal {U},\Sigma ,\mathcal {V}\)} where \(\mathcal {A} = \mathcal {U} \Sigma \mathcal {V}^T\).
Let \(k=\min (m,n)\). Then \(U\) is \(m\times k\), \(V\) is \(n\times k\), and and \(\Sigma = \mbox {diag}(\sigma _{1}, \ldots ,\sigma _{k})\), where \(\sigma _{i}\ge 0\) are the singular values of \(\mathcal {A}\); only \(r\) of these are non-zero. The singular values are the non-negative square roots of the eigenvalues of \(\mathcal {A}^T \mathcal {A}\).
\(\mathcal {U}\) and \(\mathcal {V}\) are such that \(\mathcal {UU}^T = \mathcal {VV}^T = \mathcal {V}^T \mathcal {V} = \mathcal {I}_k\).
Note: there are a number of different definitions of SVD in the literature, in some of which \(\Sigma \) is square and \(U\) and \(V\) rectangular, as here, but in others \(U\) and \(V\) are square, and \(\Sigma \) is rectangular.
\( \mathcal {Q} = \begin {pmatrix} 1 & 3 \\ -4 & 3 \\ 3 & 6 \end {pmatrix}\)
\(\begin {aligned} \mathtt {svd(\mathcal {Q})} = & \left \{ \begin {pmatrix} 0.0236042 & 0.419897 \\ -0.969049 & 0.232684 \\ 0.245739 & 0.877237 \end {pmatrix}, \begin {pmatrix} 4.83288 & 0 \\ 0 & 7.52618 \end {pmatrix}, \right . \\ & \left . \: \; \, \begin {pmatrix} 0.959473 & 0.281799 \\ - 0.281799 & 0.959473 \end {pmatrix} \right \} \\[2mm] \mathtt {svd(TP(\mathcal {Q}))} = & \left \{ \begin {pmatrix} 0.959473 & 0.281799 \\ - 0.281799 & 0.959473 \end {pmatrix}, \begin {pmatrix} 4.83288 & 0 \\ 0 & 7.52618 \end {pmatrix}, \right . \\ & \left . \: \; \, \begin {pmatrix} 0.0236042 & 0.419897 \\ -0.969049 & 0.232684 \\ 0.245739 & 0.877237 \end {pmatrix} \right \} \end {aligned}\)
swap_columns(\(\mathcal {A}\),c1,c2);
\(\mathcal {A}\) | :- | a matrix. |
c1,c1 | :- | positive integers. |
swap_columnsswaps column c1 of \(\mathcal {A}\) with column c2.
swap_rowsperforms the same task on 2 rows of \(\mathcal {A}\).
swap_columns\((\mathcal {A},2,3) = \begin {pmatrix} 1 & 3 & 2 \\ 4 & 6 & 5 \\ 7 & 9 & 8 \end {pmatrix}\)
swap_entries.
swap_entries(\(\mathcal {A}\),{r1,c1},{r2,c2});
\(\mathcal {A}\) | :- | a matrix. |
r1,c1,r2,c2 | :- | positive integers. |
swap_entriesswaps \(\mathcal {A}\)(r1,c1) with \(\mathcal {A}\)(r2,c2).
swap_entries\((\mathcal {A},\{1,1\},\{3,3\}) = \begin {pmatrix} 9 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 1 \end {pmatrix}\)
swap_columns, swap_rows.
symmetricp(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix. |
symmetricpis a boolean function that returns t if the matrix is symmetric and nil
otherwise.
\(\mathcal {M} = \begin {pmatrix} 1 & 2 \\ 2 & 1 \end {pmatrix}\)
\(\texttt {symmetricp}(\mathcal {A}) = \texttt {nil}\) \(\texttt {symmetricp}(\mathcal {M}) = \texttt {t}\)
matrixp, squarep.
toeplitz({expr\(_{1}\),expr\(_{2}\), …,expr\(_{{\tt n}}\)});
34
expr\(_{1}\),expr\(_{2}\), …,expr\(_{{\tt n}}\) | :- | algebraic expressions. |
toeplitzcreates the toeplitz matrix from the expression list.
This is a square symmetric matrix in which the first expression is placed on the diagonal and the i’th expression is placed on the (i-1)’th sub and super diagonals.
It has dimension n where n is the number of expressions.
\( \texttt {toeplitz}(\{w,x,y,z\}) = \begin {pmatrix} w & x & y & z \\ x & w & x & y \\ y & x & w & x \\ z & y & x & w \end {pmatrix} \)
triang_adjoint(\(\mathcal {A}\));
\(\mathcal {A}\) | :- | a matrix. |
triang_adjointcomputes the triangularizing adjoint \(\mathcal {F}\) of matrix \(\mathcal {A}\) due to the algorithm of Arne Storjohann. \(\mathcal {F}\) is lower triangular matrix and the resulting matrix \(\mathcal {T}\) of \(\mathcal {F * A = T}\) is upper triangular with the property that the \(i\)-th entry in the diagonal of \(\mathcal {T}\) is the determinant of the principal \(i\)-th submatrix of the matrix \(\mathcal {A}\).
triang_adjoint\((\mathcal {A}) = \begin {pmatrix} 1 & 0 & 0 \\ -4 & 1 & 0 \\ -3 & 6 & -3 \end {pmatrix}\)
\(\mathcal {F} * \mathcal {A} = \begin {pmatrix} 1 & 2 & 3 \\ 0 & -3 & -6 \\ 0 & 0 & 0 \end {pmatrix}\)
vandermonde({expr\(_{1}\),expr\(_{2}\), …,expr\(_{{\tt n}}\)});
35
expr\(_{1}\),expr\(_{2}\), …,expr\(_{{\tt n}}\) | :- | algebraic expressions. |
Vandermondecreates the Vandermonde matrix from the expression list. This is the
square matrix in which the \((i,j)\)th entry is \(\text {expr}_i^{(j-1)}\). It has dimension \(n\), where \(n\) is the number of
expressions.
\( \texttt {vandermonde}(\{x,2*y,3*z\}) = \begin {pmatrix} 1 & x & x^2 \\ 1 & 2*y & 4*y^2 \\ 1 & 3*z & 9*z^2 \end {pmatrix} \)
kronecker_product(\(M_1,M_2\))
\(M_1,M_2\) | :- | Matrices |
kronecker_productcreates a matrix containing the Kronecker product (also called
direct productor tensor product) of its arguments.
a1 := mat((1,2),(3,4),(5,6))$ a2 := mat((1,1,1),(2,z,2),(3,3,3))$ kronecker_product(a1,a2);
\( \begin {pmatrix} 1 & 1 & 1 & 2 & 2 & 2 \\ 2 & z & 2 & 4 &2*z &4 \\ 3 & 3 & 3 & 6 & 6 &6 \\ 3 & 3 & 3 & 4 & 4 &4 \\ 6 & 3*z& 6 & 8 &4*z &8 \\ 9 & 9 & 9 & 12 &12 &12\\ 5 & 5 & 5 & 6 & 6 &6 \\ 10 &5*z& 10& 12 &6*z &12 \\ 15 &15 & 15& 18 &18 &18 \end {pmatrix} \)
Many of the ideas for this package came from the Maple Linalg package.
The algorithms for cholesky, lu_decom, and svdare taken from the book Linear Algebra - J.H. Wilkinson & C. Reinsch.
The gram_schmidtcode comes from Karin Gatermann’s Symmetry package for REDUCE.
Up | Next | Prev | PrevTail | Front |