# Factor a bivariate polynomial treating one variable as constant

I have a polynomial in two variables, e.g.

16384 k^4 - 20480 k^2 + x^4 - 320 k^2 x^2 - 128 x^2 + 4096


I'd like Mathematica to factor it as $$(x^2 - f(k))(x^2 - g(k))$$ but Factor does nothing. I assume that's because it's treating $k$ on an equal footing with $x$, but I'd like it to consider $k$ as a constant and just factor with respect to $x$.

I'm sure this has been asked before, but all related-looking questions seemed to have much more complicated answers, including things like the Root function, the quartic formula, long Modules using Solve, or Groebner bases. Surely there's a simple, one-line solution for my problem?

• I guess it might be possible with Simplify and the option ComplexityFunction.
– ivbc
Jun 20, 2017 at 14:50
• poly2 = (x^2 - a) (x^2 - b) /. Solve[And @@ Thread[CoefficientList[poly, x] == CoefficientList[(x^2 - a) (x^2 - b), x]], {a, b}] Jun 20, 2017 at 18:02

Although there might be various approaches, it seems that proceeding the most obvious one should be good enough

We have

Collect[(x^2 - f[k]) (x^2 - g[k]) // Expand, x]

x^4 + x^2 (-f[k] - g[k]) + f[k] g[k]


on the other hand it should be identically equal to

Collect[4096 - 20480 k^2 + 16384 k^4 - 128 x^2 - 320 k^2 x^2 + x^4, x]

4096 - 20480 k^2 + 16384 k^4 + (-128 - 320 k^2) x^2 + x^4


Therefore equating appropriate coefficients in the both polynomials we get a simple system and solving it yields solutions

Solve[{-128 - 320 k^2 == -f[k] - g[k],
4096 - 20480 k^2 + 16384 k^4 == f[k] g[k]}, {f[k], g[k]}]

 {{f[k] -> 32 (2 + 5 k^2 - Sqrt[40 k^2 + 9 k^4]),
g[k] -> 32 (2 + 5 k^2 + Sqrt[k^2 (40 + 9 k^2)])},
{f[k] -> 32 (2 + 5 k^2 + Sqrt[40 k^2 + 9 k^4]),
g[k] -> 32 (2 + 5 k^2 - Sqrt[k^2 (40 + 9 k^2)])}}


Another, and a slightly simpler approach would be:

Solve[
PolynomialQuotientRemainder[
4096 - 20480 k^2 + 16384 k^4 - 128 x^2 - 320 k^2 x^2 + x^4,
x^2 - f[k], x] == {x^2 - g[k], 0},
{f[k], g[k]}]

• You could use CoefficientList and Thread to get the coefficients out, rather than copying the coefficients from each. Something like Solve[Thread[ CoefficientList[ 4096 - 20480 k^2 + 16384 k^4 - 128 x^2 - 320 k^2 x^2 + x^4, x] == CoefficientList[(x^2 - f[k]) (x^2 - g[k]), x]], {f[k], g[k]}] Jun 20, 2017 at 14:50
• However, this might be simpler Solve[PolynomialQuotientRemainder[ 4096 - 20480 k^2 + 16384 k^4 - 128 x^2 - 320 k^2 x^2 + x^4, x^2 - f[k], x] == {x^2 - g[k], 0}, {f[k], g[k]}] Jun 20, 2017 at 15:28

Surely there's a simple, one-line solution for my problem?

Of course.

SolveAlways[16384 k^4 - 20480 k^2 + x^4 - 320 k^2 x^2 - 128 x^2 + 4096 ==
(x^2 - f) (x^2 - g), x]
{{f -> 32 (2 + 5 k^2 - Sqrt[k^2 (40 + 9 k^2)]),
g -> 32 (2 + 5 k^2 + Sqrt[40 k^2 + 9 k^4])},
{f -> 32 (2 + 5 k^2 + Sqrt[k^2 (40 + 9 k^2)]),
g -> 32 (2 + 5 k^2 - Sqrt[40 k^2 + 9 k^4])}}