Search code examples
arrayslistfor-loopmaple

Maple - How to make a loop of 4 variables suitable for an array


Say I have two lists: M=(0,1,2,3) and N=(0,2,4,6)

And I wish to put into a list all the combinations of Mi,Mj,Ns,Nt (where i,j,s,t are       subscripts so for i=1 M=0, i=2 M=1 etc.) such that:

C = a^(Mi+Mj) + b^(Ns+Nj)

First in the list would be
C = a^(0+0) + b^(0+0)
C = a^(1+0) + b^(0+0)
C = a^(1+0) + b^(2+0)

Is there a neater way of writing this out than using the 'for' clause 4 times?

for i from 1 to 4 do
for j from 1 to 4 do
for s from 1 to 4 do
for t from 1 to 4 do

C = a^(Mi+Mj) + b^(Ns+Nj)

end do;
end do;
end do;
end do;

I will be putting it into an array, but I want to limit the recursion because maple doesn't like it! Is it possible to put this into a 2x2 array with 4 variables?

I considered making 2 lists of all the combinations of Mi+Mj and all the combinations of Ns+Nt then putting those together in an array, but it comes out similar to what I want, yet not quite right.


Solution

  • I do not use Maple, but after a quick Google search, it turns out that Maple at least supports nested loops. So....


    First, iterate through all the combinations of M, calculate a^(Mi + Mj), and store the results in A.

    for i from 1 to 4 do
    for j from 1 to 4 do
        A = a^(Mi+Mj)
    end do;
    end do;
    

    Second, do the same for N, and store the results in B.

    Third, get C.

    for i from 1 to 16 do
    for j from 1 to 16 do
        C = Ai + Bj
    end do;
    end do;
    

    Actually, the original approach requires a lot of redundant calculations, and the above approach eliminates the redundancy. Thus, I guess it is not only workable but also faster.