-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
295 lines (271 loc) · 6.41 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
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define eps 1e-6
using namespace std;
class Matrix
{
private:
int _column,_row;
vector<vector<double> > a;
public:
Matrix(int row,int column)
{
_column = column;
_row = row;
vector<vector<double> > temp(_row+1,vector<double>(_column+1));
a = temp;
}
Matrix(Matrix &mtr) {
_column = mtr.column();
_row = mtr.row();
vector<vector<double> > temp(_row+1,vector<double>(_column+1));
a = temp;
for(int i=1;i<=_row;i++)
for(int j=1;j<=_column;j++)
a[i][j] = mtr[i][j];
}
vector<double>& operator[](int i)
{
if( i > _column || i<1 )
{
cout << "matrix class array out bound error! abort" << endl;
exit(0);
}
return a[i];
}
void swap(int line1,int line2) {
vector<double> temp(a[line1]);
a[line1] = a[line2];
a[line2] = temp;
}
int column() {
return _column;
}
int row() {
return _row;
}
bool is_square() {
return this->column()==this->row();
}
void print() {
cout << "Matrix now is:" << endl;
for(int i=1;i<=this->row();i++) {
for(int j=1;j<=this->column();j++) {
//printf("%.2lf\t ",a[i][j]);
cout << a[i][j] << "\t" ;
}
cout << endl;
}
cout << endl;
}
void trim_pivot_element(int x) { //a[x][x]下选取主元,并交换行
int index = x;
for(int i=x;i<=this->row();i++) {
if(abs(a[i][1])>abs(a[index][1]))
index = i;
}
swap(index,x);
}
void solution_print() {
cout << "solution is: " << endl;
int n = this->row();
vector<double> x(n+1);
x[n] = a[n][n+1]/a[n][n];
for(int i=n;i>=1;i--) {
double sum = 0;
for(int j=i+1;j<=n;j++) {
sum += a[i][j]*x[j];
}
x[i] = (a[i][n+1]-sum)/a[i][i];
}
for(int i=1;i<=n;i++) {
cout << "x[" << i << "] = " << x[i]<< endl;
}
cout << endl;
}
double norm(int type) {
//矩阵范数
double ans = 0;
switch (type) {
case 1:
ans = 0;
for(int j=1;j<=this->column();j++){
double sum = 0;
for(int i=1;i<=this->row();i++) {
sum += abs(a[i][j]);
}
if(sum > ans)
ans = sum;
}
return ans;
break;
case 2:
cout << "norm type not support. Abort." << endl;
break;
case 3:
ans = 0;
for(int i=1;i<=this->row();i++){
double sum = 0;
for(int j=1;j<=this->column();j++) {
sum += abs(a[i][j]);
}
if(sum > ans)
ans = sum;
}
return ans;
break;
default:
cout << "norm type not support. Abort." << endl;
return -1;
break;
}
return -1;
}
double cond(int type) {
//条件数
if(this->is_square()){
return this->norm(type)*this->inv_matrix().norm(type);
}
else {
int n = min(this->row(),this->column());
Matrix square_matrix(n,n);
for(int i=1;i<=square_matrix.row();i++) {
for(int j=1;j<=square_matrix.column();j++) {
square_matrix[i][j] = a[i][j];
}
}
return square_matrix.cond(type);
}
}
Matrix inv_matrix() {
if(this->column()!=this->row()) {
cout << "matrix row not equals column. Abort" << endl;
return *this;
}
Matrix matrix(this->row(),2*this->row());
for(int i=1;i<=this->row();i++) {
for(int j=1;j<=this->column();j++)
matrix[i][j] = a[i][j];
}
for(int i=1;i<=this->row();i++) {
for(int j=this->column()+1;j<=matrix.column();j++) {
if(j-i==this->column()) matrix[i][j]=1;
else matrix[i][j]=0;
}
}
//构造右单位矩阵完毕
for(int x=1;x<matrix.row();x++) {
matrix.trim_pivot_element(x);
for(int i=x+1;i<=matrix.row();i++) {
if(matrix[x][x]==0) {
cout << "divisor zero error! abort." << endl;
return matrix;
}
double l = matrix[i][x]/matrix[x][x];
for(int j=x;j<=matrix.column();j++) {
matrix[i][j] -= l*matrix[x][j];
}
//matrix.print();
}
}
for(int i=matrix.row();i>=1;i--){
for(int j=i-1;j>=1;j--) {
if(matrix[i][i]==0) {
cout << "divisor zero error! abort." << endl;
return matrix;
}
double l = matrix[j][i]/matrix[i][i];
for(int k=1;k<=matrix.column();k++) {
matrix[j][k] -= matrix[i][k]*l;
}
//matrix.print();
}
}
//消成上三角
for(int i=1;i<=matrix.row();i++){
double divisor = matrix[i][i];
//cout << "matrix [i][i]=" << matrix[i][i] << "\n"<< endl;
for(int j=1;j<=matrix.column();j++) {
//cout << matrix[i][j] << " / " << divisor << " = " << matrix[i][j]/divisor << endl;
matrix[i][j] =matrix[i][j]/divisor;
}
}
//消成单位阵列
for(int i=1;i<matrix.row();i++) {
for(int j=1;j<matrix.column();j++) {
if(abs(matrix[i][j])<eps) matrix[i][j]=0;
}
}
//matrix.print();
//处理精度
Matrix ans(matrix.row(),matrix.row());
for(int i=1;i<=matrix.row();i++) {
for(int j=1;j<=matrix.row();j++) {
ans[i][j] = matrix[i][j+matrix.row()];
}
}
//提取右侧逆矩阵
return ans;
}
void Gaussian_Elimination() {
//Gauss消去法
for(int i=1;i<=this->row();i++) {
Gaussian_Elimination(*this,i);
}
}
void Gaussian_Elimination_pro() {
//Gauss列主元消去法
for(int i=1;i<=this->row();i++) {
//int kk = i;
this->trim_pivot_element(i);
Gaussian_Elimination(*this,i);
}
}
void Gaussian_Elimination_self_define() {
//Gauss自定义主元消去法
for(int i=1;i<=this->row();i++) {
cout << "pivot element: ";
cout << "select index: ";
for(int j=i;j<=this->row();j++) {
cout << "(" << j << ") " << a[j][i] << " ";
}
cout << endl;
int index;
cin >> index;
if(index>this->row() || index<i) {
cout << "privot choose error! abort." << endl;
return;
}
this->swap(i,index);
this->print();
Gaussian_Elimination(*this,i);
}
}
void Gaussian_Elimination(Matrix &matrix,int x) {
//Gauss顺序消去法
if(x>=matrix.row()) {
cout << "Gaussian Elimination Finished" << endl;
matrix.print();
matrix.solution_print();
return;
}
int y = x;
for(int i=x+1;i<=matrix.row();i++) {
if(matrix[x][y]==0) {
cout << "divisor zero error! abort." << endl;
return;
}
double l = matrix[i][y]/matrix[x][y];
for(int j=y;j<=matrix.column();j++) {
matrix[i][j] -= l*matrix[x][j];
}
//matrix.print();
}
}
};