3

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.

bytecode77
  • 14,163
  • 30
  • 110
  • 141

1 Answers1

0

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.

Dante May Code
  • 11,177
  • 9
  • 49
  • 81
  • Thank you! That definitely gives all the combinations! My larger problem is an integration of the array that just isn't coming out right, but at least I know my i and j values (which are the only parts that change) are fine :) thank you again. I hope this helps some other people too, I know loops can cause a lot of confusion. – Emily Finnerty Nov 17 '12 at 22:07