Skip to main content

bugs - Cannot get nearest neighbors from Nearest


Bug introduced in 10 and fixed in 10.3


There still persists the failure to return 30.72 (another bug according to Daniel Lichtblau but from the developer's point of view it may be a feature).




Nearest[data,x,{n,r}] is supposed to give the n or fewer nearest neighbors to x that are within radius r of x. Example:


Nearest[{1, 2, 3, 4, 5, 6}, 3.6, {10, 1.5}] (* gives {4, 3, 5} *)
Nearest[{1, 2, 3, 4, 5, 6}, 3, {10, 2}] (* gives {3, 2, 4, 1, 5}, so it's inclusive *)

But this is not working for my data:


v = {10.38,17.77,21.25,20.38,14.34,15.7,19.98,20.83,21.82,24.04,23.24,17.89,24.8,23.95,22.61,27.54,20.13,20.68,22.15,14.36,15.71,12.44,14.26,23.04,21.38,16.4,21.53,20.25,25.27,15.05,25.11,18.7,23.98,26.47,17.88,21.59,21.72,18.42,25.2,20.82,21.58,21.35,24.81,20.28,21.81,17.6,16.84,18.66,14.63,22.3,21.6,16.34,18.24,18.7,22.02,18.75,18.57,21.59,19.31,11.79,14.88,20.98,22.15,13.86,23.84,23.94,21.01,19.04,17.33,16.49,21.31,14.64,24.52,15.79,16.52,19.65,10.94,16.15,23.97,18,20.97,15.86,24.91,26.29,15.65,18.52,21.46,24.59,21.8,15.24,24.02,22.76,14.76,18.3,19.83,23.03,17.84,19.94,12.84,19.77,24.98,13.43,20.52,19.4,19.29,15.56,18.33,18.54,19.67,21.26,16.99,20.76,19.65,20.19,15.83,21.53,15.76,16.67,22.91,20.01,10.82,17.12,20.2,10.89,16.39,17.21,24.69,18.91,16.39,25.12,13.29,19.48,21.54,13.93,21.91,22.47,16.67,15.39,17.57,13.39,11.97,18.05,17.31,15.92,14.97,14.65,16.58,18.77,15.18,17.91,20.78,20.7,15.34,13.08,15.34,17.94,20.74,19.46,12.74,12.96,20.18,15.94,18.15,22.22,22.04,19.76,9.71,18.8,24.68,16.95,12.39,19.63,11.89,14.71,15.15,14.45,18.06,20.11,22.22,13.06,21.87,26.57,20.31,14.92,22.41,15.11,18.58,17.19,17.39,15.9,23.12,21.41,18.22,26.86,23.21,16.33,22.29,21.84,22.49,20.22,19.56,19.32,26.67,23.75,18.6,16.68,17.27,20.26,22.54,12.91,22.14,18.94,18.47,25.56,23.81,16.93,18.35,17.48,21.56,32.47,13.16,13.9,17.53,20.25,17.02,13.47,15.46,15.51,23.97,22.33,19.08,27.08,33.81,27.81,15.91,21.25,26.97,21.46,27.85,39.28,15.6,15.04,18.19,23.77,23.5,19.86,17.43,14.11,25.22,14.93,23.56,18.45,19.82,17.08,19.33,17.05,28.77,17.27,23.2,33.56,27.06,23.06,22.13,19.38,22.07,31.12,18.95,21.84,16.21,20.39,16.82,13.04,20.99,15.67,24.48,17.36,14.16,19.98,17.84,15.18,26.6,14.02,18.18,18.77,15.7,18.4,20.76,13.12,19.96,18.89,19.73,19.1,16.02,17.46,13.78,13.27,12.35,18.14,18.17,23.09,18.9,19.89,23.86,18.61,18.16,24.49,15.82,14.4,12.71,13.84,19.11,15.69,13.37,10.72,18.6,16.85,14.08,18.87,18.9,17,16.18,19.66,13.32,21.51,15.21,17.3,12.88,17.93,20.71,21.88,15.51,19.35,19.86,14.78,19.02,21,14.23,21.43,17.53,24.27,16.54,16.84,14.96,21.68,15.45,14.71,18.9,14.74,16.03,14.96,17.07,19.22,17.46,25.74,14.07,19.07,18.59,16.21,15.49,18.32,18.07,21.57,18.84,18.29,16.95,21.78,26.83,18.02,17.25,21.9,23.29,13.21,15.1,17.35,16.07,16.07,20.22,28.21,15.15,18.83,12.96,14.93,22.72,17.48,13.72,23.29,14.09,16.16,15.5,23.21,12.22,16.84,19.97,22.28,17.72,17.18,18.89,17.46,14.83,17.26,21.02,10.91,18.29,16.17,14.95,18.59,14.86,21.37,20.66,17.92,17.57,16.83,21.68,22.11,29.81,21.17,21.7,21.08,12.17,21.41,19.04,13.98,16.02,19.13,19.12,21.28,14.98,21.98,16.62,17.67,22.53,17.68,19.54,21.97,16.94,19.62,19.54,15.98,19.6,15.66,17.2,25.42,15.79,18.32,16.85,24.89,28.03,17.66,19.34,20.52,21.54,25,28.23,13.98,17.15,30.72,29.29,25.25,25.13,28.2,27.15,26.27,26.99,18.36,18.22,20.13,20.74,18.1,23.33,18.18,18.49,28.14,14.93,29.97,15.62,15.73,20.53,16.62,14.59,19.51,18.03,19.24,14.06,17.64,11.28,16.41,16.85,18.82,16.17,20.2,22.44,13.23,20.56,12.83,20.54,20.21,18.17,17.31,17.52,21.24,16.74,24.49,16.32,19.83,12.87,13.14,20.04,17.12,15.7,23.95,14.69,14.7,20.52,13.66,19.07,18.61,20.58,20.26,18.22,16.7,13.9,21.6,19.83,18.68,15.68,13.1,18.75,12.27,13.17,13.44,17.56,20.02,16.33,20.67,17.62,20.86,22.55,24.44,25.49,25.44,14.44,24.99,25.42,28.06,20.7,23.23,16.35,16.58,19.34,24.21,21.48,22.44,29.43,21.94,28.92,27.61,19.59,27.88,22.68,23.93,27.15,29.37,30.62,25.09,22.39,28.25,28.08,29.33,24.54}


Nearest[v, 39.28, 6]
(* gives {39.28, 33.81, 33.56, 32.47, 31.12, 30.72}, which is fine *)

Nearest[v, 39.28, {6, 8.56}]
(* should have given the same answer because 39.28 - 30.72 = 8.56,
but gives {39.28} instead *)

Nearest[v, 39.28, {6, 9}]
(* same as above, which is even stranger *)


I cannot explain this behavior. Any ideas?


Mathematica version: 10.2.0.0




Comments

Popular posts from this blog

calculus and analysis - Integral of the Sinc product

Let us consider the following integral $$ B_n = \int_0^\infty \prod_{k=1,3,5,\dots}^n\frac{\sin (x/k)}{x/k}dx $$ By definition, Sinc[x] == Sin[x]/x , therefore B[n_?OddQ] := Integrate[Product[Sinc[x/k], {k, 1, n, 2}], {x, 0, Infinity}] B /@ Range[1, 13, 2] {Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2, Pi/2} OK, everything is fine. But... B[15] 467807924713440738696537864469 Pi/935615849440640907310521750000 What's going on? Answer As Eckhard wrote in comments B[n] is the n-th Borwein integral . (The letter B was not accidental :) ) This funny properties of Borwein integrals is related to the Fourier transform of Sinc function FourierTransform[Sinc[x], x, k] 1/2 Sqrt[Pi/2] (Sign[1 - k] + Sign[1 + k]) Plot[%, {k, -2, 2}, Filling -> 0] which is the box function. The result is $\pi/2$ while the sum $$ 1/3+1/5+\dots+1/n $$ B_n = \frac{\pi}{2} - \pi \bigg(\sum_{k=3,5,\ldots}^n\frac{1}{k} -1\biggr)^\frac{n-1}{2}\prod_{k=3,5,\dots}^n\frac{k}{k-1}. $$ For $n=15$ it is equal to $$ \frac{46780...

list manipulation - Solving word search puzzles

I am trying to create a code that can identify the following terms in a grid of letters: MATHEMATICA, STACK, EXCHANGE and USERS. list1={"M","S","T","A","S","I","S","X","X","T","R","X"}; list2={"A ","T ","H ","X ","R ","X ","G ","R ","S ","H ","X ","A"}; list3={"M","A","T","H","E","M","A","T","I","C","A","I"}; list4={"A","X","S","G","S","X","A","I","R","T","X","T"}; list5={"T","I","T","G","U","C","C","I","R","N",...

How do I constrain movement for lists of Locators using 2nd argument of Dynamic?

I need to display edge labels of a graph in a way that allows the edge labels to be moved. Locator seems the simplest and most obvious function to use. My application requires an interface that generates new graphs with different numbers of vertices and edges, so I can't treat the locators individually. With[ {coords = {{1.08, 0.94}, {1.08, 0.036}, {0., 0.97}, {0., 0.}, {1.94, 0.49}}, edges = {{1, 2}, {1, 3}, {2, 5}, {3, 4}, {4, 2}, {5, 1}}}, DynamicModule[ {edgePosns = Table[0.5, {Length@edges}]}, (betweenPnt[a_, b_, l_] := (1 - l) a + l b; DynamicModule[ {edgeCentres = MapThread[ With[{av = coords[[#1[[1]]]], bv = coords[[#1[[2]]]]}, Dynamic[betweenPnt[av, bv, #2]]] &, {edges, edgePosns}]}, Graphics[ GraphicsComplex[ coords, {{Line[edges]}, {Darker@Red, PointSize[0.02], Map[Point, Range[5]]}, Map[Locator, edgeCentres]}], ImageSize -> 400]])]] The locators can be moved, as expected....