Returns the determinant of the input matrix. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Returns the determinant of the input matrix.

Demo Code

/*/*from  w w w . java2 s  . c om*/
 *  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>Returns the determinant of the input matrix.
     * </p>
     * 
     * <p>This uses a fairly naive calculation - it will work for small sized
     * matrices but will not be efficient enough for larger sizes.</p>
     * 
     * @param matrix
     * @return determinant of matrix
     * @throws Exception if supplied a non-square matrix
     */
    public static double determinant(double[][] matrix) throws Exception {
        int rows = matrix.length;
        for (int r = 0; r < rows; r++) {
            if (matrix[r].length != rows) {
                throw new Exception(
                        "Cannot compute the determinant of a non-square matrix");
            }
        }
        return recursiveDeterminant(matrix);
    }

    /**
     * <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