Matrix Fast Power

Source: Internet
Author: User

Often referred to the matrix Fast Power, the study today, is to transform the problem into binary discretization, subtly reduce the computational volume.

The fast power of matrices is used to efficiently calculate the high-order of matrices. Reduce the time complexity of Simple O (n) to log (n).

Generally a matrix of n-Squares, we will pass the n-1 times to get its n power. But the simple improvement can reduce the number of times of the multiplication, the method is as follows:

22 Grouping of n matrices, for example: A*a*a*a*a*a = (a*a) * (a*a) * (a*a) The benefit of this is that you only need to calculate the a*a once and then multiply the result (a*a) by yourself twice to get a^6, ie (a*a) ^3=a^6. It is found that this time has been multiplied by 3 times, less than the original 5 times. In fact, we can also take a^3 as a basic unit. The principle is the same: using the combination of matrix multiplication to reduce the number of repeated calculations. The above is to take a specific number as the smallest unit length, although this can improve efficiency, but the flaw is also very obvious, take a limit of the example (may be a bit inappropriate, but the basic can explain the problem), when the n infinity, you now take the length of the 1 is no different. Therefore, we need to find a growth rate of n "to adapt" to the "unit length", then the length of how to take it??? This is the question we have to think about. With the above knowledge, we will now look at how quickly to find the matrix of the N power.

Now that you want to reduce repetitive calculations, make the most of the existing calculations! ~ How to make full use of the results of the calculation??? Consider the idea of two points here. The first thing you should realize is that any integer n can be represented by a binary: Everyone should know this point, but the connotation of it is really deep and deep (this I feel very deep, at the end of the article, I will talk about my feelings)!! Computer processing is discrete information, are 0, one as a signal processing. It is conceivable that the binary system plays a pivotal role in the computer.  It can transform the analog signal into a digital signal, the original continuous actual model, with a discrete algorithm model to solve. Well, a little bit more, but I believe this writing to the following explanation is still useful.

Looking back at the fast power of the matrix, can we also disperse it? For example a^19 = (a^16) * (a^2) * (a^1), obviously in such a way to calculate the number of factors will be log (n) level (the original factor is N), not only that, there is a link between the factors, such as a^4 through (a^2) * (a^2) to get, a^ 8 can be obtained through (a^4) * (A^4), which also makes full use of the available results as a favourable condition. Here's an example: now requires a^156, while 156 (10) =10011100 (2) also has a^156=> (a^4) * (a^8) * (a^16) * (a^128) Considering the relationship between the factors, We start from the far right side of binary 10011100 to the leftmost end. Here's the detail, here's the core code:

 while (N) {                if(n&1)                       res=res*A;                n>>=1;                A=a*A;}

Inside the multiplication sign, is the matrix multiplication, res is the result matrix.

The 3rd line of code is every time the binary number is less than one 1 of the last face. The number of binaries is 1 how many times the 3rd line of code executes.

Well, the quick power of the matrix is explained here. In the article I finally give the specific code that implements the fast power (the code takes the 3*3 matrix as an example).

Now I'm going to say my thoughts about binary:

When we do a lot of "continuous" problems, we use the binary to simplify their discretization.

1. Multiple knapsack problems

2. Tree-like array

3. State compression DP

............... There's a lot more ... It is still the sentence: the continuity of the discrete. Most of the time we don't use binary to solve a problem, it's more time to use it for optimization. So if you want your program to be more adaptable to big data, learning to learn binary and its algorithmic thinking will help you a lot.

Here are the experiments I did:

//Matrix Fast Power//The fast power of matrices is used to efficiently calculate the high-order of matrices. Reduce the time complexity of Simple O (n) to log (n). #include<iostream>using namespacestd;intN;structmatrix{inta[3][3];}    Origin,res;matrix Multiply (matrix x, Matrix y) {matrix temp; memset (Temp.a,0,sizeof(TEMP.A));  for(size_t i =0; I <3; i++)    {         for(size_t j =0; J <3; J + +)        {             for(size_t k =0; K <3; k++) {Temp.a[i][j]+ = x.a[i][k] *Y.a[k][j]; }        }    }    returntemp;}voidinit () {printf ("The random array is as follows: \ n");  for(size_t i =0; I <3; i++)    {         for(size_t j =0; J <3; J + +) {Origin.a[i][j]= rand ()%Ten; printf ("%d", Origin.a[i][j]); printf ("\ t"); } printf ("\ n"); } printf ("\ n"); memset (RES.A,0,sizeof(RES.A)); res.a[0][0] = res.a[1][1] = res.a[2][2]=1;//initialize to the unit matrix}voidCalcintN) {printf ("The result of%d powers is as follows: \ n", N);  while(n) {if(n&1) {res= Multiply (res,origin);//the unit matrix and any matrix multiplication equals the matrix itself a^156=> (a^4) * (a^8) * (a^16) * (a^128) Extract useful origin to multiply} n>>=1; Origin= Multiply (Origin, Origin);//This is still a tiring ride .    }     for(size_t i =0; I <3; i++)    {         for(size_t j =0; J <3; J + +) {printf ("%d", Res.a[i][j]); printf ("\ t"); } printf ("\ n"); } printf ("\ n");}intMain () { while(cin>>N) {init ();    Calc (N); }    return 0;}

Matrix Fast Power

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.