4
$\begingroup$

How to avoid that Mathematica simplifies a ratio?

I have this code (visualizing Simpson's paradox):

Manipulate[
 Grid[{{a/at, a/at < b/bt, b/bt}, {c/ct, c/ct < d/dt, d/dt}, {(
    a + c)/(at + ct), (a + c)/(at + ct) < (b + d)/(bt + dt), (b + d)/(
    bt + dt)}}, Frame -> All], {{a, 3}, 1, 100, 1}, {{at, 8}, 1, 100, 
  1}, {{b, 16}, 1, 100, 1}, {{bt, 32}, 1, 100, 1}, {{c, 24}, 1, 100, 
  1}, {{ct, 32}, 1, 100, 1}, {{d, 7}, 1, 100, 1}, {{dt, 8}, 1, 100, 
  1}]

The problem is that Mathematica simplifies 8/8 to 1 (for example), but I want it to show 8/8. How is this done?

$\endgroup$
4
  • $\begingroup$ see: 69655 $\endgroup$
    – Kuba
    Commented Sep 1, 2017 at 11:16
  • $\begingroup$ @Kuba Thanks! But should I apply HoldForm to each expression in the list seperately? $\endgroup$ Commented Sep 1, 2017 at 11:18
  • $\begingroup$ @Kuba HoldForm doesn't seem to work, it gives: \!( TagBox[ FractionBox["FEa$$178", "FEat$$178"], HoldForm]) $\endgroup$ Commented Sep 1, 2017 at 11:37
  • $\begingroup$ It works how it is supposed to work but you can't just wrap everything with it. $\endgroup$
    – Kuba
    Commented Sep 1, 2017 at 11:39

2 Answers 2

3
$\begingroup$
With[
    {range = Sequence[1, 100, 1]}
  , Manipulate[
        Grid[
            {Fraction[#, #2], #/#2 < #3/#4, Fraction[##3]
            } & @@@ {
                {a, at, b, bt}
              , {c, ct, d, dt}
              , {(a + c), (at + ct), (b + d), (bt + dt)}
            }, Frame -> All
        ]
      , {{a, 3}, range}, {{at, 8}, range}, {{b, 16}, range}
      , {{bt, 32}, range}, {{c, 24}, range}, {{ct, 32}, range}
      , {{d, 7}, range}, {{dt, 8}, range}
      , Initialization :> (
            Fraction /: MakeBoxes[Fraction[x_, y_], fmt_
            ] := FractionBox[MakeBoxes[x, fmt], MakeBoxes[y, fmt]]
        )
    ]

 ]
$\endgroup$
2
  • 1
    $\begingroup$ Could use Internal`RationalNoReduce instead of Fraction. $\endgroup$
    – Carl Woll
    Commented Sep 1, 2017 at 14:51
  • $\begingroup$ @CarlWoll maybe, but it doesn't even have usage message. And it is not the same, e.g.: 2 Fraction[1, 2] $\endgroup$
    – Kuba
    Commented Sep 4, 2017 at 14:13
3
$\begingroup$

Let me display a slight variation of Kuba's code, while incorporating a modification of eldo's take:

Manipulate[Column[{disp[a, at, b, bt], disp[c, ct, d, dt],
                   disp[a + c, at + ct, b + d, bt + dt]}],
           {{a, 3}, 1, 100, 1}, {{at, 8}, 1, 100, 1},
           {{b, 16}, 1, 100, 1}, {{bt, 32}, 1, 100, 1},
           {{c, 24}, 1, 100, 1}, {{ct, 32}, 1, 100, 1},
           {{d, 7}, 1, 100, 1}, {{dt, 8}, 1, 100, 1}, 
           Initialization :> (disp[p1_, q1_, p2_, q2_] :=
                              DisplayForm[RowBox[{FractionBox[p1, q1],
                                                  Switch[Sign[p1/q1 - p2/q2],
                                                         -1, "<", 0, "=", 1, ">"],
                                                  FractionBox[p2, q2]}]])]

Simpson's paradox demo

$\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.