Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Re: Re:Mapping down two lists

  • To: mathgroup at smc.vnet.net
  • Subject: [mg25534] Re: [mg25515] Re:Mapping down two lists
  • From: "Carl K. Woll" <carlw at u.washington.edu>
  • Date: Sat, 7 Oct 2000 03:35:39 -0400 (EDT)
  • References: <200010060350.XAA24897@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

John,

A faster approach then the ones you have listed is to use Dot, as in

#.#&@(lst1[[All,1]]-lst2)

At least, in my tests this was almost 5 times faster than your In[5]
version.

Carl


----- Original Message -----
From: "John Satherley" <js1 at liverpool.ac.uk>
To: mathgroup at smc.vnet.net
Subject: [mg25534] [mg25515] Re:Mapping down two lists


> 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[1]=lst1 = Table[{Random[], Random[]}, {1000000}];
> In[2]=lst2 = Table[Random[], {1000000}];
>
> In[3]=compiledfunction =  Compile[{{x, _Real, 2}, {y,  _Real, 1}},  Plus
@@
> (First[Transpose[x]] - y)^2];
>
> In[4]= Plus  @@ ((#[[1]] - #[[3]])^2  &  /@
Transpose[Join[Transpose[lst1],
> {lst2}]]) // Timing
> In[5]= Plus @@ (First[Transpose[lst1]] - lst2)^2 // Timing
> In[6]= Plus @@ (Transpose[lst1][[1]] - lst2)^2 // Timing
> In[7]= Plus @@ (Take[Transpose[lst1], 1][[1]] - lst2)^2 //Timing
> In[8]= Plus @@ (#^2 & /@ (Transpose[lst1][[1])] - lst2) // Timing
> In[9]= Plus @@ (#^2 & /@ (#[[1]] & /@ lst1 - lst2)) // Timing
> In[10]= compiledfunction[lst1, lst2] // Timing
>
> Out[4]= {10.71 Second, 166795.}
> Out[5]= {4.4 Second, 166795.}
> Out[6]= {4.34 Second, 166795.}
> Out[7]= {4.39 Second, 166795.}
> Out[8]= {4.89 Second, 166795.}
> Out[9]= {7.74 Second, 166795.}
> Out[10]= {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,([#[[1]]&/@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: Syntax
  • Next by Date: Re: Hold, HoldForm, ReleaseHold when Plotting multiple functions
  • Previous by thread: Re:Mapping down two lists
  • Next by thread: Re: Re:Mapping down two lists