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

2 comments:

  1. I don't believe the word "adjutant" has meaning in this context and that the word you were looking for was "adjugate". Correct me if I'm wrong, else update the page to avoid anybody getting confused while reading this page. Very helpful stuff, thank you for taking the time to provide us with this code.

    ReplyDelete
    Replies
    1. Thank you for seeing that mistake and for the feedback. I always try to improve the quality of my content.

      Delete

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