Showing posts with label NMat. Show all posts
Showing posts with label NMat. Show all posts

Saturday, August 4, 2012

Other Matrix Operations in NMat

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

NMat provides several other operations for matrix calculus, such as:
-Finding the minimum/maximum element of a matrix
-Computing the sum/product of all elements of a matrix
-Extracting rows and columns from a matrix into a submatrix
-Computing the arithmetic, geometric and harmonic mean of all elements of a matrix

The prototypes for the functions implementing these operations are:
/*
 * Description:
 *  Finds the minimum element of the matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The minimum element. If an error happened it will return REAL_MAX
 */
real NMatrix_MinElement(const NMatrix* mat);
/*
 * Description:
 *  Finds the maximum element of the matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The maximum element. If an error happened it will return REAL_MIN
 */
real NMatrix_MaxElement(const NMatrix* mat);
/*
 * Description:
 *  Sums up all the elements of the matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The sum of all the elements. If an error happened it will return NAN
 */
real NMatrix_ElementSum(const NMatrix* mat);
/*
 * Description:
 *  Computes the product of all elements in the matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The product of all elements in the matrix. If an error happened it will
 *  return NAN.
 */
real NMatrix_ElementProduct(const NMatrix* mat);
/*
 * Description:
 *  Computes the number of elements in the matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The number of elements in the matrix. If an error happened it will
 *  return -1.
 */
integer NMatrix_ElementCount(const NMatrix* mat);
/*
 * Description:
 *  Creates a submatrix of the current matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 *  startRow,endRow - the row delimiters
 *  startColumn, endColumn - the column delimiters
 * Returns:
 *  The specified submatrix. If an error happened it will return NULL.
 */
NMatrix* NMatrix_Submatrix(const NMatrix* mat,
      integer startRow, integer endRow,
      integer startColumn, integer endColumn);
/*
 * Description:
 *  Computes the arithmetic mean of all elements
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The arithmetic mean. If an error happened it will return NAN.
 */
real NMatrix_ArithmeticMean(const NMatrix* mat);
/*
 * Description:
 *  Computes the harmonic mean of all elements
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The harmonic mean. If an error happened it will return NAN.
 */
real NMatrix_HarmonicMean(const NMatrix* mat);
/*
 * Description:
 *  Computes the geometric mean of all elements
 * Parameters:
 *  mat   - a pointer to the matrix
 * Returns:
 *  The geometric mean. If an error happened it will return NAN.
 */
real NMatrix_GeometricMean(const NMatrix* mat);
Example:
#include<stdio.h>
#include"NMatrix.h"

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

int main(void)
{
   NMatrix *mat = NULL;
   NMatrix *smat1 = NULL, *smat2=NULL;
   integer i,j;

   mat = NMatrix_Create(3,3);
   for(i=0; i<mat->rows; i++)
   {
      for(j=0; j<mat->columns; j++)
      {
         mat->data[i][j] = (real)(i+j+1);
      }
   }

   puts("The matrix: ");
   PrintMatrix(mat);

   printf("The smallest element is                :%f \n",
          NMatrix_MinElement(mat));
   printf("The largest element is                 :%f \n",
          NMatrix_MaxElement(mat));
   printf("The sum of all elements is             :%f \n",
          NMatrix_ElementSum(mat));
   printf("The product of all elements is         :%f \n",
          NMatrix_ElementProduct(mat));
   printf("The total number of elements is        :%d \n",
          NMatrix_ElementCount(mat));
   printf("The arithmetic mean of all elements is :%f \n",
          NMatrix_ArithmeticMean(mat));
   printf("The geometric mean of all elements is  :%f \n",
          NMatrix_GeometricMean(mat));
   printf("The harmonic mean of all elements is   :%f \n",
          NMatrix_HarmonicMean(mat));

   /*Will be a column vector containing column 1*/
   smat1 = NMatrix_Submatrix(mat,0,2,1,1);
   /*Will be a row vector containing only row 0*/
   smat2 = NMatrix_Submatrix(mat,0,0,0,2);

   puts("Submatrix 1: ");
   PrintMatrix(smat1);

   puts("Submatrix 2: ");
   PrintMatrix(smat2);
   return 0;
}
/*Output:
The matrix:
+1.000000 +2.000000 +3.000000
+2.000000 +3.000000 +4.000000
+3.000000 +4.000000 +5.000000
The smallest element is                :1.000000
The largest element is                 :5.000000
The sum of all elements is             :27.000000
The product of all elements is         :8640.000000
The total number of elements is        :9
The arithmetic mean of all elements is :3.000000
The geometric mean of all elements is  :2.432432
The harmonic mean of all elements is   :2.737729
The matrix:
+2.000000
+3.000000
+4.000000
The matrix:
+1.000000 +2.000000 +3.000000
 */

