Java Matrix Multiply matrixMultiply(double[][] A, double[][] B, int aHeight, int bWidth, int comm, int bz, double[][] C)

Here you can find the source of matrixMultiply(double[][] A, double[][] B, int aHeight, int bWidth, int comm, int bz, double[][] C)

Description

matrix Multiply

License

Apache License

Declaration

public static void matrixMultiply(double[][] A, double[][] B, int aHeight, int bWidth, int comm, int bz,
            double[][] C) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static void matrixMultiply(double[][] A, double[][] B, int aHeight, int bWidth, int comm, int bz,
            double[][] C) {

        int aHeightBlocks = aHeight / bz; // size = Height of A
        int aLastBlockHeight = aHeight - (aHeightBlocks * bz);
        if (aLastBlockHeight > 0) {
            aHeightBlocks++;/*from w  ww  .  ja  v a  2s  .  co  m*/
        }

        int bWidthBlocks = bWidth / bz; // size = Width of B
        int bLastBlockWidth = bWidth - (bWidthBlocks * bz);
        if (bLastBlockWidth > 0) {
            bWidthBlocks++;
        }

        int commnBlocks = comm / bz; // size = Width of A or Height of B
        int commLastBlockWidth = comm - (commnBlocks * bz);
        if (commLastBlockWidth > 0) {
            commnBlocks++;
        }

        int aBlockHeight = bz;
        int bBlockWidth = bz;
        int commBlockWidth = bz;

        for (int ib = 0; ib < aHeightBlocks; ib++) {
            if (aLastBlockHeight > 0 && ib == (aHeightBlocks - 1)) {
                aBlockHeight = aLastBlockHeight;
            }
            bBlockWidth = bz;
            commBlockWidth = bz;
            for (int jb = 0; jb < bWidthBlocks; jb++) {
                if (bLastBlockWidth > 0 && jb == (bWidthBlocks - 1)) {
                    bBlockWidth = bLastBlockWidth;
                }
                commBlockWidth = bz;
                for (int kb = 0; kb < commnBlocks; kb++) {
                    if (commLastBlockWidth > 0 && kb == (commnBlocks - 1)) {
                        commBlockWidth = commLastBlockWidth;
                    }

                    for (int i = ib * bz; i < (ib * bz) + aBlockHeight; i++) {
                        for (int j = jb * bz; j < (jb * bz) + bBlockWidth; j++) {
                            for (int k = kb * bz; k < (kb * bz) + commBlockWidth; k++) {
                                if (A[i][k] != 0 && B[k][j] != 0) {
                                    C[i][j] += A[i][k] * B[k][j];
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    public static void matrixMultiply(double[][] A, double[] B, int aHeight, int bWidth, int comm, int bz,
            double[] C) {

        int aHeightBlocks = aHeight / bz; // size = Height of A
        int aLastBlockHeight = aHeight - (aHeightBlocks * bz);
        if (aLastBlockHeight > 0) {
            aHeightBlocks++;
        }

        int bWidthBlocks = bWidth / bz; // size = Width of B
        int bLastBlockWidth = bWidth - (bWidthBlocks * bz);
        if (bLastBlockWidth > 0) {
            bWidthBlocks++;
        }

        int commnBlocks = comm / bz; // size = Width of A or Height of B
        int commLastBlockWidth = comm - (commnBlocks * bz);
        if (commLastBlockWidth > 0) {
            commnBlocks++;
        }

        int aBlockHeight = bz;
        int bBlockWidth = bz;
        int commBlockWidth = bz;

        double kTmp;
        int iOffset;
        for (int ib = 0; ib < aHeightBlocks; ib++) {
            if (aLastBlockHeight > 0 && ib == (aHeightBlocks - 1)) {
                aBlockHeight = aLastBlockHeight;
            }
            bBlockWidth = bz;
            commBlockWidth = bz;
            for (int jb = 0; jb < bWidthBlocks; jb++) {
                if (bLastBlockWidth > 0 && jb == (bWidthBlocks - 1)) {
                    bBlockWidth = bLastBlockWidth;
                }
                commBlockWidth = bz;
                for (int kb = 0; kb < commnBlocks; kb++) {
                    if (commLastBlockWidth > 0 && kb == (commnBlocks - 1)) {
                        commBlockWidth = commLastBlockWidth;
                    }

                    for (int i = ib * bz; i < (ib * bz) + aBlockHeight; i++) {
                        iOffset = i * bWidth;
                        for (int j = jb * bz; j < (jb * bz) + bBlockWidth; j++) {
                            for (int k = kb * bz; k < (kb * bz) + commBlockWidth; k++) {
                                kTmp = B[k * bWidth + j];
                                if (A[i][k] != 0 && kTmp != 0) {
                                    C[iOffset + j] += A[i][k] * kTmp;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Related

  1. matrix4x4f_Mult(float[] result, float[] a, float[] b)
  2. matrix_matrix_mult(double ad[][], double ad1[][], double ad2[][])
  3. matrixMult(final float[] result, float[] m1, float[] m2)
  4. matrixMultiplication(double[][] w, double[][] v)
  5. matrixMultiply(double[][] A, double[][] B, int n)
  6. matrixMultiply(double[][] M, float[] V)
  7. matrixMultiply(double[][] M1, double[][] M2)
  8. matrixmultiply(final double[][] A, final double[] b)