|
Calling Sequence
|
|
BinaryGrayCode(n, opts)
|
|
Parameters
|
|
n
|
-
|
nonnegint; number of bits
|
opts
|
-
|
(optional) equation(s) of the form option = value; specify options for the BinaryGrayCode command
|
|
|
|
|
Options
|
|
•
|
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.
|
|
True means compile the iterator. The default is true.
|
|
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.
|
|
|
Description
|
|
•
|
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.
|
|
|
|
Examples
|
|
Construct an iterator that loops through all 3-bit binary Gray codes.
>
|
|
| (1) |
Compute the number of iterations.
Use the append_change option to display the index that changed and the direction of the change.
>
|
|
| (3) |
Return the element with rank equal to 4.
|
Simplified Dudney's Century Puzzle
|
|
Find all sequences such that , 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.
| (5) |
Use the iterator to generate all the solutions.
>
|
|
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].
>
|
|
>
|
|
memory used=1.78MiB, alloc change=0 bytes, cpu time=39.00ms, real time=40.00ms, gc time=0ns
| |
>
|
|
memory used=3.10MiB, alloc change=0 bytes, cpu time=37.00ms, real time=38.00ms, gc time=0ns
| |
|
|
|
References
|
|
|
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.
|
|
|
Compatibility
|
|
•
|
The Iterator[BinaryGrayCode] command was introduced in Maple 2016.
|
•
|
The Iterator[BinaryGrayCode] command was updated in Maple 2022.
|
•
|
The n parameter was updated in Maple 2022.
|
|
|
|