# C + + learning diary #2 -- finding the principal eigenvalue of matrix by power method

The main function of power method is to find the principal eigenvalue of matrix. This method is especially suitable for finding large sparse matrix.

## Theorem:

Let A ∈There are n linearly independent eigenvectors, principal eigenvaluessatisfy>·····, then for any non-zero initial vector(0), a vector sequence constructed according to the following method{}，{}:

(P.S) process iteration explanation:

Step 1: define an initial normalization vector.

Step 2: initial normalization vectorMultiply with matrix A to obtain a result matrix

Step 3: take the result matrixMaximum value of, i.e({})

Step 4: divide the initial normalized vector by the matrixThe maximum value of is the normalization vector required for the next iteration.

Step 5: repeat the above four steps with the newly obtained normalized vector.

## Iteration results:

notes:

When the number of iterations k tends to infinity,   Result matrixMaximum value of, i.e({}) main eigenvalue.

When the number of iterations k tends to infinity, the normalized vector is the eigenvector corresponding to the main eigenvalue (this eigenvector is obtained by dividing by the maximum value)

### Main ideas of the procedure:

It is mainly divided into three modules: assignment, solving function and output;

Here we mainly explain the solution function and output module.

### Solving function module:

It is mainly composed of four loop statements and two calling functions.

1. Execute the first cycle, which mainly calculates the multiplication of a row and column of the two matrices

2. Execute the second cycle, which is mainly used to calculate the new matrix obtained by multiplying the two matrices

3. Execute the third loop, which is mainly used to multiply the normalized vector obtained after the last iteration with the matrix

4. An insertion loop, which is located between the second loop and the third loop. It mainly acts as a bridge. The matrix obtained by each multiplication is assigned to a temporary array, which is mainly used to get the maximum value function.

Call function:

One is the maximum function and the other is the normalized vector function

These two calling functions are followed by the above insertion loop, mainly because the insertion loop provides a temporary array that can obtain the maximum value after multiplying the two matrices, and then provide the maximum value for the normalized vector. The maximum value will be defined by a definition variable (M_v[k]) with the number of iterations as the coordinate In this way, a one-to-one correspondence between the number of iterations and the maximum value can be constructed for subsequent result output.

### Output module:

The key point of this module is to unify the number of iterations, normalization vector and maximum value.

In this module, I establish a storage array of normalized vectors and maximum values for each iteration by taking the number of iterations as the coordinate. This has the advantage that in the loop statement with increasing number of iterations, the number of iterations, normalized vectors and maximum values can be output every time the loop is executed.

(p.s) all the above are my own personal understanding, and the explanation is not very professional.

## Attached implementation program

```#include <iostream>
#Include < iomanip > / / used to cout keep decimals

double a[100][100];//Define A matrix
double u[100][100];//Define normalized vector U
double v[100][100];//Define the result matrix of Au
double max_v;//Defines the maximum value of the result matrix of Au
double M_v[100]; //Define the maximum value of the result matrix of Au for subsequent output outside the loop
double X_x[100];//Define the Au result matrix for each iteration of assignment
int In;//Define the number of iterations of this time (p.s is convenient to assign the normalized vector u required for the next iteration in the solution loop calling function)
int I[100];//Define the number of iterations (P.S) to facilitate subsequent output, so that the number of iterations corresponds to the iteration results one by one
double max(double X_x[],int n);//Define maximum function
void dev_u(double X_x[], int n);//Defines the function of each element of the Au result matrix divided by the maximum value of the element
using namespace std;

int main()
{
int n;
cout << "Please enter A Order of matrix n" << endl;
cin >> n;
int N;
cout << "Please enter the number of iterations N" << endl;
cin >> N;
cout << "************Please enter A Elements of the matrix************" << endl;
cout << endl;
for (int i = 1; i <= n; i++)
{
cout << "Please enter page" << i << "Element of row" << endl;
for (int j = 1; j <= n; j++)
{
cin >> a[i][j];
}
}
cout << "Please enter the initial normalization vector" << endl;
for (int i = 1; i <= n; i++)
{
cin >> u[1][i];
}//Assignment module
for (int k = 1; k <= N; k++)//Execute the third loop, which is mainly used to multiply the normalized vector obtained after the last iteration with the matrix
{
I[k] = k;
In = k;
for (int i = 1; i <= n; i++)//Execute the second cycle, which is mainly used to calculate the new matrix obtained by multiplying the two matrices
{
for (int j = 1; j <= n; j++)//Execute the first cycle, which mainly calculates the multiplication of a row and column of the two matrices
{
v[k][i] += a[i][j] * u[k][j];
}
}
for (int p = 1; p <= n; p++)//The circular assignment mainly acts as a bridge, assigning the matrix obtained by each multiplication to a temporary array, which is mainly used in the function of taking the maximum value;
{
X_x[p] = v[k][p];
}//
max(X_x, n);//Call the max function
dev_u(X_x, n);//Call normalized vector function
M_v[k] = max_v;//Store the maximum value obtained each time for subsequent output
}//Eigenvalue function solving module
cout << "****************Output calculation results****************" << endl;
cout << endl;
cout << setiosflags(ios::fixed) << setprecision(8);//Keep 8 decimal places for output results
cout << "Number of iterations" << "   max{vk}  " << "      Normalized vector uk^(T)   " << endl;
for (int i = 1; i <= N; i++)
{
cout <<" The first" << I[i] <<"second" << "     " << M_v[i] << "     ";
for (int p = 1; p <= n - 1; p++)
{
cout << u[i + 1][p] << " , ";
}
cout << u[i + 1][n] << endl;
}//Solution result output module
cout << endl;
cout <<"The main eigenvalues and corresponding eigenvectors obtained after the maximum number of iterations are output" << endl;
cout << "Main characteristic value: " << M_v[N] << "  Corresponding eigenvector: ";
for (int p = 1; p <= n - 1; p++)
{
cout << u[N + 1][p] << " , ";
}
cout << u[N + 1][n] << endl;

return 0;
}

double max(double X_x[], int n)
{
max_v = X_x[1];

for (int p = 2; p <= n; p++)
{
if (X_x[p] > max_v)
{
max_v = X_x[p];
}
}
return max_v;
}

void dev_u(double X_x[], int n)
{
for (int p = 1; p <= n; p++)
{
u[In+1][p]= X_x[p] / max_v;
}
}```

## Examples

The power method is used to solve the principal eigenvalues and corresponding eigenvectors of A matrix. This example appears in the fifth edition of numerical analysis and is compiled by Li Qingyang et al

Screenshot of output result

### Later words

Now I'm mainly reading and learning C++ primer Plus, which is my first book to contact C + +. Because the course learning needs to contact numerical analysis In this course, the iterative method in the book is programmed, and the knowledge learned can be well combined. The program has used the for loop most so far. But after understanding the sequence relationship between the loop relationships, it will be clearly recognized, at least I am. After programming, it is enough to bring convenience. So far, it is So far, the programs have not considered the problem of resource occupation and optimization, but only the simple implementation. The articles are described from the perspective of Xiaobai, who has been in contact with the program language for a long time. They are not very professional, but they are still very friendly to Xiaobai.

Tags: C++

Posted on Sat, 30 Oct 2021 00:35:50 -0400 by egturnkey