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 IdentityMatrix[3]
gives
{{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!