Re: Sum of Products
- To: mathgroup at smc.vnet.net
- Subject: [mg126678] Re: Sum of Products
- From: Dana DeLouis <dana01 at me.com>
- Date: Wed, 30 May 2012 04:12:49 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
> Is there any other, shorter, built-in Mathematica object . .
> Total[Times @@@ Subsets[{a, b, c}, {2}]]
Hi. This wasn't shorter, but appears faster with a large list.
It will be interesting to see other various ideas:
v=Range[1000];
{tim1,ans1} = Total[Times@@@Subsets[v,{2}]]//Timing
{0.407345,125083208250}
{tim2,ans2} = Sum[v[[j]] *Total@ v[[j+1;;-1]],{j,Length[v]-1}]//Timing
{0.007906,125083208250}
Not likely, but if your list of numbers were actually 1 to n:
(1/24)*(3*n + 2)*(n^3 - n) /. n -> 1000
125083208250
That came from the suggested closed form here:
Table[Total[Times@@@Subsets[Range[j],{2}]],{j,15}] //ToString //WolframAlpha
<< output >>
= = = = = = = = = =
HTH :>)
Dana DeLouis
Mac & Math 8
= = = = = = = = = =
On May 29, 5:51 am, "Harvey P. Dale" <h... at nyu.edu> wrote:
> If I have a list and want to sum the products of each possible
> grouping of two elements in the list, this program will do that:
>
> Total[Times @@@ Subsets[{a, b, c}, {2}]]
>
> Is there any other, shorter, built-in Mathematica object that will
> produce the same result?
>
> Thanks.
>
> Harvey