简体   繁体   English

下三角矩阵和上三角矩阵给我错误的答案

[英]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.

相关问题 当0时下三角矩阵给出错误答案 - lower triangular matrix give wrong answer when 0 表示下/上三角矩阵的有效方法 - efficient way to represent a lower/upper triangular matrix 检查数组是否为下三角矩阵 - Check if the array is a lower triangular matrix OpenCL有效的方法来组合下三角矩阵 - OpenCL efficient way to group a lower triangular matrix 如何使用带有并行hdf5的mpi将三角形(上/下)矩阵存储到单个文件中? - How to store triangular (upper/lower) matrix into a single file using mpi with parallel hdf5? 编写一个 function 以 'a' 作为参数,如果 'a' 是上三角矩阵则返回 1,否则返回 0 - write a function that takes 'a'as a parameter and returns 1 if 'a' is upper triangular matrix , 0 otherwise 如何使 C 程序对上三角矩阵进行反向替换? - How to make C program to do backward substitution for upper triangular matrix? 给定矩阵&#39;a&#39;和矩阵&#39;b&#39;使用上三角矩阵的向后替换计算向量x - Calculating vector x using backward substitution for upper triangular matrix given matrix 'a' and matrix 'b' 以下在 c 中显示下三角矩阵的代码在开发 c++ 编译器中无限运行? 任何人都可以检查并纠正我吗? - the following code of displaying lower triangular matrix in c is running infinitely in dev c++ compiler? can anyone please check and correct me? 填充三角矩阵的顺序是什么? - Which is the order of filling a triangular matrix?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM