Re: Re: Re: Minimization
- To: mathgroup at smc.vnet.net
- Subject: [mg42312] Re: [mg42293] Re: [mg42275] Re: Minimization
- From: Bobby Treat <drmajorbob-MathGroup3528 at mailblocks.com>
- Date: Sat, 28 Jun 2003 03:21:58 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Selwyn, Your first version doesn't define 'target'. Bobby -----Original Message----- From: Selwyn Hollis <selwynh at earthlink.net> To: mathgroup at smc.vnet.net Subject: [mg42312] [mg42293] Re: [mg42275] Re: Minimization That's beautiful! This is nice case study. Since Carl Woll and I were using essentially the same approach, let's see what happens as my clumsy code morphs into Carl's elegant code. pts = Table[{Random[],Random[],Random[]}, {10000}]; p0= {0,1,0}; Here's my first version: Timing[ With[{dists = (#.#)^(1/2)& /@ ((#-target &) /@ pts)}, pts[[Flatten[Position[dists,#]& /@ Sort[dists][[Range[5]]] ] ]] ] ] {0.28 Second, Null} Next a cleaner version: Timing[ With[{sqdists = (# - p0).(# - p0) & /@ pts}, pts[[Flatten[Position[sqdists,#]& /@ Sort[sqdists][[Range[5]]] ] ]] ]] {0.17 Second, Null} Now using Carl's clever Plus@@((Transpose[pts]-p0)^2): Timing[ With[{sqdists = Plus @@ ((Transpose[pts] - p0)^2)}, pts[[Flatten[Position[sqdists,#]& /@ Sort[sqdists][[Range[5]]] ] ]] ]] {0.09 Second, Null} Finally, if I use Ordering, the two become equivalent: Timing[ With[{sqdists = Plus @@ ((Transpose[pts] - p0)^2)}, pts[[Ordering[sqdists, 5]]] ] ] {0.03 Second, Null} ----- Selwyn Hollis http://www.math.armstrong.edu/faculty/hollis On Thursday, June 26, 2003, at 05:36 AM, Carl K. Woll wrote: > John, > > Here is a version which is a bit faster on my machine than Hartmut's, > where > I use pts instead of l1. > > pts[[Ordering[Plus@@((Transpose[pts]-p0)^2),5]]] > > The improvement in speed comes from using > > Plus@@((Transpose[pts]-p0)^2) > > instead of Hartmut's > > With[{r=#-p0},r.r]&/@pts > > Further improvements in speed may be achievable if you use Compile. > > Carl Woll > Physics Dept > U of Washington > > <Moranresearch at aol.com> wrote in message > news:bdbeu1$2dq$1 at smc.vnet.net... >> >> I have a list of points l1= (xi,yi, zi) and a target point (x0,y0,z0) >> how >> would I efficiently find the 5 points in l1 closest to, ie with the > smallest >> Euclidian disance to, the target point? Thank you. >> John >> > > >