Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.5K Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Evaluation inside If or Which function

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 on ly evaluated when the first case is True. Can I get some help? Thanks

Attachments:
POSTED BY: Alain RIWAN
2 Replies
Posted 10 years ago

thanks. Yes it works for this tailor-made example, But the issue is this one in fact

Clear[aa,bb,r1,r2,r3];
    aa= u+v+w;bb= 2 u-v-w;
    r4[u_,v_]=Which[aa>bb,AA,aa==bb,AB,aa>bb,AA,bb>aa,BB];

    r4[2,1] yields to
Which[3 + w > 3 - w, AA, aa == bb, AB, aa > bb, AA, bb > aa, BB]

where only the first occurence of aa and bb are replaced by their value in u,v,w and then evaluated

POSTED BY: Alain RIWAN

I would define fIf and fWhich this way, with explicit w variable:

fIf[u_, v_, w_] := Module[{aa = u + v + w, bb = 2 u - v - w},
  r = Which[bb > aa, BB, aa > bb, AA, aa == bb, AB];
  {aa, bb, r}]
fWhich[u_, v_, w_] := Module[{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}]
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard