How to realize Matrix continuous multiplication in C language
This article mainly explains "how to realize matrix multiplication in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize matrix multiplication in C language".
Dynamic programming method
Topic description: given n matrices {A _ 1 ~ A _ 2an}, in which Ai and Ai+1 can be multiplied, it is judged that the n matrices are multiplied by adding parentheses to minimize the number of times!
Take the matrix chain ABCD as an example
The optimal value is calculated according to the increment of matrix chain length.
When the length of matrix chain is 1, the optimal values of matrix chains A, B, C and D are calculated respectively.
When the length of matrix chain is 2, the optimal values of matrix chain AB, BC and CD are calculated respectively.
When the length of matrix chain is 3, the optimal values of matrix chain ABC and BCD are calculated respectively.
When the length of matrix chain is 4, the optimal value of matrix chain ABCD is calculated.
Dynamic regression equation:
Analysis:
K is the position where the matrix chain is broken.
D array stores the optimal value of matrix chain calculation. D [I] [j] is the optimal value of matrix chain beginning with I matrix and ending with j matrix, I > 0
The row and column information of the matrix chain is stored in the m array. M [I-1] and m [I] are the rows and columns of the I-th matrix (I = 1,2,3.)
C language implementation code:
# include # define N 20 void MatrixChain (int p [N], int NLINT m [N] [N], int s [N] [N]) {int iGrainj int r; / record the number of matrices multiplied by variables for