MathGroup Archive 2009

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

Search the Archive

Re: Map and functional constructs to replace iterative statements

  • To: mathgroup at smc.vnet.net
  • Subject: [mg96933] Re: Map and functional constructs to replace iterative statements
  • From: mike.honeychurch at gmail.com
  • Date: Fri, 27 Feb 2009 06:15:52 -0500 (EST)
  • References: <go31ba$f47$1@smc.vnet.net>

On Feb 25, 3:00 am, Andreas <aa... at ix.netcom.com> wrote:
> I have begun to replace Table, Do, While, or For, but in my thinking with=
 Map, Inner, Outer and other functional programming constructs as I learn m=
ore about Mathematica.
>
> But, I'm stuck.  I need to figure out how to use Map (or some related a=
pproach) when a function needs both a current value and a previous value in=
 a time series, such as a percentage change function (or perhaps more gener=
ally stated, when I need to refer to previous values in a series or even pr=
evious calculations).  But first a simple case:
>
> percentChange[currentValue_, previousValue_] := 100 * ( currentValue - =
previousValue) / previousValue
>
> I know how to apply this iteratively, but how can I do it with Map or som=
e other functional construct?
>
> Say I have a list: timeSeries
>
> I thought to try something like this:
>
> Map[percentChange[?,?], timeSeries]
>
> but I don't know how what to put in for "?".
>
> Any help much appreciated.
>
> Thx.

Daniel's solution is probably the best in terms of simplicity but this
seems to be slightly faster:

100*Differences[d]/Most[d]

I had initially thought of ListCorrelate which also gives a fast reply
with large data sets:

100*ListCorrelate[{-1, 1}, d]/Most[d];

Trying to get it all to happen within a single function only slowed
things down:

gg[x_, y_] := y/x - 1

100*ListCorrelate[{1, 1}, ls, {1, -1}, 0, Times, gg]

(far too much happening to slow things down)

Mike


  • Prev by Date: Re: how to specify sheet names in xls export?
  • Next by Date: Re: Galois resolvent
  • Previous by thread: Re: Map and functional constructs to replace iterative statements
  • Next by thread: MathLink message handler not set by default