class_docstr: 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
class_docstr_tok: ["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"]
docstr: 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
docstr_tok: ["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"]
code: def __new__(cls, n, *args, **kw_args):
      """
      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
  
      """
      if n < 1 or int(n) != n:
          raise ValueError(
              "Gray code dimension must be a positive integer, not %i"
              % n
          )
      n = Integer(n)
      args = (n,) + args
      obj = Basic.__new__(cls, *args)
      if "start" in kw_args:
          obj._current = kw_args["start"]
          if len(obj._current) > n:
              raise ValueError(
                  "Gray code start has length %i but "
                  "should not be greater than %i"
                  % (len(obj._current), n)
              )
      elif "rank" in kw_args:
          if int(kw_args["rank"]) != kw_args["rank"]:
              raise ValueError(
                  "Gray code rank must be a positive integer, "
                  "not %i" % kw_args["rank"]
              )
          obj._rank = int(kw_args["rank"]) % obj.selections
          obj._current = obj.unrank(n, obj._rank)
      return obj
  
file_path: sympy/sympy/combinatorics/graycode.py
imports: from sympy.core import Basic, Integer
  import random
label_desc: Use image node_id 1 for calling the GrayCode obj's underlying member method code with example usage: obj.__new__(cls, n) and returns: obj