Home > Backend Development > C++ > body text

C++ program to find normal vectors and traces

WBOY
Release: 2023-09-09 16:41:09
forward
1050 people have browsed it

C++ program to find normal vectors and traces

Two-dimensional arrays or matrices are very useful in several applications. A matrix has rows and columns and stores numbers in it. In C, we can also use multidimensional arrays to define two-dimensional matrices. In this article, we will see how to calculate the norm and trace of a given matrix using C.

The normal is the square root of the sum of all elements in the matrix. The trace is the sum of the elements present in the main diagonal. Let's look at the algorithm and C code representation.

Matrix normal

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

Sum of all elements: (5 + 1 + 8 + 4 + 3 + 9 + 2 + 7 + 3) = 42
Normal: (Square root of the sum of all elements) = √42 = 6.48
Copy after login

In the above example, we took a 3 x 3 matrix, here we get the sum of all elements and then take the square root of it. Let us see the algorithm for better understanding.

algorithm

  • Read matrix M as input
  • Consider M with n rows and n columns
  • Sum: = 0
  • For i from 1 to n, execute
    • For j from 1 to n, perform the following operations
      • sum := sum M[ i ][ j ]
    • End loop
  • End loop
  • res := sum of square roots
  • Return results

Example

#include <iostream>
#include <cmath>
#define N 5
using namespace std;
float solve( int M[ N ][ N ] ){
   int sum = 0;
   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},
      {48, 2, 98, 6, 63},
      {85, 12, 10, 6, 9},
      {6, 12, 18, 32, 5},
      {8, 45, 74, 69, 1},
   };
   cout << "Normal of the first matrix is: " << solve( mat1 ) << endl;
   int mat2[ N ][ N ] = {
      {6, 8, 35, 21, 87},
      {99, 2, 36, 326, 25},
      {15, 215, 3, 157, 8},
      {96, 115, 17, 5, 3},
      {56, 4, 78, 5, 10},
   };
   cout << "Normal of the second matrix is: " << solve( mat2 ) << endl;
}
Copy after login

Output

Normal of the first matrix is: 28.0357
Normal of the second matrix is: 37.8418
Copy after login

Matrix trace

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

Sum of all elements in main diagonal: (5 + 3 + 3) = 11 which is
the trace of given matrix
Copy after login

In the above example, we took a 3 x 3 matrix, where we got the sum of all the elements on the main diagonal. This sum is the trace of the matrix. Let’s take a look at the algorithm for better understanding.

algorithm

  • Read matrix M as input
  • Consider M with n rows and n columns
  • Sum: = 0
  • For i from 1 to n, execute
    • sum := sum M[ i ][ i ]
  • End loop
  • Return the sum

Example

#include <iostream>
#include <cmath>
#define N 5
using namespace std;
float solve( int M[ N ][ N ] ){
   int sum = 0;
   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},
      {48, 2, 98, 6, 63},
      {85, 12, 10, 6, 9},
      {6, 12, 18, 32, 5},
      {8, 45, 74, 69, 1},
   };
   cout << "Trace of the first matrix is: " << solve( mat1 ) << endl;
   int mat2[ N ][ N ] = {
      {6, 8, 35, 21, 87},
      {99, 2, 36, 326, 25},
      {15, 215, 3, 157, 8},
      {96, 115, 17, 5, 3},
      {56, 4, 78, 5, 10},
   };
   cout << "Trace of the second matrix is: " << solve( mat2 ) << endl;
}
Copy after login

Output

Trace of the first matrix is: 50
Trace of the second matrix is: 26
Copy after login

in conclusion

Normals and traces are both matrix operations. In order to perform these two operations, we need a square matrix (because a square trace matrix is ​​required). The normal is simply the square root of the sum of all elements present in the matrix, and the trace is the sum of the elements present on the main diagonal of the matrix. This matrix can be represented using a two-dimensional array in C. Here we give two examples of matrices with 5 rows and 5 columns (25 elements in total). Accessing the matrix requires a loop statement with indexing operations. For normal calculations we need to iterate through each element, so two nested loops are required. The complexity of this program is O(n2). For tracing, since we only need to look at the main diagonal, the row index and column index will be the same. So just one for loop is enough. Can be calculated in O(n) time.

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

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template