Java Matrix Multiply multiply(double[][] x, double[][] y)

Here you can find the source of multiply(double[][] x, double[][] y)

Description

matrix multiplication

License

Open Source License

Parameter

Parameter Description
x a parameter
y a parameter

Return

x*y

Declaration

public static double[][] multiply(double[][] x, double[][] y) 

Method Source Code

//package com.java2s;
/*//from   ww  w .  j  av  a2s  .c om
 *  Copyright (C) 2017 Daniel H. Huson
 *
 *  (Some files contain contributions from other authors, who are then mentioned separately.)
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * matrix multiplication
     *
     * @param x
     * @param y
     * @return x*y
     */
    public static double[][] multiply(double[][] x, double[][] y) {
        final int rowsX = x.length;
        final int colsX = x[0].length;
        final int rowsY = y.length;
        final int colsY = y[0].length;

        if (colsX != rowsY)
            throw new RuntimeException("multiply(x,y): incompatible dimensions");

        final double[][] z = new double[rowsX][colsY];

        for (int a = 0; a < rowsX; a++) {
            for (int b = 0; b < colsY; b++) {
                double value = 0;
                for (int c = 0; c < colsX; c++) {
                    value += x[a][c] * y[c][b];
                }
                z[a][b] = value;
            }
        }
        return z;
    }
}

Related

  1. multiply(double[][] a, double[][] x)
  2. multiply(double[][] dest, double[][] a, double[][] b)
  3. multiply(double[][] m, double[] x)
  4. multiply(double[][] m1, double[][] m2)
  5. multiply(double[][] p, double[][] q)
  6. multiply(final double[][] A, final double[][] B)
  7. multiply(float[][] a, float num)
  8. multiply(float[][] a, float[][] b)
  9. multiply(int[][] mat1, int[][] mat2)