Matrix multiplication (a): the basic operation

      Matrix, one of the basic concepts of linear algebra. A m × n matrix of m × n is the number m rows and n columns arranged in a matrix number. In the computer, a matrix is ​​actually a two-dimensional array. Thus, the matrix may be defined as a structure:

      the Matrix struct
      {
            int MAT [110] [110]; // matrix elements stored
            int row, col; // size of the matrix, row row, col columns
      };

     Matrix multiplication is a basic matrix operations.

     Let A m × n matrix, B is n × k matrix, then their product AB (sometimes denoted A · B) is a m × k matrix.

     I-th row which A · B product matrix elements of the j-th column is obtained by multiplying the number n corresponding to the number n of the matrix A on the first i-th row and j-th column of the second matrix B n a sum of products. which is:

 

      Note that: only when the same number of rows and columns of the matrix A matrix B, A × B matrix makes sense. Thus, matrix multiplication is not commutative. Let A be 3 × 4 matrix, B is the matrix of 4 × 5, multiplied with A B, A · B is a 3 × 5 matrix; B · A but simply can not operational.

      Matrix multiplication is associative.

[Example 1] matrix multiplication.
      A matrix and the input matrix data b, the output of new matrix c = a * b.

      For example, sample input

. 4. 3
. 1 2. 3
. 4. 5. 6
. 7. 8. 9
10. 11 12 is
. 3. 5
. 7. 9 10. 8. 11
. 4. 5. 6. 7. 8
. 1 2. 4. 3. 5
sample output
18 is 42 is 36 24 30
54 is 69 84 114 99
90 138 162 186 114
126 159 192 225 258

      (1) programming ideas.

       According to the definition of matrix multiplication, the computation is completed by a triple loop.

      (2) source.

#include <stdio.h>
#include <string.h>
struct Matrix
{
      int mat[110][110]; // 存储矩阵中各元素
      int row,col; // 矩阵的大小,row行,col列
};
Matrix matMul(Matrix a ,Matrix b)      // 矩阵A*B
{
      Matrix c;
      c.row=a.row;
      c.col=b.col;
      memset(c.mat,0,sizeof(c.mat));
      int i,j,k;
      for (i = 0; i<=a.row ; i++)
         for (j=0 ;j<b.col; j++)
              for (k = 0 ;k<a.col;k++)
                   c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
      return c;
}
int main()
{
      int i,j,x,y;
      Matrix a,b,c;
      scanf("%d%d",&x,&y);
      a.row=x;
      a.col=y;
      for (i=0;i<x;i++)
          for (j=0;j<y;j++)
              scanf("%d" ,&a.mat[i][j]);
      scanf("%d%d",&x,&y);
      b.row=x;
      b.col=y;
      for (i=0;i<x;i++)
           for (j=0;j<y;j++)
               scanf("%d" ,&b.mat[i][j]);
      c=matMul(a,b);
      for (i = 0 ;i <c.row;i++)
      {
           for (j=0;j<c.col;j++)
               printf("%5d" ,c.mat[i][j]);
           printf("\n");
      }
      return 0;
}

      In practical applications, we often use the power matrix operations.

      A n matrix multiplication referred to as n-th power A or A ^ n is called n-th power of the matrix A.

      A n-th power of the Matrix usually fast exponentiation. Let's explore the idea of ​​fast exponentiation.

      Since the binding matrix multiplication with the law, and therefore A ^ 4 = A * A * A * A = (A * A) * (A * A) = A ^ 2 * A ^ 2. It can be concluded that: when n is even, A ^ n = A ^ (n / 2) * A ^ (n / 2); when n is odd, A ^ n = A ^ (n / 2 ) * A ^ (n / 2) * A (where n / 2 rounded). In this way, we can use a similar dichotomy of thinking quickly obtain power matrix.

       For example, A ^ 9 = A * A * A * A * A * A * A * A * A (by a one to nine times by)

                        = A*(A*A)*(A*A)*(A*A)*(A*A)

                        = A*(A^2)^4  

                        = A * ((A ^ 2) ^ 2) ^ 2 (the square of the A, and then squared, and then squared, multiplied by the remaining one of A, to multiply 4)

       Set C = A ^ k, C is initialized to an identity matrix, i.e., in addition to the elements of the matrix C is a diagonal, the remaining elements all 0.

       c.mat[i][i]=1 ,          c,mat[i][j]=0  (i!=j)。

       Any matrix is ​​a matrix by itself. That can be an integer of 1 equivalent matrix. Therefore, the power of the matrix fast algorithm is described as follows:

       while (k!=0)

       {

               if (k% 2 == 1) c = c * a; // c = c * a, and c represents a matrix multiplication, the result sent c

               a=a*a;

               b=b/2;

       }

       To deepen understanding, C = A ^ 9 analog hand count.

       k = 9, k! = 0 C = C * A (operation result C = A) A = A * A (calculation result A = A ^ 2)

       k = k / 2 k = 4! = 0 4% 2 == 0 A = A * A (calculation result A = A ^ 4)

       k = k / 2 k = 2! = 0 2% 2 == 0 A = A * A (calculation result A = A ^ 8) 

       k = 2/2 k = 1! = 0 1% 2 == 1 C = C * A (operation result C = A * A ^ 8 = A ^ 9) A = A * A (calculation result A = A ^ 16 )

       End k = 1/2 k = 0 Algorithm.

       As can be seen, the hand and the binary number count process just 9 1001 represents fit.

 

