Java Matrix Invert invert2x2(final double[][] src, final double[][] dest)

Here you can find the source of invert2x2(final double[][] src, final double[][] dest)

Description

Copy the inverse of the 2-by-2 matrix src intro 2-by-2 matrix dest

License

Open Source License

Parameter

Parameter Description
src <tt>2</tt> by <tt>2</tt> matrix
dest <tt>2</tt> by <tt>2</tt> matrix to store the inverse of src

Exception

Parameter Description
Exception an exception

Declaration

public static void invert2x2(final double[][] src, final double[][] dest) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from   w  w w . ja v a  2s .  c om*/
     * Copy the inverse of the 2-by-2 matrix src intro 2-by-2 matrix dest 
     * 
     * @param src
     *            <tt>2</tt> by <tt>2</tt> matrix
     * @param dest
     *            <tt>2</tt> by <tt>2</tt> matrix to store the inverse of src
     * @throws Exception 
     * 
     */
    public static void invert2x2(final double[][] src, final double[][] dest) throws Exception {
        assert hasShape(src, 2, 2);
        assert hasShape(dest, 2, 2);
        assert src != dest;

        final double tmp = src[0][0] * src[1][1] - src[0][1] * src[1][0];
        if (tmp == 0.) {
            throw new Exception();
        }
        dest[0][0] = src[1][1] / tmp;
        dest[1][1] = src[0][0] / tmp;
        dest[0][1] = -src[0][1] / tmp;
        dest[1][0] = -src[1][0] / tmp;
    }

    /**
     * Returns whether matrix mat has size l1-by-l2 or not 
     * @param mat   a matrix
     * @param l1   first dimension (lines)
     * @param l2   second dimension (columns)
     * @return
     */
    public static boolean hasShape(final double[][] mat, int l1, int l2) {
        assert mat != null;
        assert l1 > 0;
        assert l2 > 0;

        if (mat.length != l1) {
            return false;
        }
        for (int i = 0; i < mat.length; i++) {
            if (mat[i].length != l2) {
                return false;
            }
        }
        return true;
    }

    public static boolean hasShape(final double[][][] mat3d, int l1, int l2, int l3) {
        assert mat3d != null;
        assert l1 > 0;
        assert l2 > 0;
        assert l3 > 0;

        if (mat3d.length != l1) {
            return false;
        }
        for (int i = 0; i < mat3d.length; i++) {
            if (mat3d[i].length != l2) {
                return false;
            }
            for (int j = 0; j < mat3d[i].length; j++) {
                if (mat3d[i][j].length != l3) {
                    return false;
                }
            }
        }
        return true;
    }
}

Related

  1. invert(double a[][])
  2. invert(final int[][] input)
  3. invert(float[] mat)
  4. invert3x3(float[] mat)
  5. invert44Matrix(final double[] m, final double[] invOut)
  6. invertDataSet(double[][] dataSet)
  7. invertMatrix(int[][] matrix)