001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
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 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
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 × 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 * @version $Id: DecompositionSolver.java 1566017 2014-02-08 14:13:34Z tn $
034 * @since 2.0
035 */
036public interface DecompositionSolver {
037
038    /**
039     * Solve the linear equation A &times; X = B for matrices A.
040     * <p>
041     * The A matrix is implicit, it is provided by the underlying
042     * decomposition algorithm.
043     *
044     * @param b right-hand side of the equation A &times; X = B
045     * @return a vector X that minimizes the two norm of A &times; X - B
046     * @throws org.apache.commons.math3.exception.DimensionMismatchException
047     * if the matrices dimensions do not match.
048     * @throws SingularMatrixException if the decomposed matrix is singular.
049     */
050    RealVector solve(final RealVector b) throws SingularMatrixException;
051
052    /**
053     * Solve the linear equation A &times; X = B for matrices A.
054     * <p>
055     * The A matrix is implicit, it is provided by the underlying
056     * decomposition algorithm.
057     *
058     * @param b right-hand side of the equation A &times; X = B
059     * @return a matrix X that minimizes the two norm of A &times; X - B
060     * @throws org.apache.commons.math3.exception.DimensionMismatchException
061     * if the matrices dimensions do not match.
062     * @throws SingularMatrixException if the decomposed matrix is singular.
063     */
064    RealMatrix solve(final RealMatrix b) throws SingularMatrixException;
065
066    /**
067     * Check if the decomposed matrix is non-singular.
068     * @return true if the decomposed matrix is non-singular.
069     */
070    boolean isNonSingular();
071
072    /**
073     * Get the <a href="http://en.wikipedia.org/wiki/Moore%E2%80%93Penrose_pseudoinverse">pseudo-inverse</a>
074     * of the decomposed matrix.
075     * <p>
076     * <em>This is equal to the inverse  of the decomposed matrix, if such an inverse exists.</em>
077     * <p>
078     * If no such inverse exists, then the result has properties that resemble that of an inverse.
079     * <p>
080     * In particular, in this case, if the decomposed matrix is A, then the system of equations
081     * \( A x = b \) may have no solutions, or many. If it has no solutions, then the pseudo-inverse
082     * \( A^+ \) gives the "closest" solution \( z = A^+ b \), meaning \( \left \| A z - b \right \|_2 \)
083     * is minimized. If there are many solutions, then \( z = A^+ b \) is the smallest solution,
084     * meaning \( \left \| z \right \|_2 \) is minimized.
085     * <p>
086     * Note however that some decompositions cannot compute a pseudo-inverse for all matrices.
087     * For example, the {@link LUDecomposition} is not defined for non-square matrices to begin
088     * with. The {@link QRDecomposition} can operate on non-square matrices, but will throw
089     * {@link SingularMatrixException} if the decomposed matrix is singular. Refer to the javadoc
090     * of specific decomposition implementations for more details.
091     *
092     * @return pseudo-inverse matrix (which is the inverse, if it exists),
093     * if the decomposition can pseudo-invert the decomposed matrix
094     * @throws SingularMatrixException if the decomposed matrix is singular and the decomposition
095     * can not compute a pseudo-inverse
096     */
097    RealMatrix getInverse() throws SingularMatrixException;
098}