7
$\begingroup$

I have an inequality, ie: 1.57116 < g < 14.1405

There will be a large set of similar inequalities, and I want to find the intersection of them all. I figured it would be easier to convert the inequalities to intervals first. Then find the intersection.

Is there an easy way to convert inequalities to intervals as the inequalities are generated? Alternatively, a way to find the intersection of a group of inequalities.

$\endgroup$
2
  • 1
    $\begingroup$ Try Reduce[3 < x < 5 && 2 < x < 4] $\endgroup$ Commented Jul 18, 2013 at 14:34
  • 2
    $\begingroup$ Interval represents closed intervals. Do you wish to keep track of open and half-open intervals? $\endgroup$
    – Michael E2
    Commented Jul 18, 2013 at 14:56

5 Answers 5

7
$\begingroup$

I voted once to close this question as OP gave us not enough information about what is the desired output and the context.

Since I can't vote to close the second time I decided to write something :)


Couple of thoughs at the begining.

First of all, I'd use Reduce to deal with this problem and convert only the result. But if you ask I find it a good exercise to try doing what you ask for.

Pattern approach, as suggested by Anon, is no a bad idea. One just has to remember the issue I've pointed out in comments. Also, the origin of inequalities is important too.

Please take a look:

in = Reduce[3 < x < 5 && 2 < x < 4] (*progenitor of the inequality :)*)
in // FullForm
in // LogicalExpand // FullForm (*modyfied*)
3 < x < 4 // FullForm (*typed by hand*)
Inequality[3,Less,x,Less,4]
And[Less[3,x],Less[x,4]]
Less[3,x,4]

All the same, all different. That's why I think using LogicalExpand for preventive purposes is a good idea. It will create, not the most compact solution, but matching the same pattern.

Reduce[3 < x < 5 && 2 < x < 4] (*automatic and modyfied*)    
3 < x < 4 // LogicalExpand // FullForm (*by hand & modyfied*)
And[Less[3,x],Less[x,4]]
And[Less[3,x],Less[x,4]]

But still we can see that there is some kind of inconsitence which will result in to much complicated patterns or to many of them. I mean that there is: Less[3,x] And Less[x,4] which could be also written Greater[x, 3] And Less[x, 4]. Good to know that we can convert all to consistent form with Reduce:

3 > x // FullForm
Reduce[3 > x] // FullForm
Greater[3,x]
Less[x,3]

Ok, now we can create solution, step by step:

(3 > x || x > 4 /. i_[s_?NumericQ, f_] :> Reduce[i[s, f]])
x < 3 || x > 4
x < 3 || x > 4 /. {i_[s_, f_?NumericQ] :> Which[
                          MemberQ[{Greater, GreaterEqual}, i], Interval[{f, Infinity}],
                          MemberQ[{Less, LessEqual}, i], Interval[{-Infinity, f}]
                                               ]
                  }
Interval[{-Infinity, 3}] || Interval[{4, Infinity}]
Interval[{-Infinity, 3}] || Interval[{4, Infinity}] /. {Or -> IntervalUnion, 
                                                        And -> IntervalIntersection}
Interval[{-Infinity, 3}, {4, Infinity}]

And all together

