Class LinearSystem

  • Direct Known Subclasses:
    InvertibleMatrix

    public class LinearSystem
    extends SquareMatrix
    From: Java Number Cruncher The Java Programmer's Guide to Numerical Computation by Ronald Mak Solve a system of linear equations using LU decomposition.
    • Field Detail

      • permutation

        protected int[] permutation
        row index permutation vector
      • exchangeCount

        protected int exchangeCount
        row exchange count
    • Constructor Detail

      • LinearSystem

        public LinearSystem​(int n)
        Constructor.
        Parameters:
        n - the number of rows = the number of columns
      • LinearSystem

        public LinearSystem​(double[][] values)
        Constructor.
        Parameters:
        values - the array of values
    • Method Detail

      • set

        protected void set​(double[][] values)
        Set the values of the matrix.
        Overrides:
        set in class SquareMatrix
        Parameters:
        values - the 2-d array of values
      • set

        public void set​(int r,
                        int c,
                        double value)
                 throws MatrixException
        Set the value of element [r,c] in the matrix.
        Overrides:
        set in class Matrix
        Parameters:
        r - the row index, 0..nRows
        c - the column index, 0..nRows
        value - the value
        Throws:
        matrix.MatrixException - for invalid index
        MatrixException
      • setRow

        public void setRow​(RowVector rv,
                           int r)
                    throws MatrixException
        Set a row of this matrix from a row vector.
        Overrides:
        setRow in class Matrix
        Parameters:
        rv - the row vector
        r - the row index
        Throws:
        matrix.MatrixException - for an invalid index or an invalid vector size
        MatrixException
      • setColumn

        public void setColumn​(ColumnVector cv,
                              int c)
                       throws MatrixException
        Set a column of this matrix from a column vector.
        Overrides:
        setColumn in class Matrix
        Parameters:
        cv - the column vector
        c - the column index
        Throws:
        matrix.MatrixException - for an invalid index or an invalid vector size
        MatrixException
      • reset

        protected void reset()
        Reset. Invalidate LU and the permutation vector.
      • solve

        public ColumnVector solve​(ColumnVector b,
                                  boolean improve)
                           throws MatrixException
        Solve Ax = b for x using the Gaussian elimination algorithm.
        Parameters:
        b - the right-hand-side column vector
        improve - true to improve the solution
        Returns:
        the solution column vector
        Throws:
        matrix.MatrixException - if an error occurred
        MatrixException
      • decompose

        protected void decompose()
                          throws MatrixException
        Compute the upper triangular matrix U and lower triangular matrix L such that A = L*U. Store L and U together in matrix LU. Compute the permutation vector permutation of the row indices.
        Throws:
        matrix.MatrixException - for a zero row or a singular matrix
        MatrixException