For my application I see no other alternative than to use nested Do loops. My idea was to use Fold to get it done. I have produced code that appears to give the expected output, but with errors.
The below code attempts to illustrate the problem:
\[CapitalNu] = 40;
progress = 0;
Do[
Do[
progress++
, {l\[LetterSpace]1,
1 + l\[LetterSpace]0, \[CapitalNu]}]
, {l\[LetterSpace]0, Range[\[CapitalNu]]}];
(* *)
Print["Nest level 2, manual"]
Print[progress]
progress = 0;
Do[
Do[
Do[
progress++,
{l\[LetterSpace]2, 1 + l\[LetterSpace]1, \[CapitalNu]}
]
, {l\[LetterSpace]1, 1 + l\[LetterSpace]0, \[CapitalNu]}]
, {l\[LetterSpace]0, Range[\[CapitalNu]]}];
(* *)
Print["Nest level 3, manual"]
Print[progress]
progress = 0;
intrnl\[LetterSpace]DoFoldList :=
Reverse[{ToExpression["l\[LetterSpace]" <> ToString[#]],
ToExpression["l\[LetterSpace]" <> ToString[# - 1]] +
1, \[CapitalNu]} & /@
Range[1, curr\[LetterSpace]n\[LetterSpace]h - 1]];
curr\[LetterSpace]n\[LetterSpace]h = 2;
intrnl\[LetterSpace]DoFoldList
Do[
Fold[Do, Unevaluated[progress++], intrnl\[LetterSpace]DoFoldList]
, {l\[LetterSpace]0, Range[\[CapitalNu]]}];
(* *)
Print["Nest level 2, automatic"]
Print[progress]
progress = 0;
curr\[LetterSpace]n\[LetterSpace]h = 3;
intrnl\[LetterSpace]DoFoldList
Do[
Fold[Do, Unevaluated[progress++], intrnl\[LetterSpace]DoFoldList]
, {l\[LetterSpace]0, Range[\[CapitalNu]]}];
(* *)
Print["Nest level 3, automatic"]
Print[progress]
progress = 0;
The first nested Do loop increments the progress value correctly to 780 over the half triangle in state space. The second manual loop increments it to 9880.
The Do loops created using Fold appear to create the same loops, but with errors: do::iterb “Iterator {l[LetterSpace]2,1+l[LetterSpace]1,40} does not have
appropriate bounds”. The value of progress appears to give the correct value regardless. I could suppress these errors with Quiet perhaps, but my first choice would be to fix them to reduce the amount of wasted computations.