5

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?

BabaYaga
  • 1,836
  • 9
  • 19

1 Answers1

7
{#, 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}

Syed
  • 52,495
  • 4
  • 30
  • 85
  • 1
    Great answer. Even shorter Intersection @@ list ;) – Ben Izd May 06 '22 at 18:34
  • 2
    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)}. – Michael E2 May 06 '22 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. – BabaYaga May 07 '22 at 07:10
  • 1
    @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 07 '22 at 07:20
  • @Syed, Is it possible to apply a failsafe to your method? For example {a^-n (1+a^n b),b,a^-n} is one such example where Intersection fails, but PolynomialGCD works as expected. But in the Intersection way, although it can not find the common factor, it tries anyway to write the intersection. One would expect to return in such cases just a common factor 1 instead of errors and symbolical form of the common factor with intersection of terms. – BabaYaga May 13 '22 at 10:48
  • 1
    Please see update for a possible failsafe mechanism. Do try this with the PolynomialGCD and see if it helps. – Syed May 13 '22 at 11:10
  • Also useful to put || (AllTrue[list,Head[#] === Symbol &]) in the condition if list={a,b,c} which is also a problem for Intersection. – BabaYaga May 13 '22 at 15:41