Function Repository Resource:

Maximal

Source Notebook

Get the maximal item in terms of canonical ordering

Contributed by: Wolfram Staff

ResourceFunction["Maximal"][list]

returns the canonically maximal item in list.

ResourceFunction["Maximal"][list,p]

uses the ordering function p.

Details

Values of list are compared using the same canonical order as in Sort.
Use MaximalBy instead if ordering through a criterion function is desired.
In a numerical list, Ordering[list,n] gives the positions of the n smallest elements. Ordering[list,-n] gives the positions of the n largest elements.

Examples

Basic Examples (5) 

Using canonical ordering, return the maximal item:

In[1]:=
items = {2, 6, 1, 9, 1, 2, 3, Sqrt[2] - 1, -\[Infinity]};
ResourceFunction["Maximal"][items]
Out[2]=

If the last item is dropped, the more numerically complicated item is returned:

In[3]:=
ResourceFunction["Maximal"][Most[items]]
Out[3]=

Sort the items:

In[4]:=
Sort[items]
Out[4]=

The numeric maximum is different:

In[5]:=
Max[items]
Out[5]=

Use a different ordering function to obtain the same result:

In[6]:=
ResourceFunction["Maximal"][items, NumericalOrder]
Out[6]=

Scope (2) 

Use Maximal on an association:

In[7]:=
ResourceFunction["Maximal"][<|a -> 4, b -> 1, c -> 3, d -> 2, e -> 2|>]
Out[7]=

Use Maximal on a sparse vector:

In[8]:=
ResourceFunction["Maximal"][SparseArray[{{3} -> 2, {7} -> -1}, 10]]
Out[8]=

Properties and Relations (2) 

Find the item with the largest second item using an ordering function:

In[9]:=
ResourceFunction["Maximal"][{{a, 3}, {b, 2}, {a, 2}, {d, 1}, {b, 3}}, NumericalOrder[Last[#1], Last[#2]] &]
Out[9]=

The result of the function MaximalBy has a similar form:

In[10]:=
MaximalBy[{{a, 3}, {b, 2}, {a, 2}, {d, 1}, {b, 3}}, Last]
Out[10]=

Version History

  • 1.1.0 – 03 February 2023
  • 1.0.0 – 14 February 2022

Related Resources

License Information