Re: alternating sums

• To: mathgroup at christensen.Cybernetics.NET
• Subject: [mg336] Re: alternating sums
• From: perkins at colorado.edu (Tyler Perkins)
• Date: Fri, 16 Dec 1994 12:29:49 -0700

```>  Here's an elementary question that has me partially
>  stumped.  I often have occasion to convert  mylist =
>  {a1,a2,a3,...} into an alternating sum, a1-a2+a3-a4+...
>  . I find it aesthetically unsatisfactory to generate a
>  list of alternating plus and minus ones, multiply mylist
>  with the alternating list and then Apply Plus to the
>  product.  It works of course, but I have a feeling there
>  is a much better way to do this.  (This is clearly not
>  of earthshaking importance!!.  I'm just curious.) Jack
>  Goldberg Univ of Mich Mathematics

Richard Mercer's solution,
> In[49]:=
> mylist = {a1,a2,a3,a4,a5,a6};
> Plus @@ Apply[Subtract,Partition[mylist,2],{1}]
> Out[50]=
> a1 - a2 + a3 - a4 + a5 - a6

is certainly the most elegant so far, but only for sequences of even
length!  The nth term will be omitted from the sum if the list has odd
length.

Here's my entry.  I enjoy solutions which involve pattern-matching.  Their
elementary nature seems elegant to me, and they illustrate the pattern-
matching foundation of Mathematica.

In[1]:=
aList = {1,2,3,4,5,6,7,8,9};
0 @@ aList //. sum_[odd_, even_:0, rest___] -> (sum + odd - even)[rest]
]
Out[1]=
5

```

• Prev by Date: Re: Web pages
• Next by Date: ArcLengthPartition/plane curve segments
• Previous by thread: Re: alternating sums
• Next by thread: Alternating Sums