Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Divide and conquer algorithm for matrix multiplication:
MMult(A, B, n)

If  n= 1  Output  A×B

Else  Compute A11,B11, . . . , A22,B22 % by computing  m=n/2

X1←MMult(A11, B11, n/2)

X2←MMult(A12, B21, n/2)

X3←MMult(A11, B12, n/2)

X4←MMult(A12, B22, n/2)

X5←MMult(A21, B11, n/2)

X6←MMult(A22, B21, n/2)

X7←MMult(A21, B12, n/2)

X8←MMult(A22, B22, n/2)

C11←X1+X2

C12←X3+X4

C21←X5+X6

C22←X7+X8

Output C

End If


What I have tried:

I don't understand what data structure can i use here or, how the above algorithm can be implemented...plz help me giving some pieces of c code.
#Thanks...experts
Posted
Updated 5-Apr-16 9:11am
v3
Comments
Matt T Heffron 5-Apr-16 14:48pm    
Richard's solution is right. You just need to declare, allocate, use and free arrays (A??, B??, X??, C??) locally to your MMult function.
However, the algorithm you show will work correctly only for square matrices with rank equal to a power of 2!
Is it really more efficient than the triple-nested loop that handles the general case of an NxK mult KxM giving a NxM?
Member 12378355we 5-Apr-16 15:07pm    
So I need to compute those matrix A??B?? in my recursive function?

But how could I Compute A11,B11, . . . , A22,B22 % by computing m=n/2 in c?

The easy way:
You allocate your local A?? and B?? and copy the appropriate pieces of the A and B matrices into these and then call MMult recursively. This will use lots of additional memory.

The harder (more thinking!) way:
Use a different function for the recursive calls to MMult (e.g., InternalMMult)
This function will take as parameters the original A and B but will also include the information about the row and column ranges of interest for A and B at each recursive call. This function will do its calculations based on the specified regions of interest of the A and B.

It may be possible to pass common intermediate (X) and result (C) matrices with ranges in a similar manner. This would take some serious analysis! (Are you "up" for a challenge?!)

Note:
As I noted in my comment above on the original question:
However, the algorithm you show will work correctly only for square matrices with rank equal to a power of 2.
Is it really more efficient than the triple-nested loop that handles the general case of an NxK mult KxM giving a NxM?
 
Share this answer
 
Comments
Member 12378355we 5-Apr-16 15:20pm    
Thanks, for your explanation. I'm expecting some "Serious analysis" soon...:)
Matt T Heffron 5-Apr-16 15:39pm    
Are you attempting the Strassen algorithm?
You can use arrays to hold the values of each matrix, and then just multiply according to the rules.
 
Share this answer
 
Comments
Member 12378355we 5-Apr-16 14:42pm    
But how could I Compute A11,B11, . . . , A22,B22 % by computing m=n/2 in c?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900