Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Matrix continuous multiplication in C language

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report