Class Matrix
java.lang.Object
ch.nolix.base.math.algebra.Matrix
-
Method Summary
Modifier and TypeMethodDescriptionAdds the given matrix to the currentMatrix.appendAtBottom(double... rowValues) Appends a new row with the given row values at the bottom of the currentMatrix.appendAtRight(Matrix matrix) Appends the given matrix at the right of the currentMatrix.static MatrixcreateIdendityMatrixWithLength(int length) static MatrixcreateQuadraticMatrixWithOnesAndLength(int length) The values of the createdMatrixwill be all 1.0static MatrixcreateRandomQuadraticMatrixWithLength(int length) The values of the createdMatrixwill be all a whole random number in [0, 99].static MatrixcreateRandomQuadraticMatrixWithRowCountAndColumnCount(int rowCount, int columnCount) The values of the createdMatrixwill be all a whole random number in [0, 99].booleangetClone()intVector[]getMatrixWithFirstColumns(int columnCount) getMatrixWithLastColumns(int columnCount) getMinimalFactorMatrix(Matrix solutionMatrix) This method implements the least squares algorithm.getProduct(Matrix matrix) intgetRank()intVector[]intgetSize()double[]doublegetTrace()doublegetValue(int rowIndex, int columnIndex) inthashCode()booleanhasSameSize(Matrix matrix) booleanbooleanbooleanmultiply(double factor) Multiplies the currentMatrixwith the given factor.multiplyRow(int rowIndex, double factor) Multiplies the row with the given row index with the given factor.Removes the zero rows of the currentMatrixusing an epsilonsetAllValuesTo(double value) Sets the values of the currentMatrixto the given value.setDiagonalValuesTo(double value) Sets the diagonal values of the currentMatrixto the given value.setValue(int rowIndex, int columnIndex, double value) Sets the value in the row with the given row index and the column with the given column index.setValues(double... values) Sets the values of the currentMatrix.swapRows(int row1Index, int row2Index) Swaps the rows with the given indexes.toString()toVector()Transforms the first part of the currentMatrixto an identity matrix.Transforms the currentMatrixto an equivalent upper left matrix.Transposes the currentMatrix.static MatrixwithOnesAndRowCountAndColumnCount(int rowCount, int columnCount) The values of the createdMatrixwill be all 1.0static MatrixwithRowCountAndColumnCount(int rowCount, int columnCount) static MatrixwithRowCountAndColumnCountAndDefaultValue(int rowCount, int columnCount, double defaultValue)
-
Method Details
-
createIdendityMatrixWithLength
- Parameters:
length-- Returns:
- a new identity
Matrixwith the given length. - Throws:
RuntimeException- if the given length is not positive.
-
createQuadraticMatrixWithOnesAndLength
The values of the createdMatrixwill be all 1.0- Parameters:
length-- Returns:
- a new
Matrixwith the given length. - Throws:
RuntimeException- if the given length is not positive.
-
createRandomQuadraticMatrixWithLength
-
createRandomQuadraticMatrixWithRowCountAndColumnCount
public static Matrix createRandomQuadraticMatrixWithRowCountAndColumnCount(int rowCount, int columnCount) The values of the createdMatrixwill be all a whole random number in [0, 99].- Parameters:
rowCount-columnCount-- Returns:
- a new
Matrixwith the given number of rows and the given number of columns. - Throws:
RuntimeException- if the given rowCount is not positive.RuntimeException- if the given columnCount is not positive.
-
withOnesAndRowCountAndColumnCount
The values of the createdMatrixwill be all 1.0- Parameters:
rowCount-columnCount-- Returns:
- a new
Matrixwith the given number of rows and the given number of columns. - Throws:
RuntimeException- if the given rowCount is not positive.RuntimeException- if the given columnCount is not positive.
-
withRowCountAndColumnCount
- Parameters:
rowCount-columnCount-- Returns:
- a new
Matrixwith the given rowCount and columnCount. - Throws:
RuntimeException- if the given rowCount is not positive.RuntimeException- if the given columnCount is not positive.
-
withRowCountAndColumnCountAndDefaultValue
public static Matrix withRowCountAndColumnCountAndDefaultValue(int rowCount, int columnCount, double defaultValue) - Parameters:
rowCount-columnCount-defaultValue-- Returns:
- a new
Matrixwith the given rowCount and columnCount. The values of the matrix will be set to the given defaultValue. - Throws:
RuntimeException- if the given rowCount is not positive.RuntimeException- if the given columnCount is not positive.
-
add
Adds the given matrix to the currentMatrix.- Parameters:
matrix-- Returns:
- the current
Matrix. - Throws:
UnequalArgumentException- if the given matrix has not as many rows as the currentMatrix.UnequalArgumentException- if the given matrix has not as many columns as the currentMatrix.
-
appendAtRight
-
appendAtBottom
Appends a new row with the given row values at the bottom of the currentMatrix.- Parameters:
rowValues-- Returns:
- the current
Matrix. - Throws:
RuntimeException- if not as many row values are given than the number of columns of the currentMatrix.
-
equals
-
getColumnCount
public int getColumnCount()- Returns:
- the number of columns of the current
Matrix
-
getClone
-
getColumnVectors
-
getInverse
- Returns:
- the inverse matrix of the current
Matrix. - Throws:
RuntimeException- if the currentMatrixis not regular.
-
getMatrixWithFirstColumns
- Parameters:
columnCount-- Returns:
- a matrix with the first columns of the current
Matrix. - Throws:
RuntimeException- if the given column count is not valid.
-
getMatrixWithLastColumns
- Parameters:
columnCount-- Returns:
- a matrix with the last columns of the current
Matrix. - Throws:
RuntimeException- if the given column count is not valid.
-
getMinimalFactorMatrix
This method implements the least squares algorithm.- Parameters:
solutionMatrix-- Returns:
- a matrix A so that the matrix X*A-Y is minimal. -X is the current
Matrix. -Y is the given solution matrix. The following formula is used: A = (X_t*X)^-1*X_t*Y. - Throws:
UnequalArgumentException- if the given solution matrix has not 1 column.UnequalArgumentException- if the given solution matrix has not as many rows as the currentMatrix.
-
getProduct
- Parameters:
matrix-- Returns:
- the product of the current
Matrixand the given matrix. - Throws:
UnequalArgumentException- if the given matrix has not as many rows as the currentMatrixcolumns has.
-
getPseudoInverse
- Returns:
- a pseudo inverse matrix of the current
Matrix. - Throws:
RuntimeException- if the currentMatrixis not quadratic.
-
getRank
public int getRank()- Returns:
- the rank of the current
Matrix. - Throws:
RuntimeException- if the currentMatrixis not quadratic.
-
getRowCount
public int getRowCount()- Returns:
- the number of rows of the current
Matrix
-
getRowVectors
-
getSize
public int getSize()- Returns:
- the size of the current
Matrix
-
getSolutionAsExtendedMatrix
public double[] getSolutionAsExtendedMatrix()- Returns:
- the solution when the current
Matrixis an extended matrix of a linear equation system
-
getSum
- Parameters:
matrix-- Returns:
- the matrix that is the sum of the current
Matrixand the given matrix - Throws:
UnequalArgumentException- if the given matrix has not the same size as the currentMatrix
-
getTransposed
-
getTrace
public double getTrace()- Returns:
- the trace of the current
Matrix. - Throws:
RuntimeException- if the currentMatrixis not quadratic.
-
getValue
public double getValue(int rowIndex, int columnIndex) - Parameters:
rowIndex-columnIndex-- Returns:
- the value in the row with the given row index and the column with the given column index.
- Throws:
RuntimeException- if the currentMatrixdoes not contain a row with the given row index.RuntimeException- if the currentMatrixdoes not contain a column with the given column index.
-
hashCode
-
hasSameSize
-
isIdentityMatrix
public boolean isIdentityMatrix()- Returns:
- true if the current
Matrixis an identity matrix, false otherwise.
-
isQuadratic
public boolean isQuadratic()- Returns:
- true if the current
Matrixis a quadratic matrix, false otherwise.
-
isRegular
public boolean isRegular()- Returns:
- true if the current
Matrixis regular, false otherwise.
-
multiply
-
multiplyRow
Multiplies the row with the given row index with the given factor.- Parameters:
rowIndex-factor-- Returns:
- the current
Matrix. - Throws:
RuntimeException- if the currentMatrixdoes not contain a row with the given row index.
-
removeZeroRows
-
setAllValuesTo
-
setValues
Sets the values of the currentMatrix.- Parameters:
values-- Returns:
- the current
Matrix. - Throws:
RuntimeException- if not as many values are given as the currentMatrixcontains.
-
setValue
Sets the value in the row with the given row index and the column with the given column index.- Parameters:
rowIndex-columnIndex-value-- Returns:
- the current
Matrix. - Throws:
RuntimeException- if the currentMatrixdoes not contain a row with the given row index.RuntimeException- if the currentMatrixdoes not contain a column with the given column index.
-
setDiagonalValuesTo
Sets the diagonal values of the currentMatrixto the given value.- Parameters:
value-- Returns:
- the current
Matrix - Throws:
RuntimeException- if the currentMatrixis not quadratic
-
swapRows
Swaps the rows with the given indexes.- Parameters:
row1Index-row2Index-- Returns:
- the current
Matrix. - Throws:
RuntimeException- if the currentMatrixdoes not have a row with the given row1 index.RuntimeException- if the currentMatrixdoes not have a row with the given row1 index.
-
toPolynom
- Returns:
- a polynom representation of the current
Matrix - Throws:
RuntimeException- if the currentMatrixdoes not represent aPolynom.
-
toVector
- Returns:
- a vector representation of the current
Matrix - Throws:
RuntimeException- if the currentMatrixdoes not represent aVector.
-
tranformFirstPartToIdentityMatrix
Transforms the first part of the currentMatrixto an identity matrix.- Returns:
- the current
Matrix - Throws:
RuntimeException- if the currentMatrixhas more rows than columns.RuntimeException- if the currentMatrixhas linear depending rows.
-
transformToEquivalentUpperLeftMatrix
-
toString
-
transpose
-