<s> from fractions import gcd <EOL> from numpy import array , arange , concatenate , convolve <EOL> from gfields import GF , polymultiply , poly_to_string <EOL> from commpy . utilities import dec2bitarray , bitarray2dec <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def cyclic_code_genpoly ( n , k ) : <EOL> """<STR_LIT>""" <EOL> if n % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> raise ValueError , "<STR_LIT>" <EOL> for m in arange ( <NUM_LIT:1> , <NUM_LIT> ) : <EOL> if ( <NUM_LIT:2> ** m - <NUM_LIT:1> ) % n == <NUM_LIT:0> : <EOL> break <EOL> x_gf = GF ( arange ( <NUM_LIT:1> , <NUM_LIT:2> ** m ) , m ) <EOL> coset_fields = x_gf . cosets ( ) <EOL> coset_leaders = array ( [ ] ) <EOL> minpol_degrees = array ( [ ] ) <EOL> for field in coset_fields : <EOL> coset_leaders = concatenate ( ( coset_leaders , array ( [ field . elements [ <NUM_LIT:0> ] ] ) ) ) <EOL> minpol_degrees = concatenate ( ( minpol_degrees , array ( [ len ( field . elements ) ] ) ) ) <EOL> y_gf = GF ( coset_leaders , m ) <EOL> minpol_list = y_gf . minpolys ( ) <EOL> idx_list = arange ( <NUM_LIT:1> , len ( minpol_list ) ) <EOL> poly_list = array ( [ ] ) <EOL> for i in xrange ( <NUM_LIT:1> , <NUM_LIT:2> ** len ( minpol_list ) ) : <EOL> i_array = dec2bitarray ( i , len ( minpol_list ) ) <EOL> subset_array = minpol_degrees [ i_array == <NUM_LIT:1> ] <EOL> if int ( subset_array . sum ( ) ) == ( n - k ) : <EOL> poly_set = minpol_list [ i_array == <NUM_LIT:1> ] <EOL> gpoly = <NUM_LIT:1> <EOL> for poly in poly_set : <EOL> gpoly_array = dec2bitarray ( gpoly , <NUM_LIT:2> ** m ) <EOL> poly_array = dec2bitarray ( poly , <NUM_LIT:2> ** m ) <EOL> gpoly = bitarray2dec ( convolve ( gpoly_array , poly_array ) % <NUM_LIT:2> ) <EOL> poly_list = concatenate ( ( poly_list , array ( [ gpoly ] ) ) ) <EOL> return poly_list . astype ( int ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> genpolys = cyclic_code_genpoly ( <NUM_LIT> , <NUM_LIT> ) <EOL> for poly in genpolys : <EOL> print poly_to_string ( poly ) </s>
