# Finding Functions / Better Documentation Search

Posted 9 months ago
483 Views
|
0 Replies
|
4 Total Likes
|

This is another cross-post of something I did first here

As someone who has used Mathematica for a reasonably long time now, one thing I often forget is how hard it can be to find the functions for a given task when you're just starting out.

Mathematica has a nice Documentation Center , but finding stuff in it often turns into a long slog of randomly clicking through links. The documentation search is okay, but it's generally cluttered with links and cruft and not clever enough on the fuzzy-matching that people usually want.

So we're going to develop and alternative for finding the right function for the job. I'll build up to this and provide a package implementation at the end.

## Names and Wildcards

The heart of this approach will be the function Names . This takes a string pattern and returns all the symbols matching that pattern. If you remember nothing else from this, keep in mind that Names is your best friend when working with Mathematica. It allows us to use string pattern-matching syntax to find functions. That lets us use wildcards and other things. For example, to find all the "*View" functions we could do:

Names["*View"]

(*Out:*)



And then we can look at the docs for each of these

## HelpLookup

With these in hand, we can turn to their doc pages. Now, we could obviously select the string and press Command-F for each, but we can do this more automatically, using the function DocumentationHelpLookup . For instance, if we wanted to find the doc page for a function that we know has "Log" and the name and ends in "Plot" , we could just do:

DocumentationHelpLookup@First@Names["*Log*Plot"]

(*Out:*)


## PrintDefinitionsLocal

Most of the time, though, the documentation page isn't enough. It's a template for how to use a function, but doesn't give us the whole story. Better is to be able to spelunk a symbol . This is actually built-in as of 10.1 and the function is GeneralUtilitiesPrintDefinitionsLocal (there's also a version that opens a new notebook but that one's generally clumsier in my view).

Now we can do stuff like look at functions that help us define layers in NNs:

LinearLayer; (* This autoloads the NN context *)
GeneralUtilitiesPrintDefinitionsLocal@First@Names["NeuralNetworksDefine*Layer"]

(*Out:*)


## Putting it All Together

Now we can combine these parts to write a function that will take a string pattern, provide links to the documentation via DocumentationHelpLookup , and also wrap in the GeneralUtilitiesPrintDefinitionsLocal data at the bottom of the page. I've written this into a package here.

You can load the package like this:

Get["https://raw.githubusercontent.com/b3m2a1/mathematica-tools/master/DocFind.wl"]


And then we can go-to-town. First let's find all the functions in the "NeuralNetworks" context:

DocFind["*", "NeuralNetworks", Select->"Function"]

(*Out:*)


Clicking on any of these links will attempt to open the doc page and will stick the PrintDefinitionsLocal data at the bottom:

(*Out:*)


This makes finding the functions we need so much nicer.

Alternately we can just search for any function with the name "DocFind" in the \$Packages :

DocFind["DocFind"]

(*Out:*)


This is how I generally use the function. And with sophisticated pattern matching we can find functions that we know should exist but we just don't have the names for. Say a constant somewhere in the "PacletManager" context that has "Dir" in its name:

DocFind["Dir", "PacletManager*", Select->"Constant"]

(*Out:*)


And now figuring out how the "PacletManager" works is as simple as doing a few DocFinds` and clicking a few links.