Consider your expression,
expr = (a + m a + b + n b + c + k c + d + e)/(a b);
This gets us almost where we want to go,
Expand@expr
(* 1/a + 1/b + c/(a b) + d/(a b) + e/(a b) + (c k)/(
a b) + m/b + n/a *)
But we have too many terms with the same denominator, so we can use GatherBy
to group them, then simplify the sums of terms with the same denominator, then sum it all back up,
Together@*Plus @@@ GatherBy[List @@ Expand[expr], Denominator] // Total
(* (c + d + e + c k)/(a b) + (1 + m)/b + (1 + n)/a *)
But as OP points out, this will not combine terms where the denominator is the same except for a constant factor, like a/(2*b)
and c/b
. This function should be able to simplify an rational expression like desired in the OP (if you come up with a way to break the function, please let me know).
fractionExpand[expr_] :=
Replace[Expand@expr,
expr2_Plus :> (Together@*Plus @@@
GatherBy[List @@ expr2, Variables@*Denominator] // Total)]
This will break up a term into the largest number of fractions, without repeating denominators. Here are a few tests,
expr2 = (1 + m)/b + (1 + n)/a + (1 + o)/c + (d + e + f g)/(
a b) + (h + i + j k)/(b c) + (l + p q)/(a b c) // Together
fractionExpand@expr2
You get back the original input.
expr3 = a/(2 b) + c/b + d/e + f/(g + z) + (a - c)/(l m b) // Together
fractionExpand@expr3
Apart
? $\endgroup$