Wolfram Computation Meets Knowledge

Wolfram Function Repository Highlights: From Country Borders to Bird Speech Bubbles

Wolfram Function Repository Highlights: From Country Borders to Bird Speech Bubbles

In June 2019, Stephen Wolfram announced the Wolfram Function Repository, a curated repository of functions that can be employed immediately in the Wolfram Language. Since then, the Repository has grown to include more than 1,000 functions in over 20 categories.

Functions included in the Repository range from those that are more general and utilitarian in nature to others with very specific applications. As with all Wolfram Language functions, Repository documentation pages contain examples showing how to use the functions. We’re featuring a few of the functions submitted to the Repository so far that showcase the variety of functions our users have built.

DropWhile: Drop Elements of a List while a Criterion Remains True

Contributed by: Sander Huisman

Like many functions in the Repository, DropWhile was created by a user to streamline his workflow. It works by taking a user-set condition to examine the elements of a list, starting with the first element, to see if it meets the given condition; if so, it is dropped and the next element is then evaluated. This continues as long as the condition is met and remains true. Once the condition is no longer true, it no longer applies and no additional elements are dropped.

This is an example of a simple yet useful idiom. If you look at its source notebook, you can see that the function definition is simple but was used often enough by the author to be written into a ResourceFunction. Viewing the source code from within the Wolfram Language is very simple:

ResourceFunction["DropWhile","DefinitionNotebook"]
&#10005

ResourceFunction["DropWhile", "DefinitionNotebook"]

As shown in the examples on the resource page, the function can be set so that all odd elements starting from the beginning of a given list of numbers are dropped until the condition is no longer met:

ResourceFunction["DropWhile"][{1, 3, 5, 9, 10, 11, 12, 15}, OddQ]
&#10005

ResourceFunction["DropWhile"][{1, 3, 5, 9, 10, 11, 12, 15}, OddQ]

DropWhile can be configured to exclude all elements in a given list that are less than 14, starting from the first element, until an element no longer meets the condition:

