Gray Code¶
- class sympy.combinatorics.graycode.GrayCode(
- n: int,
- *args: Any,
- **kw_args: Any,
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
[R169]Nijenhuis,A. and Wilf,H.S.(1978). Combinatorial Algorithms. Academic Press.
[R170]Knuth, D. (2011). The Art of Computer Programming, Vol 4 Addison Wesley
- property current¶
Returns the currently referenced Gray code as a bit string.
Examples
>>> from sympy.combinatorics import GrayCode >>> GrayCode(3, start='100').current '100'
- generate_gray(
- **hints: Any,
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
References
[R171]Knuth, D. (2011). The Art of Computer Programming, Vol 4, Addison Wesley
- property n¶
Returns the dimension of the Gray code.
Examples
>>> from sympy.combinatorics import GrayCode >>> a = GrayCode(5) >>> a.n 5
- next(
- delta: int = 1,
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'
- property rank¶
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
References
- property selections¶
Returns the number of bit vectors in the Gray code.
Examples
>>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> a.selections 8
- skip()[source]¶
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
- classmethod unrank(
- n: int | Basic,
- rank: int,
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
- graycode.random_bitstring() str[source]¶
Generates a random bitlist of length n.
Examples
>>> from sympy.combinatorics.graycode import random_bitstring >>> random_bitstring(3) 100
- graycode.gray_to_bin() str[source]¶
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
- graycode.bin_to_gray() str[source]¶
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
- graycode.get_subset_from_bitstring(
- bitstring: str,
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.graycode_subsets() Iterator[list[T]][source]¶
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
- sympy.combinatorics.graycode.T = ~T¶
Type variable.
The preferred way to construct a type variable is via the dedicated syntax for generic functions, classes, and type aliases:
class Sequence[T]: # T is a TypeVar ...
This syntax can also be used to create bound and constrained type variables:
# S is a TypeVar bound to str class StrSequence[S: str]: ... # A is a TypeVar constrained to str or bytes class StrOrBytesSequence[A: (str, bytes)]: ...
Type variables can also have defaults:
- class IntDefault[T = int]:
…
However, if desired, reusable type variables can also be constructed manually, like so:
T = TypeVar('T') # Can be anything S = TypeVar('S', bound=str) # Can be any subtype of str A = TypeVar('A', str, bytes) # Must be exactly str or bytes D = TypeVar('D', default=int) # Defaults to int
Type variables exist primarily for the benefit of static type checkers. They serve as the parameters for generic types as well as for generic function and type alias definitions.
The variance of type variables is inferred by type checkers when they are created through the type parameter syntax and when
infer_variance=Trueis passed. Manually created type variables may be explicitly marked covariant or contravariant by passingcovariant=Trueorcontravariant=True. By default, manually created type variables are invariant. See PEP 484 and PEP 695 for more details.