Java Matrix Multiply multiplyMatrices(double[][] a, double[][] b)

Here you can find the source of multiplyMatrices(double[][] a, double[][] b)

Description

Multiplies two matrices using an implementation faster than the straightforward one.

License

Open Source License

Parameter

Parameter Description
a Matrix of doubles.
b Matrix of doubles.

Return

Matrix computed by matrix multiplication of the two matrices.

Declaration

public static double[][] multiplyMatrices(double[][] a, double[][] b) 

Method Source Code

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

public class Main {
    /**/*from ww w .  jav a 2s .c o m*/
     * Multiplies two matrices using an implementation faster than the
     * straightforward one. The multiplication order is A * B and the results is
     * a matrix.
     *
     * @param a   Matrix of doubles.
     * @param b   Matrix of doubles.
     * @return   Matrix computed by matrix multiplication of the two matrices.
     */
    public static double[][] multiplyMatrices(double[][] a, double[][] b) {

        double[][] c = new double[a.length][b[0].length];
        for (int i = 0; i < a.length; i++) {
            for (int k = 0; k < b.length; k++) {
                for (int j = 0; j < b[0].length; j++) {
                    c[i][j] += a[i][k] * b[k][j];
                }
            }
        }

        return c;
    }

    /**
     * Multiplies two matrices using an implementation faster than the
     * straightforward one. The multiplication order is A * B and the result is
     * a matrix.
     *
     * @param a   Matrix of integers.
     * @param b   Matrix of integers.
     * @return   Matrix result of matrix multiplication of the two matrices.
     */
    public static double[][] multiplyMatrices(int[][] a, int[][] b) {

        double[][] c = new double[a.length][b[0].length];
        for (int i = 0; i < a.length; i++) {
            for (int k = 0; k < b.length; k++) {
                for (int j = 0; j < b[0].length; j++) {
                    c[i][j] += (double) a[i][k] * (double) b[k][j];
                }
            }
        }

        return c;
    }
}

Related

  1. multiply(float[][] a, float num)
  2. multiply(float[][] a, float[][] b)
  3. multiply(int[][] mat1, int[][] mat2)
  4. multiply(String[] tempResult, int nextIndex, String[][] pys)
  5. multiplyAffine(final double[][] A, final double[][] B, final double[][] dest, int n)
  6. multiplyMatrices(float[][] left, float[][] right)
  7. multiplyMatrices(int[][] mat1, int[][] mat2)
  8. multiplyMatrix(int[][] a, int[] b, int mod)
  9. multiplyMatrixByMatrix(double[][] a, double[][] b)