0
$\begingroup$

I am looking for the way of building a Table in a fast way. My true table evaluates huge functions with a system of differential equations . Let us for simplicity consider this:

Z = 0.9;
Table[-3 x^2 + y Z, {x, 0.1, 3, 0.5}, {y, 0.1, 5, 0.5}]

{{0.06, 0.51, 0.96, 1.41, 1.86, 2.31, 2.76, 3.21, 3.66, 
  4.11}, {-0.99, -0.54, -0.09, 0.36, 0.81, 1.26, 1.71, 2.16, 2.61, 
  3.06}, {-3.54, -3.09, -2.64, -2.19, -1.74, -1.29, -0.84, -0.39, 
  0.06, 0.51}, {-7.59, -7.14, -6.69, -6.24, -5.79, -5.34, -4.89, \
-4.44, -3.99, -3.54}, {-13.14, -12.69, -12.24, -11.79, -11.34, \
-10.89, -10.44, -9.99, -9.54, -9.09}, {-20.19, -19.74, -19.29, \
-18.84, -18.39, -17.94, -17.49, -17.04, -16.59, -16.14}}

I want to print only the values from [0 to 3.06] My question: is there a way to instruct Table to stop evaluating as soon as the conditions are fulfilled? This might considerably shorten its evaluation time.

I was thinking of using Reap/Sow. Here is my tried:

Reap[Do[If[-3 x^2 + y Z, Sow[-3 x^2 + y Z], Break[]], {x, 0.1, 3, 
   0.5}, {y, 0.1, 5, 0.5}]]

Help me?

$\endgroup$
1
$\begingroup$

This will stop at the first value that is not in $[0,3.06]$:

Z = 0.9;
Reap[Do[If[u = -3 x^2 + y Z; 0 <= u <= 3.06, Sow[u], Break[]],
        {x, 0.1, 3, 0.5}, {y, 0.1, 5, 0.5}]][[2, 1]]

(*    {0.06, 0.51, 0.96, 1.41, 1.86, 2.31, 2.76}    *)

This will generate all values that are in $[0,3.06]$:

Z = 0.9;
Reap[Do[If[u = -3 x^2 + y Z; 0 <= u <= 3.06, Sow[u]],
        {x, 0.1, 3, 0.5}, {y, 0.1, 5, 0.5}]][[2, 1]]

(*    {0.06, 0.51, 0.96, 1.41, 1.86, 2.31, 2.76, 0.36,
       0.81, 1.26, 1.71, 2.16, 2.61, 3.06, 0.06, 0.51}    *)

You could also generate $(x,y,u)$ tuples:

Z = 0.9;
Reap[Do[If[u = -3 x^2 + y Z; 0 <= u <= 3.06, Sow[{x, y, u}]],
        {x, 0.1, 3, 0.5}, {y, 0.1, 5, 0.5}]][[2, 1]]

(*    {{0.1, 0.1, 0.06}, {0.1, 0.6, 0.51}, {0.1, 1.1, 0.96},
       {0.1, 1.6, 1.41}, {0.1, 2.1, 1.86}, {0.1, 2.6, 2.31},
       {0.1, 3.1, 2.76}, {0.6, 1.6, 0.36}, {0.6, 2.1, 0.81},
       {0.6, 2.6, 1.26}, {0.6, 3.1, 1.71}, {0.6, 3.6, 2.16},
       {0.6, 4.1, 2.61}, {0.6, 4.6, 3.06}, {1.1, 4.1, 0.06},
       {1.1, 4.6, 0.51}}    *)
| improve this answer | |
$\endgroup$
  • $\begingroup$ What does [2,1] means ? $\endgroup$ – aluuzz Feb 9 at 9:21
  • $\begingroup$ @aluuzz From the documentation: Reap[expr] gives the value of expr together with all expressions to which Sow has been applied during its evaluation. Expressions sown using Sow[e] or Sow[e,Subscript[tag, i]] with different tags are given in different lists. $\endgroup$ – Roman Feb 9 at 9:36
  • $\begingroup$ Does Do short circuit evaluation, or will carry out each iteration no matter what? $\endgroup$ – aluuzz Feb 9 at 9:48
  • $\begingroup$ @aluuzz Please start reading the documentation and doing your own experiments. $\endgroup$ – Roman Feb 9 at 9:53
  • $\begingroup$ I will thanks! Last things: How to apply While iteration instead of Do if could help me with that I I really appreciate it. Reap[While[-3 x^2 + y Z 0 <= && <= 3, Sow[-3 x^2 + y Z]; {x, 2.3, 3.1, 0.01}, {y, 2.3, 3.1, 0.01}]][[2, 1]] $\endgroup$ – aluuzz Feb 9 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.