3
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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}]. $\endgroup$
    – anderstood
    Oct 26, 2020 at 18:11
  • $\begingroup$ @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. $\endgroup$
    – Kvothe
    Oct 26, 2020 at 18:16

2 Answers 2

6
$\begingroup$

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}} *)
$\endgroup$
2
  • $\begingroup$ 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.) $\endgroup$
    – Kvothe
    Oct 28, 2020 at 11:29
  • $\begingroup$ Feel free to tell the author via feedback form. I think that would be appreciated. $\endgroup$ Oct 28, 2020 at 14:18
10
$\begingroup$
PiecewiseExpand[Min[{x, x^2, 4 - x}]]

(*    4-x   if x>=2
      x     if 1<=x<2 || x<=0
      x^2   otherwise            *)
$\endgroup$
2
  • $\begingroup$ 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.) $\endgroup$
    – Kvothe
    Oct 26, 2020 at 18:25
  • $\begingroup$ @Kvothe Complicated but short examples: numerical functions such as CosIntegral, for instance. $\endgroup$
    – anderstood
    Oct 26, 2020 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.