In this challenge, you are given a polynomial \$p(x)\$, and you need to find a polynomial \$q(x)\$ whose roots are exactly the squares of the roots of \$p(x)\$ (counted with multiplicity). In other words, if \$p(x) = \prod_{i=1}^n (x - r_i)\$, then \$q(x) = \prod_{i=1}^n (x - r_i^2)\$.
For example, if \$p(x) = x^3 - 3 x^2 + 4 = (x + 1)(x - 2)^2\$, then the roots of \$p(x)\$ are \$-1, 2, 2\$. So the roots of \$q(x)\$ are \$1, 4, 4\$, and thus \$q(x) = (x - 1)(x - 4)^2 = x^3 - 9 x^2 + 24 x - 16\$. Of course, \$2 (x - 1)(x - 4)^2 = 2 x^3 - 18 x^2 + 48 x - 32\$ is also a valid answer, as it has the same roots with the same multiplicities.
The input \$p(x)\$ is guaranteed to be a non-constant polynomial with integer coefficients, but some roots may be complex or repeated. Your output \$q(x)\$ does not need to have integer coefficients, but there is always a valid answer with integer coefficients.
You may input and output the polynomials in any reasonable format. For example, the polynomial \$x^4-4x^3+5x^2-2x\$ may be represented as:
- a list of coefficients, in descending order:
[1,-4,5,-2,0]
; - a list of coefficients, in ascending order:
[0,-2,5,-4,1]
; - a string representation of the polynomial, with a chosen variable, e.g.,
x
:"x^4-4*x^3+5*x^2-2*x"
; - a built-in polynomial object, e.g.,
x^4-4*x^3+5*x^2-2*x
in PARI/GP.
When you take input as a list of coefficients, the leading coefficient is guaranteed to be nonzero.
Representing a polynomial by its roots is not a reasonable format for this challenge, as it would make the task trivial.
This is code-golf, so the shortest code in bytes wins.
Test cases
The output is not unique. Your output may be a non-zero multiple of the expected output.
Here the polynomials are represented as lists of coefficients in decreasing order.
[1, 1] -> [1, -1] # x + 1 => x - 1
[1, 1, 1] -> [1, 1, 1] # x^2 + x + 1 => x^2 + x + 1
[1, -3, 4] -> [1, -1, 16] # x^2 - 3x + 4 => x^2 - x + 16
[1, 2, 3, 4] -> [1, 2, -7, -16] # x^3 + 2x^2 + 3x + 4 => x^3 + 2x^2 - 7x - 16
[1, 2, 1, 0] -> [1, -2, 1, 0] # x^3 + 2x^2 + x => x^3 - 2x^2 + x
[1, -4, 5, -2, 0] -> [1, -6, 9, -4, 0] # x^4 - 4x^3 + 5x^2 - 2x => x^4 - 6x^3 + 9x^2 - 4x
[1, 2, 3, 4, 5] -> [1, 2, 3, 14, 25] # x^4 + 2x^3 + 3x^2 + 4x + 5 => x^4 + 2x^3 + 3x^2 + 14x + 25