/* * Copyright (C) 2009 E.Albert, P.Arenas, S.Genaim, G.Puebla, and D.Zanardini * https://costa.ls.fi.upm.es * * 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/>. */ package pubs; /** * A class with a multiply method which multiplies two matrices. * * @author E.Albert, P.Arenas, S.Genaim, and G.Puebla * */ public class MatMult { public int[][] multiply(int[][] A, int[][] B) { int c = A.length; //int r = A[0].length; int r = B.length; int[][] C = new int[r][c]; for(int i=0; i < r;i++){ for( int j =0;j< c;j++){ for (int k=0;k<c;k++){ C[i][j] = C[i][j] + (A[i][k] *B[k][j]); } } } return C; } public static void main(String[] args) { final int rows1 = 10 , rows2 = 10; final int columns = 10; int[][] testA = new int[rows1][columns]; int[][] testB = new int[columns][rows2]; for (int i = 0; i < testA.length; i++) { for (int j = 0; j < testA[0].length; j++) { testA[i][j] = i; } } for (int i = 0; i < testB.length; i++) { for (int j = 0; j < testB[0].length; j++) { testB[i][j] = i; } } MatMult testClass = new MatMult(); int[][] result = testClass.multiply(testA,testB); /*System.out.println("Result: "); for (int i = 0; i < result.length; i++) { for (int j = 0; j < result[0].length; j++) { System.out.println(result[i][j]+" "); } System.out.println("\n"); }*/ } }