5
$\begingroup$

How do I get Mathematica to avoid displaying nested fractions in outputs, and instead automatically display them as parenthesized, factored-out terms?

For example, I would like

(A/(B + C) + D)/F

to be displayed as $\frac{1}{F} \left(\frac{A}{B+C}+D\right)$ instead of $\frac{\left(\frac{A}{B+C}+D\right)}{F}$.

If possible, I would like to do this in a somewhat automated manner, without having to manually Inactivate multiplications, for instance.

It would be great if the solution could work for "higher-order" nested fractions too. For instance:

((A/(B + C) + D)/F + G)/H

could be displayed as $\frac{1}{H}\left(\frac{1}{F}\left(\frac{A}{B+C}+D\right)+G\right)$

Consider an actual example of expression that I have in my code:

L^2 + (-4 + (6 L)/J + (L^2 (7 - 2 \[Nu]))/J^2 + (L^4 (-9 + \[Nu]))/   J^4 + \[Nu])/c^2 + ((L (77/2 - 11 \[Nu]))/J^3 + (4 + \[Nu]/4)/   L^2 + (3 L^3 (-9 + \[Nu]))/J^5 + (-(39/2) + 6 \[Nu])/(J L) + (   L^4 (-6920 + 2580 \[Nu] - 179 \[Nu]^2))/(32 J^6) + (   168 + 12 \[Nu] + \[Nu]^2)/(32 J^2) + (   7 L^6 (360 - 96 \[Nu] + 7 \[Nu]^2))/(16 J^8) + (   L^2 (294 - 149 \[Nu] + 10 \[Nu]^2))/(4 J^4))/c^4

I would like to ideally have an output similar to:

$ L^2 + \frac{1}{c^2}\left(\frac{L^4 (\nu -9)}{J^4}+\frac{L^2 (7-2 \nu )}{J^2}+\frac{6 L}{J}+\nu -4\right) +\frac{1}{c^4}\left(\frac{7 L^6 \left(7 \nu ^2-96 \nu +360\right)}{16 J^8}+\frac{L^4 \left(-179 \nu ^2+2580 \nu -6920\right)}{32 J^6}+\frac{3 L^3 (\nu -9)}{J^5}+\frac{L^2 \left(10 \nu ^2-149 \nu +294\right)}{4 J^4}+\frac{L \left(77-22\nu \right)}{2 J^3}+\frac{\nu ^2+12 \nu +168}{32 J^2}+\frac{12 \nu - 39}{2 J L}+\frac{\nu + 16}{4 L^2} \right) $

$\endgroup$
7
  • 1
    $\begingroup$ Welcome to Mathematica StackExchange! Such manipulations of the output can be quite cumbersome in Mathematica. Can you please give some more examples of your terms, so that we know how general you want the solution to be? Otherwise, this will work for your particular example: (a/(b + c) + d)/f /. (1/x_)*y_ :> HoldForm[Defer[(1/x)] y]. $\endgroup$
    – Domen
    Commented Feb 20 at 15:32
  • 1
    $\begingroup$ If you are only interested in the display of the formula, have a look at the MaTeX package from Szabolcs. It's phenomenal. With that package you get professional typeset-quality math formatting developed by true math typeset professionals (the TeX/LaTeX folks). $\endgroup$ Commented Feb 20 at 23:32
  • $\begingroup$ @Nasser provided a very nice solution. If it solves your problem, please accept it. Otherwise, specify more clearly what is your desired output for your particular example. $\endgroup$
    – Domen
    Commented Mar 1 at 18:48
  • $\begingroup$ Thanks @Domen, I have provided further infos in the question $\endgroup$ Commented Mar 4 at 14:25
  • 1
    $\begingroup$ I have accepted Nasser's answer. I was able to get rid of the numerical factors via the FactorTerms method. $\endgroup$ Commented Mar 5 at 12:08

2 Answers 2

8
$\begingroup$

Since this is for display only, how about

ClearAll["Global`*"]
makeNice[e_] :=  If[Denominator[e] =!= 1, 1/Denominator[e]·Numerator[e], e]

And now you can use it as follows

makeNice[(a/(b + c) + d)/f]

Mathematica graphics

makeNice[(1 + x)/(x*Sin[x])/4]

Mathematica graphics

Tested only on small examples. Feel free to test it more and see if you find any bugs and will try to fix. This can be made more robust by adding more checks on the input ofcourse.

But you can't use the output since it uses center dot for multiplications. You could always replace the center dot with the real * to use it.

This works only on one term. To make it work on sum of terms, it is easy to do that. For example, given

e = (a/(b + c) + d)/f + x + (a/(b + c) + d)/g

Mathematica graphics

Then you can do

 Map[makeNice, e]

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Hi @Nasser, thanks a lot for your great solution. Ideally, I would like this to also work iteratively on nested fractions inside nested fractions, if possible. See my EDIT for an example. Do you think such a thing could be done? Finally, there is a corner case where makeNice seems to fail, for instance: makeNice[6 L/J] $\endgroup$ Commented Mar 4 at 14:25
  • 1
    $\begingroup$ @Christopher I tried your example and this is what I get on V 14 !Mathematica graphics is this not what is expected? $\endgroup$
    – Nasser
    Commented Mar 4 at 14:27
  • $\begingroup$ Yes, ideally it should only apply when fractions are nested. In this example there is only one fraction, so I would like makeNice to do nothing $\endgroup$ Commented Mar 4 at 14:38
2
$\begingroup$

Although @Nasser has provided a great straightforward answer for simple cases, more complex nested fractions might require a pattern-matching solution. The following should work for a great variety of cases.

First, we define a pattern-testing function to determine whether an expression has a non-trivial denominator in any subexpression. This can be done with FreeQ:

denominatorFreeQ[expr_] := FreeQ[expr, x_ /; !SameQ[Denominator[x], 1]];

As a test, we can apply denominatorFreeQ to a few sample cases:

denominatorFreeQ /@ 
    {a, a/x, (a + b)/(x + y), a (b + c), a (b + c/x), a b (c + (d + e/2))}

Mathematica graphics

The main function can now check an expression for subexpressions whose numerators aren't denominator-free, and inactivate the Times.

formatNestedTimes = ReplaceRepeated[
              x_ /; And[!denominatorFreeQ[Numerator[x]],!SameQ[Denominator[x], 1]]
              :> Inactive[Times][1/Denominator[x], Numerator[x]]
          ]

Testing on the sample provided in the question yields:

Mathematica graphics

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