#! /usr/bin/env python3 # def r8st_write ( filename, m, n, nst, ist, jst, ast ): #*****************************************************************************80 # ## R8ST_WRITE writes an R8ST file. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 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. # # Input, integer IST(NST), JST(NST), the row and column indices. # # Input, real AST(NST), the nonzero values. # output = open ( filename, 'w' ) for k in range ( 0, nst ): output.write ( ' %d %d %f\n' % ( ist[k], jst[k], ast[k] ) ) output.close ( ) return def r8st_write_test ( ): #*****************************************************************************80 # ## R8ST_WRITE_TEST tests R8ST_WRITE. # # Discussion: # # The matrix is: # # 11 12 0 0 15 # 21 22 0 0 0 # 0 0 33 0 35 # 0 0 0 44 0 # 51 0 53 0 55 # # The index vectors are 1 based, and so have to be converted to # 0-base before being written. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 26 September 2018 # # Author: # # John Burkardt # import numpy as np import platform from r8st_header_print import r8st_header_print from r8st_print import r8st_print m = 5 n = 5 nst = 11 ast = np.array ( [ 51.0, 12.0, 11.0, 33.0, 15.0, 53.0, 55.0, 22.0, 35.0, 44.0, 21.0 ] ) ist = np.array ( [ 5, 1, 1, 3, 1, 5, 5, 2, 3, 4, 2 ] ) jst = np.array ( [ 1, 2, 1, 3, 5, 3, 5, 2, 5, 4, 1 ] ) filename = 'a5by5_r8.st' print ( '' ) print ( 'R8ST_WRITE_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' R8ST_WRITE writes an R8ST file.' ) ist[0:nst] = ist[0:nst] - 1 jst[0:nst] = jst[0:nst] - 1 i_min = min ( ist ) i_max = max ( ist ) j_min = min ( jst ) j_max = max ( jst ) r8st_header_print ( i_min, i_max, j_min, j_max, m, n, nst ) r8st_print ( m, n, nst, ist, jst, ast, ' Sparse Triplet (ST) data:' ) r8st_write ( filename, m, n, nst, ist, jst, ast ) print ( '' ) print ( ' Wrote the matrix data to "%s".' % ( filename ) ) # # Terminate. # print ( '' ) print ( 'R8ST_WRITE_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8st_write_test ( ) timestamp ( )