Re: Re: Release questi
- To: mathgroup at smc.vnet.net
- Subject: [mg9488] Re: [mg9410] Re: [mg9366] Release questi
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Sat, 8 Nov 1997 23:04:22 -0500
- Sender: owner-wri-mathgroup at wolfram.com
Ted's resetting of the attribute HoldAll, of Plot, to HoldRest is neat. His example, reproduced at (A) below, is a dramatic (and graphic) demonstration of the value of the rule "don't repeat calculations" . I take it further in (B). (A) Ted's example fun=1/2+Sum[(-1)^n 2 Cos[(2 n+1)Pi t]/((2n+1)Pi),{n,0,20}]); (This expands Sum[] annd stores the resulting expression as the value of fun.) (1) Plot[Evaluate[fun], {t, -1, 3}]//Timing//First 1.42157 Second (2) Plot[fun, {t, -1, 3}]//Timing//First 22.6043 Second And this is inspite of the fact that the only difference is that in (1) the value of fun is looked up and substituted once instead of, as in (2), every time that a plot point is calculated (B) For an even starker warning compare (1) funInfinite= 1/2+Sum[(-1)^n 2 Cos[(2 n+1)Pi t]/((2n+1)Pi),{n,0,Infinity}]; (which stores the evaluated right side 1/2 + (Sqrt[E^(2 I Pi t)] ArcTan[Sqrt[E^(-2 I Pi t)]] + ArcTan[Sqrt[E^(2 I Pi t)]]/Sqrt[E^(-2 I Pi t)])/ ((Sqrt[E^(-2 I Pi t)] Sqrt[E^(2 I Pi t)] Pi)/ E^(-(-(-I Pi t)))) as the value of funInfinite ) and Plot[Evaluate[Re[funInfinite]],{t,-1,3}]//Timing//First 0.906026 Second with (2) funInfiniteDelayed := 1/2+Sum[(-1)^n 2 Cos[(2 n+1)Pi t]/((2n+1)Pi),{n,0,Infinity}]; (whic stores the right side unevaluated as the value of funInfiniteDelayed.) and Plot[Re[funInfiniteDelayed],{t,-1,3}]//Timing//First 970.795 Second ( where the calculation of every one of the points used involves the evaluation of an expresson like (t=.01; Re[ 1/2+Sum[(-1)^n 2 Cos[(2 n+1)Pi t]/((2n+1)Pi),{n,0,Infinity}] ]) (which took 1.32818 Second) ). The use of Re seems necessary here (although the sum is real for real values). Other variants, involving N and Simplify avoid the need for Re. It would be reasonable to use Plot[Evaluate[Re[1/2+Sum[(-1)^n 2 Cos[(2 n+1)Pi t]/((2n+1)Pi),{n,0,Infinity}]],{t,-1,3}] for a single plot, but using Plot[Evaluate[Re[funInfinite]],{t,-1,3}] would allow repeated uses of the computed closed formula (for example to plot over different ranges). Allan Hayes hay at haystack.demon.co.uk http://www.haystack.demon.co.uk/training.html voice:+44 (0)116 2714198 fax: +44 (0)116 2718642 Leicester, UK