#! /usr/bin/env python # def ubvec_next ( n, ubvec ): #*****************************************************************************80 # ## UBVEC_NEXT generates the next UBVEC. # # Discussion: # # The vectors are produced in the order: # # (0,0,...,0), # (0,0,...,1), # ... # (1,1,...,1) # # and the "next" vector after (1,1,...,1) is (0,0,...,0). That is, # we allow wrap around. # # A UBVEC is a vector of N binary digits. # # A UBVEC can be interpreted as a binary representation of an # unsigned integer, with the first entry being the coefficient of # 2^(N-1) and the last entry the coefficient of 1. # # UBVEC # # ----- -- # 00000 0 # 00001 1 # 00010 2 # 10000 16 # # Example: # # N = 3 # # Input Output # ----- ------ # 0 0 0 => 0 0 1 # 0 0 1 => 0 1 0 # 0 1 0 => 0 1 1 # 0 1 1 => 1 0 0 # 1 0 0 => 1 0 1 # 1 0 1 => 1 1 0 # 1 1 0 => 1 1 1 # 1 1 1 => 0 0 0 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the dimension of the vectors. # # Input/output, integer UBVEC(N), on output, the successor to the # input vector. # for i in range ( n - 1, -1, -1 ): if ( ubvec[i] == 0 ): ubvec[i] = 1 return ubvec ubvec[i] = 0 return ubvec def ubvec_next_test ( ): #*****************************************************************************80 # ## UBVEC_NEXT_TEST tests UBVEC_NEXT. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # import numpy as np import platform from ubvec_print import ubvec_print n = 4 print ( '' ) print ( 'UBVEC_NEXT_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' UBVEC_NEXT computes the "next" unsigned binary vector.' ) print ( '' ) b = np.zeros ( n ) for i in range ( 0, 17 ): ubvec_print ( n, b, '' ) b = ubvec_next ( n, b ) # # Terminate. # print ( '' ) print ( 'UBVEC_NEXT_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ubvec_next_test ( ) timestamp ( )