[Algorithm design and analysis basics] 2. Multiplication of matrices, multiplication of algorithm design and analysis

Source: Internet
Author: User

[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:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.