# How to convert $a(x-p)^2+q$ to $a(x-\alpha+i\beta)(x-\alpha-i\beta)$ for any real $a$, $p$ and $q$ [duplicate]

I want to factorize any quadratic expressions into two complex-valued linear expressions.

My effort below

a := 1;(*needed*)
p := 2;(*needed*)
q := 3;(*needed*)
f[x_] := a (x - p)^2 + q;(*needed*)
AA := Coefficient[f[x], x^2];
BB := Coefficient[f[x], x];
CC := f[0];
DD = BB^2 - 4 AA CC;
EE = Times @@ (#[[1]] & /@
Select[FactorInteger[DD], Mod[#[[2]], 2] == 1 &])
Factor[f[x], Extension -> Sqrt[EE]] // TeXForm (*needed*)


produces $$\left(-i x+\sqrt{3}+2 i\right) \left(i x+\sqrt{3}-2 i\right)$$ rather than the expected $$\left(x-2+i\sqrt{3}\right) \left(x-2-i\sqrt{3}\right)$$.

# Question

How to convert $$a(x-p)^2+q$$ to $$a(x-\alpha+i\beta)(x-\alpha-i\beta)$$ for any real $$a$$, $$p$$ and $$q$$.

Note that $$a(x-\alpha+i\beta)(x-\alpha-i\beta)$$ must be rendered with the leading $$x$$ rather than $$a(-\alpha+i\beta+x)(-\alpha-i\beta+x)$$.

# Edit

The order is IMPORTANT: I need $$a(x-\alpha+i\beta)(x-\alpha-i\beta)$$

NOT $$a(-\alpha+i\beta+x)(-\alpha-i\beta+x)$$

because it will be piped to TeXForm!

• Why do you define a:=1? Jul 1 at 16:47
• Try this one Times @@ (x - y /. Solve[f[y] == 0, y]) Jul 1 at 17:06
• @yarchik: That'll work when a = 1 but not otherwise. Jul 1 at 17:13
• Can't the TOTAL question be: "How do I convert $(i x + a)(i x + b)$ to $(x + i c)(x + i d)$"? That's it. Done. Once you have that answer you can plug in your particular a, etc. Moreover, you won't get mixed up by solutions that happen to work with $a=1$ but not for $a \neq 1$. Jul 1 at 19:51
• Maybe I've misunderstood you, but if you're going to pipe the result to TeXForm then it doesn't seem like the position of x should matter. Both (x+1)(x+2) and (1+x)(2+x) yield $(x+1) (x+2)$ when piped to TeXForm. Jul 2 at 19:14

This is inelegant, but it'll work. Basically we look at the coefficient of x in each factor, divide each factor by that coefficient, multiply all the x coefficients together as a leading coefficient for the factorization, and then multiply it all back together.

Use your code as above, then:

factors = FactorList[f[x], Extension -> Sqrt[EE]]
coeffs = Coefficient[#, x] & /@ Drop[factors[[All, 1]], 1]
factors[[2 ;; All, 1]] = Expand[factors[[2 ;; All, 1]]/coeffs]
factors[[1, 1]] = Times @@ coeffs
result = Times @@ Apply[Power, factors, 1]

(* (-2 - I Sqrt[3] + x) (-2 + I Sqrt[3] + x) *)


EDIT:

Note that when piped to TeXForm, Mathematica puts the terms in the factor in a more "traditional" order:

result // TeXForm


$$\left(x-i \sqrt{3}-2\right) \left(x+i \sqrt{3}-2\right)$$

So unless I have misunderstood the goal, we do not need to worry about rearranging the terms in result.

sol = SolveAlways[(I*x + a) (I*x + b) == e (x + I*c) (x + I*d), x]
(*    {{a -> -c, b -> -d, e -> -1}, {a -> -d, b -> -c, e -> -1}}    *)

e (x + I*c) (x + I*d) /. sol
(*    {-(I c + x) (I d + x), -(I c + x) (I d + x)}    *)


Look at the coefficients of the expanded quadratics and solve:

cl1 = CoefficientList[(I*x + a) (I*x + b), x]
(* {a b,I a + I b,-1} *)
cl2 = CoefficientList[e (x + I*c) (x + I*d), x]
(* {-c d e, I c e + I d e, e} *)

sol = Solve[MapThread[Equal, {cl1, cl2}], {c, d, e}]
(* {{c -> -a, d -> -b, e-> -1},{c -> -b, d -> -a, e -> -1}} *)


substituting and inverse-substituting

((I*x + a) (I*x + b) /. {a -> I ai, b -> I bi}  // Factor)
/. {ai -> -I a, bi -> I b}
(*-((-I a + x) (I b + x))*)


works too

There are already several nice solutions published here. Let me put also my five cents. Let

expr = (-I x + a + b*I)*(I x + a - b*I)


be our expression.

I often use the function to take the desired factor out of the parentheses:

factor[expr_, fact_, funExpr_ : Expand, funFact_ : Identity] :=
Module[{a = fact, b = expr/fact},
funFact[Evaluate[a]]*funExpr[Evaluate[b]]]


I already published it with the description, for example, here

With this function one acts as follows:

MapAt[factor[#, I] &, expr, {{1}, {2}}]

(*  -((-I a + b - x) (-I a - b + x))  *)


Have fun!