Java Number Invert invert3x3(final double m00, final double m01, final double m02, final double m10, final double m11, final double m12, final double m20, final double m21, final double m22)

Here you can find the source of invert3x3(final double m00, final double m01, final double m02, final double m10, final double m11, final double m12, final double m20, final double m21, final double m22)

Description

Invert a 3x3 matrix given as elements in row major order.

License

Open Source License

Exception

Parameter Description
IllegalArgumentException if matrix is not invertible

Return

inverted matrix as row major double[]

Declaration

final static public double[] invert3x3(final double m00, final double m01, final double m02, final double m10,
        final double m11, final double m12, final double m20, final double m21, final double m22)
        throws IllegalArgumentException 

Method Source Code

//package com.java2s;

public class Main {
    /**//www .j a  va  2s  .  c o m
     * Invert a 3x3 matrix given as row major double[] in place.
     *
     * @param m matrix
     *
     * @throws IllegalArgumentException if matrix is not invertible
     */
    final static public void invert3x3(final double[] m) throws IllegalArgumentException {
        assert m.length >= 9 : "Not enough coordinates.";

        final double det = det3x3(m);
        if (det == 0)
            throw new IllegalArgumentException("Matrix not invertible.");

        final double i00 = (m[4] * m[8] - m[5] * m[7]) / det;
        final double i01 = (m[2] * m[7] - m[1] * m[8]) / det;
        final double i02 = (m[1] * m[5] - m[2] * m[4]) / det;

        final double i10 = (m[5] * m[6] - m[3] * m[8]) / det;
        final double i11 = (m[0] * m[8] - m[2] * m[6]) / det;
        final double i12 = (m[2] * m[3] - m[0] * m[5]) / det;

        final double i20 = (m[3] * m[7] - m[4] * m[6]) / det;
        final double i21 = (m[1] * m[6] - m[0] * m[7]) / det;
        final double i22 = (m[0] * m[4] - m[1] * m[3]) / det;

        m[0] = i00;
        m[1] = i01;
        m[2] = i02;

        m[3] = i10;
        m[4] = i11;
        m[5] = i12;

        m[6] = i20;
        m[7] = i21;
        m[8] = i22;
    }

    /**
     * Invert a 3x3 matrix given as elements in row major order.
     *
     * @return inverted matrix as row major double[]
     *
     * @throws IllegalArgumentException if matrix is not invertible
     */
    final static public double[] invert3x3(final double m00, final double m01, final double m02, final double m10,
            final double m11, final double m12, final double m20, final double m21, final double m22)
            throws IllegalArgumentException {
        final double det = det3x3(m00, m01, m02, m10, m11, m12, m20, m21, m22);
        if (det == 0)
            throw new IllegalArgumentException("Matrix not invertible.");

        return new double[] { (m11 * m22 - m12 * m21) / det, (m02 * m21 - m01 * m22) / det,
                (m01 * m12 - m02 * m11) / det, (m12 * m20 - m10 * m22) / det, (m00 * m22 - m02 * m20) / det,
                (m02 * m10 - m00 * m12) / det, (m10 * m21 - m11 * m20) / det, (m01 * m20 - m00 * m21) / det,
                (m00 * m11 - m01 * m10) / det };
    }

    /**
     * Calculates the determinant of a 3x3 matrix given as a double[] (row major).
     *
     * @param a
     *
     * @return determinant
     */
    final static public double det3x3(final double[] a) {
        assert a.length >= 9 : "Not enough coordinates.";

        return a[0] * a[4] * a[8] + a[3] * a[7] * a[2] + a[6] * a[1] * a[5] - a[2] * a[4] * a[6]
                - a[5] * a[7] * a[0] - a[8] * a[1] * a[3];
    }

    /**
     * Calculate the determinant of a 3x3 matrix.
     *
     * @param m00
     * @param m01
     * @param m02
     * @param m10
     * @param m11
     * @param m12
     * @param m20
     * @param m21
     * @param m22
     *
     * @return
     */
    final static public double det3x3(final double m00, final double m01, final double m02, final double m10,
            final double m11, final double m12, final double m20, final double m21, final double m22) {
        return m00 * m11 * m22 + m10 * m21 * m02 + m20 * m01 * m12 - m02 * m11 * m20 - m12 * m21 * m00
                - m22 * m01 * m10;
    }
}

Related

  1. invert(double slope)
  2. invert(final double angle)
  3. invert(float value)
  4. invert(int numToInvert)
  5. invertFloat(float n)
  6. invertFloat(float number)
  7. invertNumeral(int base, String number)