格雷码#
- class sympy.combinatorics.graycode.GrayCode(n, *args, **kw_args)[源代码]#
格雷码本质上是边长为1的n维立方体上的哈密顿游动。立方体的顶点由值为二进制的向量表示。Hamilton walk只访问每个顶点一次。三维立方体的灰色代码是 ['000'、'100'、'110'、'010'、'011'、'111'、'101'、'001'] .
格雷码解决了按顺序生成n个对象的所有可能子集的问题,其方法是通过删除或添加单个对象从前一个对象中获得每个子集。在上面的例子中,1表示对象存在,0表示对象不存在。
当我们想以一种有效的方式计算与子集相关的各种统计信息时,格雷码在统计学中也有应用。
实例
>>> 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']
工具书类
- property current#
以位字符串形式返回当前引用的灰色代码。
实例
>>> from sympy.combinatorics import GrayCode >>> GrayCode(3, start='100').current '100'
- generate_gray(**hints)[源代码]#
生成格雷码的位向量序列。
实例
>>> 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']
参见
工具书类
[R46]Knuth,D.(2011年)。计算机程序设计的艺术,第四卷,艾迪生·韦斯利
- property n#
返回格雷码的维数。
实例
>>> from sympy.combinatorics import GrayCode >>> a = GrayCode(5) >>> a.n 5
- next(delta=1)[源代码]#
返回格雷码一个距离
delta
(默认值=1)从当前值按规范顺序排列。实例
>>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3, start='110') >>> a.next().current '111' >>> a.next(-1).current '010'
- property rank#
对灰色代码进行排序。
排序算法确定组合对象在给定顺序的所有对象中的位置(或等级)。例如,4位二进制反射格雷码(BRGC)“0101”的秩为6,因为它出现在4位格雷码族的规范顺序中的第6位。
实例
>>> 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'
参见
工具书类
- property selections#
返回格雷码中的位向量数。
实例
>>> from sympy.combinatorics import GrayCode >>> a = GrayCode(3) >>> a.selections 8
- graycode.random_bitstring()[源代码]#
生成长度为n的随机位列表。
实例
>>> from sympy.combinatorics.graycode import random_bitstring >>> random_bitstring(3) 100
- graycode.gray_to_bin()[源代码]#
从灰度编码转换为二进制编码。
我们假设是大端编码。
实例
>>> from sympy.combinatorics.graycode import gray_to_bin >>> gray_to_bin('100') '111'
参见
- graycode.bin_to_gray()[源代码]#
从二进制编码转换为灰度编码。
我们假设是大端编码。
实例
>>> from sympy.combinatorics.graycode import bin_to_gray >>> bin_to_gray('111') '100'
参见
- graycode.get_subset_from_bitstring(bitstring)[源代码]#
获取由位字符串定义的子集。
实例
>>> 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']
- graycode.graycode_subsets()[源代码]#
生成由灰色代码枚举的子集。
实例
>>> 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']]