#! /usr/bin/env python # def ubvec_next_gray ( n, t ): #*****************************************************************************80 # #% UBVEC_NEXT_GRAY computes the next UBVEC in the Gray code. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # # Reference: # # Donald Kreher, Douglas Simpson, # Combinatorial Algorithms, # CRC Press, 1998, # ISBN: 0-8493-3988-X, # LC: QA164.K73. # # Parameters: # # Input, integer N, the number of digits in each element. # N must be positive. # # Input/output, integer T(N). # On input, T contains an element of the Gray code, that is, # each entry T(I) is either 0 or 1. # On output, T contains the successor to the input value this # is an element of the Gray code, which differs from the input # value in a single position. # import numpy as np weight = np.sum ( t ) if ( ( weight % 2 ) == 0 ): if ( t[n-1] == 0 ): t[n-1] = 1 else: t[n-1] = 0 return t else: for i in range ( n - 1, 0, -1 ): if ( t[i] == 1 ): if ( t[i-1] == 0 ): t[i-1] = 1 else: t[i-1] = 0 return t # # The final element was input. # Return the first element. # for i in range ( 0, n ): t[i] = 0 return t def ubvec_next_gray_test ( ): #*****************************************************************************80 # ## UBVEC_NEXT_GRAY_TEST tests UBVEC_NEXT_GRAY. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # import numpy as np import platform n = 4 print ( '' ) print ( 'UBVEC_NEXT_GRAY_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' UBVEC_NEXT_GRAY returns the next UBVEC in the Gray code.' ) print ( '' ) print ( ' K UBVEC' ) print ( '' ) k = 0 g = np.zeros ( n ) while ( True ): print ( ' %2d ' % ( k ), end = '' ) for j in range ( 0, n ): print ( '%2d' % ( g[j] ), end = '' ) print ( '' ) k = k + 1 g = ubvec_next_gray ( n, g ) if ( np.sum ( g ) == 0 ): break # # Terminate. # print ( '' ) print ( 'UBVEC_NEXT_GRAY_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ubvec_next_gray_test ( ) timestamp ( )