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
 */

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