Sunday, March 2, 2014

Matrix Math using C

This is a program that performs matrices math, very straight forward matrix multiplication (dot product), scalar multiplication and the calculation of the transpose of a matrix.  Here is a good video of how to do these things by hand (link)

Each matrix is defined within the program and the program is structured to pass values by reference instead of value.  To print each result make sure to remove the /* */ comments from each labeled section.  Any questions let me know


 #include <stdio.h>  
 #define ROWSx 2  
 #define COlSx 3  
 #define ROWSy 3  
 #define COlSy 2  
 #define ROWSw 3  
 #define COlSw 2  
 void scalar_multiplication(int, int[][COlSy]);  
 void matrix_multiplication(int[][COlSx], int[][COlSy], int[][COlSx]);  
 void matrix_addition(int[][COlSy], int[][COlSw]);  
 void transpose_matrix(int[][COlSw], int[][ROWSw]);  
 int main() {  
      int i, j;  
      int scalarX = 10;  
      int matrixZ[ROWSy][COlSx];  
      int matrixX[ROWSx][COlSx] = {  
           { 2, 2, 2 },  
           { 4, 4, 10 }  
      };  
      int matrixY[ROWSy][COlSy] = {  
           { 4, 3 },  
           { 4, 3 },  
           { 2, 2 }  
      };  
      int matrixW[ROWSw][COlSw] = {  
           { 1, 2 },  
           { 3, 4 },  
           { 5, 6 }  
      };  
      int matrixM[COlSw][ROWSw];  
      //scalar_multiplication(scalarX, matrixY);  
      //matrix_multiplication(matrixX, matrixY, matrixZ);  
      // matrix_addition(matrixY, matrixW);  
      transpose_matrix(matrixW, matrixM);  
      printf("\nHere is your matrix:\n");  
      for (i = 0; i<COlSw; i++)  
      {  
           for (j = 0; j<ROWSw; j++)  
           {  
                printf("%d ", matrixM[i][j]);  
           }  
           printf("\n");  
      }  
 }  
 void matrix_addition(int Yptr[][COlSy], int Wptr[][COlSw])  
 {  
      int f, g;  
      for (g = 0; g < ROWSw; g++)  
      {  
           for (f = 0; f < COlSw; f++)  
           {  
                Wptr[g][f] = Yptr[g][f] + Wptr[g][f];  
           }  
      }  
 }  
 void scalar_multiplication(int Xptr, int Yptr[][COlSy])  
 {  
      int a,b;  
      for (a = 0; a < ROWSy; a++)  
      {  
           for (b = 0; b < COlSy; b++)   
           {  
                Yptr[a][b] = Xptr * Yptr[a][b];  
           }  
      }  
 }  
 void matrix_multiplication(int matXptr[][COlSx], int matYptr[][COlSy], int matZptr[][COlSx])  
 {  
      int c, d, e;  
      int sum;  
      for (c = 0; c < ROWSy; c++)        
      for (d = 0; d < COlSx; d++)        
            matZptr[ROWSy][COlSx] = 0; // resultant matrix is row_of_y by column_of_x size  
      for (c = 0; c < ROWSy; c++) {     // row of matrix Y  
           for (d = 0; d < COlSx; d++) {   // column of matrix X  
                sum = 0;  
                for (e = 0; e < COlSy; e++)  
                     sum += matYptr[c][e] * matXptr[e][d];  
                 matZptr[c][d] = sum;  
           }  
      }  
 }  
 void transpose_matrix(int matZpt[][COlSw], int transZpt[][ROWSw])   
 {  
      int f, g, h, i;  
      for (f = 0; f < COlSw; f++)  
      for (g = 0; g < ROWSw; g++)  
           transZpt[f][g] = 0; // transposed matrix is this dimension  
      for (h = 0; h < COlSw; h++)  
      {  
           for (i = 0; i < ROWSw; i++)  
           {  
                transZpt[h][i] = matZpt[i][h];  
           }  
      }  
 }