Message Boards Message Boards


Factoring out common factors from a list / vector / matrix

Posted 5 years ago
3 Replies
5 Total Likes
I'd like a way to pull out common factors from lists, basically a way to reverse the automatic mapping of multiplication across a list.  If you type

Mathematica performs the scalar multiplication immediately:
{a x, a y, a z}

2 IdentityMatrix[3]


But what if we want to factor back out the 2?  Is there a simple way to factor out common factors from lists, or lists of lists?  FactorTerms and FactorTermsList do something similar, but only for numerical factors of polynomials. 

I'm thinking of turning the list/vector/matrix into a polynomial, then FactorTermsList could be used to identify common factors, and then the list or matrix would have to be reconstructed.  Like this:
a {x, y, z}
% . Table[Z^(k-1),{k,Length[%]}]
Row[{Times @@ Most[%], CoefficientList[Last[%], Z]}]

I could wrap that in a function, preferably first generalizing it to handle matrices or arbitrary depth lists of lists.  But does anyone have a simpler idea?  Thanks!
3 Replies
Here's maybe a simpler form, but I don't know how general it is:

m = a {x, y, z};
gcd = PolynomialGCD @@ Flatten[m];
Row[{gcd, m/gcd}]

I also don't know if there might be a more direct/built-in way of doing it.

This simpler form also avoids a glitch of the first method: if the vector's last entry is zero, then the output vector is shorter than the input.

In[166]:= a {x, y, 0}

%.Table[Z^(k - 1), {k, Length[%]}]

FactorTermsList[%, Z]

Row[{Times @@ Most[%], CoefficientList[Last[%], Z]}]

Out[166]= {a x, a y, 0}

Out[167]= a x + a y Z

Out[168]= {1, a, x + y Z}

Out[169]= Row[{a, {x, y}}]

This may seem silly, but note that the second method can't handle the zero vector. I discovered this because I was applying the commands to all entries in table of vectors; most of them were nonzero vectors, but some of them were zero.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract