Function Repository Resource:

SymbolicSort

Source Notebook

Sort symbolic expressions using assumptions

Contributed by: Sjoerd Smit

ResourceFunction["SymbolicSort"][list,vars]

attempts to sort list by assuming that the variables vars are real-valued.

ResourceFunction["SymbolicSort"][list,vars,assum]

sorts by assuming that vars satisfy assumptions assum.

ResourceFunction["SymbolicSort"][list,vars,assum,dom]

carries out symbolic manipulations over the domain dom.

ResourceFunction["SymbolicSort"][list,vars,assum,dom,List]

always returns a List, even if some elements do not have a definite ordering.

ResourceFunction["SymbolicSort"][list,vars,assum,dom,Graph]

returns a Graph showing the order relations found between the elements of list, including elements that do not have a definite ordering.

Details and Options

The argument vars can be either a List or a single variable.
ResourceFunction["SymbolicSort"] uses Resolve and ForAll to determine if one expression is uniformly greater than another.
The default value for assum is $Assumptions.
The default domain is Reals.
If no definite ordering can be found, ResourceFunction["SymbolicSort"] remains unevaluated and raises a message. Use ResourceFunction["SymbolicSort"][list,vars,assum,dom,List] to force the function to return a List.
In the Graph produced by ResourceFunction["SymbolicSort"][,Graph], undirected edges will be used to indicate that two expressions cannot be ordered. Directed edges will only be drawn between consecutively ordered elements. To calculate the Graph, all possible comparisons between all elements will be made, so this functionality can be slow for large lists.
A TimeConstraint (default 5) option value can be specified.

Examples

Basic Examples (3) 

Sort expressions in x defined for real numbers:

In[1]:=
ResourceFunction["SymbolicSort"][{Exp[x], x}, x]
Out[1]=
Image

Plot to illustrate that Exp[x] is always greater than x:

In[2]:=
Plot[Evaluate@Reverse[%], {x, -3, 3}, PlotLegends -> "Expressions"]
Out[2]=
Image

Use assumptions for sorting:

In[3]:=
ResourceFunction["SymbolicSort"][{x, Log[x], Exp[x], x^2 + 1/2}, x, x > 0]
Out[3]=
Image
In[4]:=
Plot[Evaluate@Reverse[%], {x, 0, 3}, PlotLegends -> "Expressions"]
Out[4]=
Image

Sort expressions of a monotonic function applied to different variables:

In[5]:=
ResourceFunction["SymbolicSort"][{Exp[y], Exp[x], Exp[z]}, {x, y, z}, x < y < z]
Out[5]=
Image

Scope (2) 

If no definite order exists, a message will be raised and SymbolicSort remains unevaluated:

In[6]:=
ResourceFunction["SymbolicSort"][{Sin[x], Exp[x], Cos[x]}, x, 0 <= x <= 2 \[Pi]]
Image
Out[6]=
Image

Use List as the last argument to force a result:

In[7]:=
ResourceFunction["SymbolicSort"][{Sin[x], Exp[x], Cos[x]}, x, 0 <= x <= 2 \[Pi], Reals, List]
Out[7]=
Image

If not all expressions have a definite ordering, you can instead produce a Graph that shows the ordering relationships between the elements of the List:

In[8]:=
ResourceFunction["SymbolicSort"][{Sqrt[x], Log[x], x, x^2, Exp[x]}, x,
  x > 0, Reals]
Image
Out[8]=
Image
In[9]:=
ResourceFunction["SymbolicSort"][{Sqrt[x], Log[x], x, x^2, Exp[x]}, x,
  x > 0, Reals, Graph]
Out[9]=
Image

The undirected edges between Image, x and x2 show that these expressions have no definite ordering.

Options (1) 

TimeConstraint (1) 

A time constraint can be set for symbolic comparisons. If the time constraint is too low, no result will be found:

In[10]:=
ResourceFunction["SymbolicSort"][{x, Log[x], Exp[x], x^2 + 1/2}, x, x > 0, TimeConstraint -> 10^-3]
Image
Image
Out[10]=
Image

Applications (1) 

Sort the eigenvalues of a symbolic matrix:

In[11]:=
ResourceFunction["SymbolicSort"][
 Eigenvalues[{{a, 1}, {1, c}}],
 {a, b, c},
 0 < a < c,
 Reals
 ]
Out[11]=
Image

Publisher

Sjoerd Smit

Version History

  • 1.0.0 – 14 October 2020

Related Resources

License Information