forked from r-tron18/3d-Helix-Visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.cpp
More file actions
233 lines (194 loc) · 4 KB
/
Copy pathmatrix.cpp
File metadata and controls
233 lines (194 loc) · 4 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include<iostream>
#include<algorithm>
using namespace std;
void fix(int &a,int &b)
{
int mn = min(a,b);
int mx = max(a,b);
a=mn; b=mx;
}
class solution
{
double x,y,z;
public:
solution(): x(0),y(0),z(0) {}
solution(double a,double b,double c): x(a),y(b),z(c) {}
void display()
{
cout<<"The Solution is\n";
cout<<" X = "<<x<<endl;
cout<<" Y = "<<y<<endl;
cout<<" Z = "<<z<<endl;
cout<<endl;
}
};
class matrix
{
int row,col;
double Matrix[3][3];
public:
matrix()
{
row=3;col=3;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
Matrix[i][j] = 0;
}
matrix(double arr[][3])
{
row=3;col=3;
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
Matrix[i][j] = arr[i][j];
}
bool isinverse()
{
if (det()) return true;
else return false;
}
matrix inverse()
{
double t = 1/this->det();
matrix temp = this->adjoint() * t ; // adjoint of matrix A / |A|
return temp;
}
matrix adjoint()
{
double arr[3][3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = cofactor(i,j);
matrix temp(arr);
return temp.transpose();
}
friend matrix operator* (matrix a, double C);
friend matrix operator* (double C, matrix a);
matrix transpose()
{
matrix temp;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
temp.Matrix[i][j] = this->Matrix[j][i];
return temp;
}
double det()
{
double DET=0;
for(int j=0;j<3;j++)
DET+= Matrix[0][j]*cofactor(0,j);
return DET;
}
double _minor(int i,int j)
{
int r1=(i+1)%3,r2=(i+2)%3;
int c1=(j+1)%3,c2=(j+2)%3;
fix(r1,r2);
fix(c1,c2);
//cout<<"r1= "<<r1<<" r2= "<<r2<<" c1= "<<c1<<" c2= "<<c2<<endl;
return Matrix[r1][c1] * Matrix[r2][c2] - Matrix[r2][c1] * Matrix[r1][c2];
}
double cofactor(int i,int j)
{
if( (i+j)%2==0 ) {return _minor(i,j); }
else {return -_minor(i,j); }
}
void display()
{
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cout<<Matrix[i][j]<<" ";
}
cout<<endl;
}
}
friend solution linear_system_sol(matrix a,double arr[3]);
};
solution linear_system_sol(matrix m,double arr[3])
{
double X=0,Y=0,Z=0;
matrix mat = m.inverse();
for(int j=0;j<mat.col;j++)
X += mat.Matrix[0][j] * arr[j];
for(int j=0;j<mat.col;j++)
Y += mat.Matrix[1][j] * arr[j];
for(int j=0;j<mat.col;j++)
Z += mat.Matrix[2][j] * arr[j];
return solution(X,Y,Z);
}
matrix operator* (matrix a, double C)
{
matrix temp;
for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
temp.Matrix[i][j] = a.Matrix[i][j] * C;
return temp;
}
matrix operator* (double C,matrix a)
{
matrix temp;
for(int i=0;i<a.row;i++)
for(int j=0;j<a.col;j++)
temp.Matrix[i][j] = a.Matrix[i][j] * C;
return temp;
}
int main()
{
// test code
cout<<"\t Testing matrix class\n";
char var[3]= {'x','y','z'};
double arr[3][3] ={
{1,1,5},
{0,2,5},
{2,5,-1}
};
double d[3] = {6,-4,27};
matrix temp;
cout<<"Default matrix made \n";
cout<<"\t Matrix is\n";
temp.display();
cout<<endl;
cout<<"Matrix made with arr value \n";
matrix mat1(arr);
cout<<"\t Matrix is\n";
mat1.display();
cout<<endl;
cout<<"\t Determinant is\n";
cout<<mat1.det();
cout<<endl;
matrix trans = mat1.transpose();
cout<<"\t Transpose Matrix is\n";
trans.display();
cout<<endl;
cout<<"\tminors are \n";
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cout<<"Minor ["<<i<<"]["<<j<<"] : "<<mat1._minor(i,j)<<endl;
cout<<endl;
cout<<"\t Cofactors are \n";
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
cout<<"Cofactor ["<<i<<"]["<<j<<"] : "<<mat1.cofactor(i,j)<<endl;
cout<<endl;
matrix adj = mat1.adjoint();
cout<<"\t Adjoint Matrix is\n";
adj.display();
cout<<endl;
matrix inv = mat1.inverse();
cout<<"\t Inverse Matrix is\n";
inv.display();
cout<<endl;
cout<<"\t The solution for system of linear equations \n";
for(int i=0;i<3;i++)
{
for(int j=0;j<2;j++)
cout<<arr[i][j]<<var[j]<<" + ";
cout<<arr[i][2]<<var[2]<<" = ";
cout<<d[i]<<endl;
}
cout<<endl;
solution s = linear_system_sol(mat1,d);
s.display();
return 0;
}