[{0, 1, 3, 7, 13, 14, 23, 27, 44, 
  53}, # < 14 &]
&#10005

ResourceFunction["DropWhile"][{0, 1, 3, 7, 13, 14, 23, 27, 44, 
  53}, # < 14 &]

Similarly, it can be utilized to drop leading zeros in a list:

[{0, 0, 0, 0, 7, 11, 0, 3.14, 24}, 
 EqualTo[0.]]
&#10005

ResourceFunction["DropWhile"][{0, 0, 0, 0, 7, 11, 0, 3.14, 24}, 
 EqualTo[0.]]

Using Characters lets it work on strings:

[Characters["jenny:867-5309"], ! DigitQ[#] &]
&#10005

ResourceFunction["DropWhile"][
 Characters["jenny:867-5309"], ! DigitQ[#] &]

There’s also a ResourceFunction for that:

["jenny:867-5309", Not@*DigitQ]
&#10005

ResourceFunction["StringFunction"][
ResourceFunction["DropWhile"]]["jenny:867-5309", Not@*DigitQ]

MaximizeOverPermutations: Find the Permutation That Maximizes a Function

Contributed by: Roman Schmied

MaximizeOverPermutations was introduced in the Mathematica forum on Stack Exchange. It takes a Method option that accepts either "Enumerate" or "MonteCarlo". While the brute-force "Enumerate" option is good for n≤12, the really excellent part is the "MonteCarlo" method, which uses a simulated annealing (Metropolis–Hastings) approach. This scales nicely to larger problems and, as shown in the documentation, it does a really good job on some notoriously difficult benchmark tests. One function curator liked it so much that he offered 250 Stack Exchange points in the hope of having it become a Repository contributed function. Said points were awarded on receipt of the function. There was no quid pro quo. Definitely not. Well, actually, there was. It was kind of brazen, really. But we got the function and it is quite good.

x = RandomReal
&#10005

x = RandomReal[{0, 1}, 100];
ResourceFunction["MaximizeOverPermutations"][#.x &, 100, 
 Method -> {"MonteCarlo", "Iterations" -> 10^5, 
   "AnnealingParameter" -> 10}]

Here, you can see the function maximize the value in the third place of permutations for the set {1,2,3,4}:

ResourceFunction[
&#10005

ResourceFunction["MaximizeOverPermutations"][#[[3]] &, 4]

In the following illustration, MaximizeOverPermutations determines the maximum function value for the given function for permutations of the numbers {1,2,3}, with two function values yielding the maximal value :

f[{1,2,3}]=0;
&#10005

f[{1, 2, 3}] = 0;

f[{1,3,2}]=2;
&#10005

f[{1, 3, 2}] = 2;

f[{2,1,3}]=-4;
&#10005

f[{2, 1, 3}] = -4;

f[{2,3,1}]=8;
&#10005

f[{2, 3, 1}] = 8;

f[{3,1,2}] =1;
&#10005

f[{3, 1, 2}] = 1;

f[{3,2,1}]=8;
&#10005

f[{3, 2, 1}] = 8;

ResourceFunction["MaximizeOverPermutations"][f, 3]
&#10005

ResourceFunction["MaximizeOverPermutations"][f, 3]

The number of permutations of n objects scales very quickly with n; this can impact the time and space needed to perform such computations. Learn more by reading the documentation page for this function.

SudokuSolve: Solve Sudoku Puzzles

Contributed by: Jon McLoone

This function helps users solve Sudoku number puzzles. SudokuSolve treats a 9×9 matrix of data as a partially solved Sudoku puzzle, filling in the entries so that every row, every column and the nine 3×3 subgrids contain the digits 1 through 9.

It also notifies users if a given puzzle cannot be solved. An option can also be set to show when an entry was a guess rather than determined with certainty using logic:

ResourceFunction["DropWhile","DefinitionNotebook"]
&#10005

ResourceFunction["SudokuSolve"][\!\(\*
TagBox[
RowBox[{"(", "", GridBox[{
{"\[Placeholder]", "\[Placeholder]", "8", "1", "7", "6", 
        "\[Placeholder]", "2", "\[Placeholder]"},
{"\[Placeholder]", "4", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "9", "7", "\[Placeholder]", 
        "\[Placeholder]"},
{"\[Placeholder]", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "\[Placeholder]", "\[Placeholder]"},
{"\[Placeholder]", "7", "1", "8", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "\[Placeholder]", "\[Placeholder]"},
{"\[Placeholder]", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "\[Placeholder]", "\[Placeholder]", "6", 
        "7", "9"},
{"\[Placeholder]", "\[Placeholder]", "\[Placeholder]", "5", "9", "7", 
        "\[Placeholder]", "8", "1"},
{"\[Placeholder]", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "2", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "\[Placeholder]"},
{"\[Placeholder]", "5", "9", "\[Placeholder]", "\[Placeholder]", 
        "\[Placeholder]", "\[Placeholder]", "1", "\[Placeholder]"},
{"\[Placeholder]", "1", "4", "6", "5", "\[Placeholder]", "3", 
        "\[Placeholder]", "\[Placeholder]"}
},
GridBoxAlignment->{"Columns" -> {{Center}}, "Rows" -> {{Baseline}}},
GridBoxSpacings->{"Columns" -> {
Offset[0.27999999999999997`], {
Offset[0.7]}, 
Offset[0.27999999999999997`]}, "Rows" -> {
Offset[0.2], {
Offset[0.4]}, 
Offset[0.2]}}], "", ")"}],
Function[BoxForm`e$, 
MatrixForm[BoxForm`e$]]]\)]

HistoricalCountryAnimate: Show an Animation of the Full History of a Historical Country’s Territory

Contributed by: Brian Wood

The next highlighted function yields a visual representation of the territory of a country for a given period of years. HistoricalCountryAnimate accesses Wolfram Knowledgebase resources in performing its calculations. This is a great example of building complex functions with a language that has built-in access to real-world knowledge. Here, we can use Canada:

ResourceFunction["HistoricalCountryAnimate"][Entity["HistoricalCountry","Canada"]]
&#10005

ResourceFunction["HistoricalCountryAnimate"][
 Entity["HistoricalCountry", "Canada"]]

This function returns an interactive animation with tooltips. Depending on system specs, generating the animated image may take a few minutes.

There are several options users can set, one of which is the option to include a map showing the borders of modern countries overlapping the historical country.

HistoricalCountryAnimate

ChordDiagram: Make a Weighted Connectivity Graph Using Circular Embedding

Contributed by: Georgios Varnavides

For users who want another option for visual representation of data, ChordDiagram provides an alternative means of doing so.

This function requires an edge-weighted graph as input. That data is used to draw each vertex of a weighted graph as a wedge, with width proportional to the WeightedAdjacencyMatrix row total:

gr = Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3,     3 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 1},    EdgeWeight -> RandomReal[{1, 6}, 4]]
&#10005

gr = Graph[{1 \[UndirectedEdge] 2, 2 \[UndirectedEdge] 3, 
   3 \[UndirectedEdge] 4, 4 \[UndirectedEdge] 1}, 
  EdgeWeight -> RandomReal[{1, 6}, 4]]

Now we can apply ChordDiagram to the graph (with randomized edge weights):

ResourceFunction["ChordDiagram"][gr]
&#10005

ResourceFunction["ChordDiagram"][gr]

BirdSay: Have a Bird Say an Expression

Contributed by: Richard Hennigan

Our final function definitely puts the “fun” in “function” with BirdSay, which gives the user a desired output in a speech bubble, as stated by a fun-loving parrot:

"Bird is the word!"
&#10005

ResourceFunction["BirdSay"]["Bird is the word!"]

BirdSay works by creating a Panel with nine-patch images for the Appearance option. Nine-patch images are a great tool for customizing design in Panel as well as many other formatting constructs such as Button, Framed, Notebook and so on. The following code returns the raw nine-patch image from a symbol within the BirdSay ResourceFunction definition:

ninePatch = Symbol
&#10005

ninePatch = Symbol[Context[
ResourceFunction["BirdSay"]] <> "$data"]

This image can be used as the value of Appearance in Button:

Button
&#10005

Button[Style["Yo! Wolfie!", 25], 
 CreateDialog[ResourceFunction["WolfieSay"]["Greetings"]], 
 Appearance -> ninePatch]

You can see the full definition of how nine-patch images are used within BirdSay by downloading the definition notebook.

Users can make many other modifications to adjust parrot placement, nest several references within a single statement and much more. Check out its documentation page to explore additional features.

And More to Come

Users are regularly contributing to the Wolfram Function Repository. To browse our newest additions, visit our Recent Functions page or subscribe to our email updates for a convenient weekly digest. If you have a great idea for a function, please submit your notebook for consideration! Just follow the File > New > Repository Item > Function Repository Item path to get a Definition Notebook where you’ll document the source code and uses. Whether you’re an advanced user of the Wolfram Language or just starting out, there’s a function in the Repository to help enhance and inspire your computational explorations. And if you haven’t found the perfect function yet—submit it!

Comments

Join the discussion

!Please enter your comment (at least 5 characters).

!Please enter your name.

!Please enter a valid email address.

1 comment

  1. Hey! That’s me! Thanks for sharing my function on the blog!

    Reply