Home  >  Article  >  Backend Development  >  C++ program to find the trace and normal of a given matrix

C++ program to find the trace and normal of a given matrix

WBOY
WBOYforward
2023-08-30 10:37:051157browse

C++ program to find the trace and normal of a given matrix

Some applications can benefit greatly from the use of two-dimensional arrays or matrices. Numbers are stored in the rows and columns of the matrix. Using multidimensional arrays, we 2D matrices can also be defined in C. In this article we will learn how to use C Determine the normal and trace of the given matrix.

The square root of the total number of elements in the matrix is ​​what is called normal. The trace consists of all the components that make up the main diagonal. let us View the representation of the algorithm in C code.

Matrix Trace

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

The sum of all elements on the main diagonal: (8 7 9) = 24, this is the trace of the given matrix

In the previous example, a 3 x 3 matrix was used and the result is the sum of each matrix The number of elements in the main diagonal. The trace of the matrix can be found in the sum. let us It helps to understand if we look at the algorithm.

algorithm

  • Read matrix M as input
  • Suppose M has n rows and n columns
  • Sum: = 0
  • For i from 1 to n, execute
    • sum := sum M[ i ][ i ]​​i>
  • Finish
  • Return the sum

Example

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

Output

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

Matrix normal

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

The sum of all elements: (8 5 3 6 7 1 2 4 9) = 45

Normal: (square root of the sum of all elements) = √45 = 6.708

In the previous example, a 3 x 3 matrix was used. We first calculate the sum of all its entries before finding its square root. Let's look at the algorithm to help us understand.

algorithm

  • Read matrix M as input
  • Suppose M has n rows and n columns
  • The sum is initialized to 0
  • For i from 1 to n, execute
    • For j ranging from 1 to n, execute
      • sum := sum M[ i ][ j ]​​i>
    • Finish
  • Finish
  • res := square root of the sum
  • Return results

Example

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

Output

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

in conclusion

Normal and trace operations belong to matrices. These two processes require Square matrix, this is what we need (because we need the trace square). The sum of the traces is The elements contained in the main diagonal of the matrix, while the normal is just The square root of the total number of elements contained in the matrix. In C, a matrix can Display using a two-dimensional array. Here we have chosen two matrices of 5 rows and 5 columns Example (25 elements total). The matrix must be accessed through a loop statement and index manipulation. Two nested loops are required because to perform the typical When calculating, we have to iterate over each element. The complexity of this program is O(n2). Since tracking only requires the main diagonal, the row and column indexes will be identical. Therefore, only a for loop is needed. Determined in O(n) time.

The above is the detailed content of C++ program to find the trace and normal of a given matrix. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete