Re: Map and functional constructs to replace iterative

• To: mathgroup at smc.vnet.net
• Subject: [mg96830] Re: [mg96783] Map and functional constructs to replace iterative
• Date: Thu, 26 Feb 2009 07:51:01 -0500 (EST)
• References: <200902250900.EAA15513@smc.vnet.net>

```Some solutions

In[3]:= vals = {3, 2.5, 7, 4.2, 8, 5.1, 2., 3};

using the built-in Differences:

In[4]:= Differences[vals]/100
Out[4]= {-0.005, 0.045, -0.027999999999999997, 0.038, -0.029000000000000005,

-0.030999999999999996, 0.01}

with list operations

In[5]:= ((1./100)*(Rest[#1] - Most[#1]) & )[vals]
Out[5]= {-0.005, 0.045, -0.027999999999999997, 0.038, -0.029000000000000005,

-0.030999999999999996, 0.01}

In[6]:= (MapThread[(1./100)*(#1 - #2) & , {Rest[#1], Most[#1]}] & )[vals]
Out[6]= {-0.005, 0.045, -0.027999999999999997, 0.038, -0.029000000000000005,

-0.030999999999999996, 0.01}

Transpose and Map

In[7]:= (({1, -1} . #1/100. & ) /@ Transpose[{Rest[#1], Most[#1]}] & )[vals]
Out[7]= {-0.005, 0.045, -0.027999999999999997, 0.038, -0.029000000000000005,

-0.030999999999999996, 0.01}

2009/2/25 Andreas <aagas at ix.netcom.com>

> 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
>
> But, I'm stuck.  I need to figure out how to use Map (or some related
> approach) 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
> generally stated, when I need to refer to previous values in a series or
> even previous 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 some
> 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.
>
>

```

• Prev by Date: Re: Series of Percentage Changes
• Next by Date: Experimental Mathematica debugger
• Previous by thread: Re: Map and functional constructs to replace iterative statements
• Next by thread: Re: Map and functional constructs to replace iterative