#! /usr/bin/env python # def r8st_data_read ( filename, m, n, nst ): #*****************************************************************************80 # ## R8ST_DATA_READ reads the data of an R8ST file. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 September 2018 # # Author: # # John Burkardt # # Parameters: # # Input, string FILENAME, the name of the file. # # Input, integer M, the number of rows. # # Input, integer N, the number of columns. # # Input, integer NST, the number of nonzeros. # # Output, integer IST(NST), JST(NST), the row and column indices. # # Output, real AST(NST), the nonzero values. # import numpy as np ist = np.zeros ( nst, dtype = np.int32 ) jst = np.zeros ( nst, dtype = np.int32 ) ast = np.zeros ( nst, dtype = np.float64 ) input = open ( filename, 'r' ) k = 0 for line in input: word = line.strip().split() ist[k] = int ( word[0] ) jst[k] = int ( word[1] ) ast[k] = float ( word[2] ) k = k + 1 input.close ( ) return ist, jst, ast def r8st_header_read ( filename ): #*****************************************************************************80 # ## R8ST_HEADER_READ reads the header of an R8ST file. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 September 2018 # # Author: # # John Burkardt # # Parameters: # # Input, string FILENAME, the name of the file. # # Output, integer I_MIN, I_MAX, the minimum and maximum rows. # # Output, integer J_MIN, J_MAX, the minimum and maximum columns. # # Output, integer M, the number of rows. # # Output, integer N, the number of columns. # # Output, integer NST, the number of nonzeros. # import numpy as np input = open ( filename, 'r' ) nst = 0 i_min = + np.inf i_max = - np.inf j_min = + np.inf j_max = - np.inf for line in input: word = line.strip().split() i = int ( word[0] ) j = int ( word[1] ) aij = float ( word[2] ) nst = nst + 1 i_min = min ( i_min, i ) i_max = max ( i_max, i ) j_min = min ( j_min, j ) j_max = max ( j_max, j ) input.close ( ) m = i_max - i_min + 1 n = j_max - j_min + 1 return i_min, i_max, j_min, j_max, m, n, nst def r8st_read_test ( ): #*****************************************************************************80 # ## R8ST_READ_TEST tests R8ST_HEADER_READ, R8ST_DATA_READ. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 September 2018 # # Author: # # John Burkardt # import platform from r8st_header_print import r8st_header_print from r8st_print import r8st_print filename = 'kershaw_r8.st' print ( '' ) print ( 'R8ST_READ_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' R8ST_HEADER_READ reads the header from an R8ST file.' ) print ( ' R8ST_DATA_READ reads the data from an R8ST file.' ) print ( '' ) print ( ' Read the data from "%s".' % ( filename ) ) i_min, i_max, j_min, j_max, m, n, nst = r8st_header_read ( filename ) r8st_header_print ( i_min, i_max, j_min, j_max, m, n, nst ) ist, jst, ast = r8st_data_read ( filename, m, n, nst ) r8st_print ( m, n, nst, ist, jst, ast, ' Sparse Triplet data from file' ) # # Terminate. # print ( '' ) print ( 'R8ST_READ_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8st_read_test ( ) timestamp ( )