[英]Lower triangular matrix and upper triangular matrix give me wrong answer
I'm working in LU Decomposition in C.My code is very simple Algorithm can be parallelized using two loops one for updating lower triangular matrix and one for updating upper triangular matrix ,but it seems I miss understand something :( 我正在用C进行LU分解工作。我的代码非常简单,可以使用两个循环并行化算法,一个循环用于更新下三角矩阵,一个循环用于更新上三角矩阵,但似乎我想不懂:(
for (i=0 ; i<N ; i++){
// A[i][i]=1;
for (j=i+1 ;j<N ;j++){
A[j][i] = A[j][i]/A[i][i]; //*Update L*//
}
for (j=i+1;j<N;j++){
for(k=i+1 ;k<N;k++){
A[j][k] = A[j][k] - A[i][k] * A[j][i];//*Update U*//
}
}
}
printf("\n Matrix after U transformation: \n");
print_matrix();
for (i=0 ; i<N ; i++){
A[i][i]=1;
for (j=i+1 ;j<N ;j++){
A[j][i] = A[j][i]/A[i][i]; //*Update L*//
}
for (j=i+1;j<N;j++){
for(k=i+1 ;k<N;k++){
A[j][k] = A[j][k] - A[i][k] * A[j][i];//*Update U*//
}
}
}
printf("\n Matrix after L transformation: \n");
print_matrix();
This is what I should to get ?! what I'm doing wrong
L =
1.0000 0 0 0 0
0.2000 1.0000 0 0 0
0.2000 0.1667 1.0000 0 0
0.2000 0.1667 0.1429 1.0000 0
0.2000 0.1667 0.1429 0.1250 1.0000
U =
50.0000 10.0000 10.0000 10.0000 10.0000
0 48.0000 8.0000 8.0000 8.0000
0 0 46.6667 6.6667 6.6667
0 0 0 45.7143 5.7143
0 0 0 0 45.0000
but what I got is ,,,,L not should be like this ?! 但是我得到的是,,,, L不应该这样吗?
Source Matrix :
50 10 10 10 10
10 50 10 10 10
10 10 50 10 10
10 10 10 50 10
10 10 10 10 50
Matrix after U transformation:
50 10 10 10 10
0 48 8 8 8
0 0 47 7 7
0 0 0 46 6
0 0 0 0 45
Matrix after L transformation:
1 10 10 10 10
0 1 6 6 6
0 -2 1 16 16
0 -2 9 1 -129
0 -2 9 -134 1
Thanks 谢谢
Your U matrix is correct, beside that these are integers and not floats. 您的U矩阵是正确的,除了这些是整数而不是浮点数。 The diagonal of your L matrix also is correct (you're setting it's values), but not the rest.
L矩阵的对角线也正确(您正在设置其值),但其余部分不正确。 After checking the code against the answer of " LU Decomposition from Numerical Recipes not working; what am I doing wrong? ", which is (changed it a bit, added some braces):
在对照“ 数值食谱中的LU分解不起作用;我在做什么错? ”的答案检查代码后,这是(更改了一下,添加了一些花括号):
for (i = 0; i < N; i++) {
// compute U
for (j = i; j < N; j++) {
for (k = 0; k < i-2; k++) {
A[i,j] -= A[i,k] * A[k,j];
}
}
// compute L
for (j = i+1; j < N; j++) {
for (k = 0; k < i-2; k++) {
A[j,i] -= A[j,k] * A[k,i];
}
}
}
I noticed that you are missing a loop in your code, which should be the problem. 我注意到您在代码中缺少循环,这应该是问题所在。 Also take a look at the mentioned SO question which provides some more useful hints.
还可以看一下提到的SO问题,它提供了一些更有用的提示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.