RE: Re: "misbehaving" Union function
- To: mathgroup at smc.vnet.net
- Subject: [mg24128] RE: [mg24096] Re: [mg24075] "misbehaving" Union function
- From: Felix Farkas <farkas at ica.epfl.ch>
- Date: Wed, 28 Jun 2000 02:11:47 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Dear Wolf, Thank you very much for your answer. These hints help me a lot. For a beginner is pretty difficult to take advantage of all the subtleties of Mathematica if moreover he is not familiar with functional programming. best regards, Felix Farkas > [Wolf Hartmut] > > Dear Felix, > > Andrzej's explanation is correct, I just want to show how you would have > avoided the problems in first place. Essentially this is because after > > bytes += rate*duration; bytes - rate*duration > > you don't get the old value for bytes, since the numerics isn't accurate. > You just have to avoid this operation of "looking back". For a short fix, > you can accumulate bytes only where it is needed, so > > Genpoints2[events_] := > Module[{ev = events, rate, duration, time, burst, bytes = 0, points = {}}, > > rate := ee[[3]]; > duration := ee[[2]]; > time := ee[[1]]; > burst := ee[[4]]; > While[Length[ev] != 0, ee = First[ev]; > ev = Rest[ev]; > points = > Union[points, > If[rate == -1,(*then*) {{time, bytes}, {time, bytes += burst}}, > (*else*) > Print[{{time, bytes}, {time + duration, bytes + > rate*duration}}]; > {{time, bytes}, {time + duration, bytes += rate*duration}}]]]; > points] > > will not disturb you. However this programm makes very inefficient use of > Union, furthermore you make multiple access to the components of the event, > so I might propose you an alternative > > Genpoints3[events_] := > Union @@ Block[{time, duration, rate, burst, scrap1, scrap2, bytes = 0}, > Function[ee, {time, duration, rate, burst, scrap1, scrap2} = ee; > If[rate == -1,(*then*) {{time, bytes}, {time, bytes += burst}}, > (*else*) > Print[{{time, bytes}, {time + duration, bytes + > rate*duration}}]; > {{time, bytes}, {time + duration, bytes += rate*duration}}]] /@ > events] > > Kind regards, Hartmut > >