conv[ineq_] := Fold[
                  ReplaceAll[#, #2] &,
                  ineq // LogicalExpand,
                  {
                   i_[s_?NumericQ, f_] :> Reduce[i[s, f]]
                   ,
                   {i_[s_, f_?NumericQ] :> Which[
                          MemberQ[{Greater, GreaterEqual}, i], Interval[{f, Infinity}],
                          MemberQ[{Less, LessEqual}, i], Interval[{-Infinity, f}]
                                                ]
                  }
                  ,
                  {Or -> IntervalUnion, And -> IntervalIntersection}
                 }
                 ]

conv[Reduce[3 < x < 5 && 2 < x < 4]]
Interval[{3, 4}]
conv[3 < x < 5 && 2 < x < 4]
Interval[{3, 4}]

This solution assumes that intervals all always closed. Maybe I will generalize this if I will find some time in the future.

$\endgroup$
3
  • $\begingroup$ This works well for just plain numbers, but doesn't work for conv[ x >= Root[#^3 - 2 &, 1]] which often shows up as result of reduce. $\endgroup$
    – Somnium
    Commented Jul 31, 2022 at 13:27
  • $\begingroup$ @Somnium thanks for feedback. Unfortunately this is not my area of expertise so I doubt I will find time to generalize this. $\endgroup$
    – Kuba
    Commented Aug 1, 2022 at 5:50
  • $\begingroup$ No problem, I have found solution myself and posted it as separate answer. $\endgroup$
    – Somnium
    Commented Aug 1, 2022 at 7:28
5
$\begingroup$

Kuba's answer is great and has good explanation, but it is not working for intervals containing root objects. Here is version using similar logic that works for those as well.

InequalitiesToIntervals[ineq_, x_] := LogicalExpand[ineq] /. {
  Greater[x, f_] -> Interval[{f, Infinity}],
  Greater[f_, x] -> Interval[{-Infinity, f}],
  GreaterEqual[x, f_] -> Interval[{f, Infinity}],
  GreaterEqual[f_, x] -> Interval[{-Infinity, f}],
  Less[x, f_] -> Interval[{-Infinity, f}],
  Less[f_, x] -> Interval[{f, Infinity}],
  LessEqual[x, f_] -> Interval[{-Infinity, f}],
  LessEqual[f_, x] -> Interval[{f, Infinity}],
  Or -> IntervalUnion,
  And -> IntervalIntersection
};

Example usage:

InequalitiesToIntervals[Reduce[x^7 - 5 x^3 - 1 > 0, x], x]

Output:

Interval[{Root[-1 - 5 #^3 + #^7& , 1, 0], Root[-1 - 5 #^3 + #^7& , 2, 0]}, {Root[-1 - 5 #^3 + #^7& , 3, 0], Infinity}]
$\endgroup$
3
$\begingroup$

Since version 10, there is RegionBounds that can be used to do exactly this kind of job if the boolean expression is first converted to an ImplicitRegion:

boole = {
   1.57116 < x < 14.1405,
   0 < x < 1,
   0 < x < 10,
   0 <= x <= 1,
   x == 2,
   0 < x < 1 && -1 < x < 2,
   0 < x < 1 && 3 < x < 4,
   0 < x < 1 || 3 < x < 4,
   0 < x < Infinity,
   Element[x, Integers],
   x < 3 || x > 4
};

reg = ImplicitRegion[#, x] & /@ boole;

TableForm[
  {RegionBounds[#, "Minimal"], RegionBounds[#, "Sufficient"], 
     RegionBounds[#, "Finite"]} & /@ reg,
  TableHeadings -> {boole, {"Minimal", "Sufficient", "Finite"}}, 
  TableDepth -> 2]

Mathematica graphics

Default is "Minimal", but for some cases, it might not return a proper interval. "Finite" returns a finite (not necessarily the smallest) bounding region for unbounded regions, e.g. halflines, halfplanes, etc. This is useful if one wants to provide a default interval for example to a Slider that is used to manipulate x that is by default not bound.

RegionBounds works in any dimensions:

boole = {
   0 < x < 1 && 0 < y < 1,
   0 <= x < 1 && -1 < y <= 2,
   0 <= x,
   x^2 + y^2 <= 1,
   x^2 + y^2 <= 1 && Gamma[x - y] <= x + y,
   x^2 + y^2 <= 4 || (x - 3)^2 + y^2 <= 3
   };

reg = ImplicitRegion[#, {x, y}] & /@ boole;

TableForm[{Quiet@RegionPlot[#, ImageSize -> 60, FrameTicks -> None, 
        PlotRange -> {{-3, 3}, {-3, 3}}], RegionBounds[#, "Minimal"], 
      RegionBounds[#, "Sufficient"], RegionBounds[#, "Finite"]} & /@ 
    reg /. {_RegionBounds | _RegionPlot -> "..."},
  TableHeadings -> {boole, {"RegionPlot", "Minimal", "Sufficient", 
     "Finite"}}, TableDepth -> 2]

Mathematica graphics

$\endgroup$
1
$\begingroup$

Here's a refinement of @István's answer that produces Interval objects:$\infty$

bounds[inequality_, x_] := First @ RegionBounds[ImplicitRegion[inequality, x]]

ToInterval[inequality_, x_] := With[{rng = bounds[inequality, x]},
    System`Private`HoldSetValid[Interval[rng]]
]

t:ToInterval[_Or, _] := IntervalUnion @@ Thread[Unevaluated @ t, Or]

A couple examples showing the differences:

ToInterval[0<x<1 || 3<x<4, x]
ToInterval[x<3 || x>4, x]

Interval[{0, 1}, {3, 4}]

Interval[{-∞, 3}, {4, ∞}]

vs.

RegionBounds @ ImplicitRegion[0<x<1 || 3<x<4, x]
RegionBounds @ ImplicitRegion[x<3 || x>4, x]

{{0, 4}}

{{-∞, ∞}}

Another more complicated example:

ToInterval[
    Reduce[x^3 > 6 Sin[x] + 1, x],
    x
] //N

Interval[{-1.7039, -0.168253}, {1.88567, ∞}]

Visualization:

Plot[{x^3, 6 Sin[x] + 1}, {x, -3, 4}, Filling -> {1 -> {{2}, {None,Blue}}}]

enter image description here

$\endgroup$
0
-1
$\begingroup$

For each inequality:

Interval[Replace[1.57116 < g < 14.1405, Less[n__, __, m__] -> {n, m}]]

To find the intersection, as you have figured out, you can use IntervalIntersection.

$\endgroup$
4
  • 1
    $\begingroup$ Be careful with a<x<b, Reduce, Piecewise etc like to create different wrapper for them Inequality[a, Less, x, Less, b]. But LogicalExpand will make your inequalities match to _[_,_]. $\endgroup$
    – Kuba
    Commented Jul 18, 2013 at 15:11
  • $\begingroup$ If there are other types of inequalities than in the OP, this answer needs to be expanded for sure. $\endgroup$
    – C. E.
    Commented Jul 18, 2013 at 15:25
  • 1
    $\begingroup$ Of course but I'm talking about his one. Look here Reduce[1 < x < 2] // FullForm. $\endgroup$
    – Kuba
    Commented Jul 18, 2013 at 15:47
  • $\begingroup$ @Kuba Alright, that complicates things a lot. LogicalExpand may be a good approach as you suggest, but otherwise writing patterns that specifically conforms to the sort of inequality formatting the OP is dealing with should be straight forward if one knows what it is. So I guess the message to the OP is to use FullForm on his inequalities to figure that out. $\endgroup$
    – C. E.
    Commented Jul 18, 2013 at 16:31

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.