Java Gauss gaussJordanElimination(boolean[][] matrix)

Here you can find the source of gaussJordanElimination(boolean[][] matrix)

Description

same as gaussJordanElimination(boolean[][] matrix, int upToRow, int upToCol) but on full matrix

License

Apache License

Parameter

Parameter Description
matrix boolean matrix

Return

rank

Declaration

public static int gaussJordanElimination(boolean[][] matrix) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**/*from w  ww .  ja  va 2s.  com*/
     * same as gaussJordanElimination(boolean[][] matrix, int upToRow, int upToCol) but on full matrix
     * @param matrix boolean matrix
     * @return rank
     */
    public static int gaussJordanElimination(boolean[][] matrix) {
        if (matrix == null) {
            return -1;
        }
        return gaussJordanElimination(matrix, matrix.length, matrix[0].length);
    }

    /**
     * same as gaussJordanElimination(boolean[][] matrix, int upToRow, int upToCol) but with all the columns
     * @param matrix boolean matrix
     * @return rank
     */
    public static int gaussJordanElimination(boolean[][] matrix, int upToRow) {
        if (matrix == null) {
            return -1;
        }
        return gaussJordanElimination(matrix, upToRow, matrix[0].length);
    }

    /**
     * Perform Gauss-Jordan elimination on matrix. Note that the matrix passed in will be changed to rref in the process so you may want to save a copy. 
     * @param matrix boolean matrix
     * @param upToRow ignore the rows after upToRow (1 base)
     * @param upToCol ignore the columns after upToCol (1 base)
     * @return the rank of the matrix
     */
    public static int gaussJordanElimination(boolean[][] matrix, int upToRow, int upToCol) {
        if (matrix == null) {
            return -1;
        }
        int m, n;
        m = Math.min(upToRow, matrix.length);
        if (m < 1) {
            return 0;
        }
        if (m == 1) {
            return 1;
        }
        n = Math.min(upToCol, matrix[0].length);
        int pivot = 0;
        int startRow = 0;
        while (pivot < n && startRow < m) {
            //find leading one
            int leadingRow = pickLeadingOne(startRow, pivot, m, n, matrix);
            if (leadingRow < 0) {
                pivot++;
                continue;
            } else {
                //swap
                swapRows(startRow, leadingRow, matrix);
                //elimination
                eliminate(startRow, pivot, m, matrix);

                pivot++;
                startRow++;
            }
        }
        return startRow;
    }

    private static int pickLeadingOne(int startRow, int pivot, int m, int n, boolean[][] matrix) {
        int leadingRow = -1;
        for (int i = startRow; i < m; i++) {
            if (matrix[i][pivot]) {
                leadingRow = i;
                break;
            }
        }
        return leadingRow;
    }

    private static void swapRows(int i, int j, boolean[][] matrix) {
        int m = matrix.length;
        if (m == 0 || i >= m || j >= m || i == j) {
            // do nothing
            return;
        }
        int n = matrix[0].length;
        boolean temp;
        for (int k = 0; k < n; k++) {
            temp = matrix[i][k];
            matrix[i][k] = matrix[j][k];
            matrix[j][k] = temp;
        }
    }

    private static void eliminate(int pr, int pc, int m, boolean[][] matrix) {
        int n;
        n = matrix[0].length;
        for (int i = 0; i < m; i++) {
            //skip those rows that we don't care 
            if (i == pr || !matrix[i][pc]) {
                continue;
            }
            matrix[i][pc] = false;
            for (int j = pc + 1; j < n; j++) {
                matrix[i][j] = matrix[pr][j] ^ matrix[i][j];
            }
        }
    }
}

Related

  1. gaussianFilter(float[] weights, float sigma)
  2. gaussianIntegral(double x)
  3. gaussianPDF(double mean, double variance, double x)
  4. gaussianPDF(double mu, double sigma, double x)
  5. gaussianWindow(double mean1, double mean2, double std)
  6. gaussTable(final int steps)