piecewise function with integrals



Your program does some cool stuff.  pw.mac can do the same thing in some cases indirectly.  Your approach is more direct, which may be better.  I want to show what is possible with your program potentially and pw post FWIW.

(%i1) display2d:false;
(out1) false
(%i4) if x>-10 and x<10 or x>12 and x<17 then x else x^2$
(%i5) ifthen2sum(%);
(out5) x*(between(x,12,17,open)+between(x,-10,10,open))+x^2*(-between(x,12,17,open)-between(x,-10,10,open)+1)
(%i6) toiif(%); /* very messy but watch how is cleans up */
(out6) x*((iif(x+10 > 0,1,iif(x+10 < 0,-1,0))-iif(equal(x+10,0),1,0)-iif(x-10 > 0,1,iif(x-10 < 0,-1,0))-iif(equal(x-10,0),1,0))/2
         +(iif(x-12 > 0,1,iif(x-12 < 0,-1,0))-iif(equal(x-12,0),1,0)-iif(x-17 > 0,1,iif(x-17 < 0,-1,0))-iif(equal(x-17,0),1,0))/2)
+x^2*(-(iif(x+10 > 0,1,iif(x+10 < 0,-1,0))-iif(equal(x+10,0),1,0)-iif(x-10 > 0,1,iif(x-10 < 0,-1,0))-iif(equal(x-10,0),1,0))/2
      -(iif(x-12 > 0,1,iif(x-12 < 0,-1,0))-iif(equal(x-12,0),1,0)-iif(x-17 > 0,1,iif(x-17 < 0,-1,0))-iif(equal(x-17,0),1,0))/2+1)
(%i7) pulliniif(%); /* this function is cool, you might want to duplicate it for ?if then else?, maybe rename it too */
(out7) iif(x+10 > 0,iif(x-10 > 0,iif(x-12 > 0,iif(x-17 > 0,x^2,iif(x-17 < 0,x,x^2)),iif(x-12 < 0,x^2,x^2)),iif(x-10 < 0,x,x^2)),iif(x+10 < 0,x^2,x^2))
(%i8) simpiifs(%);
(out8) iif(x+10 > 0,iif(x-10 > 0,iif(x-12 > 0,iif(x-17 > 0,x^2,iif(x-17 < 0,x,x^2)),x^2),iif(x-10 < 0,x,x^2)),x^2)
(%i9) iif2ifthen(%);
(out9) if x+10 > 0
            then (if x-10 > 0 then (if x-12 > 0 then (if x-17 > 0 then x^2 else (if x-17 < 0 then x else x^2)) else x^2) else (if x-10 < 0 then x else x^2))
            else x^2
(%i11)

I don?t know any easy way to get back to the original relatively simple form.  I think that would be very useful and worth being able to do. Is there a way? If so, how?  I am asking the mailing list for some help, I might be able to write the code if I knew how to do this kind of problem.  Notice the final result does not use ?and? or ?or?, which is why it is hard to understand, I think.

Rich


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