From: Rainer Schöpf <rainer.schoepf@gm...>  20120924 11:18:31

On Mon, 24 Sep 2012, Raffaele Vitolo wrote: > Dear All, > > I am summing large numbers of monomials (~12000) indexed by an operator > `c' as coefficient. The resulting expression will be of the type > c(1)*mon1 + c(2)*mon2 + ... > > Taking these sums seems to be extremely timeconsuming: on my server it > took 8.5 hours, and this is the slowest part of my computations. I used > the following syntax: > > % Loads the file with the monomials; it is a list > % `linoddt' of about 12000 monomials > in "kz3d_linoddt.red"$ > % Counter for the coefficients: > ctel := 0 $ > % Operator for the coefficients: > operator c $ > % Sum of the monomials: > ct:=(for each el in linoddt sum (c(ctel:=ctel+1)*el))$ > > If you are interested you might find the monomials here (~450k): > http://poincare.unisalento.it/vitolo/tempo/kz3d_linoddt.red > > My question is: it is possible to speed up this sum? Or is this > operation so intrinsically complex that it is not possible to do better? Hello, the main reason why this computation takes such a long time is that REDUCE tries to simplify the intermediate sum for every step of the foreach loop. It is much faster to collect the terms of the sum individually and compute the sum afterwards: ctlist := foreach el in linoddt collect (c(ctel:=ctel+1)*el) $ ct := (part(ctlist,0) := plus)$ It is even faster to not use an operator, but to construct the coefficients as variables, using mkid: ctlist := foreach el in linoddt collect (mkid(c,ctel:=ctel+1)*el) $ ct := (part(ctlist,0) := plus)$ Rainer Schöpf 