piecewise function with integrals



I don?t know where this conversation is going, but I want to add my two cents.  Pw.mac, which I wrote, can convert expressions from ?if then? form to other ?mostly equivalent? forms.  For this particular example you could do the following (not sure if this is all that great).

(%i4) display2d:false;
(out4) false
(%i5) if a>b then abs(b-a) else abs(b-a);
(out5) if a > b then abs(b-a) else abs(b-a)
(%i6) ifthen2iif(%);
(out6) iif(a > b,a-b,b-a)
(%i7) iif2ifthen(%);
(out7) if a > b then a-b else b-a

Rich

From: Stavros Macrakis 
Sent: Sunday, January 20, 2013 3:09 PM
To: Robert Dodier 
Cc: maxima at math.utexas.edu 
Subject: Re: [Maxima] piecewise function with integrals

Here's a simple-minded solution:

(%i1) expr: if cond then print(3) else print(4);
(%o1) if cond then print(3) else print(4)
(%i2) map(ev,%);
3                                << note evaluation of both branches
4
(%o2) if cond then 3 else 4      << evaluated values in 'if' expression

This is somewhat limited:

(%i3) if a>b then abs(a-b) else abs(a-b);
(%o3) if a>b then abs(b-a) else abs(b-a)
(%i4) map(ev,%);
(%o4) if a>b then abs(b-a) else abs(b-a)   << doesn't locally assume a>b or not(a>b)

Compare:

(%i5) assume(a>b)$ abs(a-b);
(%o6) a-b
(%i10) forget(a>b)$ assume(not(a>b))$ abs(a-b);
(%o12) b-a


On Sun, Jan 20, 2013 at 2:26 PM, Robert Dodier <robert.dodier at gmail.com> wrote:

  On 2013-01-19, Rupert Swarbrick <rswarbrick at gmail.com> wrote:

  > Except that people expect "if" to short-circuit. That is
  >
  >  if x>0 then sqrt(x) else error("aargh");
  >
  > shouldn't cause an error when x is positive... Similarly, what if one
  > branch was a long computation? (Such as a complicated integral!)


  Sure, that's a sensible default, but the problem at hand is that there
  is no way to force it to be something different. The only way I can
  think of is something like: apply ("if", [x > 0, foo, true, bar]);
  where foo and bar are the stuff on the branches.

  On a bit of a tangent -- a proposal was floated a while ago to make
  "and", "or" and "not" simplifying commutative operators, and create new
  ones "and_then", "or_else" as short-circuit operators. Makes sense to
  me, and it probably wouldn't really be a lot of work.

  best,


  Robert Dodier

  _______________________________________________
  Maxima mailing list
  Maxima at math.utexas.edu
  http://www.math.utexas.edu/mailman/listinfo/maxima





--------------------------------------------------------------------------------
_______________________________________________
Maxima mailing list
Maxima at math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima