#! /usr/bin/env python3 # def power_method ( A, its ): #*****************************************************************************80 # ## power_method() estimates the page rank vector using the power method. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 08 April 2022 # # Author: # # John Burkardt # # Input: # # real A(N,N): the transition matrix or the Google matrix. # # integer ITS: the number of iterations. # # Output: # # real R(N): the estimated page rank vector. # import numpy as np n = A.shape[0] for it in range ( 0, its ): if ( it == 0 ): r = np.random.rand ( n ) else: r = np.dot ( A, r ) r = r / np.linalg.norm ( r ) return r def power_method_test ( ): #*****************************************************************************80 # ## power_method_test() tests power_method(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 April 2022 # # Author: # # John Burkardt # import numpy as np T = np.array ( [ \ [ 0, 0, 0.5, 1 ], \ [ 1, 0, 0, 0 ], \ [ 0, 1, 0, 0 ], \ [ 0, 0, 0.5, 0 ] ] ) its = 60 r = power_method ( T, its ) print ( '' ) print ( ' Power method result:' ) print ( r ) if ( __name__ == "__main__" ): power_method_test ( )