BinaryGrayCode - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


Iterator

  

BinaryGrayCode

  

generate n-bit binary Gray code

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

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.

• 

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.

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

withIterator:

Construct an iterator that loops through all 3-bit binary Gray codes.

MBinaryGrayCode3:

seqm,m=M

000,100,110,010,011,111,101,001

(1)

Compute the number of iterations.

NumberM

8

(2)

Use the append_change option to display the index that changed and the direction of the change.

MBinaryGrayCode3,append_change:

seqm,m=M

0000,1001,1102,010−1,0113,1111,101−2,001−1

(3)

Return the element with rank equal to 4.

UnrankM,4

010

(4)

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.

iterBinaryGrayCode8

iterBinaryGrayCode8

(5)

Use the iterator to generate all the solutions.

forviniterdosVecToStrv;ifparsesthenprintf%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].

MMatrix11&comma;rand11&colon;

CodeTools:-UsagePermanentM

memory used=1.78MiB, alloc change=0 bytes, cpu time=39.00ms, real time=40.00ms, gc time=0ns

4852455795314422

(6)

CodeTools:-UsageLinearAlgebra:-PermanentM

memory used=3.10MiB, alloc change=0 bytes, cpu time=37.00ms, real time=38.00ms, gc time=0ns

4852455795314422

(7)

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.

• 

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

Iterator[MixedRadixGrayCode]

Iterator[MixedRadixTuples]