-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
137 lines (120 loc) · 3.42 KB
/
matrix.cpp
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "matrix.h"
/*******************************************************************************
* Function Name : determinant
* Description : Given a square matrix array[][]. Calculates determinant.
*******************************************************************************/
float determinant(float array[N][N],float order)
{
if(order==1)
{
return array[0][0];
}
float ans=0;
float array1[N][N];
int itemp,jtemp;
for(int c=0;c<order;c++)
{
itemp=0;
for(int i=1;i<order;i++)
{
jtemp=0;
for(int j=0;j<order;j++)
{
if(j==c)
{
continue;
}
else
{
array1[itemp][jtemp]=array[i][j];
jtemp++;
}
}
itemp++;
}
if(c%2==0){
ans+=array[0][c]*determinant(array1,order-1);
}
else{
ans+=-1*array[0][c]*determinant(array1,order-1);
}
}
return ans;
}
/*******************************************************************************
* Function Name : inverse
* Description : Given a square matrix array[][]. Calculates inverse_array.
*******************************************************************************/
void inverse(float inv[N][N],float array[N][N],float order)
{
if(order==1)
{
inv[0][0]=1/array[0][0];
}
else
{
float temp[N][N];
float det = determinant(array,order);
for(int i=0;i<order;i++)
{
for(int j=0;j<order;j++)
{
int rtemp,ktemp;
rtemp=0;
for(int r=0;r<order;r++)
{
if(r==i)
{
continue;
}
else
{
ktemp=0;
for(int k=0;k<order;k++)
{
if(k==j)
{
continue;
}
else
{
temp[rtemp][ktemp]=array[r][k];
ktemp++;
}
}
}
rtemp++;
}
inv[j][i] = (pow(-1,i+j)*determinant(temp,order-1))/det;
}
}
}
}
/*************************************************************
Function: multiply
Description: Matrix multiplication, First Matrix is the Product,
second & Third are matrices to be multiplied,
next 2 integers are row and columns of First Matrix
Next 2 Integers are row and columns of second Matrix
******************************************************/
void multiply(float cipher[N][N],float array1[N][N],float array2[N][N], int order_1,int order_2,int marker_1,int marker_2)
{
if(order_2!=marker_1)
{
cout<<"Multiplication Error, Size mismatch\n";
}
else
{
for(int i=0;i<order_1;i++)
{
for(int j=0;j<order_2;j++)
{
cipher[i][j]=0;
for(int k=0;k<order_2;k++)
{
cipher[i][j]+=array1[i][k]*array2[k][j];
}
}
}
}
}