Strassen Algorithm for Matrix Multiplication

Source: Internet
Author: User

In general, matrix multiplication requires three for loops. the time complexity is O (n ^ 3). Now we will partition the matrix (from introduction to MIT algorithms) generally, the algorithm needs to multiply r = a * e + B * g eight times; s = a * f + B * h; t = c * e + d * g; u = c * f + d * h; strassen converts it into seven multiplications, because we all know that multiplication consumes more than addition and subtraction, and all time is more complex and more complex! Strassen's processing is: Order: p1 = a * (f-h) p2 = (a + B) * hp3 = (c + d) * ep4 = d * (g-e) p5 = (a + d) * (e + h) p6 = (B-d) * (g + h) p7 = (a-c) * (e + f) so we can know: r = p5 + p4 + p6-p2s = p1 + p2t = p3 + p4u = p5 + p1-p3-p7 we can see that there are only seven multiplication and multiple addition and subtraction operations above, the ultimate goal is to reduce the complexity to O (n ^ lg7 )~ = O (n ^ 2.81); Code implementation: [cpp] // strassen algorithm: reduce the complexity of matrix multiplication to O (n ^ lg7 )~ = O (n ^ 2.81) // The principle is to reduce 8 multiplications to 7 Operations // The best algorithm in theory is O (n ^ 2,367 ), it's just theoretical. // the following code is just a simple instance. You don't have to be honest ~ // The following space can be optimized, so it won't be difficult here ~ # Include <stdio. h> # define N 10 // matrix + matrix void plus (int t [N/2] [N/2], int r [N/2] [N/2], int s [N/2] [N/2]) {int I, j; for (I = 0; I <N/2; I ++) {for (j = 0; j <N/2; j ++) {t [I] [j] = r [I] [j] + s [I] [j] ;}} // matrix-matrix void minus (int t [N/2] [N/2], int r [N/2] [N/2], int s [N/2] [N/2]) {int I, j; for (I = 0; I <N/2; I ++) {for (j = 0; j <N/2; j ++) {t [I] [j] = r [I] [j]-s [I] [j] ;}}// matrix * matrix void mul (int t [N/2] [N/2], int r [N/2] [N/2], int s [N/2] [N/2]) {int I, j, k; for (I = 0; I <N/2; I ++) {for (j = 0; j <N/2; j ++) {t [I] [j] = 0; for (k = 0; k <N/2; k ++) {t [I] [j] + = r [I] [k] * s [k] [j] ;}}} int main () {int I, j, k; int mat [N] [N]; int m1 [N] [N]; int m2 [N] [N]; int a [N/2] [N/2], B [N/2] [N/2], c [N/2] [N/2], d [N/2] [N/2]; int e [N/2] [N/2], f [N/2] [N/2 ], G [N/2] [N/2], h [N/2] [N/2]; int p1 [N/2] [N/2], p2 [N/2] [N/2], p3 [N/2] [N/2], p4 [N/2] [N/2]; int p5 [N/2] [N/2], p6 [N/2] [N/2], p7 [N/2] [N/2]; int r [N/2] [N/2], s [N/2] [N/2], t [N/2] [N/2], u [N/2] [N/2], t1 [N/2] [N/2], t2 [N/2] [N/2]; printf ("\ nInput the first matrix...: \ n "); for (I = 0; I <N; I ++) {for (j = 0; j <N; j ++) {scanf ("% d", & m1 [I] [j]) ;}} printf ("\ nInput the second matrix...: \ n "); for (I = 0; I <N; I + +) {For (j = 0; j <N; j ++) {scanf ("% d", & m2 [I] [j]) ;}} // a B c d e f g h for (I = 0; I <N/2; I ++) {for (j = 0; j <N/2; j ++) {a [I] [j] = m1 [I] [j]; B [I] [j] = m1 [I] [j + N/2]; c [I] [j] = m1 [I + N/2] [j]; d [I] [j] = m1 [I + N/2] [j + N/2]; e [I] [j] = m2 [I] [j]; f [I] [j] = m2 [I] [j + N/2]; g [I] [j] = m2 [I + N/2] [j]; h [I] [j] = m2 [I + N/2] [j + N/2];} // p1 minus (r, f, h ); mul (p1, A, r); // p2 plus (r, a, B); mul (p2, r, h); // p3 plus (r, c, d ); mul (p3, r, e); // p4 minus (r, g, e); mul (p4, d, r); // p5 plus (r,, d); plus (s, e, f); mul (p5, r, s); // p6 minus (r, B, d); plus (s, g, h); mul (p6, r, s); // p7 minus (r, a, c); plus (s, e, f); mul (p7, r, s); // r = p5 + p4-p2 + p6 plus (t1, p5, p4); minus (t2, t1, p2); plus (r, t2, p6); // s = p1 + p2 plus (S, p1, p2); // t = p3 + p4 plus (t, p3, p4 ); // u = p5 + p1-p3-p7 = p5 + p1-(p3 + p7) plus (t1, p5, p1); plus (t2, p3, p7 ); minus (u, t1, t2); for (I = 0; I <N/2; I ++) {for (j = 0; j <N/2; j ++) {mat [I] [j] = r [I] [j]; mat [I] [j + N/2] = s [I] [j]; mat [I + N/2] [j] = t [I] [j]; mat [I + N/2] [j + N/2] = u [I] [j];} printf ("\ n below is the strassen algorithm processing result: \ n "); for (I = 0; I <N; I ++) {f Or (j = 0; j <N; j ++) {printf ("% d", mat [I] [j]);} printf ("\ n") ;}// The following is the result of processing the SIMPLE algorithm printf ("\ n: \ n"); for (I = 0; I <N; I ++) {for (j = 0; j <N; j ++) {mat [I] [j] = 0; for (k = 0; k <N; k ++) {mat [I] [j] + = m1 [I] [j] * m2 [I] [j] ;}} for (I = 0; I <N; I ++) {for (j = 0; j <N; j ++) {printf ("% d ", mat [I] [j]);} printf ("\ n");} return 0;} the greatest complexity of matrix multiplication is O (n ^ 2.376 ), but it is only a theoretical result. For more information, see ~

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.