# Applying loop functions with input list of iterators

Given an iterator, I am trying to generate a table of all possible pairs of values. The form of the iterator I'd like fixed, because I want to run other loops with it as well. The closest to what I want is below.. For some reason the b value isn't iterated over. What is the reason why? Is there an easier way to implement what I want given any loop function, say Do?

iterator = {{a, 0, 4}, {b, 0, 5}};
Table[iterator[[All, 1]], #] & @@ iterator


outputting:

{{0, b}, {1, b}, {2, b}, {3, b}, {4, b}}


EDIT

For those who are interested, the reason I want to preserve the iterator as an input is because it seemed the easiest way to iterate through NDEigenvalues in my situation. For an arbitrary potential energy, I am trying to numerically solve for the coefficients that produce a 0 energy ground state in Schrodinger's equation. I want to input the potential, and the range to search over in the coefficient space. The iterator acts as a guessed range based on analytic work. For example,

Input:

V[r_] := -Exp[-mS r]/r + a Exp[-mV r]/r;

iterator = {{a, 0.5, 1.5, .1}, {mS, 0.5, 1.5, .1}, {mV, 0.5, 1.5, .1}};


program will then take the potential and transform the Schrodinger's eqn into a form easier to use by NDEigenvalues, radialEq. radialEq is a differential equation with parameters a, mS, mV, operating on u[r]. The form of radialEq is why I want to iterate this way. u[r] is transformed to finite domain. So now I have something like:

radialEq = V[r] u[r] - 1/2 u''[r]
DirichletCondition[u[r] == 0,True]}, u, {r, 0, Pi/2}, 20, (*options*)];
(*appending ground state energy to coefficient-tuple in this round*),
##]&@@iterator;


iterator is then updated to refine the search, based on where the 0 evals are found.

The output will be tables of the coefficient~tuples with the ground state energies at different stages of refinement.

• I am not too sure why, but perhaps the following helps: Table[iterator[[All, 1]], Evaluate[#[]], Evaluate[#[]]] &@iterator – Dunlop Jul 3 '18 at 4:03
• Welcome alefs to our Mathematica site. You can see that you got quite some answers to your question. The reason is that although your question is clearly formulated, I and others too are not sure you are handling the underlying problem in the best way Mathematica can provide. One question I had is if you really need explicit iterators because most similar things can be better implemented completely without them. That means I would like to encourage to present an example (like you did!) but let us know why you want to do something. You might be surprised what nice different solutions exist. – halirutan Jul 3 '18 at 7:44
• Thanks for your responses! I would like to have an iterator as an input because I am working to repeatedly apply NDEigenvalues on Schrodinger's eqn with an inputted potential, numerically solving for the coefficients that produce a 0 energy ground state, and refining that coefficient grid for the next loop. NDEigenvalues takes the differential operator, and i figured the best way to loop over the coefficients was to iterate over them directly. Will provide example – alefs Jul 3 '18 at 16:32
• Did any of the answers satisfied your need? There are things to do after your question is answered. It's a good idea to stay vigilant for some time, better approaches may come later improving over previous replies. Experienced users may point alternatives, caveats or limitations. New users should test answers before voting and wait 24 hours before accepting the best one. One weeks is enough wait. Participation is essential for the site, please do your part. – rhermans Jul 9 '18 at 22:37

You need to use SlotSequence (##) in place of Slot (#).

# refers to the first element (in your case to {a, 0, 4}) in a sequence of slots.

Table[iterator[[All, 1]], ##] & @@ iterator


{{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}},
{{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}},
{{2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}},
{{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}},
{{4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}}}

Less flexibly, you could also use

Table[iterator[[All, 1]], #, #2] & @@ iterator


same result

In case you think you need an explicit iterator, let me show how it can be done without one

Outer[List, Range[0, 4], Range[0, 5]]


...or

Table[{a, b}, {a, 0, 4}, {b, 0, 5}];


...or

Table[{a, b}, Evaluate[iterator[]], Evaluate[iterator[]]]


or..

Array[List, {5, 6}] - 1


{{{0, 0}, {0, 1}, {0, 2}, {0, 3}, {0, 4}, {0, 5}}, {{1, 0}, {1, 1}, {1, 2}, {1, 3}, {1, 4}, {1, 5}}, {{2, 0}, {2, 1}, {2, 2}, {2, 3}, {2, 4}, {2, 5}}, {{3, 0}, {3, 1}, {3, 2}, {3, 3}, {3, 4}, {3, 5}}, {{4, 0}, {4, 1}, {4, 2}, {4, 3}, {4, 4}, {4, 5}}}

Array[List, {5, 6}, {0, 0}]