# How to factor a polynomial expression in MATHEMATICA?

I want to factorize the following polynomial in MATHEMATICA: $$1 - 2 r + r^2 - 2 s + 2 r s + s^2 - 2 t + 2 r t + 2 s t - 4 r s t + t^2$$.

1 - 2 r + r^2 - 2 s + 2 r s + s^2 - 2 t + 2 r t + 2 s t - 4 r s t + t^2


If done by hand it is easy to see that the above expression can be written in the form of (a+b)(a-b) as: $$(-1 + r + s + t)^2 - (2 \sqrt{r s t})^2$$.

I tried using the "Factor" command in MATHEMATICA but it doesn't help, so if there is any command or any code that can help me with the same is appreciated.

Edit: This specific factorization is required as I want the degree of each factored part to be 1. And also if there is any general algorithm so as to obtain factorization for such polynomials in $$(a-b)(a+b)$$ form so that it can be used with other such polynomials too.

• It's an odd choice of factorization, but Factor[poly + 4 r s t] - 4 r s t works. There's also a polynomial in r, FullSimplify[poly] giving r^2 + (-1 + s + t)^2 + 2 r (-1 + s + t - 2 s t) Jul 13 '20 at 12:40
• Thanks. Yes that works but what if I have some other polynomial which could be written in the form of (a-b)(a+b), is there a general algorithm for that.( sorry I have to make that clear in the edit after you gave the solution) Jul 13 '20 at 12:51
• If you can write it as (a-b)(a+b) and Factor isn't working as you'd like, then you can always try to solve: Solve[(a - b) (a + b) == poly, {a, b}] Jul 13 '20 at 12:53
• Solve[1-2 r+r^2-2 s+2 r s+s^2-2 t+2 r t+2 s t-4 r s t+t^2==0,{r}]/.{Rule->Subtract,List->Times} Jul 13 '20 at 12:58
• Thanks, I tried doing that but it returns just two values of $b$ and doesn't return anything for $a$. Jul 13 '20 at 12:58

Perhaps this is what you can use in this case

ex = 1-2r+r^2-2s+2r s+s^2-2t+2r t+2s t-4r s t+t^2;
(ex/.r s t->u^2//Factor)/.u->Sqrt[r s t]


which returns

(-1 + r + s + t - 2*Sqrt[r*s*t])*(-1 + r + s + t + 2*Sqrt[r*s*t]


I don't think this can be generalized. Any expression with more than one term can be written as $$a-b = (\sqrt{a})^2 - (\sqrt{b})^2= (\sqrt{a}-\sqrt{b})(\sqrt{a}+\sqrt{b})$$ but the choice of $$a$$ and $$b$$ is arbitrary but also with the proper choice of signs of the square roots.

• For real a, Sqrt[a^2] == Abs[a] so the arbitrary a and b must each be nonnegative. Jul 13 '20 at 15:07

Another way assuming $$r\ge 0,s\ge 0,t\ge 0$$.

expr = 1 - 2 r + r^2 - 2 s + 2 r s + s^2 - 2 t + 2 r t + 2 s t - 4 r s t + t^2;
EXPR = expr /. {s -> S^2 , r -> R^2, t -> T^2} // Factor
EXPR /.{S -> Sqrt[s], R -> Sqrt[r], T -> Sqrt[t]}

• I tried that and it worked fine. But I tried to use the similar idea for one more polynomial I had $r^2 - 2 r s + s^2 - 2 r t - 2 s t + 4 r s t + t^2$ and the Factor command didn't work. Is there anything special I need to take care of while using this command ? Jul 15 '20 at 5:24
• Solving tor $t$ we obtain $\left(-2 r s-2 \sqrt{(r-1) r (s-1) s}+r+s-t\right) \left(-2 r s+2 \sqrt{(r-1) r (s-1) s}+r+s-t\right)$ Jul 15 '20 at 8:47