Java Matrix Multiply multiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n)

Here you can find the source of multiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n)

Description

Multiplies two affine matrices A and B (n by n+1, linear part plus translation).

License

Open Source License

Parameter

Parameter Description
A an affine <tt>n</tt>-by-<tt>n+1</tt> matrix
B an affine <tt>n</tt>-by-<tt>n+1</tt> matrix
dest this affine <tt>n</tt>-by-<tt>n+1</tt> matrix holds the product of <tt>A</tt> and <tt>B</tt> on return
n dimension of matrices (without translational component)

Declaration

public static void multiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n) 

Method Source Code

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

public class Main {
    /**//from ww w .jav a 2s  .com
     * Multiplies two affine matrices <tt>A</tt> and <tt>B</tt> (n by n+1,
     * linear part plus translation). The result is stored in <code>dest</code>,
     * that is
     * 
     * <pre>
     * dest = A * B
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            an affine <tt>n</tt>-by-<tt>n+1</tt> matrix
     * @param B
     *            an affine <tt>n</tt>-by-<tt>n+1</tt> matrix
     * @param dest
     *            this affine <tt>n</tt>-by-<tt>n+1</tt> matrix holds the
     *            product of <tt>A</tt> and <tt>B</tt> on return
     * @param n
     *            dimension of matrices (without translational component)
     */
    public static void multiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n) {
        assert A != null;
        assert B != null;
        assert dest != null;
        assert hasShape(A, n, n + 1);
        assert hasShape(B, n, n + 1);
        assert hasShape(dest, n, n + 1);

        for (int i = 0; i < n; i++) {
            // regular linear part
            for (int j = 0; j < n; j++) {
                dest[i][j] = A[i][0] * B[0][j];
                for (int k = 1; k < n; k++) {
                    dest[i][j] += A[i][k] * B[k][j];
                }
            }
            // translational part
            dest[i][n] = A[i][n]; // * 1
            for (int k = 0; k < n; k++) {
                dest[i][n] += A[i][k] * B[k][n];
            }
        }
    }

    /**
     * Multiplies the affine matrix <tt>A</tt> (n by n+1, linear part plus
     * translation) with the column vector <tt>v</tt> (n-by-1) and adds the
     * translational component of <tt>A</tt> to <tt>v</tt>. The result is stored
     * in <code>dest</code>, that is
     * 
     * <pre>
     * dest = (A * v) + translation
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            an affine <tt>n</tt> by <tt>n+1</tt> matrix
     * @param v
     *            a column vector of length <tt>n</tt>
     * @param dest
     *            this column vector of length <tt>n</tt> holds the result of
     *            the affine transformation
     * @param n
     *            size of the arrays to be multiplied (not including the
     *            extended column/row)
     */
    public static void multiplyAffine(final double[][] A, final double[] v, final double[] dest, int n) {
        assert hasShape(A, n, n + 1);
        assert v != null;
        assert dest != null;
        assert v.length >= n;
        assert dest.length >= n;

        for (int i = 0; i < n; i++) {
            dest[i] = A[i][n]; // translation
            for (int j = 0; j < n; j++) {
                dest[i] += A[i][j] * v[j]; // linear part
            }
        }
    }

    /**
     * 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. multiply(final double[][] A, final double[][] B)
  2. multiply(float[][] a, float num)
  3. multiply(float[][] a, float[][] b)
  4. multiply(int[][] mat1, int[][] mat2)
  5. multiply(String[] tempResult, int nextIndex, String[][] pys)
  6. multiplyMatrices(double[][] a, double[][] b)
  7. multiplyMatrices(float[][] left, float[][] right)
  8. multiplyMatrices(int[][] mat1, int[][] mat2)
  9. multiplyMatrix(int[][] a, int[] b, int mod)