Iterator - Maple Help

 Iterator

The Iterator package provides Maple objects that implement fast methods for iterating over discrete structures .

 > with(Iterator)
 $\left[{\mathrm{BinaryGrayCode}}{,}{\mathrm{BinaryTrees}}{,}{\mathrm{BoundedComposition}}{,}{\mathrm{CartesianProduct}}{,}{\mathrm{Chase}}{,}{\mathrm{Combination}}{,}{\mathrm{Count}}{,}{\mathrm{FillRucksack}}{,}{\mathrm{Inversion}}{,}{\mathrm{MixedRadix}}{,}{\mathrm{MixedRadixGrayCode}}{,}{\mathrm{MixedRadixTuples}}{,}{\mathrm{MultiPartition}}{,}{\mathrm{NearPerfectParentheses}}{,}{\mathrm{NestedParentheses}}{,}{\mathrm{OrientedForests}}{,}{\mathrm{Partition}}{,}{\mathrm{PartitionFixedSize}}{,}{\mathrm{Permute}}{,}{\mathrm{Product}}{,}{\mathrm{Reverse}}{,}{\mathrm{RevolvingDoorCombination}}{,}{\mathrm{Select}}{,}{\mathrm{SetPartitionFixedSize}}{,}{\mathrm{SetPartitions}}{,}{\mathrm{SplitRanks}}{,}{\mathrm{TopologicalSorts}}{,}{\mathrm{Trees}}\right]$ (1)

Integer Divisors

An efficient way to compute all divisors, given a prime factorization ${\mathrm{b__1}}^{\mathrm{e__1}}\cdot {\mathrm{b__2}}^{\mathrm{e__2}}\cdot ...\cdot {\mathrm{b__m}}^{\mathrm{e__m}}$, is to loop through the exponents with a mixed-radix Gray code with radices $\mathrm{r__j}=\mathrm{e__j}+1$, and then multiply or divide the previously computed divisor by $\mathrm{b__j}$, depending on whether the $j$-th exponent is increased or decreased.

 > p := 12*5*8*24*13*9;
 ${p}{≔}{1347840}$ (1.1)
 > f := ifactors(p)[2];
 ${f}{≔}\left[\left[{2}{,}{8}\right]{,}\left[{3}{,}{4}\right]{,}\left[{5}{,}{1}\right]{,}\left[{13}{,}{1}\right]\right]$ (1.2)

Extract the lists of the bases and exponents.

 > b := map2(op,1,f); e := map2(op,2,f);
 ${b}{≔}\left[{2}{,}{3}{,}{5}{,}{13}\right]$
 ${e}{≔}\left[{8}{,}{4}{,}{1}{,}{1}\right]$ (1.3)

Create a MixedRadixGrayCode iterator using the append_change option so the last index contains a signed value of the index that changed (the initial value of the index is 0).

 > G := MixedRadixGrayCode(e +~ 1, append_change):

Get the position of the index that indicates the change. The output method of the iterator object, $G$, returns the Array that is used to output the results.

 > n := upperbound(output(G)):

Update and return the divisor ($d$) given $g$, a vector whose $n$-th slot stores the index of the prime that changed, with a positive value indicating an increase by one and a negative value, a decrease by one.

 > update_d := proc(g,b,n) global d; local i;     i := g[n];     if   i < 0 then d := d/b[-i];     elif i > 0 then d := d*b[+i];     else            d := 1;     end if;     return d; end proc:

