private double[] solveLinearEquation( double[][] matrixA, double[] vectorb ) { RealMatrix coefficients = new Array2DRowRealMatrix( matrixA, false ); // LU-decomposition DecompositionSolver solver = new LUDecompositionImpl( coefficients ).getSolver(); RealVector constants = new ArrayRealVector( vectorb, false ); RealVector solution = null; try { solution = solver.solve( constants ); } catch ( SingularMatrixException e ) { LOG.error( e.getLocalizedMessage() ); e.printStackTrace(); } return solution.getData(); }
/** * Get the inverse of the decomposed matrix. * @return inverse matrix * @throws InvalidMatrixException * if decomposed matrix is singular */ public RealMatrix getInverse() throws InvalidMatrixException { if (!isNonSingular()) { throw new SingularMatrixException(); } final int m = realEigenvalues.length; final double[][] invData = new double[m][m]; for (int i = 0; i < m; ++i) { final double[] invI = invData[i]; for (int j = 0; j < m; ++j) { double invIJ = 0; for (int k = 0; k < m; ++k) { final double[] vK = eigenvectors[k].getDataRef(); invIJ += vK[i] * vK[j] / realEigenvalues[k]; } invI[j] = invIJ; } } return MatrixUtils.createRealMatrix(invData); }
/** * Get the inverse of the decomposed matrix. * @return inverse matrix * @throws InvalidMatrixException * if decomposed matrix is singular */ public RealMatrix getInverse() throws InvalidMatrixException { if (!isNonSingular()) { throw new SingularMatrixException(); } final int m = realEigenvalues.length; final double[][] invData = new double[m][m]; for (int i = 0; i < m; ++i) { final double[] invI = invData[i]; for (int j = 0; j < m; ++j) { double invIJ = 0; for (int k = 0; k < m; ++k) { final double[] vK = eigenvectors[k].getDataRef(); invIJ += vK[i] * vK[j] / realEigenvalues[k]; } invI[j] = invIJ; } } return MatrixUtils.createRealMatrix(invData); }