-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeek5_1.cpp
More file actions
223 lines (198 loc) · 5.52 KB
/
Copy pathWeek5_1.cpp
File metadata and controls
223 lines (198 loc) · 5.52 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
/*
มินาเสะ นางิสะ เด็กสาวมัธยมปลายในโรงเรียนแห่งนึง ได้เรียนคาบคณิตศาสตร์ในคาบสุดท้ายก่อนเลิกเรียน ซึ่งเป็นเรื่องเมตริกซ์
ซึ่งหลังจบคาบอาจารย์ได้สั่งการบ้านไว้ แต่ มินาเสะ มัวแต่ใจลอยคิดถึง นาโอยะ แฟนหนุ่มของเธอ มินาเสะ เลยทำการบ้านไม่เป็น
แล้วนาโอยะก็ดันไม่อยู่อีก มินาเสะ อยากคุณช่วยเขียนโปรแกรมคำนวณเมตริกซ์ โดยโปรแกรมจะรับค่าเมตริก A และ B เป็นจำนวนเต็ม ซึ่งเป็นเมตริกขนาด 3x3
แล้วโปรแกรมจะแสดงผลลัพธ์ต่อไปนี้ Transpose(A),Transpose(B),Det(A),Det(B),Inverse(A),Inverse(B),A+B,A-B,AB (Inverse แสดงทศนิยม 4 ตำแหน่ง)
*/
//Actually Method
#include<iostream>
using namespace std;
void inputMatrix(int** mat)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cin >> mat[i][j];
}
}
}
void printMatrix(int** mat)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
void printMatrixFloat(float** mat)
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
printf("%.4f ", mat[i][j]);
cout << endl;
}
}
void transpose(int** mat)
{
int** arr = new int*[3];
for(int i=0;i<3;i++)
arr[i] = new int[3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = mat[j][i];
printMatrix(arr);
delete arr;
}
void summation(int** matA,int** matB)
{
int** arr = new int*[3];
for(int i=0;i<3;i++)
arr[i] = new int[3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = matA[i][j] + matB[i][j];
printMatrix(arr);
delete arr;
}
void subtraction(int** matA,int** matB)
{
int** arr = new int*[3];
for(int i=0;i<3;i++)
arr[i] = new int[3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = matA[i][j] - matB[i][j];
printMatrix(arr);
delete arr;
}
void multiplication(int** matA,int** matB)
{
int** arr = new int*[3];
for(int i=0;i<3;i++)
arr[i] = new int[3];
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
arr[i][j] = 0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
for(int k=0;k<3;k++)
arr[i][j] += matA[i][k]*matB[k][j];
printMatrix(arr);
delete arr;
}
void cofactor(int** mat,int **target,int row,int col,int n)
{
int a=0,b=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(i != row && j != col)
{
target[a][b++] = mat[i][j];
if(b == n-1)
{
b=0;
a++;
}
}
}
}
}
int determinant(int** mat,int n)
{
int d=0;
int** temp = new int*[n-1];
for(int i=0;i<n-1;i++)
temp[i] = new int[n-1];
if(n==1)
return mat[0][0];
int sign=1;
for(int i=0;i<n;i++)
{
cofactor(mat,temp,0,i,n);
d+=sign*mat[0][i]*determinant(temp,n-1);
sign=-sign;
}
delete temp;
return d;
}
void adjoint(int **mat,int **target,int n)
{
int** cofac = new int*[n-1];
for(int i=0;i<n-1;i++)
cofac[i] = new int[n-1];
int sign=1;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cofactor(mat,cofac,i,j,n);
target[j][i] = sign*determinant(cofac,n-1);
sign=-sign;
}
}
delete cofac;
}
void inverse(int **mat,int n)
{
int d = determinant(mat,3);
if(d==0)
{
cout << "No inverse available" << endl;
return;
}
int** temp = new int*[n];
for(int i=0;i<n;i++)
temp[i] = new int[n];
float** inverse = new float*[n];
for(int i=0;i<n;i++)
inverse[i] = new float[n];
adjoint(mat,temp,n);
for(int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
inverse[i][j] = float(temp[i][j]) / d;
}
}
printMatrixFloat(inverse);
delete temp;
delete inverse;
}
main()
{
int** matA = new int*[3];
for(int i=0;i<3;i++)
matA[i] = new int[3];
int** matB = new int*[3];
for(int i=0;i<3;i++)
matB[i] = new int[3];
cout << "Input Matrix A:" << endl;
inputMatrix(matA);
cout << "Input Matrix B:" << endl;
inputMatrix(matB);
cout << "Transpose(A):" << endl;
transpose(matA);
cout << "Transpose(B):" << endl;
transpose(matB);
cout << "Det(A):" << endl;
cout << determinant(matA,3) << endl;
cout << "Det(B):" << endl;
cout << determinant(matB,3) << endl;
cout << "Inverse(A):" << endl;
inverse(matA,3);
cout << "Inverse(B):" << endl;
inverse(matB,3);
cout << "A+B:" << endl;
summation(matA,matB);
cout << "A-B:" << endl;
subtraction(matA,matB);
cout << "AB:" << endl;
multiplication(matA,matB);
delete matA;
delete matB;
}