K i+PddlmZmZddlZGddeZdZdZdZdZd Z y) )BasicIntegerNceZdZdZdZdZdZdZddZe dZ e dZ d Z d Z e d Ze d Zed Zy)GrayCodea A Gray code is essentially a Hamiltonian walk on a n-dimensional cube with edge length of one. The vertices of the cube are represented by vectors whose values are binary. The Hamilton walk visits each vertex exactly once. The Gray code for a 3d cube is ['000','100','110','010','011','111','101', '001']. A Gray code solves the problem of sequentially generating all possible subsets of n objects in such a way that each subset is obtained from the previous one by either deleting or adding a single object. In the above example, 1 indicates that the object is present, and 0 indicates that its absent. Gray codes have applications in statistics as well when we want to compute various statistics related to subsets in an efficient manner. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> list(a.generate_gray()) ['000', '001', '011', '010', '110', '111', '101', '100'] >>> a = GrayCode(4) >>> list(a.generate_gray()) ['0000', '0001', '0011', '0010', '0110', '0111', '0101', '0100', '1100', '1101', '1111', '1110', '1010', '1011', '1001', '1000'] References ========== .. [1] Nijenhuis,A. and Wilf,H.S.(1978). Combinatorial Algorithms. Academic Press. .. [2] Knuth, D. (2011). The Art of Computer Programming, Vol 4 Addison Wesley FrNc|dkst||k7rtd|zt|}|f|z}tj|g|}d|vrG|d|_t |j |kDr#tdt |j |fz|Sd|vrft|d|dk7rtd|dzt|d|jz|_|j||j|_|S)ax Default constructor. It takes a single argument ``n`` which gives the dimension of the Gray code. The starting Gray code string (``start``) or the starting ``rank`` may also be given; the default is to start at rank = 0 ('0...0'). Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> a GrayCode(3) >>> a.n 3 >>> a = GrayCode(3, start='100') >>> a.current '100' >>> a = GrayCode(4, rank=4) >>> a.current '0110' >>> a.rank 4 z6Gray code dimension must be a positive integer, not %istart?Gray code start has length %i but should not be greater than %irankz1Gray code rank must be a positive integer, not %i) int ValueErrorrr__new___currentlen selections_rankunrank)clsnargskw_argsobjs b/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/sympy/combinatorics/graycode.pyrzGrayCode.__new__6s: q5CFaKH1LN N AJtd{mmC'$' g "7+CL3<< 1$ "036s||3Da2H"IJJ w 76?#wv6 ""6?"+,,GFO,s~~=CI::a3CL cdt|j|j|z|jzS)aX Returns the Gray code a distance ``delta`` (default = 1) from the current value in canonical order. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3, start='110') >>> a.next().current '111' >>> a.next(-1).current '010' )r )rrr r)selfdeltas rnextz GrayCode.nextfs' dii%&74??%JKKrc d|jzS)z Returns the number of bit vectors in the Gray code. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> a.selections 8 )rrs rrzGrayCode.selectionsxs$&&yrc |jdS)z Returns the dimension of the Gray code. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(5) >>> a.n 5 r)rr!s rrz GrayCode.nsyy|rc+FK|j}d}d|vr|d}n'd|vr#tj|j|d}|||_|j}t |}t ||jkDrtdt ||fzt|d|_tdj|d}t|d|zD]H}|jrd|_ n|j||dzz }||dz z } |j| z |_Jd |_yw) a Generates the sequence of bit vectors of a Gray Code. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> list(a.generate_gray()) ['000', '001', '011', '010', '110', '111', '101', '100'] >>> list(a.generate_gray(start='011')) ['011', '010', '110', '111', '101', '100'] >>> list(a.generate_gray(rank=4)) ['110', '111', '101', '100'] See Also ======== skip References ========== .. [1] Knuth, D. (2011). The Art of Computer Programming, Vol 4, Addison Wesley Nr r r r rFr) rrrrcurrent gray_to_binrr r joinrange_skip) rhintsbitsr r% graycode_bin graycode_intibbtcgbtcs r generate_grayzGrayCode.generate_grays&8vv e 'NE u_OODFFE&M:E  !DM,,"7+ | tvv %%(+L(94'@AB BGQ 277<0!4 |Q$Y/ 3Azz" ll"QKDDAI&D!]]T1DM 3 sDD!cd|_y)a Skips the bit generation. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> for i in a.generate_gray(): ... if i == '010': ... a.skip() ... print(i) ... 000 001 011 010 111 101 100 See Also ======== generate_gray TN)r)r!s rskipz GrayCode.skips 6 rcz|j$tt|jd|_|jS)a Ranks the Gray code. A ranking algorithm determines the position (or rank) of a combinatorial object among all the objects w.r.t. a given order. For example, the 4 bit binary reflected Gray code (BRGC) '0101' has a rank of 6 as it appears in the 6th position in the canonical ordering of the family of 4 bit Gray codes. Examples ======== >>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> list(a.generate_gray()) ['000', '001', '011', '010', '110', '111', '101', '100'] >>> GrayCode(3, start='100').rank 7 >>> GrayCode(3, rank=7).current '100' See Also ======== unrank References ========== .. [1] https://web.archive.org/web/20200224064753/http://statweb.stanford.edu/~susan/courses/s208/node12.html r )rr r&r%r!s rr z GrayCode.ranks0F :: [6:DJzzrc|jxsd}t|tst|dd}|j |j dS)z Returns the currently referenced Gray code as a bit string. Examples ======== >>> from sympy.combinatorics import GrayCode >>> GrayCode(3, start='100').current '100' 0r N)r isinstancestrbinrjustr)rrvs rr%zGrayCode.currents@]] !c"c"RBxx$$rc fd||S)a Unranks an n-bit sized Gray code of rank k. This method exists so that a derivative GrayCode class can define its own code of a given rank. The string here is generated in reverse order to allow for tail-call optimization. Examples ======== >>> from sympy.combinatorics import GrayCode >>> GrayCode(5, rank=3).current '00010' >>> GrayCode.unrank(5, 3) '00010' See Also ======== rank c|dk(rt|dzSd|dz z}||krd||dz zSd|||zz dz |dz zS)Nrr r61)r8)krm_unranks rrAz GrayCode.unrank.._unrank8sdAv1q5z!AE A1uWQA...a!eq!a%88 8r)rrr rAs @rrzGrayCode.unrank s0 9tQr)r)__name__ __module__ __qualname____doc__r)rrrrpropertyrrr1r3r r% classmethodrrBrrrrs)V EH E.`L$    3j:$$L%%   rrcdjt|Dcgc]}tjdc}Scc}w)z Generates a random bitlist of length n. Examples ======== >>> from sympy.combinatorics.graycode import random_bitstring >>> random_bitstring(3) # doctest: +SKIP 100 r$01)r'r(randomchoice)rr.s rrandom_bitstringrMBs. 77q:AFMM$': ;;:s<c |dg}tdt|D]%}|tt||dz ||k7z }'dj |S)a Convert from Gray coding to binary coding. We assume big endian encoding. Examples ======== >>> from sympy.combinatorics.graycode import gray_to_bin >>> gray_to_bin('100') '111' See Also ======== bin_to_gray rrr$r(rr8r r'bin_listbr.s rr&r&Ps\$ ! A 1c(m $/ SQq1uX!,- ../ 771:rc |dg}tdt|D].}|tt||t||dz z z }0dj |S)a Convert from binary coding to gray coding. We assume big endian encoding. Examples ======== >>> from sympy.combinatorics.graycode import bin_to_gray >>> bin_to_gray('111') '100' See Also ======== gray_to_bin rrr$rOrPs r bin_to_grayrThsa$ ! A 1c(m $: SXa[!CQ$88 99: 771:rct|t|k7r tdt|Dcgc]\}}||dk(r||c}}Scc}}w)ai Gets the subset defined by the bitstring. Examples ======== >>> from sympy.combinatorics.graycode import get_subset_from_bitstring >>> get_subset_from_bitstring(['a', 'b', 'c', 'd'], '0011') ['c', 'd'] >>> get_subset_from_bitstring(['c', 'a', 'c', 'c'], '1100') ['c', 'a'] See Also ======== graycode_subsets z$The sizes of the lists are not equalr>)rr enumerate) super_set bitstringr.js rget_subset_from_bitstringrZsX$ 9~Y'?@@%.y%9 $TQ|s" aL $$ $sA c#Kttt|jD]}t ||yw)a` Generates the subsets as enumerated by a Gray code. Examples ======== >>> from sympy.combinatorics.graycode import graycode_subsets >>> list(graycode_subsets(['a', 'b', 'c'])) [[], ['c'], ['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'c'], ['a']] >>> list(graycode_subsets(['a', 'b', 'c', 'c'])) [[], ['c'], ['c', 'c'], ['c'], ['b', 'c'], ['b', 'c', 'c'], ['b', 'c'], ['b'], ['a', 'b'], ['a', 'b', 'c'], ['a', 'b', 'c', 'c'], ['a', 'b', 'c'], ['a', 'c'], ['a', 'c', 'c'], ['a', 'c'], ['a']] See Also ======== get_subset_from_bitstring N)listrrr1rZ) gray_code_setrXs rgraycode_subsetsr^s>*(3}#56DDFGB ' yAABsAA) sympy.corerrrKrrMr&rTrZr^rBrrr`s4% y uy x <00$0Br