#! /usr/bin/env python # def ubvec_check ( n, ubvec ): #*****************************************************************************80 # ## UBVEC_CHECK checks an unsigned binary vector. # # Discussion: # # The only check made is that the entries are all 0 or 1. # # 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 # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer N, the length of the vectors. # # Input, integer UBVEC(N), the vector to be checked. # # Output, integer VALUE, is True if the UBVEC is legal. # value = True for i in range ( 0, n ): if ( ubvec[i] < 0 or 2 <= ubvec[i] ): value = False return value return value def ubvec_check_test ( ): #*****************************************************************************80 # ## UBVEC_CHECK_TEST tests UBVEC_CHECK. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 22 November 2015 # # Author: # # John Burkardt # import numpy as np import platform n = 5 print ( '' ) print ( 'UBVEC_CHECK_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' UBVEC_CHECK check an unsigned binary vector.' ) print ( '' ) print ( ' CHECK? UBVEC' ) print ( '' ) ubvec = np.array ( [ 1, 0, 0, 1, 1 ] ) check = ubvec_check ( n, ubvec ) print ( ' %5s: ' % ( check ), end = '' ) for j in range ( 0, n ): print ( '%d' % (ubvec[j] ), end = '' ) print ( '' ) ubvec = np.array ( [ 1, 0, 0, 1, 9 ] ) check = ubvec_check ( n, ubvec ) print ( ' %5s: ' % ( check ), end = '' ) for j in range ( 0, n ): print ( '%d' % (ubvec[j] ), end = '' ) print ( '' ) ubvec = np.array ( [ 1, 3, 0, 1, 1 ] ) check = ubvec_check ( n, ubvec ) print ( ' %5s: ' % ( check ), end = '' ) for j in range ( 0, n ): print ( '%d' % (ubvec[j] ), end = '' ) print ( '' ) # # Terminate. # print ( '' ) print ( 'UBVEC_CHECK_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ubvec_check_test ( ) timestamp ( )