The NMat Library

NMat is a ANSI C library that provides an API for matrix operations.

The current operations implemented by NMatlib are:
Operation Description
Create Creates a NMatrix object
Destroy Destroys a NMatrix object
Clone Creates a hard copy of an existent NMatrix object
Sum Computes the sum of two matrices
Scalar Multiplication Multiplies the matrix with a scalar
Product Computes the product of two matrices
Secondary Diagonal Returns the secondary diagonal of a matrix
Primary Diagonal Returns the primary diagonal of a matrix
Minor Returns the minor of a matrix
Determinant Computes the determinant of a matrix
Transpose Computes the transpose of a matrix
Adjugate Computes the adjugate of a matrix
Inverse Computes the inverse of a matrix
Minimum Element Returns the smallest element of a matrix
Maximum Element Returns the largest element of a matrix
Sum of all elements Returns the sum of all elements
Product of all Elements Returns the product of all elements
Number of Elements Returns the total number of elements that exist in a matrix
Submatrix Returns a submatrix specified by a start/end column/row
Arithmetic Mean Returns the arithmetic mean of all elements from a matrix
Harmonic Mean Returns the harmonic mean of all elements from a matrix
Geometric Mean Returns the geometric mean of all elements from a matrix

The NMatrix structure is implemented as:
typedef double real;
typedef int integer;

typedef struct
{
   real** data;
   integer rows;
   integer columns;
}NMatrix;
The numeric matrix structure will contain information about the number of rows, columns and an dynamic 2D array to hold the data.

The prototypes for the functions are:
Operation Prototype
Create NMatrix* NMatrix_Create(integer rows, integer columns);
Destroy NMatrix* NMatrix_Destroy(NMatrix *matrix);
Clone NMatrix* NMatrix_Clone(const NMatrix *source);
Sum NMatrix* NMatrix_Sum(const NMatrix* mat1,const NMatrix* mat2);
Scalar Multiplication NMatrix* NMatrix_MultiplyWithScalar(const NMatrix* mat, real value);
Product NMatrix* NMatrix_Product(const NMatrix* mat1,const NMatrix* mat2);
Secondary Diagonal NMatrix* NMatrix_GetSecondaryDiagonal(NMatrix* mat);
Primary Diagonal NMatrix* NMatrix_GetPrimaryDiagonal(NMatrix *mat);
Minor NMatrix* NMatrix_Minor(const NMatrix *mat,
integer row, integer column,
Integer order);
Determinant real NMatrix_Determinant(const NMatrix *mat, integer order);
Transpose NMatrix* NMatrix_Transpose(const NMatrix* mat);
Adjugate NMatrix* NMatrix_Adjugate(const NMatrix* mat);
Inverse NMatrix* NMatrix_Inverse(const NMatrix* mat);
Minimum Element real NMatrix_MinElement(const NMatrix* mat);
Maximum Element real NMatrix_MaxElement(const NMatrix* mat);
Sum of all elements real NMatrix_ElementSum(const NMatrix* mat);
Product of all Elements real NMatrix_ElementProduct(const NMatrix* mat);
Number of Elements integer NMatrix_ElementCount(const NMatrix* mat);
Submatrix NMatrix* NMatrix_Submatrix(const NMatrix* mat,
integer startRow, integer endRow,
Integer startColumn, integer endColumn);
Arithmetic Mean real NMatrix_ArithmeticMean(const NMatrix* mat);
Harmonic Mean real NMatrix_HarmonicMean(const NMatrix* mat);
Geometric Mean real NMatrix_GeometricMean(const NMatrix* mat);


