MatrixUtils { public static void solveUpperTriangularSystem(RealMatrix rm, RealVector b) throws DimensionMismatchException, MathArithmeticException, NonSquareMatrixException { if ((rm == null) || (b == null) || ( rm.getRowDimension() != b.getDimension())) { throw new DimensionMismatchException( (rm == null) ? 0 : rm.getRowDimension(), (b == null) ? 0 : b.getDimension()); } if( rm.getColumnDimension() != rm.getRowDimension() ){ throw new NonSquareMatrixException(rm.getRowDimension(), rm.getColumnDimension()); } int rows = rm.getRowDimension(); for( int i = rows-1 ; i >-1 ; i-- ){ double diag = rm.getEntry(i, i); if( FastMath.abs(diag) < Precision.SAFE_MIN ){ throw new MathArithmeticException(LocalizedFormats.ZERO_DENOMINATOR); } double bi = b.getEntry(i)/diag; b.setEntry(i, bi ); for( int j = i-1; j>-1; j-- ){ b.setEntry(j, b.getEntry(j)-bi*rm.getEntry(j,i) ); } } } private  MatrixUtils(); static RealMatrix createRealMatrix(final int rows, final int columns); static FieldMatrix<T> createFieldMatrix(final Field<T> field,
                                                                               final int rows,
                                                                               final int columns); static RealMatrix createRealMatrix(double[][] data); static FieldMatrix<T> createFieldMatrix(T[][] data); static RealMatrix createRealIdentityMatrix(int dimension); static FieldMatrix<T> createFieldIdentityMatrix(final Field<T> field, final int dimension); static RealMatrix createRealDiagonalMatrix(final double[] diagonal); static FieldMatrix<T> createFieldDiagonalMatrix(final T[] diagonal); static RealVector createRealVector(double[] data); static FieldVector<T> createFieldVector(final T[] data); static RealMatrix createRowRealMatrix(double[] rowData); static FieldMatrix<T> createRowFieldMatrix(final T[] rowData); static RealMatrix createColumnRealMatrix(double[] columnData); static FieldMatrix<T> createColumnFieldMatrix(final T[] columnData); static void checkSymmetric(RealMatrix matrix,
                                      double eps); static boolean isSymmetric(RealMatrix matrix,
                                      double eps); static void checkMatrixIndex(final AnyMatrix m,
                                        final int row, final int column); static void checkRowIndex(final AnyMatrix m, final int row); static void checkColumnIndex(final AnyMatrix m, final int column); static void checkSubMatrixIndex(final AnyMatrix m,
                                           final int startRow, final int endRow,
                                           final int startColumn, final int endColumn); static void checkSubMatrixIndex(final AnyMatrix m,
                                           final int[] selectedRows,
                                           final int[] selectedColumns); static void checkAdditionCompatible(final AnyMatrix left, final AnyMatrix right); static void checkSubtractionCompatible(final AnyMatrix left, final AnyMatrix right); static void checkMultiplicationCompatible(final AnyMatrix left, final AnyMatrix right); static Array2DRowRealMatrix fractionMatrixToRealMatrix(final FieldMatrix<Fraction> m); static Array2DRowRealMatrix bigFractionMatrixToRealMatrix(final FieldMatrix<BigFraction> m); static void serializeRealVector(final RealVector vector,
                                           final ObjectOutputStream oos); static void deserializeRealVector(final Object instance,
                                             final String fieldName,
                                             final ObjectInputStream ois); static void serializeRealMatrix(final RealMatrix matrix,
                                           final ObjectOutputStream oos); static void deserializeRealMatrix(final Object instance,
                                             final String fieldName,
                                             final ObjectInputStream ois); static void solveLowerTriangularSystem(RealMatrix rm, RealVector b); static void solveUpperTriangularSystem(RealMatrix rm, RealVector b); static RealMatrix blockInverse(RealMatrix m,
                                          int splitIndex); static RealMatrix inverse(RealMatrix matrix); static RealMatrix inverse(RealMatrix matrix, double threshold); static final RealMatrixFormat DEFAULT_FORMAT; static final RealMatrixFormat OCTAVE_FORMAT; }
@Test public void testSolveUpperTriangularSystem(){ RealMatrix rm = new Array2DRowRealMatrix( new double[][] { {1,2,3 }, { 0,1,1 }, { 0,0,2 } }, false); RealVector b = new ArrayRealVector(new double[] { 8,4,2 }, false); MatrixUtils.solveUpperTriangularSystem(rm, b); TestUtils.assertEquals( new double[]{-1,3,1} , b.toArray() , 1.0e-12); }