# Factoring terms out of a polynomial

I don't do much algebra in Mathematica and was surprised to discover, while attempting to answer this question, that I had no idea how to factor out an expression from a polynomial.

The question was, given

τ = (1 + Sqrt[5])/2 (* golden ratio *)


and

coords = {{0,0,1},{0,1/2 (1+Sqrt[5]),-1},{1/2 (1+Sqrt[5]),1/2 (-1-Sqrt[5]),1/2 (1+Sqrt[5])},
{1/2 (-1-Sqrt[5]),0,1/2 (1+Sqrt[5])},{1/2 (1+Sqrt[5]),1,1/2 (-1-Sqrt[5])},{1/2 (-1-Sqrt[5]),
1/2 (3+Sqrt[5]),1/2 (-1-Sqrt[5])},{1/2 (3+Sqrt[5]),-1,0},{1,1/2 (-3-Sqrt[5]),
1/2 (3+Sqrt[5])},{1/2 (-3-Sqrt[5]),1/2 (1+Sqrt[5]),0},{-1,1/2 (-1-Sqrt[5]),1/2 (3+Sqrt[5])},
{1,1/2 (1+Sqrt[5]),1/2 (-3-Sqrt[5])},{-1,1/2 (3+Sqrt[5]),1/2 (-3-Sqrt[5])},{1/2 (3+Sqrt[5]),
1/2 (-1-Sqrt[5]),0},{1/2 (1+Sqrt[5]),1/2 (-3-Sqrt[5]),1/2 (1+Sqrt[5])},
{1/2 (-3-Sqrt[5]),1,0},{1/2 (-1-Sqrt[5]),-1,1/2 (1+Sqrt[5])},{1/2 (1+Sqrt[5]),
0,1/2 (-1-Sqrt[5])},{1/2 (-1-Sqrt[5]),1/2 (1+Sqrt[5]),1/2 (-1-Sqrt[5])},
{0,1/2 (-1-Sqrt[5]),1},{0,0,-1}}


how can coords be expressed in terms of τ.

I came up with

Map[
If[
AtomQ@#,
#,
(Simplify[#/τ]*HoldForm@τ) /. {
τ -> HoldForm@τ,
-τ -> HoldForm@-τ}] &,
coords, {2}]


but obviously this is not a generally robust solution, and it is ugly.

I assume there must be better ways to do this. This overlaps somewhat with the original question, but I am hoping for a more comprehensive explanation than that question requires.

• In such cases it really matters how much would you like to force to factor out. The simplest solution, but not necessairly the general one, is coords /. (Sqrt[5]) -> (2 \[Tau] - 1) // Simplify. Remember to ClearAll[\[Tau]]earlier.
– Kuba
Commented Jun 26, 2014 at 6:14
• @Kuba That is quite clever Commented Jun 26, 2014 at 13:33
• Thanks, maybe not general but this method helped me couple of times. Notice that the result is quite different but it is equivalent, just a matter of nice GoldenRatio relations.
– Kuba
Commented Jun 26, 2014 at 13:35
• Clear \[Tau] first. Then this ugly hack works: Map[(a \[Tau]^b /. First@FullSimplify[Quiet@Solve[a GoldenRatio^b == # && (a == 1 || a == 0 || a == -1) && b >= 0 && b \[Element] Integers, {a, b}, Reals], b \[Element] Integers && b >= 0]) &, coords, {2}] Commented Jun 26, 2014 at 20:32

I have a very simple mind, so I'd approach it this way.

My idea is to never assign a value to the symbol representing the golden ratio (I'll use ϕ), but to just use rules.

Clear[ϕ];
rules = {(1 + Sqrt[5])/2 -> ϕ, (-1 - Sqrt[5])/2 -> -ϕ,
Simplify[(1 + Sqrt[5]) (-1 - Sqrt[5])/4] -> -ϕ^2,
Simplify[(1 + Sqrt[5])^2/4 -> ϕ^2]};

symbolicCoords = coords /. rules

{
{0, 0, 1}, {0, ϕ, -1}, {ϕ, -ϕ, ϕ}, {-ϕ, 0, ϕ}, {ϕ, 1, -ϕ}, {-ϕ, ϕ^2, -ϕ}, {ϕ^2, -1, 0},
{1, -ϕ^2, ϕ^2}, {-ϕ^2, ϕ, 0}, {-1, -ϕ, ϕ^2}, {1, ϕ, -ϕ^2}, {-1, ϕ^2, -ϕ^2}, {ϕ^2, -ϕ, 0},
{ϕ, -ϕ^2, ϕ}, {-ϕ^2, 1, 0}, {-ϕ, -1, ϕ}, {ϕ, 0, -ϕ}, {-ϕ, ϕ, -ϕ}, {0, -ϕ, 1}, {0, 0, -1}
}


Of course, if for some reason you later need to recover coords from symbolicCoords, you can always do

symbolicCoords /. ϕ -> (1 + Sqrt[5])/2 // Simplify


The recovery is verified by

(symbolicCoords /. ϕ -> (1 + Sqrt[5])/2 // Simplify)  == coords

True

• I was surprised that it was necessary to spell out positive and negative versions of the rule, as you have also done. Do you know if this is a design choice versus an unintentional limitation? Commented Jun 26, 2014 at 16:13
• @mfvonh. It's what came into my head. It may be overkill, but I was sure it would work. Try simplifying the rules. I don't have the time. Commented Jun 26, 2014 at 16:48