Matrix Multiplication in C

redline.jpg (2244 bytes) GIVEN: Two arrays, A, which is m x n, and B, which is n x p.

The Product AB is determined as the dot products of the ith row in A and the jth column in B,
placed in ith row and jth column of the resulting m x p matrix C.
For example:

if we have
the 2 x 3 matrix A =
1 3 4
2 0 1
... and multiply it by
the 3 x 4 matrix B =
1 2 3 1
2 2 2 2
3 2 1 4

the result would be

the 2 x 4 matrix C = AB =
 
19 16 13 23
5 6 7 6

To get this, we found the dot-product of each row in A with each column in B.
For example:

C[1,1] = (1 x 1) + (3 x 2) + (4 x 3) = 19, and
C [2,3] = (2 x 3) + (0 x 2) + (1 x 1) = 7

 

redline.jpg (2244 bytes) In the C language, we might test out the code like this:
#include <stdio.h>
#include <stdlib.h>
// function prototypes
void Matrix_Mult( int a1[][3], int a2[][4], int a3[][4] );
void Matrix_MultAlt( int a1[][3], int a2[][4], int a3[][4] );
int dot3(const int a1[][3], const int a2[][4], int row, int col);
void PrnNx4 (int ar[][4], int n);

//---------------------------------------------------------------------------------
//   Function:   main(void) 
//   Description: 
//                demonstration of Matrix Multiplication  
//
//   Programmer:  Paul Bladek
//   
//   Date:        10/31/2001
// 
//   Version:     1.0
//  
//   Environment: Hardware:IBM Pentium 4 
//                Software:   Microsoft XP with .NET framework for execution; 
//                Compiles under Microsoft Visual C++.Net 2005
//
//      Calls:	Matrix_Mult(int a1[][3], int a2[][4], int a3[][4])
//		Matrix_MultAlt(int a1[][3], int a2[][4], int a3[][4])	
//		PrnNx4(int ar[][4]
//      
//
//      Parameters: 	int a1[][3] -- left matrix
//			int a2[][4] -- right matrix
//			int a3[][4] -- answer matrix
//
//	Returns:	EXIT_SUCCESS
// ------------------------------------------------------------------------------ 
int main(void)
{
   int A[2][3] = {{1, 3, 4},
                  {2, 0, 1}},
       B[3][4] = {{1, 2, 3, 1},
                  {2, 2, 2, 2},
                  {3, 2, 1, 4}},
       C[2][4] = {{0, 0, 0, 0},
                  {0, 0, 0, 0}};

   Matrix_Mult(A, B, C);
   PrnNx4(C, 2);
   
   Matrix_MultAlt(A, B, C); // alternate form that calls dot3
   PrnNx4(C, 2);  
   return EXIT_SUCCESS;
} 

//---------------------------------------------------------------------------------
//   Function:   Matrix_Mult(int a1[][3], int a2[][4], int a3[][4]) 
//   Description: 
//                multiplies a 2X3 matrix by a 3X4 matrix  
//
//   Programmer:  Paul Bladek
//   
//   Date:        10/31/2001
// 
//   Version:     1.0
//  
//   Environment: Hardware:IBM Pentium 4 
//                Software:   Microsoft XP with .NET framework for execution; 
//                Compiles under Microsoft Visual C++.Net 2005
//
//      Calls:      None
//
//      Called By:  main()
//
//      Parameters: int a1[][3] -- left matrix
//                  int a2[][3] -- right matrix
//                  int a3[][3] -- answer matrix
// ------------------------------------------------------------------------------ 
void Matrix_Mult(int a1[][3], int a2[][4], int a3[][4])
{
   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++) 
                   a3[i][j] +=  a1[i][k] * a2[k][j];
}

//---------------------------------------------------------------------------------
//   Function:   Matrix_MultAlt(int a1[][3], int a2[][4], int a3[][4]) 
//   Description: 
//                multiplies a 2X3 matrix by a 3X4 matrix -- Alternate Form 
//
//   Programmer:  Paul Bladek
//   
//   Date:        10/31/2001
// 
//   Version:     1.0
//  
//   Environment: Hardware:IBM Pentium 4 
//                Software:   Microsoft XP with .NET framework for execution; 
//                Compiles under Microsoft Visual C++.Net 2005
//
//      Calls:      dot3(const int a1[][3], const int a2[][4], int row, int col)
//
//      Called By:  main()
//
//      Parameters: int a1[][3] -- left matrix
//                  int a2[][3] -- right matrix
//                  int a3[][3] -- answer matrix
// ------------------------------------------------------------------------------
void Matrix_MultAlt(int a1[][3], int a2[][4], int a3[][4])
{
   int i = 0;
   int j = 0;
   for( i = 0; i < 2; i++) 
       for( j = 0; j < 4; j++) 
           a3[i][j] =  dot3(a1, a2, i, j);
}

//---------------------------------------------------------------------------------
//   Function:   dot3(const int a1[][3], const int a2[][4], int row, int col) 
//   Description: 
//                dot product of a1 row and  a2 col
//
//   Programmer:  Paul Bladek
//   
//   Date:        10/31/2001
// 
//   Version:     1.0
//  
//   Environment: Hardware:IBM Pentium 4 
//                Software:   Microsoft XP with .NET framework for execution; 
//                Compiles under Microsoft Visual C++.Net 2005
//
//      Calls:      None
//
//      Called By:  Matrix_MultAlt(int a1[][3], int a2[][4], int a3[][4])
//
//      Parameters: int a1[][3] -- left matrix
//                  int a2[][3] -- right matrix
//                  int row -- the row number
//                  int col -- the column number
//
//	Returns:	the dot product
// ------------------------------------------------------------------------------
int dot3(const int a1[][3], const int a2[][4], int row, int col)
{
    int k = 0;
    int sum = 0;
	for( k = 0; k < 3; k++)
	    sum += a1[row][k] * a2[k][col];
    return sum;
}

//---------------------------------------------------------------------------------
//   Function:   PrnNx4(int ar[][4], int n) 
//   Description: 
//                prints out an NX4 matrix 
//
//   Programmer:  Paul Bladek
//   
//   Date:        10/31/2001
// 
//   Version:     1.0
//  
//   Environment: Hardware:IBM Pentium 4 
//                Software:   Microsoft XP with .NET framework for execution; 
//                Compiles under Microsoft Visual C++.Net 2005
//
//      Called By:  main()
//
//      Parameters: int ar[][4] -- matrix to print
//                  int n -- number of elements
// ------------------------------------------------------------------------------
void PrnNx4 (int ar[][4], int n)
{
    int i = 0;
    int j = 0;
    for(i = 0; i < n; i++)
    {
        for( j = 0; j < 4; j++)
            printf("%4d", ar[i][j]);
        putchar('\n'); 
    }
}

 

[top] [previous]
[Edmonds Community College]


For Further information, contact pbladek@edcc.edu


copyright©2001,2008 Edmonds Community College
[top]