2017-10-20 45 views
-5

我發現互聯網上的程序,計算矩陣的行列式:瞭解矩陣的特定功能行列式的

/* 
* C++ Program to Find the Determinant of a Given Matrix 
*/ 
#include<iostream> 
#include<math.h> 
#include<conio.h> 
using namespace std; 
double d = 0; 
double det(int n, double mat[10][10]) 
{ 
    int c, subi, i, j, subj; 
    double submat[10][10]; 
    if (n == 2) 
    { 
     return((mat[0][0] * mat[1][1]) - (mat[1][0] * mat[0][1])); 
    } 
    else 
    { 
     for(c = 0; c < n; c++) 
     { 
      subi = 0; 
      for(i = 1; i < n; i++) 
      { 
       subj = 0; 
       for(j = 0; j < n; j++) 
       {  
        if (j == c) 
        { 
         continue; 
        } 
        submat[subi][subj] = mat[i][j]; 
        subj++; 
       } 
       subi++; 
      } 
     d = d + (pow(-1 ,c) * mat[0][c] * det(n - 1 ,submat)); 
     } 
    } 
    return d; 
} 
int main() 
{ 
    int n; 
    cout<<"enter the order of matrix" ; 
    cin>>n; 
    double mat[10][10]; 
    int i, j; 
    cout<<"enter the elements"<<endl; 
    for(i=0;i<n;i++) 
    { 
     for(j=0;j<n;j++) 
     { 
      cin>>mat[i][j]; 
     } 
    } 
    cout<<"\ndeterminant"<<det(n,mat); 
    getch(); 
} 

來源:http://www.sanfoundry.com/cpp-program-find-determinant-given-matrix/

我想從它,但我不學習不明白。它與高斯消除有什麼關係?否則,你知道哪個進程使用這個算法嗎?

預先感謝您的任何一個誰也許能幫助我

+0

它是一個線性代數數學問題。答案當然是在你的線性代數教科書或[這裏](https://en.wikipedia.org/wiki/Determinant): –

+0

如果你想更好地學習你自己寫的代碼,這個坦率地說很沒用因爲它只適用於只有一個特定尺寸(10x10) – user463035818

+0

@ tobi303的矩陣,所以它適用於從1x1到10x10的矩陣。 –

回答

0

這是使用Lapace擴展,通過計算(n-1) x (n-1) subminorsň決定其遞歸計算一個n x n矩陣的行列式的算法。 2 x 2矩陣的行列式應該是顯而易見的。

有更好的方法來做到這一點,如LU分解。

+1

非常感謝您的快速回復 – Muclos

0

該程序使用遞歸函數創建子矩陣並計算子矩陣爲2x2時的行列式。

當程序具有子矩陣的行列式時,它可以對它進行加減運算,就像您在Wikipedia page上看到的有關行列式。

最後,遞歸函數返回完整矩陣的行列式。

+1

非常感謝您的快速回復 – Muclos