Click here to Skip to main content
16,018,460 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need help, i can't imagine a C++ code for this.
I have to resolve the matrix mult. A*B=C and A is a 256x128 elements and B is a 128x256 elements, so C is a 256x256 elements.
But to solve this i need to create "sections" of A and B matrix (for example, Asection=4x128 and Bsection 128x4 and that mult. will give a Csection=4x4 matrix) and then mult. Asection with the next section of B (Bsection finished on 4 column, so the next section is 128x4 but starts in column 5). And this new mult. will give next Csection, etc.
This link has a representation of the problem

Imgur: The magic of the Internet[^]

THE BIG DEAL is that actually A B and C aren't "matrix", they're Vectors so A=[0,1,2....127(here finishes the first row),128,129....255 (2nd row, etc till 32768 elements)], the same for B and C. (pd: i know that vectors are matrix, but you know what i mean).

I did a code but just for a simply matrix mult, A has 2048 elemets and B too (64x32 and 32x64). This is the structure of the mult in my original problem, and could be the first "section" of C but i don't know how to put the others C sections.

What I have tried:

for (int i = 0; i < 64; i++)
{
for (int j = 0; j < 64; j++)
{
for (int k = 0; k < 32; k++)
{
C[i * 64 + j] += A[i * 32 + k] * B[j * 32 + k];

}
}
}
Posted
Updated 16-Aug-20 3:43am
Comments
[no name] 16-Aug-20 6:07am    
For me your code looks fine. The "sections" of 'C' are calculated by 'i*64' while you are doing this: 'C[i*64+j]'. Or what am I missing?

Preface
Of course in c++ there are much more elegant solutions possible. This in case you implement a class Matrix (and Vector). But that was not your question.

Implementation trial according to your question
Here an implementation completely untested, but I think because of some meaningfull namings more easy to understand and hopefully correct. Another thing: I'm assuming the matrices hold double values. Change the type according to your needs.

C++
void MatrixMul(double A[], int ARows, int ACols,
               double B[], int BRows, int BCols,
               double C[])
{
    // Assertion test. For matrix multiplication the left hand column count needs to match the right hand row count.  
    if (ACols != BRows)
    {
      throw(*new Exception("Dimension Error"));
    }

   int CRows= ARows; // Result Rows
   int CCols= BCols; // Result Cols

   for(int CRowIx= 0; CRowIx < CRows; CRowIx++)
   {
      for(int CColIx= 0; CColIx < CCols; CColIx++)
      {
         double Sum= 0.0; // Assuming the matrices hold double values. 
         for(int k= 0; k < ACols; k++)
         {
            // sum+= B[k][CColIx] * A[CRowIx][k];
            Sum+= B[k * BCols + CColIx] * A[CRowIx * ACols + k];
         }
         // C[CRowIx][CColIx]= sum;
         C[CRowIx * CCols + CColIx]= Sum;
     }
   }
}


Notes
Of course more index range checking are necessary to make it 'access violation" safe.

I hope it helps.
 
Share this answer
 
v5
Comments
[no name] 16-Aug-20 10:23am    
Booooah, I hope I'm finally done with all corrections :blush:. Or with the words of Trappatoni: "Ich habe fertig" here Trappatoni - Ich habe fertig! Die legendäre Pressekonferenz vom 10.03.1998 - YouTube[^] :-)
Sandeep Mewara 16-Aug-20 12:10pm    
+5!
[no name] 16-Aug-20 12:14pm    
Thank you very much. I hope I did not mixed up rows and columns, as I usually do in my code :-)
Sandeep Mewara 16-Aug-20 12:19pm    
:-D ... well, mostly its not...further hey you can never discount effort.
[no name] 16-Aug-20 12:31pm    
thanks again
You can create matrix objects on memory just by using 2 dimensional arrays. So for your example you would code:
C++
#define A_ROWS   256
#define A_COLS   128
#define B_ROWS   A_COLS
#define B_COLS   A_ROWS

int A[A_ROWS][A_COLS];
int B[B_ROWS][B_COLS];

Now by using different values for the number of rows and columns you can address any subset of the two matrices.
 
Share this answer
 

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