# Finding the smallest function and the domain on which it is smallest

Given a list of functions what is the best way to find the domains on which each function is the smallest. Simple example

giveDomainWhereFunctionIsSmallest[{x,x^2,4-x}]

(*Out: {x,x<0|| 1<x<2, x^2, 0<x<1,x+4,x>2} *)


I am looking for something that works on more complicated functions and a decent number of them. And also for multi-variable functions.

• How do you define "best way"? Especially when the problem is not clearly defined (are your functions all polynomials? Can all roots be found analytically? etc.). Anyway, this gives you an insight pretty quickly: min[x_?NumericQ] := First@First@Position[f[x], Min[f[x]]]; Plot[min[x], {x, -10, 10}]. Oct 26, 2020 at 18:11
• @anderstood. Well I give an example of a satisfying in and output. Best way I guess means working for most general cases at acceptable speed. Simply plotting the minimum is not that useful since I want to extract the domain on which the function is minimal. Really I was also thinking of multi-variable cases (or at least 2 variables), where plotting gets even less convenient. I will add that to the question. Oct 26, 2020 at 18:16

There is a very recent addition to the Wolfram Function Repository that can be utilized here.

orders =
ResourceFunction["GenerateOrderingConditions"][Less,
{x, x^2, 4 - x}, x]

(* Out[128]= <|{x, x^2, 4 - x} ->
1/2 (-1 - Sqrt[17]) < x < 0 || 1 < x < 1/2 (-1 + Sqrt[17]), {x,
4 - x, x^2} ->
x < 1/2 (-1 - Sqrt[17]) || 1/2 (-1 + Sqrt[17]) < x < 2, {x^2, x,
4 - x} -> 0 < x < 1, {x^2, 4 - x, x} -> False, {4 - x, x, x^2} ->
x > 2, {4 - x, x^2, x} -> False|> *)


One can readily get the conditions for which the various functions are respectively smallest.

Thread[{Keys[orders][[All, 1]], Values[orders]}] /. {_, False} :>
Nothing

(* Out[133]= {{x,
1/2 (-1 - Sqrt[17]) < x < 0 || 1 < x < 1/2 (-1 + Sqrt[17])}, {x,
x < 1/2 (-1 - Sqrt[17]) || 1/2 (-1 + Sqrt[17]) < x < 2}, {x^2,
0 < x < 1}, {4 - x, x > 2}} *)

• This is a great function. It seems to work in more general cases than the above PieceWiseExpand. (It seems to works excellent in the multi-variable case too for example.) Oct 28, 2020 at 11:29
• Feel free to tell the author via feedback form. I think that would be appreciated. Oct 28, 2020 at 14:18
PiecewiseExpand[Min[{x, x^2, 4 - x}]]

(*    4-x   if x>=2
x     if 1<=x<2 || x<=0
x^2   otherwise            *)

• Great! Nice to see there is a canonical way to do this. (Less nice it does not work for the more complicated example I am trying to solve now.) (I will see whether I can get it down to a postable yet complicated enough example.) Oct 26, 2020 at 18:25
• @Kvothe Complicated but short examples: numerical functions such as CosIntegral, for instance. Oct 26, 2020 at 18:52