MathGroup Archive 2003

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: nth differences

  • To: mathgroup at smc.vnet.net
  • Subject: [mg39867] Re: [mg39852] nth differences
  • From: Rob Pratt <rpratt at email.unc.edu>
  • Date: Sun, 9 Mar 2003 05:28:49 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

On Sat, 8 Mar 2003, Zachary Turner wrote:

> Say I have a list of k integers and I want to produce a list containing the
> first differences?  For example, given {1, 2, 5, 7, 8, 9} the first
> differences are {1, 3, 2, 1, 1}, and the second differences are {2, -1, -1,
> 0}, the third are {-3, 0, 1}, etc

In[1]:= lis = {1, 2, 5, 7, 8, 9}

Out[1]= {1, 2, 5, 7, 8, 9}

In[2]:= diff[x_List] := Rest[x - RotateRight[x]]

In[3]:= diff[lis]

Out[3]= {1, 3, 2, 1, 1}

In[4]:= Nest[diff, lis, 2]

Out[4]= {2, -1, -1, 0}

In[5]:= NestList[diff, lis, 3]

Out[5]= {{1, 2, 5, 7, 8, 9}, {1, 3, 2, 1, 1}, {2, -1, -1, 0}, {-3, 0, 1}}

Rob Pratt
Department of Operations Research
The University of North Carolina at Chapel Hill

rpratt at email.unc.edu

http://www.unc.edu/~rpratt/




  • Prev by Date: Help with Format Background Color Cell Gray Box
  • Next by Date: Re: nth differences
  • Previous by thread: Re: nth differences
  • Next by thread: Re: nth differences