Java Matrix Multiply matrixMultiplyTwo(final double[][] first, final double[][] second)

Here you can find the source of matrixMultiplyTwo(final double[][] first, final double[][] second)

Description

matrix Multiply Two

License

Open Source License

Declaration

public static final double[][] matrixMultiplyTwo(final double[][] first, final double[][] second) 

Method Source Code

//package com.java2s;
/*/*from   w ww. j  a va2 s  .co  m*/
 * ====================================================
 * Copyright (C) 2013 by Idylwood Technologies, LLC. All rights reserved.
 *
 * Developed at Idylwood Technologies, LLC.
 * Permission to use, copy, modify, and distribute this
 * software is freely granted, provided that this notice
 * is preserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * The License should have been distributed to you with the source tree.
 * If not, it can be found at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author: Charles Cooper
 * Date: 2013
 * ====================================================
 */

import java.util.Arrays;

public class Main {
    public static final double[][] matrixMultiplyTwo(final double[][] first, final double[][] second) {
        final int firstRows = first.length;
        final int firstCols = first[0].length;
        final int secondRows = second.length;
        final int secondCols = second[0].length;
        if (firstCols != secondRows)
            throw new ArrayIndexOutOfBoundsException("Trying to multiply matrices of different dimensions?!");
        final double ret[][] = new double[firstRows][secondCols];
        final double err[][] = new double[firstRows][secondCols];

        final int unroll = 3;
        final int len = secondCols - secondCols % unroll;

        for (int j = 0; j < firstRows; j++) {
            for (int i = 0; i < secondCols; i++) {
                Arrays.fill(ret[j], 0);
                Arrays.fill(err[j], 0);
                for (int k = 0; k < firstCols; k++) {
                    final double prod = first[j][k] * second[k][i];
                    final double sum = ret[j][i];
                    final double hi = sum + prod;
                    ret[j][i] = hi;
                    err[j][i] += hi - sum - prod;
                }
            }
        }

        for (int i = 0; i < firstRows; i++)
            for (int j = 0; j < secondCols; j++)
                ret[i][j] -= err[i][j];
        return ret;
    }
}

Related

  1. matrixMultiply(double[][] A, double[][] B, int aHeight, int bWidth, int comm, int bz, double[][] C)
  2. matrixMultiply(double[][] A, double[][] B, int n)
  3. matrixMultiply(double[][] M, float[] V)
  4. matrixMultiply(double[][] M1, double[][] M2)
  5. matrixmultiply(final double[][] A, final double[] b)
  6. matrixMultiplyWithThreadOffset(double[] A, double[] Adiag, double[] B, int aHeight, int bWidth, int comm, int bz, int threadRowOffset, int rowOffset, double[] C)
  7. matrixScalarMultiplication(double[][] w, double v)
  8. multiply(boolean[][] m1, boolean[][] m2)
  9. multiply(boolean[][] matrix, boolean[] vector)