Message Boards Message Boards

3
|
16696 Views
|
3 Replies
|
5 Total Likes
View groups...
Share
Share this post:

Factoring out common factors from a list / vector / matrix

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!
POSTED BY: Ken Caviness
3 Replies

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 BY: Gary Kennedy

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}}]

POSTED BY: Gary Kennedy
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.
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract