
    \h+                     P    S SK JrJr  S SKr " S S\5      rS rS rS rS rS	 r	g)
    )BasicIntegerNc                       \ rS rSrSrSrSrSrS rSS jr	\
S 5       r\
S	 5       rS
 rS r\
S 5       r\
S 5       r\S 5       rSrg)GrayCode   a  
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


Fr   Nc                    US:  d  [        U5      U:w  a  [        SU-  5      e[        U5      nU4U-   n[        R                  " U /UQ76 nSU;   aI  US   Ul        [        UR
                  5      U:  a#  [        S[        UR
                  5      U4-  5      e U$ SU;   af  [        US   5      US   :w  a  [        SUS   -  5      e[        US   5      UR                  -  Ul        UR                  XR                  5      Ul        U$ )a  
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 %istart?Gray code start has length %i but should not be greater than %irankz1Gray code rank must be a positive integer, not %i)
int
ValueErrorr   r   __new___currentlen
selections_rankunrank)clsnargskw_argsobjs        T/var/www/auris/envauris/lib/python3.13/site-packages/sympy/combinatorics/graycode.pyr   GrayCode.__new__6   s   : q5CFaKH1LN NAJtd{mmC'$'g"7+CL3<< 1$  "036s||3Da2H"I J J % 
 w76?#wv6  ""6?"+ , ,GFO,s~~=CI::a3CL
    c                 ^    [        U R                  U R                  U-   U R                  -  S9$ )a   
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   )r   r   r   r   )selfdeltas     r   nextGrayCode.nextf   s'      dii%&74??%JKKr   c                      SU R                   -  $ )z
Returns the number of bit vectors in the Gray code.

Examples
========

>>> from sympy.combinatorics import GrayCode
>>> a = GrayCode(3)
>>> a.selections
8
   )r   r   s    r   r   GrayCode.selectionsx   s     $&&yr   c                      U R                   S   $ )z
Returns the dimension of the Gray code.

Examples
========

>>> from sympy.combinatorics import GrayCode
>>> a = GrayCode(5)
>>> a.n
5
r   )r   r$   s    r   r   
GrayCode.n   s     yy|r   c              +   Z  #    U R                   nSnSU;   a  US   nO)SU;   a#  [        R                  U R                   US   5      nUb  X0l        U R                  n[        U5      n[        U5      U R                   :  a  [        S[        U5      U4-  5      e[        US5      U l        [        SR                  U5      S5      n[        USU-  5       HL  nU R                  (       a  SU l        OU R                  v   XwS-   -  nXS-	  -  n	U R                  U	-  U l        MN     S	U l        g7f)
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#    r	   Fr   )r   r   r   r   currentgray_to_binr   r   r   joinrange_skip)
r   hintsbitsr
   r*   graycode_bingraycode_intibbtcgbtcs
             r   generate_grayGrayCode.generate_gray   s    8 vve'NEu_OODFFE&M:E!M,,"7+|tvv% %(+L(94'@A B BGQ277<0!4|Q$Y/Azz"
ll"QKDAI&D!]]T1DM 0 s   D)D+c                     SU l         g)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    r   skipGrayCode.skip   s    6 
r   c                 |    U R                   c$  [        [        U R                  5      S5      U l         U R                   $ )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#   )r   r   r+   r*   r$   s    r   r   GrayCode.rank   s0    F ::[6:DJzzr   c                     U R                   =(       d    Sn[        U[        5      (       d  [        U5      SS nUR	                  U R
                  S5      $ )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   
isinstancestrbinrjustr   )r   rvs     r   r*   GrayCode.current  sC     ]]!c"c""RBxx$$r   c                     ^ U4S jmT" X!5      $ )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                    > US:X  a  [        U S-  5      $ SUS-
  -  nX:  a  ST" XS-
  5      -   $ ST" X U-  -
  S-
  US-
  5      -   $ )Nr	   r#   r>   1)r@   )kr   m_unranks      r   rJ    GrayCode.unrank.<locals>._unrank8  s^    Av1q5z!AE
AuWQA...!eq!a%888r    )r   r   r   rJ   s      @r   r   GrayCode.unrank   s    0	9 tr   )r   r   r.   )r	   )__name__
__module____qualname____firstlineno____doc__r.   r   r   r   r    propertyr   r   r6   r9   r   r*   classmethodr   __static_attributes__rL   r   r   r   r      s    )V EHE.`L$    3j: $ $L % %     r   r   c                     SR                  [        U 5       Vs/ s H  n[        R                  " S5      PM     sn5      $ s  snf )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-   randomchoice)r   r3   s     r   random_bitstringrZ   B  s2     77q:AFMM$':;;:s    A c           	          U S   /n[        S[        U 5      5       H$  nU[        [        XS-
     X   :g  5      5      -  nM&     SR	                  U5      $ )z
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
r   r	   r)   r-   r   r@   r   r,   bin_listbr3   s      r   r+   r+   P  sS    $ 
!A1c(m$	SQ1uX,-.. %771:r   c           
          U S   /n[        S[        U 5      5       H-  nU[        [        X   5      [        XS-
     5      -  5      -  nM/     SR	                  U5      $ )z
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
r   r	   r)   r\   r]   s      r   bin_to_grayra   h  sX    $ 
!A1c(m$	SX[!CQ$8899 %771:r   c                     [        U 5      [        U5      :w  a  [        S5      e[        U5       VVs/ s H  u  p#X   S:X  d  M  X   PM     snn$ s  snnf )a9  
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 equalrG   )r   r   	enumerate)	super_set	bitstringr3   js       r   get_subset_from_bitstringrg     sZ    $ 9~Y'?@@%.y%9 $%9TQ|s" IL%9 $ $ $s   AAc              #      #    [        [        [        U 5      5      R                  5       5       H  n[	        X5      v   M     g7f)a0  
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)listr   r   r6   rg   )gray_code_setre   s     r   graycode_subsetsrk     s5     * (3}#56DDFG	'AA Hs   AA)

sympy.corer   r   rX   r   rZ   r+   ra   rg   rk   rL   r   r   <module>rm      s4    % y u y x	<00$0Br   