#! /usr/bin/env python # def r8row_compare ( m, n, a, i, j ): #*****************************************************************************80 # ## R8ROW_COMPARE compares rows in an R8ROW. # # Discussion: # # An R8ROW is an M by N array of R8's, regarded as an array of M rows, # each of length N. # # Example: # # Input: # # M = 4, N = 3, I = 2, J = 4 # # A = ( # 1 5 9 # 2 6 10 # 3 7 11 # 4 8 12 ) # # Output: # # VALUE = -1 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 February 2016 # # Author: # # John Burkardt # # Parameters: # # Input, integer M, N, the number of rows and columns. # # Input, real A(M,N), the M by N array. # # Input, integer I, J, the rows to be compared. # I and J must be between 0 and M-1. # # Output, integer VALUE, the results of the comparison: # -1, row I < row J, # 0, row I = row J, # +1, row J < row I. # from sys import exit # # Check. # if ( i < 0 or m <= i ): print '' print 'R8ROW_COMPARE - Fatal error!' print ' Row index I is out of bounds.' print ' I = %d' % ( i ) exit ( 'R8ROW_COMPARE - Fatal error!' ) if ( j < 0 or m <= j ): print '' print 'R8ROW_COMPARE - Fatal error!' print ' Row index J is out of bounds.' print ' J = %d' % ( j ) exit ( 'R8ROW_COMPARE - Fatal error!' ) value = 0 if ( i == j ): return value k = 0 while ( k < n ): if ( a[i,k] < a[j,k] ): value = -1 return value elif ( a[j,k] < a[i,k] ): value = +1 return value k = k + 1 return value def r8row_compare_test ( ): #*****************************************************************************80 # ## R8ROW_COMPARE_TEST tests R8ROW_COMPARE. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 28 February 2016 # # Author: # # John Burkardt # import numpy as np from i4mat_print import i4mat_print from r8row_print import r8row_print print '' print 'R8ROW_COMPARE_TEST' print ' R8ROW_COMPARE compares rows of an R8ROW,' print ' returning -1, 0 or +1 for comparison.' m = 6 n = 5 a = np.zeros ( [ m, n ] ) for j in range ( 0, n ): for i in range ( 0, m ): a[i,j] = ( ( i + j ) % 3 ) r8row_print ( m, n, a, ' Matrix A:' ) c = np.zeros ( [ m, m ] ) for j in range ( 0, m ): for i in range ( 0, m ): c[i,j] = r8row_compare ( m, n, a, i, j ) i4mat_print ( m, m, c, ' C(I,J) = Row I compare Row J:' ) # # Terminate. # print '' print 'R8ROW_COMPARE_TEST:' print ' Normal end of execution.' return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) r8row_compare_test ( ) timestamp ( )