Computes the determinant of a matrix. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Computes the determinant of a matrix.

Demo Code


//package com.java2s;

public class Main {
    /**/*from  w  w w .j a  v  a  2 s.c o m*/
     * Computes the determinant of a matrix. The matrix has to be a square
     * matrix; otherwise, an exception is thrown.
     *
     * @param matrix   Matrix input.
     * @return   The determinant of the matrix.
     * @throws ArithmeticException Throws exception when the matrix is not
     * square.
     */
    public static double determinant(double[][] matrix)
            throws ArithmeticException {

        if (matrix.length != matrix[0].length) {
            throw new ArithmeticException();
        }

        double det = 0;
        int sign = 1;

        if (matrix.length == 1) {
            return matrix[0][0];
        } else {
            double[][] smallerMat = new double[matrix.length - 1][matrix.length - 1];
            for (int i = 0; i < matrix.length; i++) {
                int a = 0;
                int b = 0;
                for (int j = 1; j < matrix.length; j++) {
                    for (int k = 0; k < matrix.length; k++) {
                        if (k < i) {
                            smallerMat[j - 1][k] = matrix[j][k];
                        } else if (k > i) {
                            smallerMat[j - 1][k - 1] = matrix[j][k];
                        }
                    }
                }
                det += sign * matrix[0][i] * determinant(smallerMat);
                sign = -sign;
            }
        }

        return det;
    }

    /**
     * Compute the determinant of a matrix. The matrix has to be a square
     * matrix; otherwise, an exception is thrown.
     *
     * @param matrix   Matrix input.
     * @return   The determinant of the matrix.
     * @throws ArithmeticException Throws exception when the matrix is not
     * square.
     */
    public static double determinant(int[][] matrix)
            throws ArithmeticException {

        if (matrix.length != matrix[0].length) {
            throw new ArithmeticException();
        }

        double det = 0;
        int sign = 1;

        if (matrix.length == 1) {
            return matrix[0][0];
        } else {
            int[][] smallerMat = new int[matrix.length - 1][matrix.length - 1];
            for (int i = 0; i < matrix.length; i++) {
                int a = 0;
                int b = 0;
                for (int j = 1; j < matrix.length; j++) {
                    for (int k = 0; k < matrix.length; k++) {
                        if (k < i) {
                            smallerMat[j - 1][k] = matrix[j][k];
                        } else if (k > i) {
                            smallerMat[j - 1][k - 1] = matrix[j][k];
                        }
                    }
                }
                det += sign * matrix[0][i] * determinant(smallerMat);
                sign = -sign;
            }
        }

        return det;
    }

    /**
     * Compute the determinant of a matrix. The matrix has to be a square
     * matrix; otherwise, an exception is thrown.
     *
     * @param matrix   Matrix input.
     * @return   The determinant of the matrix.
     * @throws ArithmeticException Throws exception when the matrix is not
     * square.
     */
    public static double determinant(float[][] matrix)
            throws ArithmeticException {

        if (matrix.length != matrix[0].length) {
            throw new ArithmeticException();
        }

        double det = 0;
        int sign = 1;

        if (matrix.length == 1) {
            return matrix[0][0];
        } else {
            float[][] smallerMat = new float[matrix.length - 1][matrix.length - 1];
            for (int i = 0; i < matrix.length; i++) {
                int a = 0;
                int b = 0;
                for (int j = 1; j < matrix.length; j++) {
                    for (int k = 0; k < matrix.length; k++) {
                        if (k < i) {
                            smallerMat[j - 1][k] = matrix[j][k];
                        } else if (k > i) {
                            smallerMat[j - 1][k - 1] = matrix[j][k];
                        }
                    }
                }
                det += sign * matrix[0][i] * determinant(smallerMat);
                sign = -sign;
            }
        }

        return det;
    }

    /**
     * Compute the determinant of a matrix. The matrix has to be a square
     * matrix; otherwise, an exception is thrown.
     *
     * @param matrix   Matrix input.
     * @return   The determinant of the matrix.
     * @throws ArithmeticException Throws exception when the matrix is not
     * square.
     */
    public static double determinant(long[][] matrix) {

        if (matrix.length != matrix[0].length) {
            throw new ArithmeticException();
        }

        double det = 0;
        int sign = 1;

        if (matrix.length == 1) {
            return matrix[0][0];
        } else {
            long[][] smallerMat = new long[matrix.length - 1][matrix.length - 1];
            for (int i = 0; i < matrix.length; i++) {
                int a = 0;
                int b = 0;
                for (int j = 1; j < matrix.length; j++) {
                    for (int k = 0; k < matrix.length; k++) {
                        if (k < i) {
                            smallerMat[j - 1][k] = matrix[j][k];
                        } else if (k > i) {
                            smallerMat[j - 1][k - 1] = matrix[j][k];
                        }
                    }
                }
                det += sign * matrix[0][i] * determinant(smallerMat);
                sign = -sign;
            }
        }

        return det;
    }

    /**
     * Compute the determinant of a matrix. The matrix has to be a square
     * matrix; otherwise, an exception is thrown.
     *
     * @param matrix   Matrix input.
     * @return   The determinant of the matrix.
     * @throws ArithmeticException Throws exception when the matrix is not
     * square.
     */
    public static double determinant(byte[][] matrix)
            throws ArithmeticException {

        if (matrix.length != matrix[0].length) {
            throw new ArithmeticException();
        }

        double det = 0;
        int sign = 1;

        if (matrix.length == 1) {
            return matrix[0][0];
        } else {
            byte[][] smallerMat = new byte[matrix.length - 1][matrix.length - 1];
            for (int i = 0; i < matrix.length; i++) {
                int a = 0;
                int b = 0;
                for (int j = 1; j < matrix.length; j++) {
                    for (int k = 0; k < matrix.length; k++) {
                        if (k < i) {
                            smallerMat[j - 1][k] = matrix[j][k];
                        } else if (k > i) {
                            smallerMat[j - 1][k - 1] = matrix[j][k];
                        }
                    }
                }
                det += sign * matrix[0][i] * determinant(smallerMat);
                sign = -sign;
            }
        }

        return det;
    }
}

Related Tutorials