Sunday, November 20, 2011

Creating, Destroying and Copying A Matrix using NMat

Before reading about how create, destroy and copy a matrix using the NMat library, make sure you've read this article:
The article explains the principles and the structure of the matrices used by NMat.

The operations Create, Destroy and Copy are implemented by the following functions:
/*
 * Description:
 *  Alocates spaces for a NMatrix structure according to the parameters
 * @rows si @columns.
 * Parameters:
 *  rows - the number of rows
 *  columns - the number of columns
 * Returns:
 *  A pointer to the allocated matrix structure.
 */
NMatrix* NMatrix_Create(integer rows, integer columns);
/*
 * Description:
 *  Free the space hold by a NMatrix structure
 * Parameters:
 *  matrix - a pointer to the NMatrix structure
 * Returns:
 *  NULL
 * Preconditions:
 *  @matrix must not be NULL
 */
NMatrix* NMatrix_Destroy(NMatrix *matrix);
/*
 * Description:
 *  Creates a hard copy of the matrix @source
 * Parameters:
 *  source - the matrix who shall be copied
 *  destination - the matrix where the copy
 *       will be stored
 * Returns:
 *  A pointer to the clone matrix
 * Preconditions:
 *  @matrix must not be NULL
 */
NMatrix* NMatrix_Clone(const NMatrix *source);
If you want to see how the functions are implemented, click the link below:
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* mat = NULL;
   NMatrix* matCopy = NULL;
   /*Creating a 2x2 NMatrix structure*/
   mat = NMatrix_Create(2, 2);
   /*Assigning values to the structure*/
   mat->data[0][0] = 3.3;
   mat->data[0][1] = 2.1;
   mat->data[1][0] = 5.2;
   mat->data[1][1] = -6.3;
   /*Printing the matrix*/
   puts("The original matrix: ");
   PrintMatrix(mat);
   /*Creating a copy of the first matrix*/
   matCopy = NMatrix_Clone(mat);
   /*Destroying the first matrix*/
   mat = NMatrix_Destroy(mat);
   /*Printing the second matrix*/
   puts("The copy of the matrix: ");
   PrintMatrix(matCopy);
   return 0;
}
/*Output:
 The original matrix:
 +3.300000 +2.100000
 +5.200000 -6.300000

 The copy of the matrix:
 +3.300000 +2.100000
 +5.200000 -6.300000
 */

Tuesday, October 18, 2011

The Transpose, Adjugate and Inverse of a Matrix in NMat

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

1.The Transpose of a Matrix

The transpose function takes as argument a m x n matrix and returns a n x m matrix having the same values (the rows and columns are interchanged).
The transpose formula
Example:
Transpose example
Observation: A matrix doesn't need to be square in order to have a transpose.

The function's definition is:
/*
 * Description:
 *  Computes the transpose matrix of a given matrix
 * Parameters:
 *  mat   - a pointer to the original matrix
 * Returns:
 *  A pointer to the transpose matrix of the original matrix.
 * Preconditions:
 *  @mat must not be NULL
 */
NMatrix* NMatrix_Transpose(const NMatrix* mat);
The implementation of the function is:
NMatrix* NMatrix_Transpose(const NMatrix* mat)
{
   integer i = 0, j = 0;
   NMatrix* transpose = NULL;
   transpose = NMatrix_Create(mat->columns,mat->rows);
   for(i=0;i<transpose->rows;i++)
   {
      for(j=0;j<transpose->columns;j++)
      {
         transpose->data[i][j] = mat->data[j][i];
      }
   }
   return transpose;
}
2.The Adjugate Matrix of a Matrix

