# How to expand a rational function as a single fraction

This has been annoying me for a long time. I want a rational function to be represented in the form

$$\frac{a_nx^n+....a_0x^0}{b_mx^m+....b_0x^0}$$

But this seems to be quite difficult. Mathematica really loves to factor things out. //ExpandAll usually gives me a sum of bunch of rational functions. I just want the numerator to be a straightforward polynomial and the denominator to be a straightforward polynomial.

I can //Together to get a single fraction and //Expand the numerator and denominator separately, and then combine them by hand, but that is very annoying and it feels like there should be a command to do this. The form I wrote above is the most common rational function form, so why would it be so difficult to arrive at?

For example what do I need to do the fraction below to get it to be a polynomial divided by a polynomial?

-2/n^3 + (32 n)/(1 - 8 n^2 + 16 n^4)

• Actually the Together plus Expand on numerator and denominator is likely to be your best bet. Together typically does not actually factor but it also does not go out of its way to avoid factors, and sometimes it feels a need to do a square-free factorization. It can be a bit headstrong that way. Commented May 6, 2021 at 19:59
• I think ExpandNumerator and ExpandDenominator in certain cases can help. Try -2/n^3 + (32 n)/(1 - 8 n^2 + 16 n^4) // Together //ExpandDenominator // ExpandNumerator for your example. Commented Jul 1, 2022 at 8:22

Look at the commands ExpandNumerator and ExpandDenominator .

-2/n^3 + (32 n)/(1 - 8 n^2 + 16 n^4) // Together //
ExpandNumerator // ExpandDenominator

(*   (-2 + 16 n^2)/(n^3 - 8 n^5 + 16 n^7)  *)


Try

Together[-2/n^3 + (32 n)/(1 - 8 n^2 + 16 n^4)]
(*(2 (-1 + 8 n^2))/(n^3 (-1 + 4 n^2)^2)*)

• It factors the numerator and the denominator. The result is not a straightforward polynomial. It is a factored polynomial in the numerator and the denominator. Commented May 6, 2021 at 19:55
• This works: F[x_] := TraditionalForm[Expand[Numerator[Together[x]]]]/ TraditionalForm[Expand[Denominator[Together[x]]]] , but is there really no default function to see a rational function in the form I want? Commented May 6, 2021 at 19:56
• The function NumeratorDenominator was introduced in v12.0. F[x_] := TraditionalForm[Divide @@ Expand@NumeratorDenominator@Together@x] Commented May 6, 2021 at 22:33

Apply Factor or Together to the expression, separate the numerator and denominator, then Expand and form the fraction.

eq = -2/n^3 + (32 n)/(1 - 8 n^2 + 16 n^4);

(#1 / #2) &@@ Expand[Through[{Numerator, Denominator}[Together[eq]]]] // TraditionalForm


$$\frac{16 n^2-2}{16 n^7-8 n^5+n^3}$$

Try this:

With[{eq = -2/n^3 + (32 n)/(1 - 8 n^2 + 16 n^4)},  Inactive@Evaluate@
Divide @@ ((Expand@#@Simplify@eq) & /@ {Numerator, Denominator})]
// TeXForm


which returns $$\left(16 n^2-2\right)/\left(16 n^7-8 n^5+n^3\right)$$