Search This Blog

Simplicity is the ultimate sophistication.” — Leonardo da Vinci
Contact me: sreramk360@gmail.com

Monday 16 February 2015

a short program for the inverse of a 3X3 matrix in C



A short program for the inverse of a 3X3 matrix in C
 

The following is a short program for inverse of a $3 \times 3$ ordered matrix. It uses the mathematical formula $A^{-1} = \frac {adj(A)}{|A|}$. There are three functions, one for obtaining the minors, the other for finding the determinant and the other for finding the adjoin. The part of dividing the adjoin by the determinant is computed in the main function and the inverse matrix is displayed as the result.

/**
* a short program to find the inverse of a matrix
* By K Sreram
*/
#include <stdio.h>
#include <conio.h>
/*************************************
*  finds the determinant of each of the minors
**************************************
*/
float detMinor(int y, int x, float matrix[3][3])
{/// finds the determinant of the minors
    int i,j;
    float mt[2][2];
    for(i=0; i<3; i++)
        for(j=0; j<3; j++)
    {
        if(i==y || j==x)
            continue;
        mt[i-(i>y)][j-(j>x)]=matrix[i][j];
    }
    return (mt[0][0]*mt[1][1]-mt[0][1]*mt[1][0]);
}
/**************************************
*  finds the determinant of a $3\times3$ matrix
***************************************
*/
float determinent3x3(float matrix[3][3])
{/// finds the determinant of the 3x3 matrix
    return (detMinor(0,0,matrix)*matrix[0][0]-
            detMinor(0,1,matrix)*matrix[0][1]+detMinor(0,2,matrix)*matrix[0][2]);
}
/*************************************
*   finds the adjoin of the matrix
**************************************
*/
void adjoint(float m_to[3][3], float m_from[3][3])
{/// finds the adjoin of the matrix m_from and stores it in m_to
    int i,j,a=1;
    for(i=0; i<3; i++)
        for(j=0; j<3; j++)
        {
            if((i+j)%2==0)
                a=1;
            else
                a=-1;
            m_to[j][i]=a*detMinor(i,j,m_from);
        }
}
int main()
{
    int i,j;
    float matrix[3][3],m_inv[3][3],d;
    printf("enter the 3x3 matrix:");   /// ask for the matrix as an user input
    for(i=0; i<3; i++)
        for(j=0; j<3; j++)
            scanf("%f",&matrix[i][j]);    /// get the input
    d=determinent3x3(matrix); /// find the determinant and store it in the variable d
    if(d==0)   /// inverse does not exist if the determinant is zero
        {
            printf("inverse does not exist as determinant is zero");
            getch();
            return 0;
        }
    adjoint(m_inv,matrix);/// finds the adjoin and stores it in m_inv
    for(i=0; i<3; i++)
        for(j=0; j<3; j++) ///divides each element by d
    {
        m_inv[i][j]= m_inv[i][j]/d;
    } /// by now the major computation process is over
    for(i=0; i<3; i++)
        {
            for(j=0; j<3; j++)/// displays the inverse matrix
                {
                    printf("\t%f",m_inv[i][j] );
                }
                printf("\n");
        }
    getch();
    return 0;
}

about my blog 

copyright (c) 2015 K Sreram, all rights reserved.

No comments:

Post a Comment

Featured post

Why increasing complexity is not good?

“ Simplicity is the ultimate sophistication.” — Leonardo da Vinci Why is complicating things wrong ? - K Sr...