Private method to compute the determinant recursively. - Java java.lang

Java examples for java.lang:Math Calculation

Description

Private method to compute the determinant recursively.

Demo Code

/*/*from   w  w w. ja v  a2  s.c o  m*/
 *  Java Information Dynamics Toolkit (JIDT)
 *  Copyright (C) 2012, Joseph T. Lizier
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
//package com.java2s;

public class Main {
    /**
     * <p>Private method to compute the determinant recursively.
     * {@link determinant()} calls this after checking the matrix dimensions. <br/>
     * @see {@link http://mathworld.wolfram.com/Determinant.html}
     * </p>
     * 
     * @param matrix
     * @return
     */
    private static double recursiveDeterminant(double[][] matrix) {
        int rows = matrix.length;
        double result = 0;
        // Base cases:
        if (rows == 1) {
            return matrix[0][0];
        }
        if (rows == 2) {
            return (matrix[0][0] * matrix[1][1] - matrix[0][1]
                    * matrix[1][0]);
        }
        // Recursive case
        int multiplier = 1;
        for (int col = 0; col < rows; col++) {
            // Construct the next sub-matrix to compute the determinant of
            double minor[][] = copyMatrixEliminateRowAndColumn(matrix, 0,
                    col);
            result += (double) multiplier * matrix[0][col]
                    * recursiveDeterminant(minor);
            multiplier *= -1;
        }

        return result;
    }

    public static double[][] copyMatrixEliminateRowAndColumn(
            double[][] matrix, int rowToEliminate, int colToEliminate) {
        double[][] newMatrix = new double[matrix.length - 1][matrix[0].length - 1];
        for (int r = 0; r < matrix.length; r++) {
            if (r == rowToEliminate) {
                continue;
            }
            for (int c = 0; c < matrix.length; c++) {
                if (c == colToEliminate) {
                    continue;
                }
                int newRow = r;
                int newCol = c;
                if (newRow > rowToEliminate) {
                    newRow--;
                }
                if (newCol > colToEliminate) {
                    newCol--;
                }
                newMatrix[newRow][newCol] = matrix[r][c];
            }
        }
        return newMatrix;
    }
}

Related Tutorials