The adjugate matrix can be obtained analytically by replacing every element with the determinant of the minor according to the element's row and column. The sign of the new elements will be determined according to the sum of the indexes (if the sum if even, the sign will be +, otherwise the signwill be -).

Example:
For this example, we shall consider a 3x3 matrix named A
A's adjutant matrix
The function's definition is:
/*
 * Description:
 *  Computes the adjugate matrix of a given matrix
 * Parameters:
 *  mat   - a pointer to the original matrix
 * Returns:
 *  A pointer to the adjugate matrix of the original matrix.
 * Preconditions:
 *  @mat must not be NULL
 */
NMatrix* NMatrix_Adjugate(const NMatrix* mat);
The implementation of the function is:
NMatrix* NMatrix_Adjugate(const NMatrix* mat)
{
   integer i = 0, j = 0, dim = 0;
   real det = 0.0;
   NMatrix* minor = NULL;
   NMatrix* adjugate = NULL;
   if(mat->columns == mat->rows)
   {
       dim = mat->columns;
       adjugate = NMatrix_Create(dim,dim);
       for(j=0;j<dim;j++)
       {
          for(i=0;i<dim;i++)
          {
             minor = NMatrix_Minor(mat,i,j,dim-1);
             det = NMatrix_Determinant(minor,dim-1);
             adjugate->data[i][j] = pow(-1.0f,i+j+2.0f) * det;
          }
      }
   }
   return adjugate;
}
3.The Inverse Matrix of a Matrix

The inverse matrix is obtained through a simple formula:
A - the matrix who is going to be inversed
|A| - the determinant of A
C - the cofactor matrix, which by transposition becomes the adjutant matrix.

Example:

The inverse cannot be computed if the matrix is not square of if the matrix's determinant it 0.

The function's definition is:
/*
 * Description:
 *  Computes the inverse matrix of a given matrix
 * Parameters:
 *  mat   - a pointer to the original matrix
 * Returns:
 *  A pointer to the inverse matrix of the original matrix.
 * Preconditions:
 *  @mat must not be NULL
 *  @mat must not be singular (det(@mat)!=0)
 */
NMatrix* NMatrix_Inverse(const NMatrix* mat);
The implementation of the function is:
NMatrix* NMatrix_Inverse(const NMatrix* mat)
{
    NMatrix* inv = NULL;
    real det = 0.0f;
    real coeficent = 0.0f;
    if(mat->rows==mat->columns)
    {
        det = NMatrix_Determinant(mat,mat->rows);
        if(det != 0.0f)
        {
            inv = NMatrix_Create(mat->rows,mat->columns);
            coeficent = 1.0f/NMatrix_Determinant(mat,mat->rows);
            inv = NMatrix_Adjugate(mat);
            inv = NMatrix_MultiplyWithScalar(inv,coeficent);
        }
    }
    return inv;
}
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[])
{
   integer i = 0, j = 0;
   NMatrix *mat = NULL;
   NMatrix *res = NULL;
   /*Creates and initializes the matrix*/
   mat = NMatrix_Create(3, 3);
   for (i = 0; i < 3; i++)
   {
      for (j = 0; j < 3; j++)
      {
         mat->data[i][j] = (real) (i + j);
      }
   }
   mat->data[0][0] = 32;

   puts("Original matrix: ");
   PrintMatrix(mat);

   res = NMatrix_Transpose(mat);
   puts("Transpose matrix: ");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);

   res = NMatrix_Adjugate(mat);
   puts("Adjugate matrix: ");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);

   res = NMatrix_Inverse(mat);
   puts("Inverse matrix: ");
   PrintMatrix(res);
   res = NMatrix_Destroy(res);
   return 0;
}
/*:
Original matrix:
32.000000 1.000000 2.000000
1.000000 2.000000 3.000000
2.000000 3.000000 4.000000

Transpose matrix:
32.000000 1.000000 2.000000
1.000000 2.000000 3.000000
2.000000 3.000000 4.000000

Adjugate matrix:
-1.000000 2.000000 -1.000000
2.000000 124.000000 -94.000000
-1.000000 -94.000000 63.000000

Inverse matrix:
0.031250 -0.062500 0.031250
-0.062500 -3.875000 2.937500
0.031250 2.937500 -1.968750
 */

