完全主元素消去法

高斯消元法,建立在完全列主元素消元法上,选取顺序主子式中最大元素作为顺序主子式中的第一行第一列.

写的过程中被C++中浮点数精度问题折磨了很久….一直以为是程序的BUG.

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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//打印矩阵
template<typename T>
void print_mat(T* matrix, int row, int col)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
cout << std::left << setw(9) << matrix[i * col + j] << " ";

cout << endl;
}
cout << endl;
}

//打印向量
template<typename T>
void print_vec(T* vector, int len)
{
for (int i = 0; i < len; ++i)
cout << vector[i] << endl;
cout << endl;
}


//打印增广矩阵
template<typename T, typename I>
void print_matrix_vec(T* matrix, int row, int col, I* vector, int len)
{
for (int i = 0; i < row; ++i)
{
for (int j = 0; j < col; ++j)
cout << std::left << setw(9) << matrix[i * col + j] << " ";
cout << vector[i] << ends;
cout << endl;
}
cout << endl;
}


//交换两行
//template<typename T>
void swap_row(double* matrix, int row, int col, int source_row, int dest_row)
{
//source_row与dest_row交换
//cout << "第 " << source_row << " 行与第 " << dest_row << " 行互换";
double temp_val = 0;
for (int i = 0; i < col; ++i)
{
temp_val = matrix[(dest_row - 1) * col + i];
matrix[(dest_row - 1) * col + i] = matrix[(source_row - 1) * col + i];
matrix[(source_row - 1) * col + i] = temp_val;
}
cout << endl;
}


//template<typename T>
void swap_col(double* matrix, int row, int col, int source_col, int dest_col)
{
double temp_val = 0;
for (int i = 0; i < row; ++i)
{
temp_val = matrix[i * col + (dest_col - 1)];
matrix[i * col + (dest_col - 1)] = matrix[i * col + (source_col - 1)];
matrix[i * col + (source_col - 1)] = temp_val;
}

}

//交换AX=b中向量 b 的元素
template<typename T>
void swap_vect_row(T* vector, int len, int source_row, int dest_row)
{

T temp_val = 0.0;
//cout <<"向量:::"<< source_row << ends << dest_row << endl;
temp_val = vector[dest_row - 1];
vector[dest_row - 1] = vector[source_row - 1];
vector[source_row - 1] = temp_val;
}

//某行乘以某数
template<typename T>
void mul_row(T* matrix, int row, int col, int source_row, double multiple)
{
//cout << "第 " << source_row << " 行乘以 " << multiple <<endl;
for (int i = 0; i < col; i++)
matrix[(source_row - 1) * col + i] *= multiple;
}

template<typename T>
void add_row(T* matrix, int row, int col, int source_row, int dest_row)
{
for (int i = 0; i < col; ++i)
matrix[(dest_row - 1) * col + i] += matrix[(source_row - 1) * col + i];
}

template<typename T>
void add_vec_row(T* vector, int len, int source_row, int dest_row)
{
vector[dest_row - 1] += vector[source_row - 1];
}

template<typename T>
void mul_vec_row(T* vector, int len, int source_row, double mul)
{
vector[source_row - 1] *= mul;
}


//矩阵上三角化
//template<typename T,typename I>
void up_triangle(double* matrix, int row, int col, double* vector, int len) // vector为AX=b中的b
{
int k = 1;
double mul_val = 0;
double* temp_array = new double[row];
print_matrix_vec((double*)matrix, row, col, vector, len);
//三角化的过程中是先行再列..
//这里是从矩阵的视角来看的 所以从第1行开始..
for (int j = 1; j < col; ++j) //j为列
{
//print_matrix_vec((double*)matrix, row, col, vector, len); //打印三角化的过程
/*
* 选出顺序主子式中最大的元素
* 记录其行和列
*/
double max_val = 0.0;
int max_row = 0;
int max_col = 0;
for (int m = j; m <= col; ++m)
{
for (int n = j; n <= row; ++n)
{
if (fabs(matrix[(m - 1)* col + (n - 1)]) > fabs(max_val))
{
max_row = m ;
max_col = n ;
max_val = matrix[(m - 1) * col + (n - 1)];
}
}
}

swap_col((double*)matrix, row, col, max_col, j);
swap_row((double*)matrix, row, col, max_row, j);
swap_vect_row((double*)vector, len, max_row, j);
//print_matrix_vec((double*)matrix, row, col, vector, len);
//////////////////////////////////////////////////////////////////////////////////////////

for (int i = j + 1; i <= row; ++i)
{
mul_val = matrix[(j - 1) * col + (j - 1)] / matrix[(i - 1) * col + (j - 1)]; //对角线元素是下面元素的倍数
//cout << mul_val<<endl;
mul_val *= -1;
mul_row((double*)matrix, row, col, i, mul_val);
mul_vec_row((double*)vector, len, i, mul_val);

add_row((double*)matrix, row, col, j, i);
add_vec_row((double*)vector, len, j, i);

}
print_matrix_vec((double*)matrix, row, col, vector, len);
}
}

//回带求解x
void solution(double* matrix, int row, int col, double* vector, int len)
{
if (len != row)
{
cout << "input error!" << endl;
return;
}
double mul = 0.0;

up_triangle((double*)matrix, row, col, vector, len); //上三角化
double* heap_arry = new double[len]; //存放解
for (int i = row; i >= 1; --i) //行
{
mul = 1.0 / matrix[(i - 1) * col + (i - 1)];
mul_row((double*)matrix, row, col, i, mul);
mul_vec_row(vector, len, i, mul);
for (int j = row; j > i; --j) //列
{
mul = matrix[(i - 1) * col + (j - 1)];
vector[i - 1] -= vector[j - 1] * mul;

matrix[(i - 1) * col + (j - 1)] = 0.0;
//matrix[(i - 1) * col + (j - 1)] -= mul * matrix[(j - 1) * col + (j - 1)];
print_matrix_vec((double*)matrix, row, col, vector, len);
}
heap_arry[i - 1] = vector[i - 1];
}
for (int k = 0; k < row; k++)
cout << "x" << k + 1 << " = " << heap_arry[k] << " ";
cout << endl;
}



int main()
{
double matrix[][3] = { {2,2,2},{3,2,4},{1,3,9} };

double vec[3] = { 1,0.5,2.5 };
/*print_mat((double*)matrix, 3, 3);
swap_col((double*)matrix,3,3,1,3);
print_mat((double*)matrix, 3, 3);*/
//up_triangle((double*)matrix,3,3,vec,3);

solution((double*)matrix, 3, 3, vec, 3);

}

输入矩阵:

输出矩阵: