Iterator - Maple Programming Help

Online Help

All Products    Maple    MapleSim


Home : Support : Online Help : Mathematics : Discrete Mathematics : Combinatorics : Iterator : Iterator/Chase

Iterator

  

Chase

  

generate all (s,t)-combinations of zeros and ones in near-perfect order

 

Calling Sequence

Parameters

Options

Description

Examples

References

Compatibility

Calling Sequence

Chase(s, t, opts)

Parameters

s

-

nonnegint; number of zeros in each combination

t

-

nonnegint; number of ones in each combination

opts

-

(optional) equation(s) of the form option = value; specify options for the Chase command

Options

• 

compile = truefalse

  

True means compile the iterator. The default is true.

• 

rank = nonnegint

  

Specify the starting rank of the iterator. The default is one. The starting rank reverts to one when the iterator is reset, reused, or copied.

Description

• 

The Chase command returns an iterator that generates all combinations of s zeros and t ones in Chase's sequence.

• 

Transitions are strongly homogeneous; each transition is either 0110 or 001100.

• 

The s parameter is the number of zeros in each combination.

• 

The t parameter is the number of ones in each combination.

Methods

The 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 generates all combinations of three 0's and two 1's, in Chase's sequence.

s,t3,2:

MChases,t:

forVinMdoprintf%d\n,Venddo:

0 0 0 1 1
0 0 1 0 1
1 0 0 0 1
0 1 0 0 1
0 1 1 0 0
1 0 1 0 0
1 1 0 0 0
1 0 0 1 0
0 1 0 1 0
0 0 1 1 0

Compute the number of iterations.

NumberM

10

(1)

Return the element with rank equal to 4.

UnrankM,4

01001

(2)

Copy the iterator, but start with rank equal to 4.

NObjectM,rank=4:

seqv,v=N

01001,01100,10100,11000,10010,01010,00110

(3)

Winning Patterns

Enumerate the different ways to win a first-to-win w match. Let zero be a loss and one a win by the side that wins the match. Each winning pattern has w ones, zero to w1 zeros, and ends in a one.

Basic Approach

We can use a double loop to generate each winning pattern. The outer loop sets the number of losses, the inner loop generates patterns with l zeros, w1 ones, and appends a final one.

w3:PArray1..2w1:forlfrom0tow1dotw+l1;Pt+11;CChasel,w1;forcinCdoP1..tc;printf%{}d\n,Penddoenddo:

1 1 1 0 0
0 1 1 1 0
1 0 1 1 0
1 1 0 1 0
0 0 1 1 1
1 0 0 1 1
0 1 0 1 1
0 1 1 0 1
1 0 1 0 1
1 1 0 0 1

Iterator Solution

The following code assigns an appliable object, Win, that returns an iterator that generates the winning pattern. It illustrates how the iterators in this package can be used to construct specialized iterators.

Win := module()
option object;
local w := w;  # self-assignment is to avoid a mint nag
export
    ModuleApply :: static := proc(w :: posint)
        Object(Win, w, _rest);
    end proc;
export
    ModuleCopy :: static := proc(self :: Win
                                 , proto :: Win
                                 , w :: posint := proto:-w
                                )
        self:-w := w;
    end proc;
# Assign the ModuleIterator, which returns the two procedures,
# hasNext and getNext, used by Maple to iterate.  Because these
# procedures do not have direct access to the object's locals,
# we have to copy them to a local of ModuleIterator; here we
# copy the 'w' local.
export
    ModuleIterator :: static := proc(self :: Win)
    local C,L,P,first,h,hasNext,g,getNext,l,o,w;
        w := self:-w;         # copy needed
        l := -1;              # incremented when a new Chase iterator is created
        P := Array(1..2*w-1); # stores the output
        first := true;        # flag to handle first time
        # Assign the local hasNext.  This does all the work.
        hasNext := proc()
        local val;
            if not first and h() then
                val := true;
            elif l = w-1 then
                return false;
            else
                first := false;
                l := l+1;
                # Set the final win position to 1.
                P[l+w] := 1;
                # Extract the has/get procedures from the Chase iterator.
                # Call the get procedure (g) to extract the output Vector, o,
                # which we will copy into P when it is updated.  Call the has
                # procedure (h), which updates o and returns the boolean value
                # used by this iterator.
                (h,g) := :-ModuleIterator(Iterator:-Chase(l,w-1));
                o := g();
                val := h();
            end if;
            P[1..l+w-1] := o; # copy output to P
            val;              # return true or false
        end proc;
        getNext := proc() P end proc;
        (hasNext, getNext);
    end proc;
end module:

Print the results from using the iterator.

forwinWin3doprintf%{}d\n,wenddo:

1 1 1 0 0
0 1 1 1 0
1 0 1 1 0
1 1 0 1 0
0 0 1 1 1
1 0 0 1 1
0 1 0 1 1
0 1 1 0 1
1 0 1 0 1
1 1 0 0 1

References

  

Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 3; generating all combinations and partitions, sec. 7.2.1.3, generating all combinations, algorithm C (Chase's sequence), p. 13.

Compatibility

• 

The Iterator[Chase] command was introduced in Maple 2016.

• 

For more information on Maple 2016 changes, see Updates in Maple 2016.

See Also

Iterator

Iterator[Combination]