Power Method (Largest Eigen Value & Vector) Python Program


Real world applications of science and engineering requires to calculate numerically the largest or dominant Eigen value and corresponding Eigen vector. There are different methods like Cayley-Hamilton method, Power Method etc. Out of these methods, Power Method follows iterative approach and is quite convenient and well suited for implementing on computer.

In this tutorial, we are going to implement Power Method to calculate dominant or largest Eigen Value & corresponding Eigen Vector in python programming language.


# Power Method to Find Largest Eigen Value and Eigen Vector
# Importing NumPy Library
import numpy as np
import sys

# Reading order of matrix
n = int(input('Enter order of matrix: '))

# Making numpy array of n x n size and initializing 
# to zero for storing matrix
a = np.zeros((n,n))

# Reading matrix
print('Enter Matrix Coefficients:')
for i in range(n):
    for j in range(n):
        a[i][j] = float(input( 'a['+str(i)+']['+ str(j)+']='))

# Making numpy array n x 1 size and initializing to zero
# for storing initial guess vector
x = np.zeros((n))

# Reading initial guess vector
print('Enter initial guess vector: ')
for i in range(n):
    x[i] = float(input( 'x['+str(i)+']='))

# Reading tolerable error
tolerable_error = float(input('Enter tolerable error: '))

# Reading maximum number of steps
max_iteration = int(input('Enter maximum number of steps: '))

# Power Method Implementation
lambda_old = 1.0
condition =  True
step = 1
while condition:
    # Multiplying a and x
    x = np.matmul(a,x)
    
    # Finding new Eigen value and Eigen vector
    lambda_new = max(abs(x))
    
    x = x/lambda_new
    
    # Displaying Eigen value and Eigen Vector
    print('\nSTEP %d' %(step))
    print('----------')
    print('Eigen Value = %0.4f' %(lambda_new))
    print('Eigen Vector: ')
    for i in range(n):
        print('%0.3f\t' % (x[i]))
    
    # Checking maximum iteration
    step = step + 1
    if step > max_iteration:
        print('Not convergent in given maximum iteration!')
        break
    
    # Calculating error
    error = abs(lambda_new - lambda_old)
    print('errror='+ str(error))
    lambda_old = lambda_new
    condition = error > tolerable_error

Output

Enter order of matrix: 2
Enter Matrix Coefficients:
a[0][0]=5
a[0][1]=4
a[1][0]=1
a[1][1]=2
Enter initial guess vector: 
x[0]=1
x[1]=1
Enter tolerable error: 0.001
Enter maximum number of steps: 10

STEP 1
----------
Eigen Value = 9.0000
Eigen Vector: 
1.000	
0.333	
errror=8.0

STEP 2
----------
Eigen Value = 6.3333
Eigen Vector: 
1.000	
0.263	
errror=2.666666666666667

STEP 3
----------
Eigen Value = 6.0526
Eigen Vector: 
1.000	
0.252	
errror=0.2807017543859649

STEP 4
----------
Eigen Value = 6.0087
Eigen Vector: 
1.000	
0.250	
errror=0.04393592677345559

STEP 5
----------
Eigen Value = 6.0014
Eigen Vector: 
1.000	
0.250	
errror=0.007248474171017705

STEP 6
----------
Eigen Value = 6.0002
Eigen Vector: 
1.000	
0.250	
errror=0.0012060398307225384

STEP 7
----------
Eigen Value = 6.0000
Eigen Vector: 
1.000	
0.250	
errror=0.00020095009195664204