Click here to Skip to main content
15,921,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do I perform matrix multiplication using pointers?
See the code scenerio.
C++
void tom::multiply(void*btr)
{
  short*X =(short*)btr;
  .
  .
  .
}

X is a 4x4 matrix stored as an array of 16 elements
int C[4][4]={1,1,1,1,1,-2,2,1,1,1,-1,-1,1,1,-1,-1};

What I want is : CXC' Where C' Means transpose of C
Posted
Updated 17-Oct-10 22:49pm
v4

1 solution

Since


(CXC')ik = CijXjkC'kl = CijXjkClk



I would do this way:

C++
int C[]={1,1,1,1,1,-2,2,1,1,1,-1,-1,1,1,-1,-1};
// index {i,j} to offset inside the array
#define I(i,j)  (i)*4+(j)
// really I don't know why you would use 'void * btr'
void transform(int * btr)
{
  int tmp[16]; // backup of the source matrix
  memcpy(tmp,btr, 16*sizeof(int));
  int i,j,k,l;
  for (i=0; i<4; i++)
    for (l=0; l<4; l++)
    {
      btr[I(i,l)] = 0;
      for (j=0; j<4; j++)
        for (k=0; k<4; k++)
          btr[I(i,l)] += C[I(i,j)] * tmp[I(j,k)] * C[I(l,k)];
    }
}
int main()
{
  int X[]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16};
  transform(X);
}
 
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