Java Matrix matrixDeterminant(final float[] m, final int m_offset)

Here you can find the source of matrixDeterminant(final float[] m, final int m_offset)

Description

Returns the determinant of the given matrix

License

Open Source License

Parameter

Parameter Description
m 4x4 matrix in column-major order, the source
m_offset offset in given array <i>m</i>, i.e. start of the 4x4 matrix

Return

the matrix determinant

Declaration

public static float matrixDeterminant(final float[] m, final int m_offset) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w w w .  java  2 s  .c  o m
     * Returns the determinant of the given matrix
     *
     * @param m 4x4 matrix in column-major order, the source
     * @param m_offset offset in given array <i>m</i>, i.e. start of the 4x4
     * matrix
     * @return the matrix determinant
     */
    public static float matrixDeterminant(final float[] m, final int m_offset) {
        float a11 = m[1 + 4 * 1 + m_offset];
        float a21 = m[2 + 4 * 1 + m_offset];
        float a31 = m[3 + 4 * 1 + m_offset];
        float a12 = m[1 + 4 * 2 + m_offset];
        float a22 = m[2 + 4 * 2 + m_offset];
        float a32 = m[3 + 4 * 2 + m_offset];
        float a13 = m[1 + 4 * 3 + m_offset];
        float a23 = m[2 + 4 * 3 + m_offset];
        float a33 = m[3 + 4 * 3 + m_offset];

        float ret = 0;
        ret += m[0 + m_offset]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        a11 = m[1 + 4 * 0 + m_offset];
        a21 = m[2 + 4 * 0 + m_offset];
        a31 = m[3 + 4 * 0 + m_offset];
        ret -= m[0 + 4 * 1 + m_offset]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        a12 = m[1 + 4 * 1 + m_offset];
        a22 = m[2 + 4 * 1 + m_offset];
        a32 = m[3 + 4 * 1 + m_offset];
        ret += m[0 + 4 * 2 + m_offset]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        a13 = m[1 + 4 * 2 + m_offset];
        a23 = m[2 + 4 * 2 + m_offset];
        a33 = m[3 + 4 * 2 + m_offset];
        ret -= m[0 + 4 * 3 + m_offset]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        return ret;
    }

    /**
     * Returns the determinant of the given matrix
     *
     * @param m 4x4 matrix in column-major order, the source
     * @return the matrix determinant
     */
    public static float matrixDeterminant(final float[] m) {
        float a11 = m[1 + 4 * 1];
        float a21 = m[2 + 4 * 1];
        float a31 = m[3 + 4 * 1];
        float a12 = m[1 + 4 * 2];
        float a22 = m[2 + 4 * 2];
        float a32 = m[3 + 4 * 2];
        float a13 = m[1 + 4 * 3];
        float a23 = m[2 + 4 * 3];
        float a33 = m[3 + 4 * 3];

        float ret = 0;
        ret += m[0]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        a11 = m[1 + 4 * 0];
        a21 = m[2 + 4 * 0];
        a31 = m[3 + 4 * 0];
        ret -= m[0 + 4 * 1]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        a12 = m[1 + 4 * 1];
        a22 = m[2 + 4 * 1];
        a32 = m[3 + 4 * 1];
        ret += m[0 + 4 * 2]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        a13 = m[1 + 4 * 2];
        a23 = m[2 + 4 * 2];
        a33 = m[3 + 4 * 2];
        ret -= m[0 + 4 * 3]
                * (+a11 * (a22 * a33 - a23 * a32) - a12 * (a21 * a33 - a23 * a31) + a13 * (a21 * a32 - a22 * a31));
        return ret;
    }
}

Related

  1. matrixAbsDiff(double m1[][], double m2[][])
  2. matrixATrans_x_matrixB(double[][] matA, double[][] matB)
  3. matrixConcatLR(double[][] LMatrix, double[][] RMatrix)
  4. matrixConcatUL(double[][] UMatrix, double[][] LMatrix)
  5. matrixDestructAdd(double[][] m1, double[][] m2)
  6. matrixEquals(int[][] firstMatrix, int[][] secondMatrix)
  7. matrixIsWellformed(int numLabels, int[][] matrix)
  8. matrixKLDivergence(double m1[][], double m2[][])
  9. matrixModule(double[][] matrix, int module)