Advertisement
bnghtz

matrix_mult.c

Mar 13th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. /**
  2.  ** Howto multiply two matrixes in C using one dimensional arrays
  3.  ** Explanation at the end of the code....
  4.  **/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. void print_mx(int *mx, int x, int y)
  10. {
  11.   int i,j;
  12.   for(i = 0; i < x; i++)
  13.   {
  14.     for(j = 0; j < y; j++)
  15.     {
  16.       printf("%4d ", mx[y * i + j]);
  17.     }
  18.     printf("\n");
  19.   }
  20. }
  21.  
  22. //  test ranks:             2       3               3       4
  23. int * mx_mult(int *A, int ax, int ay, int *B, int bx, int by)
  24. {
  25.   // check matrix ranks
  26.   if (ay != bx)
  27.   {
  28.     fprintf(stderr,"Wrong matrix ranks: Cannot multiply!\n");
  29.     exit(-1);
  30.   }
  31.  
  32.   // allocate memory
  33.   int *ret = (int *) malloc(sizeof(int) * ay * bx);
  34.   if (ret == NULL)
  35.   {
  36.     fprintf(stderr,"Memory allocation error!\n");
  37.     exit(-2);
  38.   }
  39.  
  40.   // generate
  41.   int i,j,k;
  42.   for (i = 0; i < ax; i++) // 2
  43.   {
  44.     for (j = 0; j < by; j++) // 4
  45.     {
  46.       int sum = 0;
  47.       for (k = 0; k < ay; k++) // 3
  48.       {
  49.         sum += A[i * ay + k] * B[k * by + j];
  50.       }
  51.       // debug: // printf("C[%d,%d] = %d\n", i,j,sum);
  52.       ret[i * by + j] = sum;
  53.     }
  54.   }
  55.  
  56.   return ret;
  57. }
  58.  
  59. int main(int argc, char * argv[])
  60. {
  61.  
  62.   int mx1[] = {2,3,4, 2,5,6};
  63.   int mx1_x = 2, mx1_y = 3;
  64.  
  65.   int mx2[] = {6,7,7,8, 8,9,-1,-2, -2,3,-4,1};
  66.   int mx2_x = 3, mx2_y = 4;
  67.    
  68.   print_mx(mx2, mx2_x, mx2_y);
  69.   printf("\n");
  70.   print_mx(mx1, mx1_x, mx1_y);
  71.   printf("\n");
  72.  
  73.   // we should pass the ranks to know where the array ends.
  74.   int *mul = mx_mult(mx1, mx1_x, mx1_y,  mx2, mx2_x, mx2_y);
  75.   int mul_x = mx1_x, mul_y = mx2_y;
  76.  
  77.   printmx(mul, mul_x, mul_y);
  78.   printf("\n");
  79.   return 0;
  80. }
  81.  
  82.  
  83.  
  84. /**
  85.  ** Let's take 2 matrixes A and B. Rank of A = 2x3. Rank of B = 3x4.
  86.  **
  87.  ** A * B = C
  88.  **
  89.  ** Criterions of multiplication is: columns of A == lines of B
  90.  **
  91.  ** Lines   of C will be equal to lines   of A
  92.  ** Columns of C will be equal to columns of B
  93.  **
  94.  ** Running vars will be
  95.  **   i:  lines   of C  ==  lines   of A  | 0 .. 1  (2)
  96.  **   j:  columns of C  ==  columns of B  | 0 .. 3  (4)
  97.  **   k:  columns of A  ==  lines   of B  | 0 .. 2  (3)
  98.  **
  99.  **               B
  100.  **
  101.  **               g  h  i  j    3 x 4
  102.  **               k  l  m  n
  103.  **    A          o  p  q  r
  104.  **    
  105.  **    a  b  c    S  T  U  V    S = a*g + b*k + c*o
  106.  **    d  e  f    W  X  Y  Z    1st line 1st item = 1..3 SUM (A_1,k * B_k,1)
  107.  **
  108.  **    2 x 3           2 x 4
  109.  **
  110.  ** Howto define A?        Howto define B?
  111.  **   A = {a,b,c,d,e,f};   B = {g,h,i,j,k,l,m,n,o,p,q,r};
  112.  **   Ax = 2;              Bx = 3;
  113.  **   Ay = 3;              By = 4;
  114.  **
  115.  ** Howto refer to an individual item in the matrix
  116.  ** when we use single dimensional arrays?
  117.  **
  118.  ** Well we should add the rank of a line (the number of columns)
  119.  ** as many times as the line number (- 1) is.
  120.  ** Eg: the M matrix has 4 lines and 7 columns. Then rank(M) = 4x7
  121.  ** The item in the 3rd line and the 5th column will be: M[3,5]
  122.  ** In C we start the enumeration from 0, not from 1,
  123.  ** that's why we should say  M[3,5]  is actually the  M[2,4].
  124.  **
  125.  ** M[2,4] = M[ (2 * 7) + 4 ];
  126.  **
  127.  **   A[i,k] = (i * Ay) + k;
  128.  **   B[k,j] = (k * By) + j;
  129.  **   C[i,j] = (i * Cy) + j;
  130.  **
  131.  ** To calculate the individual items in matrix C, we apply the followings
  132.  **   C[i,j] = 0;
  133.  **   for(..k..) { C[i,j] += A[i,k] * B[k,j]; }
  134.  **
  135.  **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement