# How to cancel a common term from numerator and denominator?

What mathematica commands are needed to simplify $\frac{x+n x^m}{x(1+x)}$ to $\frac{1+n x^{m-1}}{(1+x)}$ ?

ClearAll[n , x, m];
num = n x^m + x;
den = x (x + 1);
s = num/den


to verify

Reduce[s == (1 + n x^(m - 1))/(1 + x)]


-
Ugly Simplify[s, ComplexityFunction -> (Count[#, x, Infinity] &)]. But for x to be a "common term" you would need n to be a natural, right? –  Rojo Aug 23 at 5:00
@Rojo well, I did not have to use any assumptions on $n$ for Reduce to say True, so this should work for any $n$. I am not sure now why you say $n$ must be rational.... –  Nasser Aug 23 at 5:07
@Rojo Is your question about n being natural related to that ComplexityFunction? If not, maybe you will consider posting this as an answer. I do not think it is ugly, we cannot expect that there will be function for each form one can imagine. Well, this seems to be natural to make this reduction but "natural" depends of our "complexity function" ;). Or I'm wrong and it should be done automatically? –  Kuba Aug 23 at 6:50
Nasse, I guess the appropriate answer to your question depends on how would you want a more general case to be treated, because I'm not sure it can be conjectured from this example only. My suggestion tries to reduce the number of appearances of the variable. If a solution tried to cancel the common roots of numerator and denominator on the other hand, then it would need assumtions on m to work in this case. That's all I meant –  Rojo Aug 23 at 17:09
@LeoFang, check LeafCount /@ {Simplify[s], (1 + n x^(-1 + m))/(1 + x)}. There's probably no reason to expect that solution instead of the one they return. –  Rojo Aug 23 at 18:54

Another possibility:

 Collect[Apart[s], (1 + x)]


=>

-
You can use it also this way: Collect[(n x^m + x)/(x (x + 1)), (1 + x), Apart], +1. –  Artes Aug 23 at 8:56

Another way:

Simplify[Expand[s], ExcludedForms -> _Power]

-

here is my try.Assuming the numerator is of the form (x^i + n*x^m) where i=1,2..

  newSimplify[s_] := Module[{num1, den1},
pOWer = Exponent[Numerator[s][[1]], x];
num1 = Numerator[s]; den1 = Denominator[s];
newNum = Expand[x^(-pOWer)*num1];
newDen = Together[Expand[x^(-pOWer)*den1]];
news = newNum/newDen]

(*(1 + n x^(-1 + m))/(1 + x)*)

-
@Kuba How about now.It will at least work with that assumption –  Hubble07 Aug 23 at 6:59
@Kuba How about now –  Hubble07 Aug 23 at 7:41
I can't find an example to break it so +1 :) –  Kuba Aug 23 at 7:46

There are several ways. What comes into head first is as follows:

  s = (x + n x^m)/(x (1 + x))
MapAt[x*Apart[Divide[#, x]] &, s, {3}]


the result is

(* (1 + n x^(-1 + m))/(1 + x)  *)


as expected. One can instead operate with the numerator separately and use Hold to protect the result from simplification, but in the heart it is the same:

    subNum = x*Apart[Divide[Numerator[s], x]];
num = Map[Hold, subNum, {2}];
num/Denominator[s] // ReleaseHold


the result is as above.

The most simple to my taste is as follows. Mathematica is reluctant to cancel x, since it does not know, what is m. Let us "cheet it": let it know that m=1+p. Then it will simplify. Then you may substitute back p=m-1:

 s = ((x + n x^m)/(x (1 + x)) /. m -> p + 1 // Simplify) /. p -> m - 1

(* (1 + n x^(-1 + m))/(1 + x)  *)

-
Simplify[s, ComplexityFunction -> (Count[#, x, Infinity] &)]