# Math – The Commons Math User Guide – Linear Algebra

Linear algebra support in commons-math provides operations on real matrices (both dense and sparse matrices are supported) and vectors. It features basic operations (addition, subtraction …) and decomposition algorithms that can be used to solve linear systems either in exact sense and in least squares sense. The RealMatrix interface represents a matrix with real numbers as entries. The following basic matrix operations are supported: The three main implementations of the interface are Array2DRowRealMatrix and BlockRealMatrix for dense matrices (the second one being more suited to dimensions above 50 or 100) and SparseRealMatrix for sparse matrices. The RealVector interface represents a vector with real numbers as entries. The following basic matrix operations are supported: The solve() methods of the DecompositionSolver interface support solving linear systems of equations of the form AX=B, either in linear sense or in least square sense. A RealMatrix instance is used to represent the coefficient matrix of the system. Solving the system is a two phases process: first the coefficient matrix is decomposed in some way and then a solver built from the decomposition solves the system. This allows to compute the decomposition and build the solver only once if several systems have to be solved with the same coefficient matrix. Each type of decomposition has its specific semantics and constraints on the coefficient matrix as shown in the following table. For algorithms that solve AX=B in least squares sense the value returned for X is such that the residual AX-B has minimal norm. Least Square sense means a solver can be computed for an overdetermined system, (i.e. a system with more equations than unknowns, which corresponds to a tall A matrix with more rows than columns). If an exact solution exist (i.e. if for some X the residual AX-B is exactly 0), then this exact solution is also the solution in least square sense. This implies that algorithms suited for least squares problems can also be used to solve exact problems, but the reverse is not true. In any case, if the matrix is singular within the tolerance set at construction, an error will be triggered when the solve method will be called, both for algorithms that compute exact solutions and for algorithms that compute least square solutions. It is possible to use a simple array of double instead of a RealVector. In this case, the solution will be provided also as an array of double. It is possible to solve multiple systems with the same coefficient matrix in one method call. To do this, create a matrix whose column vectors correspond to the constant vectors for the systems to be solved and use solve(RealMatrix), which returns a matrix with column vectors representing the solutions. Decomposition algorithms may be used for themselves and not only for linear system solving. This is of prime interest with eigen decomposition and singular value decomposition. The getEigenvalue(), getEigenvalues(), getEigenVector(), getV(), getD() and getVT() methods of the EigenDecomposition interface support solving eigenproblems of the form AX = lambda X where lambda is a real scalar. The getSingularValues(), getU(), getS() and getV() methods of the SingularValueDecomposition interface allow to solve singular values problems of the form AXi = lambda Yi where lambda is a real scalar, and where the Xi and Yi vectors form orthogonal bases of their respective vector spaces (which may have different dimensions). In addition to the real field, matrices and vectors using non-real field elements can be used. The fields already supported by the library are: Source. 