#! /usr/bin/env python3 # def transition_from_incidence ( A ): #*****************************************************************************80 # ## transition_from_incidence() computes a transition matrix from an incidence 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 for a directed graph. # # Output: # # real T(N,N): the transition matrix for the directed graph. # import numpy as np T = A.copy() # T = A doesn't do what we would expect! T = T.astype ( float ) # Otherwise, integer arithmetic! m = T.shape[0] for i in range ( 0, m ): if ( np.sum ( T[i,:] ) == 0 ): T[i,:] = 1 T[i,:] = T[i,:] / np.sum ( T[i,:] ) T = np.transpose ( T ) return T def transition_from_incidence_test ( ): #*****************************************************************************80 # ## transition_from_incidence_test() tests transition_from_incidence(). # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 10 April 2022 # # Author: # # John Burkardt # import numpy as np A = np.array ( [ \ [ 0, 1, 0, 0 ], \ [ 0, 0, 1, 0 ], \ [ 1, 0, 0, 1 ], \ [ 1, 0, 0, 0 ] ] ) print ( ' Incidence matrix A:' ) print ( A ) T = transition_from_incidence ( A ) print ( ' Transition matrix T:' ) print ( T ) if ( __name__ == "__main__" ): transition_from_incidence_test ( )