The Determinant of a Matrix in NMat

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

The determinant will be calculated using Laplace's formula:
Laplace's formula for the determinant
A - the square matrix whose determinant will be calculated
n - the size (order) of A
a - A's element at the position (i,j)
M - A's minor determined by i and j.

Example:
Let us assume that we have the following matrix:
A sample 3x3 matrix
The determinant would be calculated like:
The calculation of A's determinant

Our function will receive as parameters a matrix and the order of the determinant. The function shall return a number who is the matrix's determinant according to the specified order. The order should represent in most cases the size of the matrix. You can also specify a smaller than size order to compute the determinant of a minor of the matrix.

The function's definition is:
/*
 * Description:
 *  Computes the determinant of a given matrix
 * Parameters:
 *  mat   - a pointer to the matrix
 *  order - usually the size of square matrix, but it can be used
 *       if you want to compute a determinant of a minor
 * Returns:
 *  The value of the determinant if the input is correct.
 * Otherwise returns NaN.
 * Preconditions:
 *  @mat must not be NULL
 *  @mat must be a square matrix
 *  @column and @row must be smaller than @order
 *  @order must be greater than 1 but smaller or equal when
 *  compared to the size of the matrix
 */
real NMatrix_Determinant(const NMatrix *mat, integer order);
The implementation of the function is:
real NMatrix_Determinant(const NMatrix *mat, integer order)
{
   integer i = 0 ,j = 0 ,k = 0 ,l = 0;
   real det = 0.0;
   NMatrix *minor = NULL;
   if((mat->rows == mat->columns) && (order>=1) && (order<=mat->rows))
   {
      if(order==1)
      {
         det = mat->data[0][0];
      }
      else if(order==2)
      {
         det = (mat->data[0][0] * mat->data[1][1]) -
               (mat->data[0][1] * mat->data[1][0]);
      }
      else
      {
         for (k=0 ; k<order ; k++)
         {
            minor = NMatrix_Create(order,order);
            for(i=1 ; i<order ; i++)
            {
               l = 0;
               for (j=0;j<order;j++)
               {
                  if (j != k)
                  {
                     minor->data[i-1][l] = mat->data[i][j];
                     l++;
                  }
               }
            }
            det += pow(-1.0,k) * mat->data[0][k]
                   * NMatrix_Determinant(minor,order-1);
            minor = NMatrix_Destroy(minor);
         }
      }
   }
   else
   {
      det = NAN;
   }
   return det;
}
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[])
{
   real det = 0;
   NMatrix *mat = NULL;
   /*Creates and initializes the matrix*/
   mat = NMatrix_Create(3, 3);
   mat->data[0][0]=-2;
   mat->data[0][1]=2;
   mat->data[0][2]=-3;
   mat->data[1][0]=-1;
   mat->data[1][1]=1;
   mat->data[1][2]=3;
   mat->data[2][0]=2;
   mat->data[2][1]=0;
   mat->data[2][2]=-1;
   /*Prints the matrix*/
   puts("Matrix: ");
   PrintMatrix(mat);
   /*Computes the determinant of the matrix*/
   puts("Determinant of the matrix: ");
   det = NMatrix_Determinant(mat, 3);
   printf("%2.1f\n", det);
   /*Computes the determinant of a minor*/
   puts("Determinant of a minor (M33): ");
   det = NMatrix_Determinant(mat, 2);
   printf("%2.1f", det);
   return 0;
}
/*:
Matrix:
-2.000000 2.000000 -3.000000
-1.000000 1.000000 3.000000
2.000000 0.000000 -1.000000

Determinant of the matrix:
18.0
Determinant of a minor (M33):
0.0
 */

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
 */
Related Posts Plugin for WordPress, Blogger...