       Re:Mapping down two lists

• To: mathgroup at smc.vnet.net
• Subject: [mg25515] Re:Mapping down two lists
• From: "John Satherley" <js1 at liverpool.ac.uk>
• Date: Thu, 5 Oct 2000 23:50:35 -0400 (EDT)
• Organization: The University of Liverpool
• Sender: owner-wri-mathgroup at wolfram.com

```Many thanks to all those who sent in suggestions as to how to speed up
calculations involving lists as per my original request below.

I've collected together the suggestions, added some of my own and done some
timing tests:

In=lst1 = Table[{Random[], Random[]}, {1000000}];
In=lst2 = Table[Random[], {1000000}];

In=compiledfunction =  Compile[{{x, _Real, 2}, {y,  _Real, 1}},  Plus @@
(First[Transpose[x]] - y)^2];

In= Plus  @@ ((#[] - #[])^2  &  /@ Transpose[Join[Transpose[lst1],
{lst2}]]) // Timing
In= Plus @@ (First[Transpose[lst1]] - lst2)^2 // Timing
In= Plus @@ (Transpose[lst1][] - lst2)^2 // Timing
In= Plus @@ (Take[Transpose[lst1], 1][] - lst2)^2 //Timing
In= Plus @@ (#^2 & /@ (Transpose[lst1][)] - lst2) // Timing
In= Plus @@ (#^2 & /@ (#[] & /@ lst1 - lst2)) // Timing
In= compiledfunction[lst1, lst2] // Timing

Out= {10.71 Second, 166795.}
Out= {4.4 Second, 166795.}
Out= {4.34 Second, 166795.}
Out= {4.39 Second, 166795.}
Out= {4.89 Second, 166795.}
Out= {7.74 Second, 166795.}
Out= {2.37 Second, 166795.}

As you can see there is quite a range in the calculation speed. Compiling
the functions gives a speed enhancement by almost a factor of 2 compared to
the other fastest time. I noted that, if compiled then, all the functions
take about the same time to complete hence I only show one of them here. In
the noncompiled versions its likely that the speed increase arises from
carrying out a minimum number of calculations. The first in the list clearly
does the most and takes the longest to complete.

John Satherley

Hi MathGroup
I'm trying to sharpen up my functional programming skills and have come
across a problem which is probably very trivial but I can't find a
satisfactory solution.

I have two lists of equal length, one 2 dimensional and the other 1
dimensional:
lst1={{1,2},{2,3},{1,3},......}
lst2={1,2,2,3,4,5.....}

What I want to do is find the difference between lst1[[i,1]] and lst2[[i]]
square it and then sum up all the terms over the length of the list.

It is easy to do this in terms of Tables but I'm trying to find a fast
solution for long lists that uses map and other functional programming
tools.

The best I have come up with is:
Apply[Plus,([#[]&/@lst1-lst2)^2]
but I'm wondering if it is possible to map directly onto the two lists
without first having to extract the elements of the first list? Map seems to
only work down a single list.

I would be most grateful of any hints as to how this might be performed in
an efficient manner.

Thanks for help
Dr. John Satherley
Dept of Chem
University of Liverpool
Liverpool L69 7ZD
UK
js1 at liv.ac.uk

```

• Prev by Date: Re: Hold, HoldForm, ReleaseHold when Plotting multiple functions
• Next by Date: RE: Hold, HoldForm, ReleaseHold when Plotting multiple functions
• Previous by thread: Re: Exporting multiple page graphics
• Next by thread: Re: Re:Mapping down two lists