# Factoring out common factors from a list / vector / matrix

Posted 10 years ago
14031 Views
|
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 a{x,y,z}Mathematica performs the scalar multiplication immediately:{a x, a y, a z}Similarly,2 IdentityMatrixgives{{2,0,0},{0,2,0},{0,0,2}}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[%]}]FactorTermsList[%,Z]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
Sort By:
Posted 4 years ago
 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.
Posted 4 years ago
 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:= a {x, y, 0}%.Table[Z^(k - 1), {k, Length[%]}]FactorTermsList[%, Z]Row[{Times @@ Most[%], CoefficientList[Last[%], Z]}]Out= {a x, a y, 0}Out= a x + a y ZOut= {1, a, x + y Z}Out= Row[{a, {x, y}}]
Posted 10 years ago
 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.