Quoting helps as Barton said but the fastest way is to use if instead of %if. I am trying to make piecewise evaluate
faster. Consider this.
(%i29) %if(i>0, 'expand((x-a)^100), 'expand((x-b)^100));
Evaluation took 0.0000 seconds (0.0000 elapsed)
100 100
(out29) %if(i > 0, expand((x - a) ), expand((x - b) ))
(%i30) for i:-2000 thru 2000 do ev(%,nouns);
Evaluation took 25.8900 seconds (25.8900 elapsed)
(out30) done
(%i31) piecewise([minf, expand((x-b)^100), 0, expand((x-a)^100), inf],i)$
Evaluation took 0.0100 seconds (0.0100 elapsed)
(%i32) for i:-2000 thru 2000 do ev(%,nouns);
Evaluation took 41.0300 seconds (41.0300 elapsed)
(out32) done
(%i33) if i>0 then expand((x-a)^100) else expand((x-b)^100);
Evaluation took 0.0000 seconds (0.0000 elapsed)
100 100
(out33) if i > 0 then expand((x - a) ) else expand((x - b) )
(%i34) for i:-2000 thru 2000 do ev(%,nouns);
Evaluation took 12.8700 seconds (12.8700 elapsed)
(out34) done
The last way is the fastest. What I think I need to do is dynamically generate an if then else statement as the output
from piecewise(). I am not at all clear on how to do that.
Rich
From: Stavros Macrakis
Sent: Monday, December 28, 2009 10:15 AM
To: Richard Hennessy
Cc: Maxima List
Subject: Re: [Maxima] How to prevent evaluation.
What are you trying to do and why are you using %if rather than if?
These days, regular 'if' returns an expression if the condition cannot be evaluated, e.g.
(%i2) if a>0 then 1 else 0;
(%o2) if a > 0 then 1 else 0
-s
On Sun, Dec 27, 2009 at 5:16 PM, Richard Hennessy <rich.hennessy at verizon.net> wrote:
I tried this to see the timing information.
load(pw)$
(%i3) showtime:true;
Evaluation took 0.0000 seconds (0.0000 elapsed)
(out3)
(%i4) for i : 1 thru 2000 do %if(i>0, expand((x-y)^100), expand((x-y)^100));
Evaluation took 13.0200 seconds (13.0200 elapsed)
(out4) done...
It seems like in the %if function both the then part and the else part are always expanded even when only one needs
to be evaluated. Is there a way to stop evaluation of both parts? This would make possible major speed improvements
dealing with large expressions.