LU 矩阵分解法

LU 矩阵分解

原理见wikipedia

直接上代码!

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
#include <iostream> //至于具体的LU矩阵分解法还劳各位看官自行百度,老衲就不说了,嘎嘎
using namespace std;
#define N 4
/*
4 2 1 5
8 7 2 10
4 8 3 6
12 6 11 20*/ //测试用例
double sum(double (*L)[N] , double (*U)[N] , int j , int beg , int end)
{
double result = 0;
for(int i = beg ; i <= end ; ++i)
{
result += L[end+1][i] * U[i][j];
}
return result;
}
double sum2(double (*L)[N] , double (*U)[N] , int i ,int beg , int end)
{
double result = 0;
for(int j = beg ; j <= end ; ++j)
{
result += L[i][j] * U[j][end+1];
}
return result;
}
double sum3(double (*L)[N] , double *y , int beg , int end)
{
double result = 0;
for(int j = beg ; j <= end ; ++j)
result += L[end+1][j] * y[j];
return result;
}
double sum4(double (*U)[N] , double *x , int beg , int end)
{
double result = 0;
for(int j = beg ; j <= end ; ++j)
result += U[beg-1][j] * x[j];
return result;
}
void display(double (*L)[N] , double (*U)[N])
{
int cnt = 0;
cout<<"L 矩阵为:"<<endl;
for(int i = 0 ; i < N ; ++i)
for(int j = 0 ; j < N ; ++j)
{
cout<<L[i][j]<<" ";
if(++cnt % 4 == 0)
cout<<endl;
}
//cout<<endl;
cnt = 0;
cout<<"---------------------------------------"<<endl;
cout<<"U 矩阵为:"<<endl;
for(i = 0 ; i < N ; ++i)
for(int j = 0 ; j < N ; ++j)
{
cout<<U[i][j]<<" ";
if(++cnt % 4 == 0)
cout<<endl;
}
cout<<endl;
}
void Answer(double (*L)[N] , double (*U)[N])
{
double y[N] = {0} , x[N] = {0};
double b[N] = {(-2) , (-7) , (-7) , (-3)};
//y1 = b1;
y[0] = b[0];
//yk = bk - sum
for(int k = 1 ; k < N ; ++k)
{
y[k] = b[k] - sum3(L , y , 0 , k-1);
}
/*for(int i = 0 ; i < N ; ++i)
cout<<y[i]<<" ";
cout<<endl;*/
// xn = yn / u[n][n]
x[N-1] = y[N-1] / U[N-1][N-1];
//xk
for(k = N-2 ; k >=0 ; --k)
{
x[k] = (y[k] - sum4(U , x , k+1 , N-1)) / U[k][k];
}
int cnt = 0;
for(int i = 0 ; i < N ; ++i)
cout<<"X"<<cnt++<<" = "<<x[i]<<" "<<endl;
}
void LUdepart(double (*a)[N])
{
double L[N][N] = {0} , U[N][N] = {0};
for(int i = 0 ; i < N ; ++i) //初始化LU 矩阵
{
for(int j = 0 ; j < N ; ++j)
{
if(i == j)
L[i][j] = 1;
if(i < j)
L[i][j] = 0;
if(i > j)
U[i][j] = 0;
}
}
//初始化第一行,第一列
for(i = 0 ; i < N ; ++i)
U[0][i] = a[0][i];
for(i = 1 ; i < N ; ++i)
L[i][0] = a[i][0] / a[0][0];
for(int k = 1 ; k <= N ; ++k)
{
for(int j = k ; j < N ; ++j)
{
U[k][j] = a[k][j] - sum(L , U , j , 0 , k-1);
//L[i][k] = a[i][k] - sum2(L , U , i , 0 , k-1) / U[k][k];
}
for(int i = k+1 ; i < N ; ++i)
{
L[i][k] = (a[i][k] - sum2(L , U , i , 0 , k-1)) / U[k][k];
}
}
display(L , U);
//回代求值
Answer(L , U);
}
int main()
{
double a[N][N];
for(int i = 0 ; i < N ; ++i)
for(int j = 0 ; j < N ; ++j)
cin>>a[i][j];
LUdepart(a);
return 0;
}

热评文章