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.
Reduce[3 < x < 5 && 2 < x < 4]
$\endgroup$Interval
represents closed intervals. Do you wish to keep track of open and half-open intervals? $\endgroup$