# Factor out constant terms with square roots

I would like to use FactorTerms to factor out constant numerical terms out of an expression, this works as follows:

2 x^2 + 2 y^2 + 4 x y // FactorTerms
(* = 2 (x^2 + 2 x y + y^2) *)


However, this doesn't seem to work if there are square roots:

Sqrt[2] x^2 + Sqrt[2] y^2 + 2 Sqrt[2] x y // FactorTerms
(* = Sqrt[2] x^2 + 2 Sqrt[2] x y + Sqrt[2] y^2 *)


I'm not sure whether that's the way it's supposed to behave or a bug. If that's the desired behaviour, then what is the correct way of factoring out constant numerical terms containing square roots?

\$Version gives 11.2.0 for Mac OS X x86 (64-bit) (September 11, 2017).

It seems it works when you specify the variables in your term:

FactorTerms[Sqrt[2] x^2 + Sqrt[2] y^2 + 2 Sqrt[2] x y, {x, y}]
(* Sqrt[2] (x^2 + 2 x y + y^2) *)


To include JM's answer to your comment: If you have several variables and don't want to type them by hand, you can use

FactorTerms[poly, Variables[poly]]


to use them automatically.

• This works indeed, thanks! Even though it can be quite clumsy for a large number of variables. The description in the documentation does sound like it should do it also without specifying the variables explicitly. – Stan Mar 5 '18 at 11:29
• @Stan, in that case, you could do FactorTerms[poly, Variables[poly]] so that you don't have to bother with manually getting the variables. – J. M. is in limbo Mar 5 '18 at 11:54
Factor[Sqrt[2] x^2 + Sqrt[2] y^2 + 2 Sqrt[2] x y ]
(* Sqrt[2] (x + y)^2*)


works too!

• Hi Ulrich, thanks for the answer, but I explicitly do not want that x^2 + 2 x y + y^2 is transformed to (x + y)^2. I only want to factor out constant numerical terms without performing other possible factorisations. – Stan Mar 5 '18 at 11:37