Class QRDecomposer

java.lang.Object
com.irurueta.algebra.Decomposer
com.irurueta.algebra.QRDecomposer

public class QRDecomposer extends Decomposer
This decomposer computes QR decomposition, which consists on factoring provided input matrix into an orthogonal matrix (Q) and an upper triangular matrix (R). In other words, if input matrix is A, then: A = Q * R
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    Constant defining default round error when determining full rank of matrices.
    static final double
    Constant defining minimum allowed round error value when determining full rank of matrices.
    private Matrix
    Internal matrix containing Q factor.
    private Matrix
    Internal matrix containing R factor.
    (package private) boolean
    Boolean indicating whether decomposed matrix is singular.

    Fields inherited from class com.irurueta.algebra.Decomposer

    inputMatrix, locked
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor of this class.
    QRDecomposer(Matrix inputMatrix)
    Constructor of this class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    This method computes LU matrix decomposition, which consists on retrieving two triangular matrices (Lower triangular and Upper triangular) as a decomposition of provided input matrix.
    Returns decomposer type corresponding to QR decomposition.
    Returns the economy-sized orthogonal factor matrix.
    Returns upper triangular factor matrix.
    boolean
    Returns boolean indicating whether decomposition has been computed and results can be retrieved.
    boolean
    Returns boolean indicating whether provided input matrix has full rank or not.
    boolean
    isFullRank(double roundingError)
    Returns boolean indicating whether provided input matrix has full rank or not.
    void
    setInputMatrix(Matrix inputMatrix)
    Sets reference to input matrix to be decomposed.
    Solves a linear system of equations of the following form: A * X = B.
    solve(Matrix b, double roundingError)
    Solves a linear system of equations of the following form: A * X = B.
    void
    solve(Matrix b, double roundingError, Matrix result)
    Solves a linear system of equations of the following form: A * X = B.
    void
    solve(Matrix b, Matrix result)
    Solves a linear system of equations of the following form: A * X = B.

    Methods inherited from class com.irurueta.algebra.Decomposer

    getInputMatrix, isLocked, isReady

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_ROUND_ERROR

      public static final double DEFAULT_ROUND_ERROR
      Constant defining default round error when determining full rank of matrices. This value is zero by default.
      See Also:
    • MIN_ROUND_ERROR

      public static final double MIN_ROUND_ERROR
      Constant defining minimum allowed round error value when determining full rank of matrices.
      See Also:
    • q

      private Matrix q
      Internal matrix containing Q factor.
    • r

      private Matrix r
      Internal matrix containing R factor.
    • sing

      boolean sing
      Boolean indicating whether decomposed matrix is singular.
  • Constructor Details

    • QRDecomposer

      public QRDecomposer()
      Constructor of this class.
    • QRDecomposer

      public QRDecomposer(Matrix inputMatrix)
      Constructor of this class.
      Parameters:
      inputMatrix - Reference to input matrix to be decomposed.
  • Method Details

    • getDecomposerType

      public DecomposerType getDecomposerType()
      Returns decomposer type corresponding to QR decomposition.
      Specified by:
      getDecomposerType in class Decomposer
      Returns:
      Decomposer type.
    • setInputMatrix

      public void setInputMatrix(Matrix inputMatrix) throws LockedException
      Sets reference to input matrix to be decomposed.
      Overrides:
      setInputMatrix in class Decomposer
      Parameters:
      inputMatrix - Reference to input matrix to be decomposed.
      Throws:
      LockedException - Exception thrown if attempting to call this method while this instance remains locked.
    • isDecompositionAvailable

      public boolean isDecompositionAvailable()
      Returns boolean indicating whether decomposition has been computed and results can be retrieved. Attempting to retrieve decomposition results when not available, will probably raise a NotAvailableException.
      Specified by:
      isDecompositionAvailable in class Decomposer
      Returns:
      Boolean indicating whether decomposition has been computed and results can be retrieved.
    • decompose

      public void decompose() throws NotReadyException, LockedException, DecomposerException
      This method computes LU matrix decomposition, which consists on retrieving two triangular matrices (Lower triangular and Upper triangular) as a decomposition of provided input matrix. In other words, if input matrix is A, then: A = L * U Note: During execution of this method, this instance will be locked, and hence attempting to set some parameters might raise a LockedException. Note: After execution of this method, LU decomposition will be available and operations such as retrieving L and U matrices or computing determinants among others will be able to be done. Attempting to call any of such operations before calling this method will raise a NotAvailableException because they require computation of LU decomposition first.
      Specified by:
      decompose in class Decomposer
      Throws:
      NotReadyException - Exception thrown if attempting to call this method when this instance is not ready (i.e. no input matrix has been provided).
      LockedException - Exception thrown if attempting to call this method when this instance is not ready (i.e. no input matrix has been provided).
      DecomposerException - Exception thrown if for any reason decomposition fails while executing, like when convergence of results can not be obtained, etc.
    • isFullRank

      public boolean isFullRank() throws NotAvailableException
      Returns boolean indicating whether provided input matrix has full rank or not. Squared matrices having full rank also have determinant different from zero. Note: Because of rounding errors testing whether a matrix has full rank or not might obtain unreliable results especially for non-squared matrices. In such cases, matrices usually tend to be considered as full rank even when they are not. Note: DEFAULT_ROUND_ERROR is used to determine whether matrix is full rank or not
      Returns:
      Boolean indicating whether provided input matrix has full rank or not.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      IllegalArgumentException - Exception thrown if provided rounding error is lower than minimum allowed value (MIN_ROUND_ERROR)
      See Also:
    • isFullRank

      public boolean isFullRank(double roundingError) throws NotAvailableException
      Returns boolean indicating whether provided input matrix has full rank or not. Squared matrices having full rank also have determinant different from zero. Note: Because of rounding errors testing whether a matrix has full rank or not might obtain unreliable results especially for non-squared matrices. In such cases, matrices usually tend to be considered as full rank even when they are not.
      Parameters:
      roundingError - Determines the amount of margin given to determine whether a matrix has full rank or not due to rounding errors. If not provided, by default rounding error is set to zero, but this value can be relaxed if needed.
      Returns:
      Boolean indicating whether provided input matrix has full rank or not.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      IllegalArgumentException - Exception thrown if provided rounding error is lower than minimum allowed value (MIN_ROUND_ERROR)
      See Also:
    • getR

      public Matrix getR() throws NotAvailableException
      Returns upper triangular factor matrix. QR decomposition decomposes input matrix into Q (orthogonal matrix) and R, which is an upper triangular matrix.
      Returns:
      Upper triangular factor matrix.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      See Also:
    • getQ

      public Matrix getQ() throws NotAvailableException
      Returns the economy-sized orthogonal factor matrix. QR decomposition decomposes input matrix into Q, which is an orthogonal matrix and R (upper triangular matrix).
      Returns:
      Orthogonal factor matrix.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      See Also:
    • solve

      Solves a linear system of equations of the following form: A * X = B. Where A is the input matrix provided for QR decomposition, X is the solution to the system of equations, and B is the parameters vector/ matrix. Note: This method can be reused for different b vectors/matrices without having to recompute QR decomposition on the same input matrix. Note: Provided b matrix must have the same number of rows as provided input matrix A, otherwise a WrongSizeException will be raised. Note: Provided input matrix "A" must have at least as many rows as columns, otherwise a WrongSizeException will be raised as well. For input matrices having a higher number of rows than columns, the system of equations will be overdetermined and the least squares solution will be found. Note: If provided input matrix A is rank deficient, a RankDeficientMatrixException will be thrown. Note: In order to execute this method, a QR decomposition must be available, otherwise a NotAvailableException will be raised. In order to avoid this exception call decompose() method first. Note: To solve the linear system of equations DEFAULT_ROUND_ERROR is used Note: Solution of linear system of equations is stored in result matrix, and result matrix is resized if needed.
      Parameters:
      b - Parameters matrix that determines a linear system of equations. Provided matrix must have the same number of rows as provided input matrix for QR decomposition. Besides, each column on parameters matrix will represent a new system of equations, whose solution will be returned on appropriate column as an output of this method.
      result - Matrix containing solution of linear system of equations on each column for each column of provided parameters matrix b.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      WrongSizeException - Exception thrown if attempting to call this method using an input matrix with less rows than columns; or if provided parameters matrix (b) does not have the same number of rows as input matrix being QR decomposed.
      RankDeficientMatrixException - Exception thrown if provided input matrix to be QR decomposed is rank deficient. In this case linear system of equations cannot be solved.
      See Also:
    • solve

      public void solve(Matrix b, double roundingError, Matrix result) throws NotAvailableException, WrongSizeException, RankDeficientMatrixException
      Solves a linear system of equations of the following form: A * X = B. Where A is the input matrix provided for QR decomposition, X is the solution to the system of equations, and B is the parameters vector/ matrix. Note: This method can be reused for different b vectors/matrices without having to recompute QR decomposition on the same input matrix. Note: Provided b matrix must have the same number of rows as provided input matrix A, otherwise a WrongSizeException will be raised. Note: Provided input matrix "A" must have at least as many rows as columns, otherwise a WrongSizeException will be raised as well. For input matrices having a higher number of rows than columns, the system of equations will be overdetermined and the least squares solution will be found. Note: If provided input matrix A is rank deficient, a RankDeficientMatrixException will be thrown. Note: In order to execute this method, a QR decomposition must be available, otherwise a NotAvailableException will be raised. In order to avoid this exception call decompose() method first. Note: Solution of linear system of equations is stored in result matrix, and result matrix is resized if needed.
      Parameters:
      b - Parameters matrix that determines a linear system of equations. Provided matrix must have the same number of rows as provided input matrix for QR decomposition. Besides, each column on parameters matrix will represent a new system of equations, whose solution will be returned on appropriate column as an output of this method.
      roundingError - Determines the amount of margin given to determine whether a matrix has full rank or not due to rounding errors. If not provided, by default rounding error is set to zero, but this value can be relaxed if needed.
      result - Matrix containing solution of linear system of equations on each column for each column of provided parameters matrix b.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      WrongSizeException - Exception thrown if attempting to call this method using an input matrix with less rows than columns; or if provided parameters matrix (b) does not have the same number of rows as input matrix being QR decomposed.
      RankDeficientMatrixException - Exception thrown if provided input matrix to be QR decomposed is rank deficient. In this case linear system of equations cannot be solved.
      IllegalArgumentException - Exception thrown if provided rounding error is lower than minimum allowed value (MIN_ROUND_ERROR).
      See Also:
    • solve

      Solves a linear system of equations of the following form: A * X = B. Where A is the input matrix provided for QR decomposition, X is the solution to the system of equations, and B is the parameters vector/ matrix. Note: This method can be reused for different b vectors/matrices without having to recompute QR decomposition on the same input matrix. Note: Provided b matrix must have the same number of rows as provided input matrix A, otherwise a WrongSizeException will be raised. Note: Provided input matrix "A" must have at least as many rows as columns, otherwise a WrongSizeException will be raised as well. For input matrices having a higher number of rows than columns, the system of equations will be overdetermined and the least squares solution will be found. Note: If provided input matrix A is rank deficient, a RankDeficientMatrixException will be thrown. Note: In order to execute this method, a QR decomposition must be available, otherwise a NotAvailableException will be raised. In order to avoid this exception call decompose() method first. Note: To solve the linear system of equations DEFAULT_ROUND_ERROR is used
      Parameters:
      b - Parameters matrix that determines a linear system of equations. Provided matrix must have the same number of rows as provided input matrix for QR decomposition. Besides, each column on parameters matrix will represent a new system of equations, whose solution will be returned on appropriate column as an output of this method.
      Returns:
      Matrix containing solution of linear system of equations on each column for each column of provided parameters matrix b.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      WrongSizeException - Exception thrown if attempting to call this method using an input matrix with less rows than columns; or if provided parameters matrix (b) does not have the same number of rows as input matrix being QR decomposed.
      RankDeficientMatrixException - Exception thrown if provided input matrix to be QR decomposed is rank deficient. In this case linear system of equations cannot be solved.
      See Also:
    • solve

      public Matrix solve(Matrix b, double roundingError) throws NotAvailableException, WrongSizeException, RankDeficientMatrixException
      Solves a linear system of equations of the following form: A * X = B. Where A is the input matrix provided for QR decomposition, X is the solution to the system of equations, and B is the parameters vector/ matrix. Note: This method can be reused for different b vectors/matrices without having to recompute QR decomposition on the same input matrix. Note: Provided b matrix must have the same number of rows as provided input matrix A, otherwise a WrongSizeException will be raised. Note: Provided input matrix "A" must have at least as many rows as columns, otherwise a WrongSizeException will be raised as well. For input matrices having a higher number of rows than columns, the system of equations will be overdetermined and the least squares solution will be found. Note: If provided input matrix A is rank deficient, a RankDeficientMatrixException will be thrown. Note: In order to execute this method, a QR decomposition must be available, otherwise a NotAvailableException will be raised. In order to avoid this exception call decompose() method first.
      Parameters:
      b - Parameters matrix that determines a linear system of equations. Provided matrix must have the same number of rows as provided input matrix for QR decomposition. Besides, each column on parameters matrix will represent a new system of equations, whose solution will be returned on appropriate column as an output of this method.
      roundingError - Determines the amount of margin given to determine whether a matrix has full rank or not due to rounding errors. If not provided, by default rounding error is set to zero, but this value can be relaxed if needed.
      Returns:
      Matrix containing solution of linear system of equations on each column for each column of provided parameters matrix b.
      Throws:
      NotAvailableException - Exception thrown if attempting to call this method before computing QR decomposition. To avoid this exception call decompose() method first.
      WrongSizeException - Exception thrown if attempting to call this method using an input matrix with less rows than columns; or if provided parameters matrix (b) does not have the same number of rows as input matrix being QR decomposed.
      RankDeficientMatrixException - Exception thrown if provided input matrix to be QR decomposed is rank deficient. In this case linear system of equations cannot be solved.
      IllegalArgumentException - Exception thrown if provided rounding error is lower than minimum allowed value (MIN_ROUND_ERROR).
      See Also: