[Algorithm design and analysis basics] 2. Multiplication of matrices, multiplication of algorithm design and analysis
Recently, I was engaged in the activity, but it came to an end. I joined the learning process again and started the simultaneous learning between the algorithm and jquery. Come on !!!
Package cn. xf. algorithm. ch02;/*** calculate the product of the Matrix * @ author xiaof **/public class Matrix {/*** Matrix A, multiply by B to obtain the corresponding value * Note: when the number of columns in matrix A is equal to the number of rows in matrix B, A and B can be multiplied. * @ Param a * @ param B * @ return */public static Long [] [] matrixMultiplication (Long a [] [], Long B [] [], int aRow, int aColumn, int bRow, int bColumn) {if (aColumn! = BRow) {return null ;} // The result set is A matrix of rows in A matrix and columns in B Matrix Long result [] [] = new Long [aRow] [bColumn]; // first traverse the corresponding rows and columns of the two matrices for (int I = 0; I <aRow; ++ I) {for (int j = 0; j <aColumn; + + j) {// returns the result [I] [j] = 0l after the multiplication of the corresponding rows and columns; // first set the current value to 0for (int k = 0; k <bRow; ++ k) {// accumulate the product and Long count1 = a [I] [k]; Long count2 = B [k] [j]; result [I] [j] + = a [I] [k] * B [k] [j] ;}} return result ;} public static void main (String [] args) {// matrix a, bLong a [] [] = new Long [] [] {1l, 2l}, {3l, 4l }}; Long B [] [] = new Long [] [] {1l, 2l}, {3l, 4l }}; Long result [] [] = Matrix. matrixMultiplication (a, B, 2, 2, 2, 2); for (int I = 0; I <2; ++ I) {for (int j = 0; j <2; ++ j) {System. out. print (result [I] [j] + "\ t");} System. out. println ();}}}
Result: