Message Boards Message Boards

[✓] Inconsistent language design MinMax - Solved in V11.1

GROUPS:

The new function MinMax is defined (first line in the documentation) as follows:

enter image description here

However this does not hold for an empty list

{Min[{}], Max[{}]}
MinMax[{}]

So it is self-contradicting and slightly confusing. I know MinMax says it returns {-infinity,infinity} but that does not fit into the the definitions of Min, Max and MinMax. Like other functions like ReIm and AbsArg, the function should just returns {Min[..],Max[..]} (but more optimized) like ReIm returns {Re[..],Im[..]} and AbsArg returns {Abs[..],Arg[..]}...

Take the following example, i want the minimum and maximum of those lists. It should be the same to do the MinMax on each of them, and then do the min of all the minima, and the max of all the maxima, but the function doesn't handle empty lists in a coherent way (as Min and Max by itself does).

SHMinMax[x___]:={Min[x],Max[x]}

num1={{4,7},{5,9},{11,13,2,8}};
num2=Flatten[num1];
(* all give same results as there is no 'empty' list *)
MinMax[num2]  
{Min[#1],Max[#2]}&@@Transpose[MinMax/@num1]
{Min[#1],Max[#2]}&@@Transpose[SHMinMax/@num1]

(* empty list! built-in MinMax function is not coherent *)
num1={{4,7},{5,9},{11,13,2,8},{}};
num2=Flatten[num1];
MinMax[num2]
{Min[#1],Max[#2]}&@@Transpose[MinMax/@num1]
{Min[#1],Max[#2]}&@@Transpose[SHMinMax/@num1]

Note that just Min (or Max) is coherent:

num1 = {{4, 7}, {5, 9}, {11, 13, 2, 8}, {}};
num2 = Flatten[num1];
Min[num2]
Min@(Min /@ num1)

The very definition of Min[] to return infinity, has been done such that Min[3,Min[4],Min[-1,6]] or Min[3,4,-1,6] or Min[3,Min[],Min[4,-1],Min[6]] always give the same results, regardless of the order.

More confusingly is that it works differently for empty Interval objects and with empty lists.

(* built in MinMax IS coherent with empty intervals *)
{Min[Interval[]], Max[Interval[]]}
MinMax[Interval[]]
SHMinMax[Interval[]]

(* but not with empty lists *)
{Min[{}], Max[{}]}
MinMax[{}]
SHMinMax[{}]

To summarise, the choice of returning {-infinity, infinity} for MinMax[{}] is inconsistent with and incoherent with the definitions of Min[], Max[] and the function MinMax itself. And more puzzling gives also 'unusual' results for Interval objects.

I would like to know how this design choice was conceived. To me it seems very illogical and incoherent as it is now. While my own function SHMinMax which is the definition as given in the first definition of MinMax, does follow all the logic and is consistent throughout (for lists, empty lists, intervals and empty intervals!).

Please let me understand the logic!

POSTED BY: Sander Huisman
Answer
1 year ago

Nope, it's not fixed in 10.4.0.

POSTED BY: Szabolcs Horvát
Answer
1 year ago

I guess the reasoning is then that the output of MinMax can be interpreted as an interval, and of course {Infinity, -Infinity} makes not sense that way.

POSTED BY: Szabolcs Horvát
Answer
1 year ago

If it would be an interval, shouldn't it be a 'null' interval

Interval[]

Such that Min[Interval[]] gives correctly infinity, and Max[Interval[]] gives -infinity. To proof my point further:

{Min[Interval[]], Max[Interval[]]}
MinMax[Interval[]]

gives the same result!

POSTED BY: Sander Huisman
Answer
1 year ago

I'm not sure what was done in the handling if Interval objects. Might or might not be intended behavior.

As for handling of empty lists, I think it is a sensible departure from the design of Min and Max separately. It means that programs known to generate either purely NumericQ or empty lists can be treated in a uniform manner, without need for safety checks that the list was not empty.

POSTED BY: Daniel Lichtblau
Answer
1 year ago

I don't think I'm following you, if you have NumericQ or empty lists now, they are NOT treated in a uniform manner as I showed above...

Also if you interpret MinMax as kind-of giving the range of the numbers, then for an empty set (list) the output will be strange anyhow: if you output {-inf,inf} it is strange because out of nothing the 'size' of the set is infinite, which I guess is equally strange from giving back {0,0} or {inf,-inf}. the last one though, is compatible with the definitions of min and max separately, and the definition of how MinMax[list] should work {Min[list],Max[list]}...

My point is, that the output for an empty list is arbitrary, but there is one output that is at least consistent with the useful behavior of Min and Max... don't you agree?

POSTED BY: Sander Huisman
Answer
1 year ago

I agree that there is an output consistent with behavior of Min and Max separately. I agree MinMax does not give that consistent result. I do not agree that that behavior of Min and Max individually is useful in the context of MinMax. Whether consistency is more important is certainly a reasonable point to raise. I would not go so far as to say it trumps all other desired features of such a function, and handling of empty sets the way MinMax now does is such a feature.

POSTED BY: Daniel Lichtblau
Answer
1 year ago

What was the rational on returning {-inf,inf} for Minmax[{}] if I may ask?

POSTED BY: Sander Huisman
Answer
1 year ago

I don't know the answer. I only claim it makes sense to me, as an edge case.

POSTED BY: Daniel Lichtblau
Answer
1 year ago

Well, as of version 11.1 this has been changed; now it gives the expected (at least for me) result:

{Min[{}], Max[{}]}
MinMax[{}]

both return:

{\[Infinity], -\[Infinity]}

as one would expect.

POSTED BY: Sander Huisman
Answer
3 months ago

Group Abstract Group Abstract