列主元素消元法

列主元素消元法是在高斯消元法方法上的改进,目的是为了减小计算机在消元过程的误差.

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
#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(T* matrix, int row, int col, int source_row, int dest_row)
{
//source_row与dest_row交换
//cout << "第 " << source_row << " 行与第 " << dest_row << " 行互换";
T 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;
}

//交换AX=b中向量 b 的元素
template<typename T>
void swap_vect_row(T* vector, int len, int source_row, int dest_row)
{
T temp_val = 0;
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];
//三角化的过程中是先行再列..
//这里是从矩阵的视角来看的 所以从第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;
for (int i = j; i <= row; ++i) //选出在该列中选出最大的行元素作为首元素
{
if (fabs(matrix[(i - 1) * col + (j - 1)]) > fabs(max_val))
{
max_val = matrix[(i - 1) * col + (j - 1)];
max_row = i;
}
}
swap_row(matrix, row, col, max_row, j);
swap_vect_row(vector, len, max_row, j);
//print_mat((double*)matrix, row, col);

//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(vector, len, i, mul_val);

add_row((double*)matrix, row, col, j, i);
add_vec_row(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)] -= 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 };
//up_triangle((double*)matrix,3,3,vec,3);
solution((double*)matrix, 3, 3, vec, 3);
}

输入矩阵:

输出矩阵: