Group Abstract Group Abstract

Message Boards Message Boards

0
|
10.3K Views
|
10 Replies
|
1 Total Like
View groups...
Share
Share this post:

Evaluation within If or Which

Posted 10 years ago

For these pieces of code how can I "r" evaluated. When I use If "r" remains unevaluated. When I use Which "r" is only evaluated when the first case is True. Can I get some help? Thanks

Clear[aa, bb, fIf, r];
fIf[u_, v_] := Block[{aa = u + v + w, bb = 2 u - v - w},
  If[bb > aa, r = BB];
  If[aa > bb, r = AA];
  If[aa == bb, r = AB];
  {aa, bb, r}
  ]
{fIf[2, 2], fIf[2, 1], fIf[2, 0]} //. w -> -1
{fIf[2, 2], fIf[2, 1], fIf[2, 0]} //. w -> 0
{fIf[2, 2], fIf[2, 1], fIf[2, 0]} //. w -> 1

Clear[aa, bb, r1, r2, r3];
fWhich[u_, v_] = Block[{aa = u + v + w, bb = 2 u - v - w},
   r1 = Which[aa == bb, AB, aa > bb, AA, bb > aa, BB];
   r2 = Which[aa > bb, AA, aa == bb, AB, bb > aa, BB];
   r3 = Which[bb > aa, BB, aa > bb, AA, aa == bb, AB];
   {aa, bb, r1, r2, r3}
   ];
{{"aa", "bb", "r1", "r2", "r3"}, fWhich[2, 1], fWhich[2, 2],    fWhich[2, 0]} //. w -> -1 // MatrixForm
{{"aa", "bb", "r1", "r2", "r3"}, fWhich[2, 1], fWhich[2, 2],    fWhich[2, 0]} //. w ->  0 // MatrixForm
{{"aa", "bb", "r1", "r2", "r3"}, fWhich[2, 1], fWhich[2, 2],    fWhich[2, 0]} //. w ->  1 // MatrixForm
POSTED BY: Alain RIWAN
10 Replies
Posted 9 years ago

I'm interpreting your question to be: "Does Mathematica use 'which' where other languages use 'then', and does Mathematica use 'switch' where other languages use 'else'?"

The answer is "no".

Mathematica has an If[...] form, and it is analogous to the if {...} else {...} branching structure of procedural languages like Java.

Mathematica has a Swich[...] form, and it is analogous to the switch {case ...} branching structure of procedural languages like Java.

Mathematica has a Which[...] form, and I think most procedural languages like Java have no analog to it. It is basically the same as cond from Lisp.

POSTED BY: Eric Rimbey

Is which used in place of then and switch used in place of else? Or am I working with these ideas incorrectly. I am new to programming.

POSTED BY: Leighton Cooper
Posted 10 years ago

Thanks. I'm pretty sure I tried it at first and it didn't work!!!

POSTED BY: Alain RIWAN
Posted 10 years ago

Well, I'm not sure where you're going with this, and how it relates to the original post, but the easy way to get "a>b" to evaluate in your example is to replace

Dynamic[a] > Dynamic[b]

with

Dynamic[a > b]
POSTED BY: Eric Rimbey
Posted 10 years ago

In fact I think the core of the problem is how to get the expression "a>b" dynamically evaluated in this example

{Slider[Dynamic[a]], Dynamic[a]} {Slider[Dynamic[b]], Dynamic[b]} Dynamic[a]> Dynamic[b]

POSTED BY: Alain RIWAN
Posted 10 years ago

Thanks for your answer.

POSTED BY: Alain RIWAN
Posted 10 years ago
POSTED BY: Eric Rimbey
POSTED BY: Frank Kampas
Posted 10 years ago
POSTED BY: Alain RIWAN
Posted 10 years ago

Alain,

Your variables aa & bb don't resolve to a number until you do the final replacements of w. Consider one of your If expressions:

If[bb > aa, r = BB]

Assuming u=2 and v=1, this is effectively trying to determine 3+w > 3-w, which cannot be determined, because w is still unknown. Since your assignment to r was inside the If, it never gets evaluated.

In your second example, you are setting r to the Which expression itself, which, upon application of the replacement rules for w, sometimes resolves to one of the values. The same thing would have happened if you used r=If[...] in your first example.

What I would suggest, is that you simply make w an input variable to fIf and fWhich. Like this:

fIf[u_, v_, w_] :=
 Block[
  {aa = u + v + w, bb = 2 u - v - w},
  If[bb > aa, r = BB];
  If[aa > bb, r = AA];
  If[aa == bb, r = AB];
  {aa, bb, r}
  ]
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard