【问题标题】:How to calculate the determinant of a matrix NxN ? [Recursively]如何计算矩阵 NxN 的行列式? [递归]
【发布时间】:2019-11-20 18:54:58
【问题描述】:

我一直在互联网上搜索一种算法来计算 NxN martix 递归的行列式。 (我不知道维度,所以N可以是小于256的每一个整数)

complex<double> Matrix::matrixDeterminant(complex<double> **matrix, int n) {

  complex<double> det(0,0);

  complex<double> **submatrix;
  submatrix[i] =  new complex<double>[n]


  for(int i = 0; i< n; i++) {
      submatrix[i] = new complex<double>[n];
    }


   if (n == 2)
      return ((matrix[0][0] * matrix[1][1]) - (matrix[1][0] * matrix[0][1]));
   else {
      for (int x = 0; x < n; x++) {
            int subi = 0;
            for (int i = 1; i < n; i++) {
               int subj = 0;
               for (int j = 0; j < n; j++) {
                  if (j == x)
                  continue;
                  submatrix[subi][subj] = matrix[i][j];
                  subj++;
               }
               subi++;
            }
            det = det + (pow(-1, x) * matrix[0][x] * matrixDeterminant(submatrix, n - 1 ));
      }

   }
     return det;
}


如你所见,矩阵是一个复数矩阵,里面的所有数字都是复数,并且返回复数。

此方法无效。有什么想法可以更改以使其正常工作吗?

【问题讨论】:

  • 您尝试实现的算法是什么?请详细描述一下。
  • 第一个问题:submatrix 是一个指针数组,而不是一个复数数组。例如,使用std::vector 可以避免此类困难。
  • @ThomasSablik, Laplace expansion.
  • 这不是我的,我只是从这个链接link拿的。
  • 这就是为什么我实际上并没有将 close 设置为重复,只是在这里列出 :) 另外,除非这必须是“递归的”,否则有一些非常好的 Gauss-Jordan 消除与完整-可以在不递归的情况下有效地提供确定的旋转。

标签: c++


【解决方案1】:

以下链接中的使用 Sarrus 规则(非递归方法)示例是用 Javascript 编写的,但可以很容易地用 C 编写 https://github.com/apanasara/Faster_nxn_Determinant

nxn 行列式计算仅通过两个循环

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 2011-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 2020-03-11
    • 2018-03-14
    相关资源
    最近更新 更多