Klasse Matrix

java.lang.Object
ch.nolix.core.math.algebra.Matrix

public final class Matrix extends Object
A Matrix represents a mathematical matrix of doubles. A Matrix has at least 1 row and 1 column. All comparisons a Matrix does are approximative. For example: -The second row of the matrix [4, 5; 0.000000001, 0] is interpreted as a zero row. -The matrix [0.9999999999, 0; 0, 1] is interpreted as a identity matrix.
Version:
2016-02-01
Autor:
Silvan Wyss
  • Konstruktordetails

    • Matrix

      public Matrix(int size)
      Creates a new Matrix with the given size. The values of the Matrix will be all 0.0.
      Parameter:
      size -
      Löst aus:
      NonPositiveArgumentException - if the given size is not positive.
    • Matrix

      public Matrix(int rowCount, int columnCount)
      Creates a new Matrix with the given number of rows and the given number of columns.
      Parameter:
      rowCount -
      columnCount -
      Löst aus:
      NonPositiveArgumentException - if the given row count is not positive.
      NonPositiveArgumentException - if the given column count is not positive.
    • Matrix

      public Matrix(int rowCount, int columnCount, double value)
      Creates a new Matrix with the given number of rows and columns. The values of the matrix will be all set to the given value.
      Parameter:
      rowCount -
      columnCount -
      value -
      Löst aus:
      NonPositiveArgumentException - if the given rowCount is not positive.
      NonPositiveArgumentException - if the given columnCount is not positive.
  • Methodendetails

    • createIdendityMatrix

      public static Matrix createIdendityMatrix(int size)
      Parameter:
      size -
      Gibt zurück:
      a new identity Matrix with the given size.
      Löst aus:
      NonPositiveArgumentException - if the given size is not positive.
    • createMatrixOfOnes

      public static Matrix createMatrixOfOnes(int size)
      The values of the created Matrix will be all 1.0
      Parameter:
      size -
      Gibt zurück:
      a new Matrix with the given size.
      Löst aus:
      NonPositiveArgumentException - if the given size is not positive.
    • createMatrixOfOnes

      public static Matrix createMatrixOfOnes(int rowCount, int columnCount)
      The values of the created Matrix will be all 1.0
      Parameter:
      rowCount -
      columnCount -
      Gibt zurück:
      a new Matrix with the given number of rows and the given number of columns.
      Löst aus:
      NonPositiveArgumentException - if the given rowCount is not positive.
      NonPositiveArgumentException - if the given columnCount is not positive.
    • createRandomMatrix

      public static Matrix createRandomMatrix(int size)
      The values of the created Matrix will be all a whole random number in [0, 99].
      Parameter:
      size -
      Gibt zurück:
      a new Matrix with the given size.
    • createRandomMatrix

      public static Matrix createRandomMatrix(int rowCount, int columnCount)
      The values of the created Matrix will be all a whole random number in [0, 99].
      Parameter:
      rowCount -
      columnCount -
      Gibt zurück:
      a new Matrix with the given number of rows and the given number of columns.
      Löst aus:
      NonPositiveArgumentException - if the given rowCount is not positive.
      NonPositiveArgumentException - if the given columnCount is not positive.
    • add

      public Matrix add(Matrix matrix)
      Adds the given matrix to the current Matrix.
      Parameter:
      matrix -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      UnequalArgumentException - if the given matrix has not as many rows as the current Matrix.
      UnequalArgumentException - if the given matrix has not as many columns as the current Matrix.
    • appendAtRight

      public Matrix appendAtRight(Matrix matrix)
      Appends the given matrix at the right of the current Matrix.
      Parameter:
      matrix -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      UnequalArgumentException - if the given matrix has not as many rows as the current Matrix.
    • appendRowAtBottom

      public Matrix appendRowAtBottom(double rowValue, double... rowValues)
      Appends a new row with the given row values on the bottom of the current Matrix.
      Parameter:
      rowValue -
      rowValues -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      InvalidArgumentException - if not as many row values are given than the number of columns of the current Matrix.
    • equals

      public boolean equals(Object object)
      Setzt außer Kraft:
      equals in Klasse Object
    • equalsApproximatively

      public boolean equalsApproximatively(Matrix matrix, double epsilon)
      Parameter:
      matrix -
      epsilon -
      Gibt zurück:
      true if the current Matrix equals the given matrix with a deviation, that is smaller than the given epsilon.
    • getColumnCount

      public int getColumnCount()
      Gibt zurück:
      the number of columns of the current Matrix
    • getClone

      public Matrix getClone()
      Gibt zurück:
      a clone of the current Matrix
    • getColumnVectors

      public Vector[] getColumnVectors()
      Gibt zurück:
      the column vectors of the current Matrix
    • getInverse

      public Matrix getInverse()
      Gibt zurück:
      the inverse matrix of the current Matrix.
      Löst aus:
      InvalidArgumentException - if the current Matrix is not regular.
    • getMatrixWithFirstColumns

      public Matrix getMatrixWithFirstColumns(int columnCount)
      Parameter:
      columnCount -
      Gibt zurück:
      a matrix with the first columns of the current Matrix.
      Löst aus:
      ArgumentIsOutOfRangeException - if the given column count is not valid.
    • getMatrixWithLastColumns

      public Matrix getMatrixWithLastColumns(int columnCount)
      Parameter:
      columnCount -
      Gibt zurück:
      a matrix with the last columns of the current Matrix.
      Löst aus:
      ArgumentIsOutOfRangeException - if the given column count is not valid.
    • getMinimalFactorMatrix

      public Matrix getMinimalFactorMatrix(Matrix solutionMatrix)
      This method implements the least squares algorithm.
      Parameter:
      solutionMatrix -
      Gibt zurück:
      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.
      Löst aus:
      UnequalArgumentException - if the given solution matrix has not 1 column.
      UnequalArgumentException - if the given solution matrix has not as many rows as the current Matrix.
    • getProduct

      public Matrix getProduct(Matrix matrix)
      Parameter:
      matrix -
      Gibt zurück:
      the product of the current Matrix and the given matrix.
      Löst aus:
      UnequalArgumentException - if the given matrix has not as many rows as the current Matrix columns has.
    • getPseudoInverse

      public Matrix getPseudoInverse()
      Gibt zurück:
      a pseudo inverse matrix of the current Matrix.
      Löst aus:
      InvalidArgumentException - if the current Matrix is not quadratic.
    • getRank

      public int getRank()
      Gibt zurück:
      the rank of the current Matrix.
      Löst aus:
      InvalidArgumentException - if the current Matrix is not quadratic.
    • getRowCount

      public int getRowCount()
      Gibt zurück:
      the number of rows of the current Matrix
    • getRowVectors

      public Vector[] getRowVectors()
      Gibt zurück:
      the row vectors of the current Matrix
    • getSize

      public int getSize()
      Gibt zurück:
      the size of the current Matrix
    • getSolutionAsExtendedMatrix

      public double[] getSolutionAsExtendedMatrix()
      Gibt zurück:
      the solution when the current Matrix is an extended matrix of a linear equation system
    • getSum

      public Matrix getSum(Matrix matrix)
      Parameter:
      matrix -
      Gibt zurück:
      the matrix that is the sum of the current Matrix and the given matrix
      Löst aus:
      UnequalArgumentException - if the given matrix has not the same size as the current Matrix
    • getTransposed

      public Matrix getTransposed()
      Gibt zurück:
      the transposed matrix of the current Matrix.
    • getTrace

      public double getTrace()
      Gibt zurück:
      the trace of the current Matrix.
      Löst aus:
      InvalidArgumentException - if the current Matrix is not quadratic.
    • getValue

      public double getValue(int rowIndex, int columnIndex)
      Parameter:
      rowIndex -
      columnIndex -
      Gibt zurück:
      the value in the row with the given row index and the column with the given column index.
      Löst aus:
      ArgumentIsOutOfRangeException - if the current Matrix does not contain a row with the given row index.
      ArgumentIsOutOfRangeException - if the current Matrix does not contain a column with the given column index.
    • hashCode

      public int hashCode()
      Setzt außer Kraft:
      hashCode in Klasse Object
    • hasSameSize

      public boolean hasSameSize(Matrix matrix)
      Parameter:
      matrix -
      Gibt zurück:
      true if the current Matrix has the same size as the given matrix
    • isIdentityMatrix

      public boolean isIdentityMatrix()
      Gibt zurück:
      true if the current Matrix is an identity matrix
    • isQuadratic

      public boolean isQuadratic()
      Gibt zurück:
      true if the current Matrix is a quadratic matrix
    • isRegular

      public boolean isRegular()
      Gibt zurück:
      true if the current Matrix is regular
    • multiply

      public Matrix multiply(double factor)
      Multiplies the current Matrix with the given factor.
      Parameter:
      factor -
      Gibt zurück:
      the current Matrix
    • multiplyRow

      public Matrix multiplyRow(int rowIndex, double factor)
      Multiplies the row with the given row index with the given factor.
      Parameter:
      rowIndex -
      factor -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      ArgumentIsOutOfRangeException - if the current Matrix does not contain a row with the given row index.
    • removeZeroRows

      public Matrix removeZeroRows()
      Removes the zero rows of the current Matrix using an epsilon
      Gibt zurück:
      the current Matrix
    • setAllValuesTo

      public Matrix setAllValuesTo(double value)
      Sets the values of the current Matrix to the given value.
      Parameter:
      value -
      Gibt zurück:
      the current Matrix
    • setValue

      public Matrix 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.
      Parameter:
      rowIndex -
      columnIndex -
      value -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      ArgumentIsOutOfRangeException - if the current Matrix does not contain a row with the given row index.
      ArgumentIsOutOfRangeException - if the current Matrix does not contain a column with the given column index.
    • setValues

      public Matrix setValues(double value, double... values)
      Sets the values of the current Matrix.
      Parameter:
      value -
      values -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      InvalidArgumentException - if not as many values are given as the current Matrix contains.
    • setValues

      public Matrix setValues(double[] values)
      Sets the values of the current Matrix.
      Parameter:
      values -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      InvalidArgumentException - if not as many values are given as the current Matrix contains.
    • setDiagonalValuesTo

      public Matrix setDiagonalValuesTo(double value)
      Sets the diagonal values of the current Matrix to the given value.
      Parameter:
      value -
      Gibt zurück:
      the current Matrix
      Löst aus:
      InvalidArgumentException - if the current Matrix is not quadratic
    • swapRows

      public Matrix swapRows(int row1Index, int row2Index)
      Swaps the rows with the given indexes.
      Parameter:
      row1Index -
      row2Index -
      Gibt zurück:
      the current Matrix.
      Löst aus:
      ArgumentIsOutOfRangeException - if the current Matrix does not have a row with the given row1 index.
      ArgumentIsOutOfRangeException - if the current Matrix does not have a row with the given row1 index.
    • toPolynom

      public Polynom toPolynom()
      Gibt zurück:
      a polynom representation of the current Matrix
      Löst aus:
      UnrepresentingArgumentException - if the current Matrix does not represent a Polynom.
    • toVector

      public Vector toVector()
      Gibt zurück:
      a vector representation of the current Matrix
      Löst aus:
      UnrepresentingArgumentException - if the current Matrix does not represent a Vector.
    • tranformFirstPartToIdentityMatrix

      public Matrix tranformFirstPartToIdentityMatrix()
      Transforms the first part of the current Matrix to an identity matrix.
      Gibt zurück:
      the current Matrix
      Löst aus:
      InvalidArgumentException - if the current Matrix has more rows than columns.
      InvalidArgumentException - if the current Matrix has linear depending rows.
    • transformToEquivalentUpperLeftMatrix

      public Matrix transformToEquivalentUpperLeftMatrix()
      Transforms the current Matrix to an equivalent upper left matrix.
      Gibt zurück:
      the current Matrix
    • toString

      public String toString()
      Setzt außer Kraft:
      toString in Klasse Object
      Gibt zurück:
      a String representation of this object
    • transpose

      public Matrix transpose()
      Transposes the current Matrix.
      Gibt zurück:
      the current Matrix