Multiplies two matrices. - Java java.lang

Java examples for java.lang:Math Matrix

Description

Multiplies two matrices.

Demo Code


//package com.java2s;

public class Main {
    /**// ww  w.j  a  v a2  s. c om
     * Multiplies two matrices.
     * @param A Matrix (m x n).
     * @param B Matrix (n x o).
     * @return The resulting matrix of size m x o.
     */
    public static double[][] matrix_x_matrix(double[][] A, double[][] B) {

        final int nA = A[0].length;
        final int mC = A.length;
        final int nC = B[0].length;

        final double[][] C = new double[mC][nC];

        for (int i = 0; i < mC; i++) {
            final double[] C_row = C[i];
            final double[] A_row = A[i];
            for (int j = 0; j < nC; j++) {
                for (int k = 0; k < nA; k++) {
                    C_row[j] += A_row[k] * B[k][j];
                }
            }
        }
        return C;
    }
}

Related Tutorials