Detailed explanation of the algorithm of matrix inversion described by java

  • 2021-07-03 00:05:13
  • OfStack

Today, I am very happy to solve the problem that has puzzled me for several days, while studying the course of linear algebra. Want to achieve the program inside the calculation method, such as matrix inversion, java code how to describe it?

First of all, let's describe the algorithm in the language we communicate:

1. Find the cofactor of the determinant corresponding to a matrix A at the positions of i and j (i for row and j for column) (multiply the cofactor by-1 ^ (i+j) to obtain the algebraic cofactor);

2. According to the algebraic cosubon, the determinant of matrix A is obtained. Determinant expansion method;

3. Calculate the adjoint matrix according to the values of algebraic cosubformula and determinant;

4. Inverse matrix from adjoint matrix and matrix determinant value. (A ^-1 = A */A).

After understanding the above algorithm ideas, cut the crap and code it.

1. Find the cofactor of the determinant corresponding to a matrix A at the positions of i and j (i for rows and j for columns) (multiply the cofactor by-1 ^ (i+j) to obtain the algebraic cofactor);


/**
     * Find the matrix in i,j Placement residue formula
     * @param mat
     * @param i
     * @param j
     * @return
     */
    public static Matrix getComplementMinor(Matrix mat, int i, int j) {
        // Create 1 Three new matrices are used to receive the values representing the cosubformula, and the rows and columns need to be deleted
        Matrix m = new Matrix(mat.getRow()-1,mat.getCol()-1);
        // Used to traverse new matrices m Variables of
        int row =0 ,col=0;
        /*
         * Traversing the data of the original matrix, j2 Representation line ,k Representation column
         */
        for (int j2 = 0; j2 < mat.getRow(); j2++) {
            // In the first i Data omission of row division
            if(j2 == i) continue;
            for (int k = 0; k < mat.getCol(); k++) {
                // In the first j Data ellipsis of column
                if(k == j) continue;
                // Assignment
                m.setValue(row, col,mat.getValue(j2, k));
                // Traversing the variables of the new matrix
                col++;
                if(col >= m.getCol() ) {
                    col = 0;
                    row++;
                }
            }
        }
        return m;
    }

The value of the A determinant. Determinant expansion method;


/**
  *  Find the value of determinant of matrix 
  * @param mat
  * @return
  */
 public static double getMatrixValue(Matrix mat) {
  if(mat.getRow() != mat.getCol()) {
   System.out.println(" The matrix is not a square matrix and has no determinant ");
   return Double.MIN_VALUE;
  }
  // If yes 1*1 Matrix returns directly 
  if(mat.getRow() == 1) return mat.getValue(0, 0); 
  // If yes 2*2 The matrix is directly calculated to return the result 
  if(mat.getRow() == 2) {        
   return mat.getValue(0, 0)*mat.getValue(1, 1) - mat.getValue(0, 1)*mat.getValue(1, 0);
  }
  // Value of determinant 
  double matrixValue = 0; 
  for (int i = 0; i < mat.getCol(); i++) {
   // Get 0 , i The remainder of the position, that is, the first 1 The remainder of a row 
   Matrix m = getComplementMinor(mat, 0, i);
   // Will be 1 Cofactor addition of rows   , recursively 
   matrixValue += Math.pow(-1, i) * getMatrixValue(m);
   
  }
  return matrixValue;
 }

3. Calculate the adjoint matrix according to the values of algebraic cosubformula and determinant;


/**
     * Finding the Adjoint Matrix of Matrix
     * @param mat
     * @return
     */
    public static Matrix getWithMatrix(Matrix mat) {
        // Create 1 Matrix holds the value of adjoint matrix
        Matrix withMatrix = new Matrix(mat.getRow(),mat.getCol());
        // Traversal withMatrix Store the corresponding mat Value of
        for (int i = 0; i < withMatrix.getRow(); i++) {
            for (int j = 0; j < withMatrix.getCol(); j++) {
                double temp = Math.pow(-1, i+j) * MatrixUtil.getMatrixValue(MatrixUtil.getComplementMinor(mat, j, i));
                if(Math.abs(temp) <= 10e-6) temp = 0;
                withMatrix.setValue(i, j,temp);
            }
        }
        // Return results
        return withMatrix;   
    }

4. Inverse matrix from adjoint matrix and matrix determinant value. (A ^-1 = A */A).


/**
  *  Inverse matrix 
  * @param mat
  * @return
  */
 public static Matrix getReMatrix(Matrix mat) {
  // Create 1 Matrix receives inverse matrix data 
  Matrix reMatrix = new Matrix(mat.getRow(),mat.getCol()); 
  // Get the value of the determinant of the original matrix 
  double value = MatrixUtil.getMatrixValue(mat); 
  // Judge whether the value of matrix determinant is zero 
  if(Math.abs(value) <= 10e-6) {
   System.out.println(" The matrix is irreversible! ");
   return null;
  }
  // The original matrix mat Assignment divided by the value of the original determinant value Inverse matrix 
  for (int i = 0; i < reMatrix.getRow(); i++) {
   for (int j = 0; j < reMatrix.getCol(); j++) {
    reMatrix.setValue(i, j, MatrixUtil.getWithMatrix(mat).getValue(i, j) / value);
   }
  }
  return reMatrix;
  
 }

Related articles: