Re: A programming puzzle.
- To: mathgroup at smc.vnet.net
- Subject: [mg60853] Re: [mg60810] A programming puzzle.
- From: <bsyehuda at gmail.com>
- Date: Fri, 30 Sep 2005 03:57:31 -0400 (EDT)
- References: <200509290941.FAA01111@smc.vnet.net>
- Reply-to: bsyehuda at gmail.com
- Sender: owner-wri-mathgroup at wolfram.com
I do not know what you call an "Elegant solution", since if you already have a way of doing this, why would you need another one? Anyway s[n_Integer]:=Sum[a[i]b[i],{i,n}] generates the input sum and makeMyList[x_] := (Plus @@@ Rest[FoldList[Flatten[{List[#1, #2]}] &, {}, \ #[[1]]]].Subtract @@@ Partition[#[[2]], 2, 1, { 1, 1}, 0]) &@(x /. Times -> List /. Plus -> List) will work on it to give you the desired result that is makeMyList[s[5]] will produce a[1] (b[1] - b[2]) + (a[1] + a[2]) (b[2] - b[3]) + (a[1] + a[2] + a[3]) (b[3] - b[4]) + (a[1] + a[2] + a[3] + a[4]) (b[4] - b[5]) + (a[1] + a[2] + a[3] + a[4] + a[5]) b[5] and check by Simplify[%] and get back a[1] b[1] + a[2] b[2] + a[3] b[3] + a[4] b[4] + a[5] b[5] I do not find it elegant, do you? yehuda On 9/29/05, jackgoldberg at comcast.net <jackgoldberg at comcast.net> wrote: > > Hello everyone, > > I have a simple problem for which I would like an "elegant" solution. The > problem is to convert the series > > a1*b1 + a2*b2 + a3*b3 + ... + an*bn > > into the equivalent series > > a1(b1 - b2) + (a1+a2)(b2 - b3) + ... +(a1 + a2 + ... + an-1)(bn-1 - bn) + > (a1 + a2 + ... + an)bn > > This process, I believe, is called summation-by-parts. This problem is not > hard to do; one simply separates the ai's from the bi's, constructs the > "partial" sums for the ai series and the differences for the bi series. Then > a dot product gets the answer. I am interested in a more elegant solution, > if one exists. > Taking care of all special cases will probably make any solution rather > inelegant, so I am taking the liberty granted to all posers of only allowing > series which have at least two terms AND the bi's are not constants and bi bj is prohibited unless i = j. (This is, in fact, the situation I am > concerned with.) > > I think some of you will have fun connocting ingenious solutions ... Let > me know if you find any. Incidentally, timing is not crucial in my > applications of "summation-by-parts" nor is storage of intermediate > computations a problem. Thanks! > > Jack Goldberg > >
- References:
- A programming puzzle.
- From: jackgoldberg@comcast.net
- A programming puzzle.