Iterator
Chase
generate all (s,t)-combinations of zeros and ones in near-perfect order
Calling Sequence
Parameters
Options
Description
Examples
References
Compatibility
Chase(s, t, opts)
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
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 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 01↔10 or 001↔100.
The s parameter is the number of zeros in each combination.
The t parameter is the number of ones in each combination.
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 generates all combinations of three 0's and two 1's, in Chase's sequence.
s,t≔3,2:
M≔Chase⁡s,t:
Print⁡M,showrank:
1: 0 0 0 1 1 2: 0 0 1 0 1 3: 1 0 0 0 1 4: 0 1 0 0 1 5: 0 1 1 0 0 6: 1 0 1 0 0 7: 1 1 0 0 0 8: 1 0 0 1 0 9: 0 1 0 1 0 10: 0 0 1 1 0
Compute the number of iterations.
Number⁡M
10
Return the element with rank equal to 4.
Unrank⁡M,4
01001
Copy the iterator, but start with rank equal to 4.
N≔Object⁡M,rank=4:
seq⁡v,v=N
01001,01100,10100,11000,10010,01010,00110
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 w−1 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, w−1 ones, and appends a final one.
w≔3:P≔Array⁡1..2⁢w−1:forlfrom0tow−1dot≔w+l−1;Pt+1≔1;C≔Chase⁡l,w−1;forcinCdoP1..t≔c;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.
forwinWin⁡3doprintf⁡%{}d\n,wenddo:
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.
The Iterator[Chase] command was introduced in Maple 2016.
For more information on Maple 2016 changes, see Updates in Maple 2016.
See Also
Iterator[Combination]
Download Help Document