# Factor common terms out of a sum, without changing it otherwise?

I have some expressions, that consist of rather lengthy sums such as

-((2 Subscript[f, lm])/(\[Epsilon] Subscript[\[Epsilon], lm] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$3$$]\))) + ( 2 Subscript[f, nl])/(\[Epsilon] Subscript[\[Epsilon], nl] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$3$$]\)) - Subscript[f, lm]/(\[Epsilon] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$lm$$, $$2$$]\) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\)) - Subscript[f, lm]/(\[Epsilon]^2 Subscript[\[Epsilon], lm] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\)) + Subscript[f, nl]/(\[Epsilon] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nl$$, $$2$$]\) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\)) + Subscript[f, nl]/(\[Epsilon]^2 Subscript[\[Epsilon], nl] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\)) - Subscript[f, lm]/(2 \[Epsilon] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$lm$$, $$3$$]\) Subscript[ \[Epsilon], nm]) - Subscript[f, lm]/(2 \[Epsilon]^2 \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$lm$$, $$2$$]\) Subscript[ \[Epsilon], nm]) - Subscript[f, lm]/( 2 \[Epsilon]^3 Subscript[\[Epsilon], lm] Subscript[\[Epsilon], nm]) + Subscript[f, nl]/(2 \[Epsilon] \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nl$$, $$3$$]\) Subscript[ \[Epsilon], nm]) + Subscript[f, nl]/(2 \[Epsilon]^2 \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nl$$, $$2$$]\) Subscript[ \[Epsilon], nm]) + Subscript[f, nl]/( 2 \[Epsilon]^3 Subscript[\[Epsilon], nl] Subscript[\[Epsilon], nm]) + Subscript[f, lm]/(2 (\[Epsilon] - Subscript[\[Epsilon], lm]) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$lm$$, $$3$$]\) (-2 Subscript[\[Epsilon], lm] + Subscript[\[Epsilon], nm])) - Subscript[f, nl]/( 2 (\[Epsilon] - Subscript[\[Epsilon], nl]) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nl$$, $$3$$]\) (-2 Subscript[\[Epsilon], nl] + Subscript[\[Epsilon], nm])) - ( 16 Subscript[f, nl] Subscript[\[Epsilon], lm])/((2 \[Epsilon] - Subscript[\[Epsilon], nm]) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$3$$]\) (-2 Subscript[\[Epsilon], lm] + Subscript[\[Epsilon], nm]) (-2 Subscript[\[Epsilon], nl] + Subscript[\[Epsilon], nm])) + ( 16 Subscript[f, lm] Subscript[\[Epsilon], nl])/((2 \[Epsilon] - Subscript[\[Epsilon], nm]) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$3$$]\) (-2 Subscript[\[Epsilon], lm] + Subscript[\[Epsilon], nm]) (-2 Subscript[\[Epsilon], nl] + Subscript[\[Epsilon], nm])) - ( 8 Subscript[f, lm])/((2 \[Epsilon] - Subscript[\[Epsilon], nm]) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\) (-2 Subscript[\[Epsilon], lm] + Subscript[\[Epsilon], nm]) (-2 Subscript[\[Epsilon], nl] + Subscript[\[Epsilon], nm])) + ( 8 Subscript[f, nl])/((2 \[Epsilon] - Subscript[\[Epsilon], nm]) \!$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\) (-2 Subscript[\[Epsilon], lm] + Subscript[\[Epsilon], nm]) (-2 Subscript[\[Epsilon], nl] + Subscript[\[Epsilon], nm]))


