Tuesday, October 18, 2011

The Sum and Product of Two Matrices in NMat

In order to understand this article, you should read the following articles first:

1.The sum of two matrices

To sum two matrices, the matrices need to have the same number of rows and columns. The sum is obtained by adding the elements from the first matrix to the elements of the second matrix.

Matrix addition
Example:
Matrix addition
Matrix subtraction
The function's definition is:
/*
 * Description:
 *  Sums up two matrices
 * Parameters:
 *  mat1 - a pointer to the first matrix
 *  mat2 - a pointer to the second matrix
 * Returns:
 *  A pointer to the result matrix.
 *  If the rows/columns of mat1 and mat2 are not
 * equal, the result will be NULL
 * Preconditions:
 *  @mat1 and @mat2 must not be NULL
 */
NMatrix* NMatrix_Sum(const NMatrix* mat1,const NMatrix* mat2);
The implementation of the function is:
NMatrix* NMatrix_Sum(const NMatrix* mat1,const NMatrix* mat2)
{
    integer i = 0, j = 0;
    NMatrix *result = NULL;
    if((mat1->rows==mat2->rows) && (mat1->columns==mat2->columns))
    {
        result = NMatrix_Create(mat1->rows, mat1->columns);
        for(i=0;i<mat1->rows;i++)
        {
            for(j=0;j<mat1->columns;j++)
            {
                result->data[i][j] = mat1->data[i][j] + mat2->data[i][j];
            }
        }
    }
    return result;
}
2.Multiplying a matrix with a scalar

To multiply a matrix with a scalar value, every element of that matrix must be multiplied with the scalar value.
Scalar multiplication
Example:
Scalar multiplication example
The function's definition is:
/*
 * Description:
 *  Multiplies a NMatrix with a scalar
 * Parameters:
 *  mat - a pointer to NMatrix who shall be multiplied
 * Returns:
 *  A pointer to the result matrix.
 * Preconditions:
 *  @mat must not be NULL
 */
NMatrix* NMatrix_MultiplyWithScalar(const NMatrix* mat, real value);
The implementation of the function is:
NMatrix* NMatrix_MultiplyWithScalar(const NMatrix* mat, real value)
{
    integer i = 0, j = 0;
    NMatrix *smat = NULL;
    smat = NMatrix_Clone(mat);
    for(i=0;i<mat->rows;i++)
    {
        for(j=0;j<mat->columns;j++)
        {
            smat->data[i][j]*=value;
        }
    }
    return smat;
}
3.The product of two matrices

To obtain the product of two matrices, the number of rows from the first matrix must be equal to the number of columns from the second matrix.

A is square matrix and B is a column-vector
Matrix Product
Example:
Two square matrices
A*B Matrix Product
B*A Matrix Product

The function's definition is:
/*
 * Description:
 *  Returns the product of two matrices
 * Parameters:
 *  mat1 - a pointer to the first matrix
 *  mat2 - a pointer to the second matrix
 * Returns:
 *  A pointer to the result matrix.
 * Preconditions:
 *  @mat must not be NULL
 */
NMatrix* NMatrix_Product(const NMatrix* mat1,const NMatrix* mat2);
The implementation of the function is:
NMatrix* NMatrix_Product(const NMatrix* mat1,const NMatrix* mat2)
{
    integer i = 0 ,j = 0 ,k = 0, p = 0;
    NMatrix *result = NULL;
    if(mat1->rows==mat2->columns)
    {
        result = NMatrix_Create(mat1->rows, mat2->columns);
        for(i=0;i<mat1->rows;i++)
        {
            for(j=0;j<mat2->columns;j++)
            {
                p=0;
                for(k=0;k<mat1->columns;k++)
                {
                    p+=mat1->data[i][k]*mat2->data[k][j];
                }
                result->data[i][j]=p;
            }
        }
    }
    return result;
}
4.Example
#include<stdio.h>
#include"NMatrix.h"

void PrintMatrix(NMatrix *mat)
{
   integer i = 0, j = 0;
   for(i = 0; i < mat->rows; i++)
   {
      for(j=0; j< mat->columns; j++)
      {
         printf("%+f ",mat->data[i][j]);
      }
      putchar('\n');
   }
   putchar('\n');
}

int main(int argc, char *argv[])
{
   NMatrix *mat1 = NULL;
   NMatrix *mat2 = NULL;
   NMatrix *res =NULL;
   integer i = 0, j = 0;
   /*Creating 2 matrices*/
   mat1 = NMatrix_Create(3,3);
   mat2 = NMatrix_Create(3,3);
   /*Assigning data to the elements of the matrices*/
   for(i = 0; i < 3; i++)
   {
      for(j=0; j < 3; j++)
      {
         mat1->data[i][j]=(real)(i+j);
         mat2->data[i][j]=(real)(i-j);
      }
   }
   /*Prints the matrices*/
   puts("A");
   PrintMatrix(mat1);
   puts("B");
   PrintMatrix(mat2);
   /*Scalar multiplication*/
   res = NMatrix_MultiplyWithScalar(mat1, 2.0f);
   puts("2*A");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);
   /*Matrix addition*/
   res = NMatrix_Sum(mat1,mat2);
   puts("A+B");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);
   /*Matrix subtraction*/
   res = NMatrix_Sum(mat1,NMatrix_MultiplyWithScalar(mat2,-1.0));
   puts("A-B");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);
   /*Matrix multiplication*/
   res = NMatrix_Product(mat1,mat2);
   puts("A*B");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);
   res = NMatrix_Product(mat2,mat1);
   puts("B*A");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);
   return 0;
}
/*
A
+0.000000 +1.000000 +2.000000
+1.000000 +2.000000 +3.000000
+2.000000 +3.000000 +4.000000

B
+0.000000 -1.000000 -2.000000
+1.000000 +0.000000 -1.000000
+2.000000 +1.000000 +0.000000

2*A
+0.000000 +2.000000 +4.000000
+2.000000 +4.000000 +6.000000
+4.000000 +6.000000 +8.000000

A+B
+0.000000 +0.000000 +0.000000
+2.000000 +2.000000 +2.000000
+4.000000 +4.000000 +4.000000

A-B
+0.000000 +2.000000 +4.000000
+0.000000 +2.000000 +4.000000
+0.000000 +2.000000 +4.000000

A*B
+5.000000 +2.000000 -1.000000
+8.000000 +2.000000 -4.000000
+11.000000 +2.000000 -7.000000

B*A
-5.000000 -8.000000 -11.000000
-2.000000 -2.000000 -2.000000
+1.000000 +4.000000 +7.000000
 */

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger