Matrices Module
This module implements algorithms for matrices on rational numbers. To this end, it first implements a type for rational and complex numbers, and based on these, algorithms for inverting a matrix, for solving linear (in)equation systems, and for computing eigenvalues (e.g. to solve linear differential equation systems) are provided.
Types
Type | Description |
A general simplex problem is given by a set of linear ineqalities, i.e. lw[i] <= sum{j=0..maxCol} m[i,j] * x[j] <= up[i] for i=0..maxRows. The initial linear equations are used to define "basic variables" y[i] in terms of the nonbasic variables x[j]. Since the role of basic and nonbasic variables is changed during the simplex algorithm, we use maps basic and nonbasic to map indices to the current variable, hence, e.g., basic[i]=j means that variable j is currently the i-th basic variable. Variables are generally identified with integers that are mapped via idx to their names. The arrays lower and upper denote lower and upper bounds, if these exists for the variables, and eval denotes their current value in the solution process. |
|
This data structure represents a linear inequality system used for Fourier-Motzkin variable elimination. The linear inequality system is then simply an array of this record type that holds an optional lower and upper bound and a vector of coefficients for the linear expression sum_{i=0..L-1} vector[i] * x[i] for variables x[i]. |
|
Complex numbers cplx(re,im) are constructed by two floating point numbers which are the real and imaginary part of the complex number. |
|
Rational numbers rat(n,d) are constructed by two integers, the nominator n and the denominator d. We interprete numbers rat(0,d) as zero (where d!=0), and rat(n,0) as infinity. |
Functions and values
Function or value | Description |
Full Usage:
CompanionMatrixOfPolynomial p
Parameters:
double[]
Returns: float[,]
|
Given a polynomial p(x) = sum{i=0..n} a[i]*x^i, compute its companion matrix, i.e., a matrix whose characteristic polynomial is p. One such matrix is the one that is zero except for the entries m[i+1,i]:=1 and m[i,n-1]:=-a[i]/a[n]. This way, computing the roots of the polynomial can be done by computing the eigenvalues of its matrix.
|
Full Usage:
DecomposeQR printSteps ostr dimRow dimCol m
Parameters:
bool
ostr : TextWriter
dimRow : int
dimCol : int
m : double[,]
Returns: float[,] * double[,]
|
This function computes a QR-decomposition of a given matrix by means of Givens rotations, i.e., for a given matrix m, it computes an orthogonal matrix q (the column vectors of q are orthogonal unit length vectors) and an upper triangular matrix r such that m=q*r holds. Despite the real size of the matrix m, it will only consider the upper left submatrix of size dimRow x dimCol. Note that the inverse of an orthogonal matrix is its transpose.
|
Full Usage:
DecomposeQRC printSteps ostr dimRow dimCol m
Parameters:
bool
ostr : TextWriter
dimRow : int
dimCol : int
m : cplx[,]
Returns: cplx[,] * cplx[,]
|
This function computes a QR-decomposition of a given matrix by means of Givens rotations, i.e., for a given matrix m, it computes an orthogonal matrix q (the column vectors of q are orthogonal unit length vectors) and an upper triangular matrix r such that m=q*r holds. Despite the real size of the matrix m, it will only consider the upper left submatrix of size dimRow x dimCol. Note that the inverse of an orthogonal matrix is its transpose. See the comments on DecomposeQR to understand the implementation.
|
Full Usage:
EigenvaluesDoubleShift printSteps ostr m
Parameters:
bool
ostr : TextWriter
m : double[,]
Returns: cplx array option
|
This function computes an iteration for approximating the eigenvalues of a given matrix m over real numbers. To this end, we define the QR iteration with double shifts as follows:
|
Full Usage:
EigenvaluesLinearShiftC printSteps ostr m
Parameters:
bool
ostr : TextWriter
m : cplx[,]
Returns: cplx array option
|
This function computes an iteration for approximating the eigenvalues of a given matrix m over complex numbers. To this end, we define the QR iteration with linear shifts as follows:
|
Full Usage:
FourierMotzkin printSteps ostr vars liq
Parameters:
bool
ostr : TextWriter
vars : string[]
liq : LinearInequality[]
Returns: rat array option
|
This function implements the Fourier-Motzkin variable elimination to determine a solution of a linear inequality system.
|
Full Usage:
GaussJordan printSteps ostr m
Parameters:
bool
ostr : TextWriter
m : rat[,]
|
GaussJordan transforms the given matrix into a diagonal matrix of using in-place operations that add multiples of another row to a row. It is the basis for solving linear equation systems or for inverting matrices.
|
Full Usage:
InverseMatrixRat printSteps ostr m
Parameters:
bool
ostr : TextWriter
m : rat[,]
Returns: rat[,]
|
InverseMatrixRat inverts a given matrix in that it concatenates the unit matrix and applies then the Gauss-Jordan algorithm. The given matrix can be inverted if the left half is finally the unit matrix, and in that case the right half contains the inverse of the given matrix.
|
Full Usage:
LinInequality2Simplex liq
Parameters:
LinearInequality array
Returns: GeneralSimplexProblem
|
Convert a linear inequality system to a GeneralSimplexProblem
|
|
|
|
This function is to be called by the html page. It expects in the qscoll for the key "MatrixTask" one of the following DecomposeQR, EigenvaluesShift EigenvaluesDoubleShift, GaussJordan, Inverse, LinearEquationSystem and will then call one of the functions of this module with the appropriate html output.
|
|
Parse a GeneralSimplexProblem from a string as the following let s = "-oo <= 5 1 <= 5 -oo <= 2 1 <= 4 -oo <= 1/2 1 <= 2 -oo <= 1/5 1 <= 1 1 <= 1/2 1 <= +oo 3/2 <= 1 1 <= +oo"
|
|
|
Full Usage:
ParseMatrixFloat s
Parameters:
string
Returns: float[,]
|
Parse a matrix from a string where rows are separated by newline symbols or semicolons, and entries within a row are separated by blanks or tabs. The entries of the string are expected as floating point numbers.
|
|
Parse a matrix from a string where rows are separated by newline symbols or semicolons, and entries within a row are separated by blanks or tabs. The entries of the string are expected as rational numbers.
|
|
This function is to be called by the html page. It will parse a polynomial and will compute its roots via the eigenvalues of its companion matrix
|
Full Usage:
PrintGeneralSimplexProblemASCII ostr gs
Parameters:
TextWriter
gs : GeneralSimplexProblem
|
print a GeneralSimplex in ASCII
|
Full Usage:
PrintGeneralSimplexProblemLatex ostr gs
Parameters:
TextWriter
gs : GeneralSimplexProblem
|
print a GeneralSimplex in Latex
|
Full Usage:
PrintGeneralSimplexProblemMathML ostr gs
Parameters:
TextWriter
gs : GeneralSimplexProblem
|
print a GeneralSimplex in MathML
|
Full Usage:
PrintLinearInequalitySystem ostr vars liq
Parameters:
TextWriter
vars : string[]
liq : LinearInequality[]
|
Print a linear inequality system to output stream ostr. The additional array vars holds the names of the variables in the linear inequality system.
|
Full Usage:
PrintLinearInequalitySystemLatex ostr vars liq
Parameters:
TextWriter
vars : string[]
liq : LinearInequality[]
|
Print a linear inequality system to output stream ostr in Latex. The additional array vars holds the names of the variables in the linear inequality system.
|
Full Usage:
PrintLinearInequalitySystemMathML ostr vars liq
Parameters:
TextWriter
vars : string[]
liq : LinearInequality[]
|
Print a linear inequality system to output stream ostr in MathML. The additional array vars holds the names of the variables in the linear inequality system.
|
|
print a matrix to output stream ostr
|
|
|
Full Usage:
PrintMatrixCplxToLatex ostr indent m
Parameters:
TextWriter
indent : string
m : cplx[,]
|
|
|
|
|
print a matrix to output stream ostr
|
|
|
Full Usage:
PrintMatrixFloatToLatex ostr indent m
Parameters:
TextWriter
indent : string
m : double[,]
|
|
|
|
|
print a matrix to output stream ostr
|
|
|
Full Usage:
PrintMatrixRatToLatex ostr indent m
Parameters:
TextWriter
indent : string
m : rat[,]
|
|
|
|
Full Usage:
SimplexIntSolve optMaxCalls printSteps ostr gs
Parameters:
int option
printSteps : bool
ostr : TextWriter
gs : GeneralSimplexProblem
Returns: bool option
|
SimplexIntSolve solves the integer linear programming problem given by gs. Essentially, it first calls SimplexRatSolve to determine a rational solution if there is one, and narrows then the constraints to integer bounds until an integer solution is found. It therefore solves an integer linear programming problem which is known to be NP-complete. Note however, that it is possible that this method will not terminate since there are inequation systems with unbounded rational solutions that have no integer solutions. For this reason, one can specify an optional maximal number of recursion steps, if that is reached, the function returns None, otherwise, it will find a solution and
|
Full Usage:
SimplexRatSolve printSteps ostr gs
Parameters:
bool
ostr : TextWriter
gs : GeneralSimplexProblem
Returns: bool
|
Simplex algorithm as decision procedure: Given a general Simplex problem (see the data type of this module, this function performs the required pivoting steps to determine a variable assignment in the contained eval component that satisfies the linear equation system of the tableau and also the constraints. It returns true if a solution has been found, and false if there is no solution on the rational numbers. The computations are done in-place in the given gs. Note that the Simplex algorithm may have an exponential runtime (which shows up rarely in practise), but it is known that the problem can be decided on the rational numbers in polynomial time (Kharmarkar's algorithm, and Kachian's ellipsoid method).
|
|
This function is to be called by a teaching tool's html page. It will parse a general simplex problem, will solve it and print the results to the html page.
|
Full Usage:
SolveLinearEquationSystem printSteps ostr m
Parameters:
bool
ostr : TextWriter
m : rat[,]
Returns: Set<int> * Set<int> * rat[,]
|
Solves a linear equation system A*x=0 or A*x=b, where in the first case the given matrix is just A, and in the second case, the given matrix should be matrix (A|b). The result is a tuple (defVarSet,parVarSet,kern) where defVarSet is the set of variable indices that are defined in terms of the other ones contained in the set of parameters parVarSet, and kern is a matrix whose image is the kernel of the given matrix m. For the homogeneous equation system A*x=0, this means that all linear combinations of the column vectors of kern are solutions, and there is just the trivial solution x=0 iff the kernel is a dimCol x 0 matrix. For the inhomogeneous equation system A*x=b, there are no solutions if the variable associated with the rightmost column is a defined variable. Otherwise, the solutions of the inhomogeneous system are obtained from the solutions of the homogeneous system by setting the last parameter -1 and removing the last rows in the kernel's column vectors.
|
Full Usage:
TensorProduct a b
Parameters:
float[][]
b : float[][]
Returns: float[][]
|
tensor product
|
|
|
|
|
|
|
|
Perform a LU-decomposition of the given matrix where the matrices L and U are stored in the given matrix (L has an additional diagonal of 1s).
|
|
|
|
Perform a LUP-decomposition of the given matrix where the matrices L and U are stored in the given matrix (L has an additional diagonal of 1s) and the permutation matrix is returned as a permutation.
|
|
|
|