[Example 2] Fast Power matrix.

      Given n * n matrix A, find A ^ k.

      Input format:
      Line 1, n, k

      n + 1 to the second row, each row number n, i + 1 th row and j denotes the number of elements of a matrix row i and column j

      Output format:
      Total n rows, each row number n, the i-th row and j represents the number of i-th element of the matrix row j-th column, each mold element 10 ^ 5 + 7

      (1) programming ideas.

      Because the matrix power computation involved in computing the matrix must be n * n square. Thus, the following procedures will be simplified definition of the structure, removing the row matrix representing the ranks of the variables and col.

      Further, the matrix multiplication operation, the results tend to be large, it is generally 64-bit integer. While in general will continue modulo calculation process, to avoid high-precision operations.

      (2) source.

#include <stdio.h>
#include <string.h>
#define MODNUM 100007
struct the Matrix
{
      the __int64 MAT [101] [101]; // memory matrix elements
};
the Matrix MATMUL (the Matrix A, B the Matrix, n-int )
{
      the Matrix C;
      Memset (c.mat, 0, the sizeof (c.mat));
      int I, J, K;
      for (I =. 1; I <= n-; I ++)
          for (J =. 1; J <= n-; J ++)
             for (K =. 1; K <= n-; K ++)
             {
                   c.mat [I] [J] = (c.mat [I] [J] + a.mat [I] [K] * B .mat [K] [J])% MODNUM;
             }
      return C;
}
the matrix quickMatPow (the matrix a, n-int, int b) // n-order matrix a rapid power b
{
      the matrix C;
      memset(c.mat ,0 ,sizeof(c.mat));
      int i;
      for (i = 1 ;i <= n ;i++)
            c.mat[i][i] = 1;
      while (b!=0)
      {
           if (b & 1)
                c = matMul(c ,a ,n);      // c=c*a;
           a = matMul(a ,a ,n);          // a=a*a
           b /= 2;
      }
      return c;
}
int main()
{
      int i,j,n,k;
      Matrix a;
      scanf("%d%d",&n,&k);
      for (i=1;i<=n;i++)
           for (j=1;j<=n;j++)
               scanf("%I64d",&a.mat[i][j]);
      a=quickMatPow(a,n,k);
      for (i = 1 ;i <=n;i++)
      {
            for (j=1;j<=n;j++)
                 printf("%I64d ",a.mat[i][j]);
            printf("\n");
      }
      return 0;
}

 

Guess you like

Origin www.cnblogs.com/cs-whut/p/11445671.html