C++ Program To Find the Trace and Normal of a given Matrix

Several applications greatly benefit from the use of 2-dimensional arrays or matrices. Numbers are stored in rows and columns of matrices. Using multi-dimensional arrays, we may define 2D matrices in C++ as well. In this post, we'll look at how to use C++...

Several applications greatly benefit from the use of 2-dimensional arrays or matrices. Numbers are stored in rows and columns of matrices. Using multi-dimensional arrays, we may define 2D matrices in C++ as well. In this post, we'll look at how to use C++ to determine the Normal and Trace of a given matrix.

The square root of the total number of elements in the matrix is what is known as the Normal. The trace is made up of all the components that make up the main diagonal. Let's look at the representation of the algorithm in C++ code.

Matrix Trace

[853671249],

Sum of all elements in main diagonal: (8 + 7 + 9) = 24 which is the trace of given matrix

In the previous example, one 3 x 3 matrix was used, and the result is the total sum of the number of elements in the major diagonal. The matrix's trace can be found in the sum. Let's look at the algorithm to help us understand.

Algorithm

  • read matrix M as input
  • Assume M has n rows and n columns
  • sum := 0
  • for i ranging from 1 to n, do
    • sum := sum + M[ i ][ i ]
  • end for
  • return 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

[853671249],

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 last example, a 3 x 3 matrix was used. We first calculated the sum of all of its entries before taking its square root. Let's look at the algorithm to help us comprehend.

Algorithm

  • read matrix M as input
  • Assume M has n rows and n columns
  • the sum is initialized with 0
  • for i ranging from 1 to n, do
    • for j ranging from 1 to n, do
      • sum := sum + M[ i ][ j ]
    • end for
  • end for
  • res := square root of the sum
  • return res

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

Conclusion

The operations normal and trace belong to the matrix. These two procedures require a square matrix, which is what we need (for trace square is needed). The trace is the total of the elements contained in the matrix's principal diagonal, while the normal is simply the square root of the total number of elements included in the matrix. In C++, the matrix can be displayed using 2D arrays. Here, we've chosen two matrices with 5 rows and 5 columns as examples (a total of 25 elements). The matrix must be accessed via looping statements and index manipulation. Two nested loops are required because, to perform the typical calculation, we must iterate through each element. And this program's complexity is O (n2). Since just the major diagonal is required for tracing, the row and column indices will be the same. Therefore, just one for loop is required. In O(n) time, it is determined.

评论0

首页 导航 会员 客服