def query(self, x, k=<NUM_LIT:1>, eps=<NUM_LIT:0>, p=<NUM_LIT:2>, distance_upper_bound=np.inf):
x = np.asarray(x)<EOL>if np.shape(x)[-<NUM_LIT:1>] != self.m:<EOL><INDENT>raise ValueError("<STR_LIT>" % (self.m, np.shape(x)))<EOL><DEDENT>if p < <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>retshape = np.shape(x)[:-<NUM_LIT:1>]<EOL>if retshape != ():<EOL><INDENT>if k is None:<EOL><INDENT>dd = np.empty(retshape,dtype=np.object)<EOL>ii = np.empty(retshape,dtype=np.object)<EOL><DEDENT>elif k > <NUM_LIT:1>:<EOL><INDENT>dd = np.empty(retshape+(k,),dtype=np.float)<EOL>dd.fill(np.inf)<EOL>ii = np.empty(retshape+(k,),dtype=np.int)<EOL>ii.fill(self.n)<EOL><DEDENT>elif k == <NUM_LIT:1>:<EOL><INDENT>dd = np.empty(retshape,dtype=np.float)<EOL>dd.fill(np.inf)<EOL>ii = np.empty(retshape,dtype=np.int)<EOL>ii.fill(self.n)<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>for c in np.ndindex(retshape):<EOL><INDENT>hits = self.__query(x[c], k=k, eps=eps, p=p, distance_upper_bound=distance_upper_bound)<EOL>if k is None:<EOL><INDENT>dd[c] = [d for (d,i) in hits]<EOL>ii[c] = [i for (d,i) in hits]<EOL><DEDENT>elif k > <NUM_LIT:1>:<EOL><INDENT>for j in range(len(hits)):<EOL><INDENT>dd[c+(j,)], ii[c+(j,)] = hits[j]<EOL><DEDENT><DEDENT>elif k == <NUM_LIT:1>:<EOL><INDENT>if len(hits) > <NUM_LIT:0>:<EOL><INDENT>dd[c], ii[c] = hits[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>dd[c] = np.inf<EOL>ii[c] = self.n<EOL><DEDENT><DEDENT><DEDENT>return dd, ii<EOL><DEDENT>else:<EOL><INDENT>hits = self.__query(x, k=k, eps=eps, p=p, distance_upper_bound=distance_upper_bound)<EOL>if k is None:<EOL><INDENT>return [d for (d,i) in hits], [i for (d,i) in hits]<EOL><DEDENT>elif k == <NUM_LIT:1>:<EOL><INDENT>if len(hits) > <NUM_LIT:0>:<EOL><INDENT>return hits[<NUM_LIT:0>]<EOL><DEDENT>else:<EOL><INDENT>return np.inf, self.n<EOL><DEDENT><DEDENT>elif k > <NUM_LIT:1>:<EOL><INDENT>dd = np.empty(k,dtype=np.float)<EOL>dd.fill(np.inf)<EOL>ii = np.empty(k,dtype=np.int)<EOL>ii.fill(self.n)<EOL>for j in range(len(hits)):<EOL><INDENT>dd[j], ii[j] = hits[j]<EOL><DEDENT>return dd, ii<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>
Query the kd-tree for nearest neighbors

Parameters
----------
x : array_like, last dimension self.m
    An array of points to query.
k : integer
    The number of nearest neighbors to return.
eps : nonnegative float
    Return approximate nearest neighbors; the kth returned value
    is guaranteed to be no further than (1+eps) times the
    distance to the real kth nearest neighbor.
p : float, 1<=p<=infinity
    Which Minkowski p-norm to use.
    1 is the sum-of-absolute-values "Manhattan" distance
    2 is the usual Euclidean distance
    infinity is the maximum-coordinate-difference distance
distance_upper_bound : nonnegative float
    Return only neighbors within this distance. This is used to prune
    tree searches, so if you are doing a series of nearest-neighbor
    queries, it may help to supply the distance to the nearest neighbor
    of the most recent point.

Returns
-------
d : float or array of floats
    The distances to the nearest neighbors.
    If x has shape tuple+(self.m,), then d has shape tuple if
    k is one, or tuple+(k,) if k is larger than one. Missing
    neighbors (e.g. when k > n or distance_upper_bound is
    given) are indicated with infinite distances.  If k is None,
    then d is an object array of shape tuple, containing lists
    of distances. In either case the hits are sorted by distance
    (nearest first).
i : integer or array of integers
    The locations of the neighbors in self.data. i is the same
    shape as d.

Examples
--------
>>> from scipy import spatial
>>> x, y = np.mgrid[0:5, 2:8]
>>> tree = spatial.KDTree(zip(x.ravel(), y.ravel()))
>>> tree.data
array([[0, 2],
       [0, 3],
       [0, 4],
       [0, 5],
       [0, 6],
       [0, 7],
       [1, 2],
       [1, 3],
       [1, 4],
       [1, 5],
       [1, 6],
       [1, 7],
       [2, 2],
       [2, 3],
       [2, 4],
       [2, 5],
       [2, 6],
       [2, 7],
       [3, 2],
       [3, 3],
       [3, 4],
       [3, 5],
       [3, 6],
       [3, 7],
       [4, 2],
       [4, 3],
       [4, 4],
       [4, 5],
       [4, 6],
       [4, 7]])
>>> pts = np.array([[0, 0], [2.1, 2.9]])
>>> tree.query(pts)
(array([ 2.        ,  0.14142136]), array([ 0, 13]))
>>> tree.query(pts[0])
(2.0, 0)