I can't seem to find a function in Mathematica that factors a polynomial over the reals. Obviously, Factor doesn't work since it works over the integers, over $\mathbb{Z}_p$, or some algebraic fields extensions of the type $\mathbb{Q}(\alpha_1, \ldots, \alpha_n)$ where $\alpha_1, \ldots, \alpha_n$ are algebraic numbers. My question is general, although it arouse from my need to factor over $\mathbb{R}$ the polynomial $$ f(\lambda) = \lambda^4 + 17\lambda^2+12. $$ Of course, in this particular case I can find the roots and factor it myself.
Answer
One way is to find the roots, separate into real and complex, further separate the complex ones into conjugate pairs, then reform as a factorization (taking into account the leading coefficient). I'll illustrate with the given example.
poly = x^4 + 17 x^2 + 12;
roots = x /. NSolve[poly];
rroots = Select[rts, FreeQ[#, Complex] &]
croots = Complement[roots, rroots];
topquad = Select[croots, Im[#] > 0 &]
mult = Coefficient[poly, x, Exponent[poly, x]]
rfax = x - rroots
cfax = x^2 - 2*x*Re[topquad] + Re[topquad]^2 + Im[topquad]^2
(* Out[54]= {}
Out[56]= {0. + 0.859018 I, 0. + 4.03263 I}
Out[57]= 1
Out[58]= {}
Out[59]= {0.737913 + x^2, 16.2621 + x^2} *)
Putting together the result:
Apply[Times, Join[{mult}, rfax, cfax]]
(* Out[61]= (0.737913 + x^2) (16.2621 + x^2) *)
Comments
Post a Comment