-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSqrMatOps.c
121 lines (109 loc) · 2.63 KB
/
SqrMatOps.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include<stdlib.h>
#include<stdio.h>
#include<math.h>
#include "MatrixLibrary.h"
Matrix cofactor(Matrix mat,int exr, int exc)
{
/*
function to return cofactor matrix.
*/
Matrix sub; //holds the cofactor matrix
initialiseZero(&sub, mat.r-1, mat.c-1); //initializing the cofactor matrix
int subr = 0;
int subc = 0; //the indexes for filling the cofactor matrix
for(int row=0;row<mat.r;row++)
{
for(int col = 0;col<mat.c;col++)
{
if(col!=exc && row!= exr) //removing the exr->row and exc->column
{
sub.MatPtr[subr][subc] = mat.MatPtr[row][col]; //filling the cofactor matrix1
subc++;
if(subc == mat.c-1)
{
subc = 0; //increasing row count
subr++;
}
}
}
}
return sub; //returning the cofactor matrix
}
float determinant(Matrix mat)
{
/*
function that returns the determinant.
*/
float det=0.0;
if (mat.r == 2 && mat.c==2)
{
det = mat.MatPtr[0][0]*mat.MatPtr[1][1] - mat.MatPtr[0][1]*mat.MatPtr[1][0]; //determinant for 2x2 matrix
}
else
{
Matrix sub; //cofactor
int sign = 1; //for alternating the sign
for(int i = 0;i<mat.c;i++)
{
sub = cofactor(mat,0,i);
det += mat.MatPtr[0][i]*determinant(sub)*sign; //determinant recursion
sign = -sign;
}
}
return det; //returning det
}
Matrix transpose(Matrix A)
{
/*
The function returns transpose of a matrix
*/
Matrix t;
initialiseZero(&t, A.c, A.r);
for (int i = 0; i < t.r; i++)
{
for (int j = 0; j < t.c; j++)
t.MatPtr[i][j] = A.MatPtr[j][i]; //Finding the traspose
}
return t;
}
Matrix adjoint(Matrix mat)
{
/*
The function returns adjoint of a matrix
*/
Matrix sub; //cofactor matrix
Matrix adj; //adjoint matrix
float det;
initialiseZero(&adj, mat.r, mat.c); //init adjoint matrix
for(int row = 0;row<mat.r;row++)
{
for(int col=0;col<mat.c;col++)
{
sub = cofactor(mat,row,col); //getting cofactor
det = determinant(sub);
adj.MatPtr[row][col] = det*pow(-1,row+col+2); //putting in adjoint
}
}
adj = transpose(adj); //transpose to get adjoint
return adj; //returning adjoint
}
Matrix inverse(Matrix mat)
{
/*
The function returns inverse of the matrix if it exists(Non singular)
*/
Matrix inv,adj;
float det;
initialiseZero(&inv,mat.r,mat.c);
adj = adjoint(mat); //findind the adjoint
det = determinant(mat); //determinant
if (det == 0)
{
printf("The matrix is singualar"); //check for singular matrix
}
else
{
inv = ScalarDiv(mat, det); //getting the inverse
}
return inv; //returning the inverse
}