C++程式:查詢給定矩陣的跡和範數


許多應用程式都受益於二維陣列或矩陣的使用。矩陣將數字儲存在行和列中。在C++中,我們也可以使用多維陣列來定義二維矩陣。在這篇文章中,我們將學習如何使用C++來找到給定矩陣的範數和跡。

範數是矩陣中所有元素個數的平方根。跡是所有主對角線元素的和。讓我們看看C++程式碼中演算法的表示。

矩陣跡

$\begin{bmatrix} 8 & 5& 3\newline 6 & 7& 1\newline 2 & 4& 9\newline \end{bmatrix},$

主對角線所有元素之和:(8 + 7 + 9) = 24,這是給定矩陣的跡

在前面的例子中,我們使用了 3x3 矩陣,結果是主對角線上元素的總和。跡可以透過求和得到。讓我們看看演算法來幫助我們理解。

演算法

  • 輸入矩陣 M
  • 假設M有n行n列
  • sum := 0
  • 對於 i 從 1 到 n,執行:
    • sum := sum + M[ i ][ i ]
  • 迴圈結束
  • 返回 sum

示例

#include <iostream>
#include <cmath>
#define N 7
using namespace std;
float solve( int M[ N ][ N ] ){
   int sum = 0;
   
   // read elements through major diagonal, where row index and column index are same, both are i
   for ( int i = 0; i < N; i++ ) {
      sum = sum + M[ i ][ i ];
   }
   return sum;
}
int main(){
   int mat1[ N ][ N ] = {
      {5, 8, 74, 21, 69, 78, 25},
      {48, 2, 98, 6, 63, 52, 3},
      {85, 12, 10, 6, 9, 47, 21},
      {6, 12, 18, 32, 5, 10, 32},
      {8, 45, 74, 69, 1, 14, 56},
      {7, 69, 17, 25, 89, 23, 47},
      {98, 23, 15, 20, 63, 21, 56},
   };
   cout << "The Trace of the first matrix is: " << solve( mat1 ) << endl;
   int mat2[ N ][ N ] = {
      {6, 8, 35, 21, 87, 8, 26},
      {99, 2, 36, 326, 25, 24, 56},
      {15, 215, 3, 157, 8, 41, 23},
      {96, 115, 17, 5, 3, 10, 18},
      {56, 4, 78, 5, 10, 22, 58},
      {85, 41, 29, 65, 47, 36, 78},
      {12, 23, 87, 45, 69, 96, 12}
   };
   cout << "The Trace of the second matrix is: " << solve( mat2 ) << endl;
}

輸出

The Trace of the first matrix is: 129
The Trace of the second matrix is: 74

矩陣範數

$\begin{bmatrix} 8 & 5& 3\newline 6 & 7& 1\newline 2 & 4& 9\newline \end{bmatrix},$

所有元素之和:(8 + 5 + 3 + 6 + 7 + 1 + 2 + 4 + 9) = 45

範數:(所有元素之和的平方根) = √45 = 6.708

在最後一個例子中,我們使用了一個 3x3 矩陣。我們首先計算所有元素的和,然後取其平方根。讓我們看看演算法來幫助我們理解。

演算法

  • 輸入矩陣 M
  • 假設M有n行n列
  • sum 初始化為 0
  • 對於 i 從 1 到 n,執行:
    • 對於 j 從 1 到 n,執行:
      • sum := sum + M[ i ][ j ]
    • 迴圈結束
  • 迴圈結束
  • res := sum 的平方根
  • 返回 res

示例

#include <iostream>
#include <cmath>
#define N 7
using namespace std;
float solve( int M[ N ][ N ] ){
   int sum = 0;
   
   // go through each element. Using outer loop, access ith row, using inner loop access column. For cell (i, j) read the element and add it to the sum
   for ( int i = 0; i < N; i++ ) {
      for ( int j = 0; j < N; j++ ) {
         sum = sum + M[ i ][ j ];
      }
   }
   return sqrt( sum );
}
int main(){
   int mat1[ N ][ N ] = {
      {5, 8, 74, 21, 69, 78, 25},
      {48, 2, 98, 6, 63, 52, 3},
      {85, 12, 10, 6, 9, 47, 21},
      {6, 12, 18, 32, 5, 10, 32},
      {8, 45, 74, 69, 1, 14, 56},
      {7, 69, 17, 25, 89, 23, 47},
      {98, 23, 15, 20, 63, 21, 56},
   };
   cout << "The Normal of the first matrix is: " << solve( mat1 ) <<
       endl;
   int mat2[ N ][ N ] = {
      {6, 8, 35, 21, 87, 8, 26},
      {99, 2, 36, 326, 25, 24, 56},
      {15, 215, 3, 157, 8, 41, 23},
      {96, 115, 17, 5, 3, 10, 18},
      {56, 4, 78, 5, 10, 22, 58},
      {85, 41, 29, 65, 47, 36, 78},
      {12, 23, 87, 45, 69, 96, 12}
   };
   cout << "The Normal of the second matrix is: " << solve( mat2 ) <<
       endl;
}

輸出

The Normal of the first matrix is: 41.1947
The Normal of the second matrix is: 49.4267

結論

範數和跡是矩陣運算。這兩個運算都需要一個方陣(跡運算需要方陣)。跡是矩陣主對角線元素的總和,而範數只是矩陣中所有元素個數的平方根。在C++中,可以使用二維陣列來表示矩陣。這裡,我們以兩個 5x5 的矩陣為例(總共 25 個元素)。需要透過迴圈語句和索引操作訪問矩陣。由於要進行範數計算需要遍歷每個元素,所以需要兩個巢狀迴圈。這個程式的複雜度是 O(n2)。而跡運算只需要主對角線元素,因此行索引和列索引相同,所以只需要一個for迴圈。它的時間複雜度是 O(n)。

更新於:2022年12月14日

1K+ 瀏覽量

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告