Iterator
BinaryGrayCode
generate n-bit binary Gray code
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
BinaryGrayCode(n, opts)
n
-
nonnegint; number of bits
opts
(optional) equation(s) of the form option = value; specify options for the BinaryGrayCode command
append_change = truefalse
True means append an integer indicating the change to the output Array. The magnitude of the integer is the index that changed, the sign is the direction of the change. The default is false.
compile = truefalse
True means compile the iterator. The default is true.
rank = nonnegint
Specify the starting rank of the iterator. The default is one. Passing a value greater than one causes the iterator to skip the lower ranks; this can be useful when parallelizing iterators. The starting rank reverts to one when the iterator is reset, reused, or copied.
The BinaryGrayCode command returns an iterator that generates binary Gray code. The output is a one-dimensional Array of zeros and ones. A binary Gray code changes one element at each transition.
The n parameter specifies the number of integers in the output.
Methods
In addition to the common iterator methods, this iterator object has the following methods. The self parameter is the iterator object.
Number(self): return the number of iterations required to step through the iterator, assuming it started at rank one.
Rank(self,L): return the rank of the current iteration. Optionally pass L, a list or one-dimensional rtable, and return its rank.
Unrank(self,rnk): return a one-dimensional Array corresponding to the iterator output with rank rnk.
with⁡Iterator:
Construct an iterator that loops through all 3-bit binary Gray codes.
M≔BinaryGrayCode⁡3:
seq⁡m,m=M
000,100,110,010,011,111,101,001
Compute the number of iterations.
Number⁡M
8
Use the append_change option to display the index that changed and the direction of the change.
M≔BinaryGrayCode⁡3,append_change:
0000,1001,1102,010−1,0113,1111,101−2,001−1
Return the element with rank equal to 4.
Unrank⁡M,4
010
Simplified Dudney's Century Puzzle
Find all sequences such that 1 ∘ 2 ∘ 3 ∘ ⋯ ∘ 9 = 100, with ∘ either addition or multiplication, and normal binding strengths.
Assign a procedure that converts a boolean Vector to the relevant string.
VecToStr := proc(V) local i; cat("",seq('(i,`if`(V[i]=0,"+","*"))',i=1..8),"9=100") end proc:
Construct an iterator that steps through all 8-bit boolean Vectors.
iter≔BinaryGrayCode⁡8
Use the iterator to generate all the solutions.
forviniterdos≔VecToStr⁡v;ifparse⁡sthenprintf⁡%s\n,sendifenddo:
1*2*3*4+5+6+7*8+9=100 1*2*3+4+5+6+7+8*9=100 1+2+3+4+5+6+7+8*9=100
Compute the Permanent of a Matrix with Ryser's algorithm
The permanent of a Matrix is similar to the determinant, but in the definition the signatures of the permutations are ignored. Ryser's algorithm is an efficient method for computing the permanent.
Permanent := proc(M::Matrix) local G, J, V, aj, g, h, i, k, m, n, s, sj, sig; m, n := op(1, M); if m <> n then error "expecting a square Matrix, got dimensions %1, %2", m, n end if; G := Iterator:-BinaryGrayCode(n,'append_change'); V := Vector(n); (h,g) := ModuleIterator(G); h(); J := g(); s := 0; sig := -1; while h() do sj := sign(J[-1]); aj := abs(J[-1]); for i to n do V[i] := V[i] + sj * M[i,aj]; end do; s := s + sig*mul(V[k], k=1..n); sig := -sig; end do; expand((-1)^n*s); end proc:
Compare the time and memory usage of this routine with the one provided in LinearAlgebra[Permanent].
M≔Matrix⁡11,rand⁡11:
CodeTools:-Usage⁡Permanent⁡M
memory used=1.78MiB, alloc change=0 bytes, cpu time=39.00ms, real time=40.00ms, gc time=0ns
4852455795314422
CodeTools:-Usage⁡LinearAlgebra:-Permanent⁡M
memory used=3.10MiB, alloc change=0 bytes, cpu time=37.00ms, real time=38.00ms, gc time=0ns
Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 2; generating all tuples and permutations, sec. 7.2.1.1, generating all n-tuples, algorithm L, loopless Gray binary generation, p. 10.
The Iterator[BinaryGrayCode] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
The Iterator[BinaryGrayCode] command was updated in Maple 2022.
The n parameter was updated in Maple 2022.
See Also
combinat[graycode]
Iterator[MixedRadixGrayCode]
Iterator[MixedRadixTuples]
Download Help Document