# Factor out common terms from a list of terms

I was looking for an automated way to factor out common terms from a list.

Example (assuming n<1):

 list={(1 + r^2)^(-1/2 + n/2) (1 + w^2)^(-1/2 - n/2) (1 + r^(1 - n) w^(1 + n)) x12^-n,
r^(1 - n) (1 + r^2)^(-1/2 + n/2) w^(1 + n) (1 + w^2)^(-1/2 - n/2) x12^-n,
(1 + r^2)^(-1/2 + n/2) (1 + w^2)^(-1/2 - n/2) x12^-n}


Expected output:

(1 + r^2)^(-1/2 + n/2) (1 + w^2)^(-1/2 - n/2)   x12^-n  {  (1 +
r^(1 - n) w^(1 + n)) , r^(1 - n)  w^(1 + n), 1  }
or
{ CommonFactor->(1 + r^2)^(-1/2 + n/2) (1 + w^2)^(-1/2 - n/2)   x12^-n ,
{ (1 + r^(1 - n) w^(1 + n)) , r^(1 - n)  w^(1 + n), 1  }}


There are some ways (Extract common factor from vector or matrix) to factor out common terms using PolynomialGCD, which works in most of the cases, however, in this case, it does not do it properly (probably due to the unknown n?) Using their approach I get

 {"CommonFactor" ->   r^(-2 n) (1 + r^2)^(-(1/2) + n/2) (1 + w^2)^(-1 - n) x12^(-2n),
{r^(2 n) (1 + r^2)^(1/2 + 1/2 (-1 + n) - n/2) (1 + w^2)^(1/2 + n/2) (1 + r^(1 - n) w^(1 + n)) x12^n,
r^(1 + n) w^(1 + n) (1 + w^2)^(1/2 + n/2) x12^n,
r^(2 n) (1 + w^2)^(1/2 + n/2) x12^n}}


which is not the wanted behavior. Assuming[0<n<1 also does not help.

Is there any better way?

{#, list/#} &@Fold[Intersection, list]


$$\left\{\left(r^2+1\right)^{\frac{n}{2}-\frac{1}{2}} \left(w^2+1\right)^{-\frac{n}{2}-\frac{1}{2}} \text{x12}^{-n},\left\{r^{1-n} w^{n+1}+1,r^{1-n} w^{n+1},1\right\}\right\}$$

EDIT a possible failsafe strategy where the Intersection will likely not work

Let's say the original list is now called alist:

alist = {(1 + r^2)^(-1/2 + n/2) (1 + w^2)^(-1/2 - n/2) (1 +
r^(1 - n) w^(1 + n)) x12^-n,
r^(1 - n) (1 + r^2)^(-1/2 + n/2) w^(1 + n) (1 + w^2)^(-1/2 -
n/2) x12^-n, (1 + r^2)^(-1/2 + n/2) (1 + w^2)^(-1/2 -
n/2) x12^-n}


and another list is called blist:

blist = {a^-n (1 + a^n b), b, a^-n}


Then

{Head /@ alist, Head /@ blist}

{{Times, Times, Times}, {Times, Symbol, Power}}


A possible strategy could be to check the Head in advance and choose 1 or another method instead of the Intersection. As an example:

If[! SameQ @@ (Head /@ #), 1, Fold[Intersection, #]] & /@ {alist,
blist}


{(1 + r^2)^(-(1/2) + n/2) (1 + w^2)^(-(1/2) - n/2) x12^-n, 1}

• Great answer. Even shorter Intersection @@ list ;) May 6, 2022 at 18:34
• Works in the OP's case but not in general, neither with list = Expand[list] nor list = {r x + r^(2 - n) y, r (x + 2 y)}. May 6, 2022 at 19:18
• I think this would be too much to ask to Mathematica. I was expecting to use Assuming n>1 etc. Probably one has to make the expressions in a particular form (according to the requirement) and then apply Intersection. I find both a combination of Intersection & PolynomialGCD useful for some of my other cases similar to the one in the question. May 7, 2022 at 7:10
• @Boogeyman I looked at TreeForm and realized that parts of the tree were identical. Once you expand this expression, the peculiarities of internal FullForm representations would prohibit such a filtering in general as a structural intersection is being calculated, not a computational one.
– Syed
May 7, 2022 at 7:20
• Please see update for a possible failsafe mechanism. Do try this with the PolynomialGCD and see if it helps.
– Syed
May 13, 2022 at 11:10