Matrix is a great invention of Mathematics. It is very useful in solving system of equations through computer. Of course famous numerical computing platform, MATLAB is the well known application of matrix.
In MATLAB everything is matrix. The images are also represented as a matrix in ease. It is easy to perform operations over matrices. This is why the matrix manipulation is an important.
Here is my matrix library. It may be inefficient, since it is indented for educational purpose. I want to keep things simple so that the beginner also be able to understand. This article is also helpful for learning memory allocation in C, Pointer and its applications, recursion, modularity.
CodeListing
Sample Program to Test
In MATLAB everything is matrix. The images are also represented as a matrix in ease. It is easy to perform operations over matrices. This is why the matrix manipulation is an important.
Download the code here |
CodeListing
- /*----------------------------------------*/
- /* Program: matrix.h */
- /* Author: Vanangamudi */
- /* Date: 15/05/2011 */
- /*----------------------------------------*/
- #include<stdio.h>
- #ifndef _MATRIX_H_
- #define _MATRIX_H_
- typedef struct matrix {
- float **mat;
- int m,n;
- }Matrix;
- typedef struct matrixEquation {
- Matrix *lhs;
- Matrix *rhs;
- Matrix *Soln;
- }MatrixEqn;
- Matrix * matrix_new_Matrix(int m,int n);
- MatrixEqn * matrix_new_MatrixEqn(Matrix* lhs,Matrix *rhs);
- float matrix_Det(Matrix *matA);
- Matrix* matrix_Cofactor(Matrix *matA);
- Matrix* matrix_Minor(Matrix *matA, int m, int n);
- Matrix* matrix_Adjoint(Matrix *matA);
- Matrix* matrix_Inverse(Matrix *matA);
- int matrix_Solve_Det(MatrixEqn *matEqn);
- int matrix_Solve_Inv(MatrixEqn *matEqn);
- Matrix* matrix_Mult(Matrix *matA , Matrix *matB);
- Matrix* matrix_Add(Matrix *matA , Matrix *matB);
- Matrix* matrix_Sub(Matrix *matA , Matrix *matB);
- Matrix* matrix_tran(Matrix *matA);
- Matrix* matrix_ScalarMultiply(float X , Matrix *matA);
- Matrix* matrix_Duplicate(Matrix *matA);
- Matrix* matrix_ColumnReplace(Matrix *matA, Matrix* matB ,int IndA,int IndB );
- int matrix_Print(Matrix *matA);
- int matrix_Read(Matrix *matA);
- #endif //_MATRIX_H_
- /*-----------------------------------------------*/
- /* Program: matrix.c */
- /* Author: Vanangamudi */
- /* Date: 15/05/2011 */
- /*-----------------------------------------------*/
- #include "matrix.h"
- #include<malloc.h>
- #include<stdio.h>
- //Constructors
- Matrix* matrix_new_Matrix(int m, int n)
- {
- Matrix *newMat = (Matrix*)malloc(sizeof(float));
- newMat->mat = (float*)malloc(sizeof(float)*m);
- int i = 0;
- for(i =0 ; i < m ; i++){
- newMat->mat[i] = (float*)malloc(sizeof(float)*n);
- }
- newMat->m = m;
- newMat->n = n;
- return newMat;
- }
- MatrixEqn * matrix_new_MatrixEqn(Matrix *lhs,Matrix *rhs)
- {
- MatrixEqn* newEqn = (MatrixEqn*)malloc(sizeof(float));
- newEqn->lhs = matrix_Duplicate(lhs);
- newEqn->rhs = matrix_Duplicate(rhs);
- newEqn->Soln = matrix_new_Matrix(rhs->m,1);
- return newEqn;
- }
- // Matrix Operations
- Matrix* matrix_Minor(Matrix *matA, int m, int n)
- {
- int i,j;
- int p,q;
- Matrix *minor = matrix_new_Matrix(matA->m-1,matA->n-1);
- for(i = 0,p = 0; i < matA->m , p < minor->m; i++){
- if( i != m-1){
- for(j=0, q=0; j <matA->n , q < minor->n; j++){
- if(j != n-1){
- minor->mat[p][q] = matA->mat[i][j];
- q++;
- }
- }
- p++;
- }
- }
- return minor;
- }
- float matrix_Det(Matrix *matA)
- {
- float det=0;
- if((matA->m==1)&&(matA->n==1)){
- det = matA->mat[0][0];
- }else {
- int i;
- int sign=1;
- for(i = 0; i<matA->m ; i++){
- det += sign*matA->mat[0][i]*matrix_Det(matrix_Minor(matA,0,i));
- sign = -1*sign;
- }
- }
- return det;
- }
- Matrix* matrix_Cofactor(Matrix *matA)
- {
- Matrix *Cofact = matrix_new_Matrix(matA->m,matA->n);
- int i,j;
- int sign =1;
- for(i =0 ; i<matA->m;i++){
- for(j=0 ; j<matA->n ; j++){
- Cofact->mat[i][j] = sign*matrix_Det(matrix_Minor(matA,i,j));
- sign = sign * (-1);
- }
- if(matA->m%2 == 0){
- sign = (-1)*sign;
- }
- }
- return Cofact;
- }
- Matrix* matrix_Adjoint(Matrix *matA)
- {
- Matrix *Cofact = matrix_Cofactor(matA);
- Matrix *Adj = matrix_tran(Cofact);
- return Adj;
- }
- Matrix* matrix_Inverse(Matrix *matA)
- {
- Matrix *Inv = matrix_Adjoint(matA);
- float Det = matrix_Det(matA);
- Inv = matrix_ScalarMultiply(1/Det, Inv);
- return Inv;
- }
- //Arithmetics
- Matrix* matrix_Mult(Matrix *matA , Matrix *matB)
- {
- if(matA->n != matB->m){
- printf("\nMatrices cannot be multiplied");
- return NULL;
- }else{
- Matrix *matC = matrix_new_Matrix(matA->m,matB->n);
- int i,j,k;
- for(i = 0 ;i< matB->m; i++){
- for(j=0; j < matB->n ; j++){
- matC->mat[i][j] = 0;
- for( k =0;k < matA->n;k++){
- matC->mat[i][j]+=matA->mat[i][k]*matB->mat[k][j];
- }
- }
- }
- return matC;
- }
- }
- Matrix* matrix_ScalarMultiply(float X , Matrix *matA)
- {
- Matrix *matB = matrix_new_Matrix(matA->m,matA->n);
- int i,j;
- for(i = 0 ;i< matA->m; i++){
- for(j=0; j < matA->n ; j++){
- matB->mat[i][j] = X * matA->mat[i][j];
- }
- }
- return matB;
- }
- Matrix* matrix_Add(Matrix *matA , Matrix *matB)
- {
- Matrix *matC = matrix_new_Matrix(matA->m,matA->n);
- int i,j;
- for(i = 0 ;i< matA->m; i++){
- for(j=0; j < matA->n ; j++){
- matC->mat[i][j] = matA->mat[i][j]+matB->mat[i][j];
- }
- }
- return matC;
- }
- Matrix* matrix_Sub(Matrix *matA , Matrix *matB)
- {
- Matrix *matC = matrix_new_Matrix(matA->m,matA->n);
- int i,j;
- for(i = 0 ;i< matA->m; i++){
- for(j=0; j < matA->n ; j++){
- matC->mat[i][j] = matA->mat[i][j]-matB->mat[i][j];
- }
- }
- return matC;
- }
- Matrix* matrix_tran(Matrix *matA)
- {
- Matrix *matB = matrix_new_Matrix(matA->n,matA->m);
- int i,j;
- for(i = 0 ;i< matB->m; i++){
- for(j=0; j < matB->n ; j++){
- matB->mat[i][j] = matA->mat[j][i];
- }
- }
- return matB;
- }
- //Matrix Eqn Solvers
- int matrix_Solve_Inv(MatrixEqn *matEqn)
- {
- Matrix* Inv = matrix_Inverse(matEqn->lhs);
- matEqn->Soln = matrix_Mult(Inv,matEqn->rhs);
- }
- int matrix_Solve_Det(MatrixEqn *matEqn)
- {
- float Det = matrix_Det(matEqn->lhs);
- Matrix* matA;
- float DetXYZ;
- int i;
- for(i = 0;i <matEqn->Soln->m;i++){
- matA = matrix_ColumnReplace(matEqn->lhs,matEqn->rhs,i+1,1);
- DetXYZ = matrix_Det(matA);
- matEqn->Soln->mat[i][0] = DetXYZ/Det;
- }
- }
- //Misc
- Matrix* matrix_ColumnReplace(Matrix *matA, Matrix* matB ,int IndA,int IndB )
- {
- Matrix* matC = matrix_Duplicate(matA);
- int i;
- for(i = 0;i<matA->n; i++){
- matC->mat[i][IndA-1] = matB->mat[i][IndB-1];
- }
- return matC;
- }
- Matrix* matrix_Duplicate(Matrix *matA)
- {
- Matrix *matB = matrix_new_Matrix(matA->m,matA->n);
- int i,j;
- for(i = 0 ;i< matB->m; i++){
- for(j=0; j < matB->n ; j++){
- matB->mat[i][j] = matA->mat[i][j];
- }
- }
- return matB;
- }
- //IO Operations
- int matrix_Read(Matrix *matA)
- {
- int i=0 , j=0;
- printf("\nEnter the Elements:\n");
- for(i = 0; i < matA->m; i++){
- for(j = 0; j < matA->n; j++ ){
- scanf("%f",&matA->mat[i][j]);
- }
- }
- }
- int matrix_Print(Matrix *matA)
- {
- int i=0 , j=0;
- printf("\n");
- for(i = 0; i < matA->m; i++){
- for(j = 0; j < matA->n; j++ ){
- printf("%0.3f\t",matA->mat[i][j]);
- }
- printf("\n\n");
- }
- return 0;
- }
- /*-----------------------------------------------------*/
- /* Program: matrix_test.c */
- /* Author : Vanangamudi */
- /* Date :15/05/2011 */
- /*-----------------------------------------------------*/
- #include "matrix.h"
- #include<stdio.h>
- int main()
- {
- Matrix *matA = matrix_new_Matrix(3,3);
- Matrix *matB = matrix_new_Matrix(3,3);
- printf("\t\t|--------------------------|");
- printf("\n\t\t|**************************|");
- printf("\n\t\t|*** MATRIX ILLUSTRATION **|\n");
- printf("\t\t|**************************|\n");
- printf("\t\t|--------------------------|\n\n");
- matrix_Read(matA);
- matrix_Read(matB);
- printf("\nmatA + matB:\n");
- matrix_Print(matrix_Add(matA,matB));
- printf("\nmatA - matB:\n");
- matrix_Print(matrix_Sub(matA,matB));
- printf("\nmatA * matB:\n");
- matrix_Print(matrix_Mult(matA,matB));
- printf("\n5 * matA:\n");
- matrix_Print(matrix_ScalarMultiply(5,matA));
- printf("\nTranspose ofmatA :\n");
- matrix_Print(matrix_tran(matA));
- printf("\nDeterminant of matA:\t");
- printf("%f",matrix_Det(matA));
- printf("\nCofactor matrix of matA :\n");
- matrix_Print(matrix_Cofactor(matA));
- printf("\nInverse matrix of matA:\n");
- matrix_Print(matrix_Inverse(matA));
- printf("\nMinor matrix of ( i , j) = (1,1) of matA:\n");
- matrix_Print(matrix_Minor(matA,1,1));
- return 0;
- }
Once we wrote a library with generic functions, it easy to develop some projects easily based on our own library. Even though, there are many efficient scientific and mathematical libraries available free of charge. We learn Programming easily through writing programs like this.
Happy coding!!!
No comments:
Post a Comment