矩阵行列式算法 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21220504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:32:56  来源:igfitidea点击:

Matrix determinant algorithm C++

c++algorithmmatrixdeterminants

提问by user3144334

I'm new to programming and I was looking for a way to find the determinant of a matrix. I found this code online, but I have trouble understanding the algorithm in place here. I have no problems for the base of the recursion , but the continue and main loop I have trouble understanding. Big thanks to anyone who can explain to me the algorithm.

我是编程新手,我一直在寻找一种方法来找到矩阵的行列式。我在网上找到了这段代码,但我很难理解这里的算法。我对递归的基础没有问题,但是我无法理解 continue 和主循环。非常感谢任何可以向我解释算法的人。

int determ(int a[MAX][MAX],int n) {
  int det=0, p, h, k, i, j, temp[MAX][MAX];
  if(n==1) {
    return a[0][0];
  } else if(n==2) {
    det=(a[0][0]*a[1][1]-a[0][1]*a[1][0]);
    return det;
  } else {
    for(p=0;p<n;p++) {
      h = 0;
      k = 0;
      for(i=1;i<n;i++) {
        for( j=0;j<n;j++) {
          if(j==p) {
            continue;
          }
          temp[h][k] = a[i][j];
          k++;
          if(k==n-1) {
            h++;
            k = 0;
          }
        }
      }
      det=det+a[0][p]*pow(-1,p)*determ(temp,n-1);
    }
    return det;
  }
}

回答by Novin Shahroudi

This algorithm uses a divide-conquer approach for solving the problem (finding the determinant of an N*N Matrix).

该算法使用分治法来解决问题(找到 N*N 矩阵的行列式)。

The algorithm uses a recursive pattern which is one of divide and conquer approaches. You can find out this by noticing the algorithm is calling itself in the third condition statement.

该算法使用递归模式,这是一种分而治之的方法。您可以通过注意到算法在第三个条件语句中调用自身来发现这一点。

Every recursive algorithm have an exit condition which is the first if-statement in your code. and they also contain a section which is the solution to the most convenient problem or an atomic problem of the main big problem which is hard to solve in the first place. The atomic problem or the most-divided problem can be solved easily as you can see the the second if-statement of your code. In your case it is actually solving the determinant of a 2*2 Matrix.

每个递归算法都有一个退出条件,它是代码中的第一个 if 语句。并且它们还包含一个部分,该部分是最方便的问题的解决方案,或者是首先难以解决的主要大问题的原子问题。原子问题或最分裂的问题可以很容易地解决,因为您可以看到代码的第二个 if 语句。在您的情况下,它实际上是在解决 2*2 矩阵的行列式。

The most important part of your code to understand which is challenging a little bit too is the part you do the dividing (which is recursive too!). This part has the key to conquering either. By doing a little back trace and numerical examples you can find it out:

代码中最重要的部分是理解哪个也有点挑战,就是你做除法的部分(这也是递归的!)。这部分是征服两者的关键。通过做一点回溯和数值例子,你可以找到它:

det = det + a[0][p] * pow(-1,p) * determ(temp,n-1);

For the final suggestion try a 3*3 Matrix which only needs one dividing. Good luck with that.

对于最后的建议,尝试一个 3*3 矩阵,它只需要一个除法。祝你好运。

This book is a great one to start studying and understanding algorithms

这本书是开始学习和理解算法的好书