矩阵行列式算法的C ++ [英] Matrix determinant algorithm C++

查看:165
本文介绍了矩阵行列式算法的C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的节目,我一直在寻找一种方式来找到一个矩阵的行列式。我发现这个code网上,但我有麻烦,这里理解算法到位。我没有问题的递归的基础,但继续主循环我听不太懂。非常感谢的人谁可以给我解释的算法。

  INT determ的(INT A [MAX] [MAX],INT N){
  INT DET = 0,P,H,K,I,J,临时[MAX] [MAX];
  如果(正== 1){
    返回[0] [0];
  }否则,如果(N == 2){
    DET =(一个[0] [0] *一个[1] [1] -a [0] [1] *一个[1] [0]);
    返回DET;
  } 其他 {
    为(p = 0时,P n种,P ++){
      H = 0;
      K = 0;
      为(ⅰ= 1; I&n种;我++){
        为(J = 0; J&n种; J ++){
          如果(j == P){
            继续;
          }
          温度[H] [K] = A [1] [J]。
          ķ++;
          如果(K ==正 -  1){
            ^ h ++;
            K = 0;
          }
        }
      }
      DET = DET +一个[0] [P] * POW(-1,p)的* determ的(温度,N-1);
    }
    返回DET;
  }
}
 

解决方案

该算法采用求解(找一个N * N矩阵的行列式)该问题的鸿沟治之的办法。

该算法采用递归模式这是分而治之的方法。你可以找出该被注意到的算法,自称在第三个条件语句。

每一个递归算法有一个退出条件这是第一个if语句在code。并且它们也含有一个部分,它是解决最方便的问题或这是很难解决在首位的主要大问题的原子的问题。原子问题还是最划分的问题很容易解决,你可以看到你的code中的第二个if语句。在你的情况下,它实际上是解决一个2×2矩阵的行列式。

你的code,以了解哪些是具有挑战性有点太最重要的部分是你做分割的部分(这是递归的呢!)。 这部分有关键征服无论是。通过做一点点回溯和数值例子,你可以找到它:

  DET = DET + A [0] [P] * POW(-1,P)* determ的(温度,N-1);
 

有关最后建议尝试一个3 * 3的矩阵,只需要1分。 祝你好运。

这本书是一个伟大的开始学习和理解算法

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.

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;
  }
}

解决方案

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

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.

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.

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

这篇关于矩阵行列式算法的C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