How to multiply 3 arrays?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Winton
    New Member
    • Jan 2011
    • 22

    How to multiply 3 arrays?

    I am trying to multiply three arrays together;
    P_temp = A * A_t * P_k
    Where the size or each array is [6][6].

    Having difficulty in getting the correct answer. The method i am doing is;
    A * A_t = a then using the result in a to use again in
    P_temp = P_k * a

    Any ideas how i could simplify this at all? Any help would be greatly appreciated!!

    Code:
    void prioriestimate (int a1[][3], int a2[][4], int a3[][4])
    {
    int T = 1;	
    double A[6][6] =    {{1, 0, 0, T, 0, 0},
    		     {0, 1, 0, 0, T, 0},
    		     {0, 0, 1, 0, 0, T},
    		     {0, 0, 0, 1, 0, 0},
    		     {0, 0, 0, 0, 1, 0},
    		     {0, 0, 0, 0, 0, 1}};
        
    double A_t[6][6] =  {{1, 0, 0, 0, 0, 0},
    		     {0, 1, 0, 0, 0, 0},
    		     {0, 0, 1, 0, 0, 0},
    		     {T, 0, 0, 1, 0, 0},
    		     {0, T, 0, 0, 1, 0},
    		     {0, 0, T, 0, 1, 0}};
     
    double P_k[6][6] =  {{1, 0, 0, 0, 0, 0},
    		     {0, 1, 0, 0, 0, 0},
    		     {0, 0, 1, 0, 0, 0},
    		     {0, 0, 0, 1, 0, 0},
    		     {0, 0, 0, 0, 1, 0},
    		     {0, 0, 0, 0, 1, 0}}; 
    
    
    {
    
     int i = 0;
    
     int j = 0;
    
     int k = 0;
     int a;
            for(i = 0; i < 2; i++) 
    
               for( j = 0; j < 4; j++)
    
                   for( k = 0; k < 3; k++)  
    
                       a[6][6] +=  A[6][6] * A_k[6][6];
    
    }
    {
    
     int i = 0;
    
     int j = 0;
    
     int k = 0;
     	for(i = 0; i < 2; i++) 
    
               for( j = 0; j < 4; j++)
    
                   for( k = 0; k < 3; k++)  
    
                       P_temp[6][6] +=  a[6][6] * P_t[6][6];
    
    }
    }
    Last edited by Niheel; Jan 25 '11, 04:42 PM. Reason: please use code tags
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    to multiply c=a*b c[i][j] is evaluated
    Code:
     c[i][j] = c[i][j] + a[i][k] * b[k][j]

    Comment

    • Chris Winton
      New Member
      • Jan 2011
      • 22

      #3
      So instead of the line a[6][6] += A[6][6] * A_k[6][6];

      I can just put P_temp[i][j] = P_k[i][j] + A_t[i][k] * A[k][j]?

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        I think more likly
        Code:
        a[i][j]] += A[i][k] * A_k[k][j];
        make sure a[][] is zeroed first though

        Comment

        • vivek nandan
          New Member
          • Jan 2011
          • 9

          #5
          you can also try this:
          1.define a function that multiply two matrices and return result matrices.
          2. Then pass the matrices in the given order . pass the two and get result. Then pass the third and the result.
          3. You will have to use pointers to do this.

          I have code that can multiply two matrices of any order matrix ,order given by the user , and return the result matrix. If you want , ask me.
          Last edited by vivek nandan; Jan 20 '11, 12:12 PM. Reason: addition

          Comment

          • Chris Winton
            New Member
            • Jan 2011
            • 22

            #6
            cheers vivek if you could send me the matrix code that would be great!!

            Comment

            • vivek nandan
              New Member
              • Jan 2011
              • 9

              #7
              Here below is a generalized matrix multiplication code. You can multiply any two matrices of any order.The code is simple and you can easily figure out how to multiply n number of matrices of any order. Dont forget to say thanks :)
              Code:
               
              //Generalised matrix multilication and addition program
              
              /*Author:Vivek nandan*/
              
              #include <stdio.h>
              
              #include <stdlib.h>
              
              #define min(x1,x2) (x1>x2)?x2:x1
              
              #define max(x1,x2) (x1>x2)?x1:x2
              
              void add_mat(float*,float*,float*,int,int);
              
              void mul_mat(float*,float*,float *,int,int,int,int);
              
              void get_mat(float *,int,int);
              
              int main(void)
              
              {
              
              static float *mat1,*mat2,*mat3;
              
              static int row1,col1,row2,col2;int ch,k;
              
              int i,j;
              
              k=0;
              
              printf("\n***********Matrix A*********************\n");
              
              printf("\nEnter no of rows->");
              
              scanf("%d",&row1);
              
              printf("\nEnter no. of col->");
              
              scanf("%d",&col1);
              
              mat1=(float *)malloc(row1*col1*sizeof(float));
              
              printf("\n************** Enter Matrx A data*******************\n");
              
              get_mat(mat1,row1,col1);
              
              printf("\n***********Matrix B*********************\n");
              
              printf("\nEnter no of rows->");
              
              scanf("%d",&row2);
              
              printf("\nEnter no. of col->");
              
              scanf("%d",&col2);
              
              mat2=(float*)malloc(row2*col2*sizeof(float));
              
              printf("\n************** Enter Matrx B data*******************\n");
              
              get_mat(mat2,row2,col2);
              
              printf("\nEnter Choice: \n");
              
              printf("\n(1)Adding the matrices\n");
              
              printf("\n(2)Multiplying matrices\n");
              
              scanf("%d",&ch);
              
              switch(ch)
              
              {
              
              case 1:
              
              if((row1==row2)&&(col1==col2)){
              
              mat3=(float*)malloc(row1*col1*sizeof(float));
              
              add_mat(mat1,mat2,mat3,row1,col1);
              
              printf("\n****Result of addition***********\n"); 
              
              for(i=0;i<row1;i++)
              
              {
              
                for(j=0;j<col1;j++)
              
                      { printf("%f\t",*(mat3+k)); k++; }
              
              printf("\n");
              
              }
              
              }
              
              else{printf("\n<<<<<<<<Error!matrix cannot be added>>>>>>>>>>>>>>>\n");}
              
              break;
              
              
              
              case 2:
              
              if(row2==col1){
              
              mat3=(float*)malloc(row1*col2*sizeof(float));
              
              mul_mat(mat1,mat2,mat3,row1,row2,col1,col2);
              
              printf("\n<<<<<<<<<<<<Multiplication result>>>>>>>>>>>\n");
              
              for(i=0;i<row1;i++)
              
              {
              
                for(j=0;j<col2;j++)
              
                      { printf("%f\t",*(mat3+k)); k++; }
              
              printf("\n");
              
              }
              
              }
              
              else{printf("\nERROR! Cannot be Multipied\n");}
              
              break;
              
              
              
              default:
              
              printf("\n*******Nothing to be done!***********\n");
              
              break;
              
              };
              
              system("PAUSE");
              
              return 0;
              
              }
              
              void get_mat(float *x,int r,int c)
              
              {
              
              int i,j,k;
              
              k=0;
              
              for(i=0;i<r;i++)
              
              {printf("\n........row=%d,col=%d.............\n",i,j);
              
              
              
              for(j=0;j<c;j++)
              
                {scanf("%f",(x+k));
              
              k++;
              
                }
              
              }
              
              printf("\n<<<<<Matrix entered>>>>>>>>>>>>\n");
              
              k=0;
              
              for(i=0;i<r;i++)
              
              {//printf("\n........row=%d,col=%d.............\n",i,j);
              
              for(j=0;j<c;j++)
              
                {printf("%f\t",*(x+k));
              
              k++;
              
                }
              
              printf("\n");
              
              }
              
              
              
              }
              /* Matrix Addition Function */
              
              void add_mat(float *x,float *y,float *z,int r,int c)
              
              {
              
              int i,j,k;
              
              k=0;
              
              for(i=0;i<r;i++)
              
              {
              
                for(j=0;j<c;j++)
              
                      { *(z+k)=*(x+k)+*(y+k);
              
                           k++; }
              
              }
              
              
              
              }
              /* Matrix Multiplication function */
              
              void mul_mat(float *x,float *y,float * z,int r1,int c1,int r2,int c2) //mul_mat(*mat1,*mat2,*result,mat1_row,mat1_col,mat2_row,mat_col)
              
              {
              
              int i,j,k1,k2,k3;
              
              float sum;
              
              sum=0.00;
              
              k1=k2=k3=0;
              
              if(c1==r2)
              
               {
              
              while(k3<r1*c2)
              
              {
              
              for(i=0;i<r1;i++){
              
                                for(j=0;j<r2;j++){
              
              for(k1=0;k1<c2;k1++){
              
              sum+=*(x+i*c1+k1)*(*(y+k1*c2+j)) ;
              
                                   }
              
              *(z+k3)=sum;
              
              k3++;
              
              sum=0;
              
              }
              
              
              
              }
              
              }
              
              }
              
              else{printf("\nInside Error!\n");
              
                  }
              
              
              
              }

              Comment

              • Chris Winton
                New Member
                • Jan 2011
                • 22

                #8
                Thank you very much!!!! helps alot

                Comment

                Working...