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

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

Description

Multiplies the quadratic matrix A (n-by-n) with the quadratic matrix B (n-by-n).

License

Open Source License

Parameter

Parameter Description
A a quadratic <tt>n</tt>-by-<tt>n</tt> matrix
B a quadratic <tt>n</tt>-by-<tt>n</tt> matrix
dest this quadratic <tt>n</tt>-by-<tt>n</tt> matrix holds the product of <tt>A</tt> and <tt>B</tt> on return
n size of the arrays to be multiplied

Declaration

public static void multiplyQuad(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  w  ww.  j  av  a  2s. c  om*/
     * Multiplies the quadratic matrix <tt>A</tt> (n-by-n) with the quadratic
     * matrix <tt>B</tt> (n-by-n). The result is stored in <code>dest</code>
     * (n-by-n), that is
     * 
     * <pre>
     * dest = A * B
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            a quadratic <tt>n</tt>-by-<tt>n</tt> matrix
     * @param B
     *            a quadratic <tt>n</tt>-by-<tt>n</tt> matrix
     * @param dest
     *            this quadratic <tt>n</tt>-by-<tt>n</tt> matrix holds the
     *            product of <tt>A</tt> and <tt>B</tt> on return
     * @param n
     *            size of the arrays to be multiplied
     */
    public static void multiplyQuad(final double[][] A, final double[][] B, final double[][] dest, int n) {
        assert hasShape(A, n, n);
        assert hasShape(B, n, n);
        assert hasShape(dest, n, n);

        for (int i = 0; i < n; i++) {
            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];
                }
            }
        }
    }

    /**
     * Multiplies the quadratic matrix <tt>A</tt> (n-by-n) with column vector
     * <tt>v</tt> (n-by-1). The result is stored in <code>dest</code> (n-by-1),
     * that is
     * 
     * <pre>
     * dest = A * v
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param A
     *            a quadratic <tt>n</tt>-by-<tt>n</tt> matrix
     * @param v
     *            a column vector of length <tt>n</tt>
     * @param dest
     *            this column vector of length <tt>n</tt> hols the product of
     *            <tt>A</tt> and <tt>v</tt> on return
     * @param n
     *            size of the arrays to be multiplied
     */
    public static void multiplyQuad(final double[][] A, final double[] v, final double[] dest, int n) {
        assert hasShape(A, n, n);
        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][0] * v[0];
            for (int j = 1; j < n; j++) {
                dest[i] += A[i][j] * v[j];
            }
        }
    }

    /**
     * Multiplies the row vector <tt>v</tt> (1-by-n) with the quadratic matrix
     * <tt>A</tt> (n-by-n). The result is stored in <code>dest</code> (1-by-n),
     * that is
     * 
     * <pre>
     * dest = v * A
     * </pre>
     * 
     * Note that this method does no safety checks (null or length).
     * 
     * @param vTransposed
     *            a row vector of length <tt>n</tt>
     * @param A
     *            a quadratic <tt>n</tt>-by-<tt>n</tt> matrix
     * @param destTransposed
     *            this row vector of length <tt>n</tt> hols the product of
     *            <tt>v</tt> and <tt>A</tt> on return
     * @param n
     *            size of the arrays to be multiplied
     */
    public static void multiplyQuad(final double[] vTransposed, final double[][] A, final double[] destTransposed,
            int n) {
        assert vTransposed != null;
        assert destTransposed != null;
        assert vTransposed.length >= n;
        assert destTransposed.length >= n;
        assert hasShape(A, n, n);

        for (int j = 0; j < n; j++) {
            destTransposed[j] = vTransposed[0] * A[0][j];
            for (int i = 1; i < n; i++) {
                destTransposed[j] += vTransposed[i] * A[i][j];
            }
        }
    }

    /**
     * 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. multiplyMatrices(float[][] left, float[][] right)
  2. multiplyMatrices(int[][] mat1, int[][] mat2)
  3. multiplyMatrix(int[][] a, int[] b, int mod)
  4. multiplyMatrixByMatrix(double[][] a, double[][] b)
  5. multiplyMatrixes(double[][] m1, double[][] m2)
  6. multiplyScalars(float[][] x, float[][] y)
  7. multiplyScalars_optimizedByKnowingBothAreRectangle(float[][] x, float[][] y)
  8. multiplySparse2dense(int[][][] as, int A, int[][][] bs, int B, int[][] c)
  9. multiplyVectorAndMatrix(double[] vector, double[][] matrix)