These are the result of a partial fraction decomposition with Apart[#1,ϵ] and Expand[], so in each expression only one term depends on the variable ϵ; I want to pull these terms out and rewrite the expression as, e.g. (example created manually):

"\!$$\*FractionBox[\(1$$, $$\[Epsilon]$$]\)(-\!$$\*FractionBox[SubscriptBox[\(f$$, $$lm$$], $$\*SubscriptBox[\(\[Epsilon]$$, $$lm$$] \*SuperscriptBox[SubscriptBox[$$\[Epsilon]$$, $$nm$$], $$3$$]\)]\)+\! $$\*FractionBox[\(2 \*SubscriptBox[\(f$$, $$nl$$]\), $$\*SubscriptBox[\(\[Epsilon]$$, $$nl$$] \*SuperscriptBox[SubscriptBox[$$\[Epsilon]$$, $$nm$$], $$3$$]\)]\)+...)+\!$$\*FractionBox[\(1$$, $$\[Epsilon] - \*SubscriptBox[\(\[Epsilon]$$, $$nm$$]/2\)]\)(-\!$$\*FractionBox[\(4\\\ \*SubscriptBox[\(f$$, $$lm$$]\), $$\(\\\$$$$\*SubsuperscriptBox[\(\[Epsilon]$$, $$nm$$, $$2$$]\\\ $$(\(-2$$\\\ \*SubscriptBox[$$\[Epsilon]$$, $$lm$$] + \*SubscriptBox[$$\[Epsilon]$$, $$nm$$])\)\\\ $$(\(-2$$\\\ \*SubscriptBox[$$\[Epsilon]$$, $$nl$$] + \*SubscriptBox[$$\[Epsilon]$$, $$nm$$])\)\)\)]\)+...)+..."


Is there some way to automate this reformulation? Note that \Collect[] doesn't help here at all, even though the fractions are technically just terms of a polynomial with some negative powers.

## Current best attempt

My own best attempt works, but I was hoping for builtin functions, that would make this easier:

(* Extract \[Epsilon]-dependent prefactors
f[expr_] := (expr
// Apart[#1, \[Epsilon]] &
// Expand
// Apply[List]
// Map[split]
// GroupBy[#1[[1]] &]
// Values
// Map[{#1[[1]][[1]] // Evaluate // HoldForm,
Plus @@ Map[##1[[2]] &, #1] // Evaluate // HoldForm} &]
// Map[Apply[Times]]
// Apply[Plus]
)

(* Split expression into \[Epsilon]-dependent prefactor
and \[Epsilon]-independent body. Specialized for this specific
case, doesn't work fore more general forms. *)
split[expr_] := Module[{sans, pre},
sans = expr /. \[Epsilon] + x_ -> 1 /. \[Epsilon]^n_ -> 1 /.
n_*\[Epsilon] + x_ -> 1;
pre = expr/sans;
{pre, sans}]

(* Example. *)
(Subscript[f, nl]/(Subscript[\[Epsilon], nl] - \[Epsilon]) -
Subscript[f, lm]/(Subscript[\[Epsilon], lm] - \[Epsilon])) 1/(
2 \[Epsilon]^3 (Subscript[\[Epsilon], nm] - 2 \[Epsilon])) // f


Let your whole expression be called expr0. You confuse Mma which not in each operation can distinguish the symbols with subscripts from one another and from those without subscripts. I would recommend you for the next time to avoid using subscripts. However, since they are already there, I will first make an operation to help Mma to avoid such a confusion:

expr01 = expr0 /. Subscript[\[Epsilon], n_] -> Subscript[x, n]


yielding this you see that now x with subscripts stays instead of epsilon with subscripts. Now this

Select[List @@ expr01,
MemberQ[#, 1/\[Epsilon]] || MemberQ[#, 1/\[Epsilon]^2] &]


will transform the expression into a list and only select of it the terms with 1/eps and 1/eps^2. This

Plus @@ Select[List @@ expr01,
MemberQ[#, 1/\[Epsilon]] || MemberQ[#, 1/\[Epsilon]^2] &]


will transform the obtained list back into a sum, and this

HoldForm[1/\[Epsilon]]*
Expand[\[Epsilon]*
Plus @@ Select[List @@ expr01,
MemberQ[#, 1/\[Epsilon]] || MemberQ[#, 1/\[Epsilon]^2] &]]


will extract the factor 1/eps out of it. Like this let us form three expressions:

expr1 = HoldForm[1/\[Epsilon]]*
Expand[\[Epsilon]*
Plus @@ Select[List @@ expr01,
MemberQ[#, 1/\[Epsilon]] || MemberQ[#, 1/\[Epsilon]^2] &]]

expr2 = Plus @@
Select[List @@ expr01,
MemberQ[#, 1/(\[Epsilon] - Subscript[x, m_])] &]

expr3 = HoldForm[1/(2 \[Epsilon] - Subscript[x, nm])]*
Expand[Simplify[(2 \[Epsilon] - Subscript[x, nm])*
Plus @@ Select[List @@ expr01,
MemberQ[#, 1/(2 \[Epsilon] - Subscript[x, n_])] &]]]


and to finalize it make the sum as follows:

expr4=expr1 + expr2 + expr3 /. Subscript[x, n_] -> Subscript[\[Epsilon], n]


The result is here in the form of an image since otherwise it is much too long:

It has the terms under the HoldForm function. Therefore, if you will need to further use it in calculations, make first ReleaseHold[expr4].

Have fun!

• Thanks! But, bottom line: There is no easy [1] solution? I came up with my own solution, while you wrote your answer (see edit of original post), and ultimately both look somewhat hard to read. – [1] read: builtin, or concisely combined from builtins. – kdb Nov 14 '18 at 18:41
• I do not understand, what is "[1] solution". However, I believe to understand your question in general. The answer is: not that I know. There are good reasons. First, the expression is rather long even after simplification. Second and main, you require it in a simplified form that is convenient for some your aims and, therefore, must satisfy certain criterions. MMa, however, has different criterions. They are individual: only for you and only in precisely this calculation. For this reason, no single function, like e.g. Simplify does this job. – Alexei Boulbitch Nov 15 '18 at 13:09
• (Continuation) David Park made a package "Presentations" that contained functions making this work easy. But then you need to get this package. I used the idea of those functions in my approach. On the other hand, the best solution is the one where you understand each step and can easily change something in the code. That is most probably your solution. – Alexei Boulbitch Nov 15 '18 at 13:09
• I was mostly hoping to find a builtin method, that aids intentionally reformatting subterms in a user-defined manner, as opposed to general forms such as FullSimplify[] and ExpandAll[]. The main problem with writing these things myself, is that any implementation is essentially untested code, which makes creating a non-equivalent expression instead of just a reformatted version of the same expression exceedingly likely. And sufficiently testing code, that is meant for one-off use, kind of defeats the purpose of automating it. – kdb Nov 15 '18 at 17:28