-1
$\begingroup$

I'm having a tough time dealing with inequalities.

in[]: -1 <= x // InputForm out[]: -1 <= x in[]: -1 <= x <= 1/2 // InputForm out[]: -1 <= x <= 1/2

So far so good, but the problem is:

in[]: -1 <= x < 1/2 // InputForm out[]: Inequality[-1, LessEqual, x, Less, 1/2]

Why this format ?, why such an Inequality[] function shows up ? It looks like a FullForm and not InputForm !

How to deal prevent with this behavior ? How to get a more consistent .. < .. < .. format ?

I tried to workaround and split the Inequality but I do not really want this output...

in[]: Reduce`InequalityExpand[-1 <= x < 1/2] // InputForm out[]: -1 <= x && x < 1/2

I also tried an awful in[]: -1 <= x < 1/2 // InputForm /. {Inequality -> List, LessEqual -> "<=", Less -> "<", Greater -> ">", GreaterEqual -> ">="} out[]: {-1, "<=", x, "<", 1/2} But I do not know how to adequately concatenate this list.

NB : InputForm matters for me, I'd like to get this one line output.

$\endgroup$
10
  • 1
    $\begingroup$ "How to deal with this behavior?" What does it mean to deal with it? What is the problem? This feels like an XY problem. $\endgroup$
    – Szabolcs
    Commented Dec 2, 2020 at 13:12
  • $\begingroup$ You did not answer my question. What do you want to achieve? InputForm can't be changed, so you're on the wrong track. We can only help you if you explain your actual problem. $\endgroup$
    – Szabolcs
    Commented Dec 2, 2020 at 13:39
  • $\begingroup$ My goal is to remove Inequality[] and replace it with a consistent .. < .. <= .. format in all cases. $\endgroup$
    – Crypto
    Commented Dec 2, 2020 at 14:00
  • 1
    $\begingroup$ Re your goal: The only way to get a combination of inequality types, < and <=, is to use Inequality. That's a design choice you cannot overcome, I think. Unfortunately, Inequality[0, Less, x] evaluates to Less[0, x], so you cannot put all inequalities into a consistent Inequality[..] form. (You can expand them, as you point out, but you seem reluctant to deal with that.) I've had to "deal" with this, too, but I don't know if it's the same as your case (see Szabolcs' comments). $\endgroup$
    – Michael E2
    Commented Dec 2, 2020 at 14:58
  • 1
    $\begingroup$ In my opinion, InputForm is not FullForm, and Mathematica outputs FullForm in these cases. For me it's a problem, I cannot give you more details... $\endgroup$
    – Crypto
    Commented Dec 2, 2020 at 16:14

3 Answers 3

3
$\begingroup$

You can Unprotect Inequality and modify the InputForm for it:

Unprotect[Inequality];
Format[HoldPattern @ Inequality[a__], InputForm] := Module[{res = HoldForm[a], rel},
    rel = List @@ Replace[res[[2 ;; -1 ;; 2]],
        {
        Less -> " < ",
        LessEqual -> " <= ",
        Greater -> " > ",
        GreaterEqual -> " >= ",
        Equal -> " == ",
        Unequal -> " != "
        },
        {1}
    ];
    res = InputForm /@ res;
    res[[2 ;; -1 ;; 2]] = rel;
    Replace[res, HoldForm[z__] :> OutputForm @ HoldForm @ SequenceForm[z]]
]
Protect[Inequality];

The HoldForm and OutputForm @ HoldForm @ bits prevent evaluation leaks, and SequenceForm is a special function that behaves like Row even inside InputForm. Example:

x < 5 <= y //InputForm

x < 5 <= y

And a version showing that evaluation leaks don't occur:

Hold[2+2 < 7 <= 9] //InputForm

Hold[2 + 2 < 7 <= 9]

$\endgroup$
1
$\begingroup$
ClearAll[myInputForm];
shortcuts = {
   Less -> " < ",
   LessEqual -> " <= ",
   Greater -> " > ",
   GreaterEqual -> " >= "};
myInputForm[expr_] := Module[{ineqs, i, subs, stripped},
   ineqs = UniqueCases[expr, _Inequality];
   subs = Unique[i] & /@ ineqs;
   stripped = expr /. Thread[ineqs -> subs];
   CellPrint[Cell[
     StringReplace[ToString[stripped, InputForm, PageWidth -> 60],
      Thread[(ToString /@ subs) -> (ineqs /. 
          HoldPattern[Inequality[x__]] :> 
           StringJoin[
            Replace[{x}, Append[shortcuts, e_ :> ToString[e, InputForm]], 1]])]
      ],
     "Output",
     (* This label doesn't work properly: *)
     (* CellLabel ->
         StringJoin["Out[", ToString@$Line, "]//InputForm="] *)
     ]]
   ];

myInputForm[0 < x <= 2 \[Implies] 0 < x^2 <= 4]

(*  Implies[0 < x <= 2, 0 < x^2 <= 4]  *)
$\endgroup$
0
$\begingroup$

Try this:

Inequality[-1, LessEqual, x, Less, 1/2] // TraditionalForm

(*   -1<=x<1/2   *)

Have fun!

$\endgroup$

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