Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7556855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:58:16+00:00 2026-05-30T11:58:16+00:00

tl;dr How can something like Mathematica’s Nearest be implemented efficiently? Mathematica has a function

  • 0

tl;dr How can something like Mathematica’s Nearest be implemented efficiently?

Mathematica has a function called Nearest which will take a list of “things” (they can be numbers, coordinates in n-dimensional space, strings, etc.), and will return a NearestFunction object. This object is a function that, when applied to x, will return the list element which is closest to x by some distance metric. The distance metric can be passed as a parameter to Nearest: by default it uses Euclidean distance for numerical data and some kind of edit distance for strings.


Example (this will hopefully make the question more clear):

nf = Nearest[{92, 64, 26, 89, 39, 19, 66, 58, 65, 39}];

nf[50] will return 58, the element closest to 50. nf[50, 2] will return {58, 39}, the two closest elements.


Question: What is an efficient way to implement this functionality? What sort of data structure is NearestFunction likely to use internally? What is the best possible complexity of computing a nearest element for different types of data?

For a plain list of numbers sorting them and doing a binary search would work, but Nearest works with multidimensional data as well as with an arbitrary distance function, so I suppose it uses something more general. But I wouldn’t be surprised if it turned out to be specialized for certain kinds of data / distance functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T11:58:18+00:00Added an answer on May 30, 2026 at 11:58 am

    For distance functions that are well-behaved, there are many data structures optimized specifically for this. For multidimensional data, the k-d tree (and other binary space partitioning trees) can give excellent nearest-neighbor searches, usually in sublinear time. You may also want to look into metric trees, which are tree structures optimized to store points in some metric space in a way that supports nearest-neighbor searches. Depending on the particular metric space (Euclidean distance, edit distance, etc.), different data structures might be more or less appropriate.

    For arbitrary distance functions in which there are no restrictions on the behavior (not even things like the triangle inequality, for example), then the best you can do is a linear search, since the distance function might be infinite for all points except for one specific point in the set.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know i can do something like ab[^c]+def which should match ab_blah_hi_blah_def but is
I know that I can do something like $int = (int)99; //(int) has a
Mathematica has a function MapThread that behaves like this: MapThread[ f , { {a,b,c}
I can do something like this: validator.showErrors({ nameOfField : ErrorMessage }); And that works
In Java you can do something like: byte[] code = ReadFromClassFile(SomethingSomething.class); SendAcrossNetwork(code); And on
I know I can use something like User.sort {|a, b| a.attribute <=> b.attribute} or
In Delphi I can do something like: var hWin : HWnd; hWin := GetForegroundWindow;
I know you can do something like this: readlines(FileName) -> {ok, Device} = file:open(FileName,
How I can do something like this in C++: void my_print(format_string) { vector<string> data;
does anyone know if you can do something like this using the (N)Hibernate criteria

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.