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
>