generate all (s,t)-combinations of zeros and ones in near-perfect order
Chase(s, t, opts)
nonnegint; number of zeros in each combination
nonnegint; number of ones in each combination
(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. 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.
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.
Construct an iterator that generates all combinations of three 0's and two 1's, in Chase's sequence.
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.
Return the element with rank equal to 4.
Copy the iterator, but start with rank equal to 4.
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.
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.
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
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()
local w := w; # self-assignment is to avoid a mint nag
ModuleApply :: static := proc(w :: posint)
Object(Win, w, _rest);
ModuleCopy :: static := proc(self :: Win
, proto :: Win
, w :: posint := proto:-w
self:-w := w;
# 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.
ModuleIterator :: static := proc(self :: Win)
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()
if not first and h() then
val := true;
elif l = w-1 then
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();
P[1..l+w-1] := o; # copy output to P
val; # return true or false
getNext := proc() P end proc;
Print the results from using the iterator.
Knuth, Donald Ervin. The Art of Computer Programming, volume 4, fascicle 3; generating all combinations and partitions, sec. 188.8.131.52, 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.
Download Help Document
What kind of issue would you like to report? (Optional)