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

Online Help

All Products    Maple    MapleSim


Iterator

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

 

with(Iterator)

BinaryGrayCode,BinaryTrees,BoundedComposition,CartesianProduct,Chase,Combination,Count,FillRucksack,Inversion,MixedRadix,MixedRadixGrayCode,MixedRadixTuples,MultiPartition,NearPerfectParentheses,NestedParentheses,OrientedForests,Partition,PartitionFixedSize,Permute,Product,Reverse,RevolvingDoorCombination,Select,SetPartitionFixedSize,SetPartitions,SplitRanks,TopologicalSorts,Trees

(1)

 

Integer Divisors

Octacode

Integer Divisors

An efficient way to compute all divisors, given a prime factorization b__1e__1b__2e__2...b__me__m, is to loop through the exponents with a mixed-radix Gray code with radices r__j=e__j+1, and then multiply or divide the previously computed divisor by b__j, depending on whether the j-th exponent is increased or decreased.

p := 12*5*8*24*13*9;

p1347840

(1.1)

f := ifactors(p)[2];

f2,8,3,4,5,1,13,1

(1.2)

Extract the lists of the bases and exponents.

b := map2(op,1,f);
e := map2(op,2,f);

b2,3,5,13

e8,4,1,1

(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)];

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

(1.4)

Octacode

 

The octacode, O__8, a linear self-dual code of length 8 over &integers;__4 and minimal Lee distance 6, can be computed from the generator polynomial

gx3&plus;2x2&plus;x1&colon;

as O__8&equals;u&equals;gv k&equals;07z__u__k  with v&equals;k&equals;03v__kxk , u&equals;k&equals;06u__kxk , gv mod 4&equals;u, with u__7 selected so k&equals;07u__k&equals;0 mod 4 and 0v__0&comma;v__1&comma; v__2&comma; v__3<4.

 

Assign a procedure that computes one term of the sum, corresponding to the vector v&equals;v__0&comma;v__1&comma;v__2&comma;v__3.

 

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);

O__8z08&plus;14z04z24&plus;56z03z13z2z3&plus;56z03z1z2z33&plus;56z0z13z23z3&plus;56z0z1z23z33&plus;z18&plus;14z14z34&plus;z28&plus;z38

(2.1)