#! /usr/bin/env python3 # def google_rank ( A ): #*****************************************************************************80 # ## google_rank() uses the power method on the Google matrix. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 April 2022 # # Author: # # John Burkardt # # Input: # # integer A(N,N): the incidence matrix. # # Output: # # real X(N): the Google rankings. # from google_from_incidence import google_from_incidence import numpy as np steps = 100 print ( '' ) print ( 'google_rank():' ) print ( ' Given an NxN incidence matrix A,' ) print ( ' compute the Google matrix G,' ) print ( ' Then start with a vector of N values 1/N,' ) print ( ' and repeatedly compute x <= G*x' ) print ( '' ) print ( ' After many steps, compare last three iterates.' ) print ( ' If they are close, we are probably at an eigenvector' ) print ( ' associated with the eigenvalue 1.' ) n = A.shape[0] # # Compute the Google matrix. # G = google_from_incidence ( A ) # # Set the starting vector. # x = np.ones ( n ) / float ( n ) # # Carry out many iterations. # Normalization is not necessary because T is a transition matrix. # for i in range ( 0, steps ): x = np.dot ( G, x ) # # Compare three successive iterates. # Gx = np.dot ( G, x ) GGx = np.dot ( G, Gx ) W = np.stack ( ( x, Gx, GGx ), axis = 1 ) print ( '' ) print ( 'x, G*x, G*G*x' ) print ( W ) return x def google_rank_test ( ): #*****************************************************************************80 # ## google_rank_test() tests google_rank(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 April 2022 # # Author: # # John Burkardt # from moler_incidence import moler_incidence print ( '' ) print ( 'google_rank_test():' ) print ( ' google_rank() computes the Google rankings r()' ) print ( ' for an incidence matrix A.' ) A = moler_incidence ( ) print ( '' ) print ( ' Incidence matrix A:' ) print ( A ) r = google_rank ( A ) print ( '' ) print ( ' Google rankings r:' ) print ( r ) return if ( __name__ == '__main__' ): google_rank_test ( )