#! /usr/bin/env python # def ubvec_unrank_grlex ( rank, n ): #*****************************************************************************80 # ## UBVEC_UNRANK_GRLEX unranks a UBVEC using the GRLEX ordering. # # Discussion: # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 November 2015 # # Author: # # John Burkardt # # Parameters: # # Input, integer RANK, the rank. # 0 <= RANK < 2^N. # # Input, integer N, the size of the BVEC. # # Output, integer B(N), the UBVEC of the given rank. # import numpy as np from i4_choose import i4_choose from ksubset_colex_unrank import ksubset_colex_unrank from ubvec_reverse import ubvec_reverse from sys import exit mk = 0 for k in range ( 0, n + 1 ): mk_old = mk mk_plus = i4_choose ( n, k ) mk = mk_old + mk_plus if ( rank < mk ): rank_k = rank - mk_old t = ksubset_colex_unrank ( rank_k, k, n ) c = np.zeros ( n ) for i in range ( 0, k ): c[t[i]-1] = 1 b = ubvec_reverse ( n, c ) return b # # If we got here, the rank is too large. # print ( '' ) print ( 'UBVEC_UNRANK_GRLEX - Fatal error!' ) print ( ' Input value of rank is too high.' ) exit ( 'UBVEC_UNRANK_GRLEX - Fatal error!' ) def ubvec_unrank_grlex_test ( ): #*****************************************************************************80 # ## UBVEC_UNRANK_GRLEX_TEST tests UBVEC_UNRANK_GRLEX. # # Licensing: # # This code is distributed under the GNU LGPL license. # # Modified: # # 23 November 2015 # # Author: # # John Burkardt # import numpy as np import platform n = 4 print ( '' ) print ( 'UBVEC_UNRANK_GRLEX_TEST' ) print ( ' Python version: %s' % ( platform.python_version ( ) ) ) print ( ' UBVEC_UNRANK_GRLEX returns the UBVEC of given rank' ) print ( ' in the graded lexicographical ordering.' ) s = -1 for rank in range ( 0, 16 ): b = ubvec_unrank_grlex ( rank, n ) if ( s < np.sum ( b ) ): print ( ' -- --------' ) s = np.sum ( b ) print ( ' %2d ' % ( rank ), end = '' ) for j in range ( 0, n ): print ( '%2d' % ( b[j] ), end = '' ) print ( '' ) # # Terminate. # print ( '' ) print ( 'UBVEC_UNRANK_GRLEX_TEST' ) print ( ' Normal end of execution.' ) return if ( __name__ == '__main__' ): from timestamp import timestamp timestamp ( ) ubvec_unrank_grlex_test ( ) timestamp ( )