Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ** Howto multiply two matrixes in C using one dimensional arrays
- ** Explanation at the end of the code....
- **/
- #include <stdio.h>
- #include <stdlib.h>
- void print_mx(int *mx, int x, int y)
- {
- int i,j;
- for(i = 0; i < x; i++)
- {
- for(j = 0; j < y; j++)
- {
- printf("%4d ", mx[y * i + j]);
- }
- printf("\n");
- }
- }
- // test ranks: 2 3 3 4
- int * mx_mult(int *A, int ax, int ay, int *B, int bx, int by)
- {
- // check matrix ranks
- if (ay != bx)
- {
- fprintf(stderr,"Wrong matrix ranks: Cannot multiply!\n");
- exit(-1);
- }
- // allocate memory
- int *ret = (int *) malloc(sizeof(int) * ay * bx);
- if (ret == NULL)
- {
- fprintf(stderr,"Memory allocation error!\n");
- exit(-2);
- }
- // generate
- int i,j,k;
- for (i = 0; i < ax; i++) // 2
- {
- for (j = 0; j < by; j++) // 4
- {
- int sum = 0;
- for (k = 0; k < ay; k++) // 3
- {
- sum += A[i * ay + k] * B[k * by + j];
- }
- // debug: // printf("C[%d,%d] = %d\n", i,j,sum);
- ret[i * by + j] = sum;
- }
- }
- return ret;
- }
- int main(int argc, char * argv[])
- {
- int mx1[] = {2,3,4, 2,5,6};
- int mx1_x = 2, mx1_y = 3;
- int mx2[] = {6,7,7,8, 8,9,-1,-2, -2,3,-4,1};
- int mx2_x = 3, mx2_y = 4;
- print_mx(mx2, mx2_x, mx2_y);
- printf("\n");
- print_mx(mx1, mx1_x, mx1_y);
- printf("\n");
- // we should pass the ranks to know where the array ends.
- int *mul = mx_mult(mx1, mx1_x, mx1_y, mx2, mx2_x, mx2_y);
- int mul_x = mx1_x, mul_y = mx2_y;
- printmx(mul, mul_x, mul_y);
- printf("\n");
- return 0;
- }
- /**
- ** Let's take 2 matrixes A and B. Rank of A = 2x3. Rank of B = 3x4.
- **
- ** A * B = C
- **
- ** Criterions of multiplication is: columns of A == lines of B
- **
- ** Lines of C will be equal to lines of A
- ** Columns of C will be equal to columns of B
- **
- ** Running vars will be
- ** i: lines of C == lines of A | 0 .. 1 (2)
- ** j: columns of C == columns of B | 0 .. 3 (4)
- ** k: columns of A == lines of B | 0 .. 2 (3)
- **
- ** B
- **
- ** g h i j 3 x 4
- ** k l m n
- ** A o p q r
- **
- ** a b c S T U V S = a*g + b*k + c*o
- ** d e f W X Y Z 1st line 1st item = 1..3 SUM (A_1,k * B_k,1)
- **
- ** 2 x 3 2 x 4
- **
- ** Howto define A? Howto define B?
- ** A = {a,b,c,d,e,f}; B = {g,h,i,j,k,l,m,n,o,p,q,r};
- ** Ax = 2; Bx = 3;
- ** Ay = 3; By = 4;
- **
- ** Howto refer to an individual item in the matrix
- ** when we use single dimensional arrays?
- **
- ** Well we should add the rank of a line (the number of columns)
- ** as many times as the line number (- 1) is.
- ** Eg: the M matrix has 4 lines and 7 columns. Then rank(M) = 4x7
- ** The item in the 3rd line and the 5th column will be: M[3,5]
- ** In C we start the enumeration from 0, not from 1,
- ** that's why we should say M[3,5] is actually the M[2,4].
- **
- ** M[2,4] = M[ (2 * 7) + 4 ];
- **
- ** A[i,k] = (i * Ay) + k;
- ** B[k,j] = (k * By) + j;
- ** C[i,j] = (i * Cy) + j;
- **
- ** To calculate the individual items in matrix C, we apply the followings
- ** C[i,j] = 0;
- ** for(..k..) { C[i,j] += A[i,k] * B[k,j]; }
- **
- **/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement