# Working with deep lists

I want to store parameters value such that the function that depends on those parameters.

For example, let's say I have a function that depends on three arguments: f(x;a,b), where a and b are parameters. I want to calculate the value of the function for ranges of x, a and b. Just to avoid confusions, let's say x is in the interval [1,10], a is in the interval [-10,-1] and b is in the interval [1,10]:

f[a_, b_, x_] := a*x^2 + b*x;
fimage = Outer[
f, {Range[-10, -1, 1]}, {Range[1, 10, 1]}, {Range[1, 10, 1]}]


Is there any good practice to store the values of the parameters such that the function is positive? I want to run in the deep fimage object, search for fimage>=0 and store a and b. Ultimately what I want to have is a CountourPlot of f that indicates for which combinations of a and b f is positive.

Apologies if my problem is not clear. Please let me know if you have any doubts.

If there is no particular reason for using Outer, then this is simple and straightforward:

tab = Flatten[Table[{a, b, x, f[a, b, x]}, {a, -10, -1}, {b, 1, 10}, {x, 1, 10}], 2];

pos = Select[tab, #[[4]] > 0 &]


Part (because Length @ pos = 100) of the output:

{{-9, 10, 1, 1}, {-8, 9, 1, 1}, {-8, 10, 1, 2}, {-7, 8, 1, 1}, {-7, 9, 1, 2},...}

• Of course. Thank you for clarifying this. I will give it a try in my "real" problem. – Laura K Sep 20 '16 at 19:31

corey979's answer is simple and elegant for small numbers of values of x, a, and b. But if you are using a large number of such values, you may bump up against memory constraints from storing all such values in memory and only then selecting the positive ones. A more memory- and time-efficient method might use the Nothing command, dropping the negative values as they are calculated:

table = Flatten[Table[If[f[a, b, x] > 0, {a, b, x, f[a, b, x]}, Nothing],
{a, -10, -1}, {b, 1, 10}, {x, 1, 10}], 2]


This gives the same results.

Note, however, that if your "real" function f is computationally time-intensive, this method may slow you down since it calculates the non-negative values twice. In such instances, corey979's method will likely work better.

• You could use a With block to avoid computing f twice. – 2012rcampion Sep 21 '16 at 8:06
• Good point. I did't know about Nothing. Thank you! – Laura K Sep 21 '16 at 18:17
• You could use an anonymous function to avoid computing f twice, i.e. If[#>0,{a,b,x,#},Nothing]&@f[a,b,c] – AndreasP Nov 9 '16 at 16:24

I think I would use Reap, Sow and Do for this problem. With these functions you can get the pairs {a, b} where f > 0 in one pass through the data

f[a_, b_, x_] := a*x^2 + b*x
Reap[Do[If[f[a, b, x] > 0, Sow[{a, b}]], {a, -10, -1}, {b, 1, 10}, {x, 1, 10}]][[2, 1]]

{{-9, 10}, {-8, 9}, {-8, 10}, {-7, 8}, {-7, 9}, {-7, 10}, {-6, 7}, {-6, 8},
{-6, 9}, {-6, 10}, {-5, 6}, {-5, 7}, {-5, 8}, {-5, 9}, {-5, 10}, {-4, 5},
{-4, 6}, {-4, 7}, {-4, 8}, {-4, 9}, {-4, 9}, {-4, 10}, {-4, 10}, {-3, 4},
{-3, 5}, {-3, 6}, {-3, 7}, {-3, 7}, {-3, 8}, {-3, 8}, {-3, 9}, {-3, 9},
{-3, 10}, {-3, 10}, {-3, 10}, {-2, 3}, {-2, 4}, {-2, 5}, {-2, 5}, {-2, 6},
{-2, 6}, {-2, 7}, {-2, 7}, {-2, 7}, {-2, 8}, {-2, 8}, {-2, 8}, {-2, 9},
{-2, 9}, {-2, 9}, {-2, 9}, {-2, 10}, {-2, 10}, {-2, 10}, {-2, 10}, {-1, 2},
{-1, 3}, {-1, 3}, {-1, 4}, {-1, 4}, {-1, 4}, {-1, 5}, {-1, 5}, {-1, 5},
{-1, 5}, {-1, 6}, {-1, 6}, {-1, 6}, {-1, 6}, {-1, 6}, {-1, 7}, {-1, 7},
{-1, 7}, {-1, 7}, {-1, 7}, {-1, 7}, {-1, 8}, {-1, 8}, {-1, 8}, {-1, 8},
{-1, 8}, {-1, 8}, {-1, 8}, {-1, 9}, {-1, 9}, {-1, 9}, {-1, 9}, {-1, 9},
{-1, 9}, {-1, 9}, {-1, 9}, {-1, 10}, {-1, 10}, {-1, 10}, {-1, 10}, {-1, 10},
{-1, 10}, {-1, 10}, {-1, 10}, {-1, 10}}

• That sounds like a great idea. I will also give it a try in my true problem and I will come back to all the answers again. Thank you very much! – Laura K Sep 21 '16 at 18:18