3

I'd like to factor polynomials over the complex field. For example, how do I factor x^2+1 over $\mathbb{C}$?

Factor[x^2+1] and Factor[x^2+1, Modulus->Complexes] didn't work.

flinty
  • 25,147
  • 2
  • 20
  • 86
  • 2
    Factor's Modulus option is for integer modulus and finite fields. You could use GaussianIntegers -> True or Extension -> I for the Gaussian integers a+bi where a,b are integers - see here. But that's not $\mathbb{C}$. Factoring polynomials over $\mathbb{C}$ is just a matter of finding all the complex roots with Solve. – flinty Sep 26 '21 at 17:22
  • 1
    ^ i.e Times @@ ((x - #) & /@ (x /. Solve[x^2 + 1 == 0, x])) gives (-I + x) (I + x) – flinty Sep 26 '21 at 17:25
  • I'd like to add an example: Factor[x^2 + 2, Extension -> {I, Sqrt[2]}] produces (Sqrt[2] - I x)* (Sqrt[2] + I x). – user64494 Sep 26 '21 at 17:25
  • Also Solve[x^2 + 1 == 0, x] /. {Rule -> Subtract, List -> Times} and Roots[x^2 + 1 == 0, x] /. {Equal -> Subtract, Or -> Times}. It's surprising to me WRI hasn't written a function to factor over the Reals and Complexes, considering the other sugary shortcuts they've provided. -- Thirdly, Product[x - Root[Function[x, #], k], {k, Exponent[#, x]}] &[x^2 + 1]. – Michael E2 Sep 26 '21 at 17:30
  • What does Extension -> I do? – cheeseboardqueen Sep 26 '21 at 17:32
  • 1
    @cheeseboardqueen read this about field extensions . Suppose you use Extension->Sqrt[2] then it factors over the field consisting of elements $a+b\sqrt2$ for integers $a,b$. For Extension->I that means we factorize over the Gaussian Integers which you can also enable with GaussianIntegers->True. – flinty Sep 26 '21 at 17:36
  • @flinty, I see thank you. It is strange that there is no simpler way of doing this yet. – cheeseboardqueen Sep 26 '21 at 17:40
  • 3

2 Answers2

1

Theoretically right, but sometimes sloooow:

factorComplete[poly_] := With[{var = Variables[poly]},
   Factor[
     poly,
     Extension -> ToNumberField[
        SolveValues[poly == 0, First@var],
        All][[1, 1]] (* extract generator *)
     ] /; 
    Length@var == 1 && PolynomialQ[poly, x]
   ];
factorComplete[x^2 + 1]

(* (-I + x) (I + x) *)

Faster:

factorComplete2[poly_] := With[{var = Variables[poly]},
   Module[{factors, coeff},
     factors = Solve[poly == 0, First@var];
     coeff = Coefficient[poly, First[var]^Length[factors]];
     coeff (Times @@ Flatten[factors /. Rule -> Subtract])
     ] /; Length@var == 1 && PolynomialQ[poly, x]
   ];
factorComplete2[x^5 + x + 1] // ToRadicals

$$\eqalign{ \left(x+\sqrt[3]{-1}\right) & \left(x-(-1)^{2/3}\right) \cr & \left(x-\frac{1-i \sqrt{3}}{3\ 2^{2/3} \sqrt[3]{25-3 \sqrt{69}}}-\frac{1}{6} \left(1+i \sqrt{3}\right) \sqrt[3]{\frac{1}{2} \left(25-3 \sqrt{69}\right)}-\frac{1}{3}\right) \cr & \left(x-\frac{1+i \sqrt{3}}{3\ 2^{2/3} \sqrt[3]{25-3 \sqrt{69}}}-\frac{1}{6} \left(1-i \sqrt{3}\right) \sqrt[3]{\frac{1}{2} \left(25-3 \sqrt{69}\right)}-\frac{1}{3}\right) \cr & \left(x+\frac{1}{3} \left(-1+\sqrt[3]{\frac{2}{25-3 \sqrt{69}}}+\sqrt[3]{\frac{1}{2} \left(25-3 \sqrt{69}\right)}\right)\right) \cr}$$

Michael E2
  • 235,386
  • 17
  • 334
  • 747
0

I just tried Factor[x^2 + 1, Extension -> Sqrt[-1]] and it worked!!

Edit: See comments below. It didn't work over $\mathbb{C}$.

  • Try it on other real polynomials. – Michael E2 Sep 26 '21 at 17:36
  • I this what it's doing there is that it's factoring over $\mathbb{Q}[i]$ and not $\mathbb{C}$ unfortunately because Factor[x^2 + 3, Extension -> Sqrt[-1]] did not factor. sigh – cheeseboardqueen Sep 26 '21 at 17:38
  • 2
    This won't work: Factor[x^3 + 1, Extension -> Sqrt[-1]] you'll get two factors $(1 + x)$ and $(1 - x + x^2)$ which is irreducible in the Gaussian integers (your field extension). Instead you should use Solve to find all the roots a,b,c,d.... and then express the factors as (x-a)(x-b)(x-c)(x-d).... etc to fully factor in $\mathbb{C}$. This has already been shown in the comments. – flinty Sep 26 '21 at 17:40
  • @flinty Is that what {Rule -> Subtract, List -> Times} and {Equal -> Subtract, Or -> Times} is doing then? To be more specific that "subtract" creates the difference, "list" lists the factors, and "times" is the function (multiplication)? – cheeseboardqueen Sep 26 '21 at 17:43
  • 1
    @cheeseboardqueen those are rules used in a replacement /.. It converts Solve's solutions of the form List[List[Rule[x,a],List[Rule[x,b]],...] into a product (x-a)(x-b)... or more fully Times[Subtract[x,a],Subtract[x,b],...] so yes that's answer finds the roots with Solve and then builds an expression of the fully $\mathbb{C}$-factored polynomial. – flinty Sep 26 '21 at 17:47
  • Thank you @flinty! + others! – cheeseboardqueen Sep 28 '21 at 17:47