4
$\begingroup$

How can I code the Mathematica equivalent of the following pseudocode:

if[condition] ... goto[label_1] ... else goto[label_2]

where label_1 and label_2 are in different cells. I've trawled the documentation, the web and this SE, but can't find anything.

Specifically, I'm trying to compute the flow field of different types of fluids. The computation depends on the nature of the fluid, so, I want to say: if fluid_type_1, do algorithm_1, if fluid_type_2, do algorithm_2. Easy to do in Fortran, Python, etc., but I'd like to do it in Mathematica -- which is more accessible to my students.

$\endgroup$
  • 4
    $\begingroup$ I would not try to do this at the notebook level. You can define two functions, then call the appropriate one based on the condition. This is easier and more flexible than trying to evaluate a certain cell based on the condition. Also, it doesn't depend on one particular notebook and its structure. $\endgroup$ – Szabolcs Aug 27 '15 at 15:54
  • 4
    $\begingroup$ What exactly are you trying to do that is making you ponder coding gymnastics of this sort? $\endgroup$ – J. M.'s discontentment Aug 27 '15 at 16:20
  • 1
    $\begingroup$ To put it clearer. Perhaps (almost surely) it can be done in some twisted way. But it is very, very far from the recommended Mathematica programming practices. If you care to explain your case someone will show you a much better alternative. $\endgroup$ – Dr. belisarius Aug 27 '15 at 18:46
  • 1
    $\begingroup$ I'm trying to compute the stress state in hyperlastic materials, given the deformation state. The computation depends on the nature of the material, so, I want to say: $\endgroup$ – rdt2 Aug 27 '15 at 20:02
  • 1
    $\begingroup$ Note that Mathematica doesn't depend on the notebook interface either, it can function without it. Programs can be (and are) written in plain text. Making a program dependent on notebook magic will make it impossible to transfer it to plain text (e.g. a package) and will make it difficult to transfer it between notebooks. $\endgroup$ – Szabolcs Aug 27 '15 at 20:23
10
$\begingroup$

Don't do this. You should never ever need this.

Instead, define f[x_, algorithm_] with a flag for which algorithm to use. This is roughly how all built-in functions work, though they usually use Options instead. Same principle, though. For example, say condition were 5>6, and you wanted to perform the Tarragon transform if condition were true, and the Manifesto transform otherwise.

f[x_, "Tarragon"] := x+5
f[x_, "Manifesto"] := x/2

If[5>6, f[1, "Tarragon"], f[1, "Manifesto"]]

You could even push the boat out and define two separate functions:

tarragon[x_] := x+5
manifesto[x_] := x/2

If[5/6, tarragon, manifesto][1]

As an aside, you may find Label useful (if you put everything into one cell, because it only works within CompoundExpressions). Never use Label, it's extremely bad practice.

f[a_] := Module[{x = 1., xp},
  Label[begin];
  If[Abs[xp - x] < 10^-8, Goto[end]];
  xp = x;
  x = (x + a/x)/2;
  Goto[begin];
  Label[end];
 x]
| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ Sometimes I find it more readable to do f["Tarragon"][x_]:=... and etc. +1 $\endgroup$ – N.J.Evans Aug 27 '15 at 20:22
  • 2
    $\begingroup$ @N.J.Evans f[x_, ...]["Tarragon"] := ... would be more versatile, due to the fact that one cannot define Hold* attributes that affect subvalues. $\endgroup$ – Oleksandr R. Aug 27 '15 at 20:51
  • 2
    $\begingroup$ I have found use for Label, admittedly I was symbolically evaluating Fortran code, but I found a use. To say it is bad practice, though, is open for debate. However, it is primarily useful in multiple exit points which can be easily handled by either a Catch/Throw block or restructuring you code to be more functional, so it is likely not useful in Mathematica. Oh, and +1. $\endgroup$ – rcollyer Aug 27 '15 at 20:58
  • $\begingroup$ Your example is better written: f[a_] := Module[{x = a/2., xp = a}, Label[begin]; If[Abs[xp - x] < 1.*^-8, Return[x]]; xp = x; x = .5 (x + a/x); Goto[begin]] $\endgroup$ – m_goldberg Aug 28 '15 at 15:45
  • $\begingroup$ I just copy-pasted it from the docs - I think it's there for pedagogical value rather than anything else. $\endgroup$ – Patrick Stevens Aug 28 '15 at 15:50

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.