001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
010 *
011 * Unless required by applicable law or agreed to in writing, software
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.linear;
019
020/**
021 * Interface handling decomposition algorithms that can solve A &times; X = B.
022 * <p>
023 * Decomposition algorithms decompose an A matrix has a product of several specific
024 * matrices from which they can solve A &times; X = B in least squares sense: they find X
025 * such that ||A &times; X - B|| is minimal.
026 * <p>
027 * Some solvers like {@link LUDecomposition} can only find the solution for
028 * square matrices and when the solution is an exact linear solution, i.e. when
029 * ||A &times; X - B|| is exactly 0. Other solvers can also find solutions
030 * with non-square matrix A and with non-null minimal norm. If an exact linear
031 * solution exists it is also the minimal norm solution.
032 *
033 * @since 2.0
034 */
035public interface DecompositionSolver {
036
037    /**
038     * Solve the linear equation A &times; X = B for matrices A.
039     * <p>
040     * The A matrix is implicit, it is provided by the underlying
041     * decomposition algorithm.
042     *
043     * @param b right-hand side of the equation A &times; X = B
044     * @return a vector X that minimizes the two norm of A &times; X - B
045     * @throws org.apache.commons.math3.exception.DimensionMismatchException
046     * if the matrices dimensions do not match.
047     * @throws SingularMatrixException if the decomposed matrix is singular.
048     */
049    RealVector solve(final RealVector b) throws SingularMatrixException;
050
051    /**
052     * Solve the linear equation A &times; X = B for matrices A.
053     * <p>
054     * The A matrix is implicit, it is provided by the underlying
055     * decomposition algorithm.
056     *
057     * @param b right-hand side of the equation A &times; X = B
058     * @return a matrix X that minimizes the two norm of A &times; X - B
059     * @throws org.apache.commons.math3.exception.DimensionMismatchException
060     * if the matrices dimensions do not match.
061     * @throws SingularMatrixException if the decomposed matrix is singular.
062     */
063    RealMatrix solve(final RealMatrix b) throws SingularMatrixException;
064
065    /**
066     * Check if the decomposed matrix is non-singular.
067     * @return true if the decomposed matrix is non-singular.
068     */
069    boolean isNonSingular();
070
071    /**
072     * Get the <a href="http://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse">pseudo-inverse</a>
073     * of the decomposed matrix.
074     * <p>
075     * <em>This is equal to the inverse  of the decomposed matrix, if such an inverse exists.</em>
076     * <p>
077     * If no such inverse exists, then the result has properties that resemble that of an inverse.
078     * <p>
079     * In particular, in this case, if the decomposed matrix is A, then the system of equations
080     * $$A x = b$$ may have no solutions, or many. If it has no solutions, then the pseudo-inverse
081     * $$A^+$$ gives the "closest" solution $$z = A^+ b$$, meaning $$\left \| A z - b \right \|_2$$
082     * is minimized. If there are many solutions, then $$z = A^+ b$$ is the smallest solution,
083     * meaning $$\left \| z \right \|_2$$ is minimized.
084     * <p>
085     * Note however that some decompositions cannot compute a pseudo-inverse for all matrices.
086     * For example, the {@link LUDecomposition} is not defined for non-square matrices to begin
087     * with. The {@link QRDecomposition} can operate on non-square matrices, but will throw
088     * {@link SingularMatrixException} if the decomposed matrix is singular. Refer to the javadoc
089     * of specific decomposition implementations for more details.
090     *
091     * @return pseudo-inverse matrix (which is the inverse, if it exists),
092     * if the decomposition can pseudo-invert the decomposed matrix
093     * @throws SingularMatrixException if the decomposed matrix is singular and the decomposition
094     * can not compute a pseudo-inverse
095     */
096    RealMatrix getInverse() throws SingularMatrixException;
097}