#! /usr/bin/env python # def r8st_sort_a ( m, n, nst, ist, jst, ast ): #*****************************************************************************80 # ## R8ST_SORT_A sorts the entries of an R8ST matrix by column. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 September 2018 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, the number of rows. # # Input, integer N, the number of columns. # # Input, integer NST, the number of nonzeros. # # Input/output, integer IST(NST), JST(NST), the row and column indices. # # Input/output, real AST(NST), the nonzero matrix values. # from sort_heap_external import sort_heap_external # # Initialize. # i = 0 indx = 0 isgn = 0 j = 0 i1 = 0 j1 = 0 k0 = 0 k1 = 0 n1 = 0 # # Call the external heap sorter. # while ( True ): indx, i, j, i1, j1, k0, k1, n1 = sort_heap_external ( nst, indx, isgn, \ i1, j1, k0, k1, n1 ) # # Interchange the I and J objects. # if ( 0 < indx ): rij = ist[i] ist[i] = ist[j] ist[j] = rij cij = jst[i] jst[i] = jst[j] jst[j] = cij aij = ast[i] ast[i] = ast[j] ast[j] = aij # # Compare the I and J objects. # elif ( indx < 0 ): if ( jst[i] == jst[j] ): if ( ist[i] < ist[j] ): isgn = - 1 elif ( ist[i] == ist[j] ): isgn = 0 elif ( ist[j] < ist[i] ): isgn = + 1 elif ( jst[i] < jst[j] ): isgn = - 1 elif ( jst[j] < jst[i] ): isgn = + 1 elif ( indx == 0 ): break return ist, jst, ast def r8st_sort_a_test ( ): #*****************************************************************************80 # ## R8ST_SORT_A_TEST tests R8ST_SORT_A. # # 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 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 27 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 ] ) print ( '' ) print ( 'R8ST_SORT_A_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' R8ST_SORT_A sorts an R8ST matrix by columns.' ) 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, ' Matrix data before sorting:' ) ist, jst, ast = r8st_sort_a ( m, n, nst, ist, jst, ast ) r8st_print ( m, n, nst, ist, jst, ast, ' Matrix data after sorting:' ) # # Terminate. # print ( '' ) print ( 'R8ST_SORT_A_TEST:' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8st_sort_a_test ( ) timestamp ( )