My timing must be bad. I have been playing around with handling and?s and or?s in if then else statements recently inside pw.mac. I was having some success and then this email thread started. It would be nice to be able to do this but after figuring out how it does not seem like it was worth the effort.
From: Stavros Macrakis
Sent: Monday, January 21, 2013 7:47 PM
To: Robert Dodier
Cc: maxima at math.utexas.edu
Subject: Re: [Maxima] piecewise function with integrals
Well, here's a somewhat better (though still pretty simple-minded) approach.
It takes something like
if X then A elseif Y then B else C
and simplifies it to
if X then (simplify A assuming X)
elseif Y then (simplify A assuming not X and Y)
else (simplify C assuming not X and not Y)
(%i1) load("if_simp");
(%o1) "if_simp.mac"
(%i2) resimp_if(if a>0 then abs(a) else abs(a));
(%o2) if a>0 then a else -a
(%i3) resimp_if(if a>0 then (if b>0 then abs(a*b) else abs(a*b)) else (if b>0 then abs(a*b) else abs(a*b)));
(%o3) if a>0 then if b>0 then a*b else -a*b else if b>0 then -a*b else a*b
There is also a variant, reeval_if, which evaluates the 'then' clauses rather than resimplifying them. This can obviously be hazardous -- caveat emptor.
(%i17) if equal(a,-1) then integrate(x^a,x) else integrate(x^a,x)$
(%i18) reeval_if(%);
(%o18) if equal(a,-1) then log(x) else x^(a+1)/(a+1)
Some limitations:
It does not yet handle and's and or's in "if" clauses. (The naive approach of just passing them along to 'assume' doesn't work.)
Since it depends on the existing assume/simplify system, it does not know that
if equal(a,b) then a-b
can be simplified to
if equal(a,b) then 0
I'll be interested in feedback / bug reports....
-s
On Sun, Jan 20, 2013 at 9:43 PM, Robert Dodier <robert.dodier at gmail.com> wrote:
On 2013-01-20, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
> (%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
Hmm, yes, that's a good idea (despite the limitations which you pointed
out).
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