3
$\begingroup$

Mathematica overloads the Min and Max functions to retrieve the lower and upper bounds of an interval.

I need functions that work properly on Interval arguments. For example, the minimum of Interval[{3,4}] and Interval[{2,5}] should be Interval[{2,4}].

To clarify based on comments, I'm looking for the tightest possible bounds for the Min and Max functions, given the constraints of the intervals. For example, the Max function of the intervals {2,3} and {7,8} will be {7,8}, because all values in the interval {7,8} are greater than any value in the interval {2,3}.

(Note: I'm not looking for an ordering relation for intervals. The Min and Max operations often produce new intervals. See the first example, above.)

I've written my own functions to do this, that only work for simple intervals:

intervalopf[a_Interval, b_Interval, op_] := 
 Flatten[Outer[op, {Min[a], Max[a]}, {Min[b], Max[b]}]]
intervalop[a_Interval, b_Interval, op_] := 
 With[{v = intervalopf[a, b, op]}, Interval[{Min[v], Max[v]}]]

Are there built-in functions I should be using?

And, how can I generalize this to intervals that are unions?

$\endgroup$
6
  • $\begingroup$ From your example it seems the result of min should be an interval that goes from the min of the lower bounds to the min of the upper bounds, right? How would it work with intervals such as Interval[{min1, max1}, {min2, max2}, ...]? $\endgroup$
    – Rojo
    Commented Nov 2, 2013 at 1:56
  • 1
    $\begingroup$ In any case, I doubt there's a built-in $\endgroup$
    – Rojo
    Commented Nov 2, 2013 at 1:57
  • $\begingroup$ @Rojo A sense-in? $\endgroup$ Commented Nov 2, 2013 at 2:21
  • 1
    $\begingroup$ Since there is probably not a built-in, why don't you rephrase the question to ask how to write the custom function? You might get a better method than what you're using now. $\endgroup$
    – Mr.Wizard
    Commented Nov 2, 2013 at 7:28
  • $\begingroup$ @payne First you need to specify exactly, how this Minand Max on intervals should work, because the situation is similar to 2d vectors or complex numbers, where no ordering relation exist. $\endgroup$
    – halirutan
    Commented Nov 2, 2013 at 12:10

2 Answers 2

4
$\begingroup$

Edit: A slightly shorter version.

minInterval[i_] := Interval[Min /@ Transpose[i[[All, 1]]]]
maxInterval[i_] := Interval[Max /@ Transpose[i[[All, 1]]]]

Usage

minInterval@{Interval[{3, 4}], Interval[{2, 5}], Interval[{2, 4}] }
maxInterval@{Interval[{3, 4}], Interval[{7, 8}] }

Interval[{2, 4}]
Interval[{7,8}]


Initial Suggestion:

minInterval[intervals_]:=Interval@{Min[intervals[[All,1,1]]],Min[intervals[[All,1,2]]]}
maxInterval[intervals_]:=Interval@{Max[intervals[[All,1,1]]],Max[intervals[[All,1,2]]]}
$\endgroup$
3
  • $\begingroup$ That's exactly what I was looking for (for simple intervals). I wanted to (a) make sure I wasn't missing a built-in, and (b) understand a better Mathematica-ish idiom for doing this. Thanks! $\endgroup$
    – payne
    Commented Nov 2, 2013 at 20:16
  • $\begingroup$ I think you forgot to wrap the output in Interval in your edit version $\endgroup$
    – Rojo
    Commented Nov 3, 2013 at 4:19
  • $\begingroup$ @Rojo Yes, I did. Thanks for the heads-up. $\endgroup$
    – DavidC
    Commented Nov 3, 2013 at 14:14
3
$\begingroup$

Yet another solution

minInterval = Interval @@ MapThread[Min, List @@@ #, 2] &;

minInterval@{Interval[{1, 2}], Interval[{5, 6}], Interval[{9, 10}]}
Interval[{1, 2}]

It works also for unions of intervals (if you need to find the minimum union)

minInterval@{Interval[{1,2}, {3,4}], Interval[{5,6}, {7,8}], Interval[{9,10}, {11,12}]}
Interval[{1, 2}, {3, 4}]
$\endgroup$

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.