Use the iterator and procedure to compute all divisors.

 > [seq(update_d(g,b,n), g = G)];
 $\left[{1}{,}{2}{,}{4}{,}{8}{,}{16}{,}{32}{,}{64}{,}{128}{,}{256}{,}{768}{,}{384}{,}{192}{,}{96}{,}{48}{,}{24}{,}{12}{,}{6}{,}{3}{,}{9}{,}{18}{,}{36}{,}{72}{,}{144}{,}{288}{,}{576}{,}{1152}{,}{2304}{,}{6912}{,}{3456}{,}{1728}{,}{864}{,}{432}{,}{216}{,}{108}{,}{54}{,}{27}{,}{81}{,}{162}{,}{324}{,}{648}{,}{1296}{,}{2592}{,}{5184}{,}{10368}{,}{20736}{,}{103680}{,}{51840}{,}{25920}{,}{12960}{,}{6480}{,}{3240}{,}{1620}{,}{810}{,}{405}{,}{135}{,}{270}{,}{540}{,}{1080}{,}{2160}{,}{4320}{,}{8640}{,}{17280}{,}{34560}{,}{11520}{,}{5760}{,}{2880}{,}{1440}{,}{720}{,}{360}{,}{180}{,}{90}{,}{45}{,}{15}{,}{30}{,}{60}{,}{120}{,}{240}{,}{480}{,}{960}{,}{1920}{,}{3840}{,}{1280}{,}{640}{,}{320}{,}{160}{,}{80}{,}{40}{,}{20}{,}{10}{,}{5}{,}{65}{,}{130}{,}{260}{,}{520}{,}{1040}{,}{2080}{,}{4160}{,}{8320}{,}{16640}{,}{49920}{,}{24960}{,}{12480}{,}{6240}{,}{3120}{,}{1560}{,}{780}{,}{390}{,}{195}{,}{585}{,}{1170}{,}{2340}{,}{4680}{,}{9360}{,}{18720}{,}{37440}{,}{74880}{,}{149760}{,}{449280}{,}{224640}{,}{112320}{,}{56160}{,}{28080}{,}{14040}{,}{7020}{,}{3510}{,}{1755}{,}{5265}{,}{10530}{,}{21060}{,}{42120}{,}{84240}{,}{168480}{,}{336960}{,}{673920}{,}{1347840}{,}{269568}{,}{134784}{,}{67392}{,}{33696}{,}{16848}{,}{8424}{,}{4212}{,}{2106}{,}{1053}{,}{351}{,}{702}{,}{1404}{,}{2808}{,}{5616}{,}{11232}{,}{22464}{,}{44928}{,}{89856}{,}{29952}{,}{14976}{,}{7488}{,}{3744}{,}{1872}{,}{936}{,}{468}{,}{234}{,}{117}{,}{39}{,}{78}{,}{156}{,}{312}{,}{624}{,}{1248}{,}{2496}{,}{4992}{,}{9984}{,}{3328}{,}{1664}{,}{832}{,}{416}{,}{208}{,}{104}{,}{52}{,}{26}{,}{13}\right]$ (1.4)

Octacode

The octacode, $\mathrm{O__8}$, a linear self-dual code of length 8 over $\mathrm{ℤ__4}$ and minimal Lee distance 6, can be computed from the generator polynomial

 > $g≔{x}^{3}+2{x}^{2}+x-1:$

as   with $v=\sum _{k=0}^{3}\mathrm{v__k}\cdot {x}^{k}$ , , , with $\mathrm{u__7}$ selected so  and

Assign a procedure that computes one term of the sum, corresponding to the vector $v=\left[\mathrm{v__0},\mathrm{v__1},\mathrm{v__2},\mathrm{v__3}\right]$.

 > pterm := proc(v) local u,u7; global g, x, z;     u := [coeffs(collect(g*add(v[k+1]*x^k, k=0..3),x),x)];     u7 := modp(-add(u),4);     mul(z[modp(u,4)],u=u) * z[0]^(7-numelems(u)) * z[u7];  end proc:

Use the MixedRadixTuples iterator and sum over all values of $v$.

 > V := Iterator:-MixedRadixTuples([4,4,4,4]):
 > O__8 := add(pterm(v), v = V);
 $\mathrm{O__8}{≔}{{z}}_{{0}}^{{8}}{+}{14}{{z}}_{{0}}^{{4}}{{z}}_{{2}}^{{4}}{+}{56}{{z}}_{{0}}^{{3}}{{z}}_{{1}}^{{3}}{{z}}_{{2}}{{z}}_{{3}}{+}{56}{{z}}_{{0}}^{{3}}{{z}}_{{1}}{{z}}_{{2}}{{z}}_{{3}}^{{3}}{+}{56}{{z}}_{{0}}{{z}}_{{1}}^{{3}}{{z}}_{{2}}^{{3}}{{z}}_{{3}}{+}{56}{{z}}_{{0}}{{z}}_{{1}}{{z}}_{{2}}^{{3}}{{z}}_{{3}}^{{3}}{+}{{z}}_{{1}}^{{8}}{+}{14}{{z}}_{{1}}^{{4}}{{z}}_{{3}}^{{4}}{+}{{z}}_{{2}}^{{8}}{+}{{z}}_{{3}}^{{8}}$ (2.1)