0

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!

Display Name
  • 2,009
  • 8
  • 17
  • Why do you define a:=1? – David G. Stork Jul 01 '21 at 16:47
  • 1
    Try this one Times @@ (x - y /. Solve[f[y] == 0, y]) – yarchik Jul 01 '21 at 17:06
  • @yarchik: That'll work when a = 1 but not otherwise. – Michael Seifert Jul 01 '21 at 17:13
  • @yarchik: (though it would be easy to fix by multiplying by a at the end.) – Michael Seifert Jul 01 '21 at 17:25
  • @LetMeBeYourLostDisciple: What would have happened if instead of defining a (and p and q), you simply wrote f[x_]:= a (x - p)^2 + q, to take one example? – David G. Stork Jul 01 '21 at 17:30
  • @LetMeBeYourLostDisciple: Recommendation: Make your problem as minimal as possible, as you'll get more help. "sake of my convenience" is precisely the PROBLEM! It is fine if you then take an answer and have to "change them back." You're by no means the worst, but some OPs lazily post tons of code (for their convenience) when the problem involves just one line or one step. Sure... it makes it easier for the OP, but much harder for the solvers. Couldn't all your question be reduced to your second-to last sentence? Well? And all that TeXForm is definitely NOT needed. See why? – David G. Stork Jul 01 '21 at 19:42
  • 1
    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$. – David G. Stork Jul 01 '21 at 19:51
  • Times @@ Subtract @@@ Flatten @ Solve[f[x] == 0, x] (see also Daniel Lichtblau’s answer to the linked duplicate). – Michael E2 Jul 02 '21 at 13:26
  • 1
    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. – Michael Seifert Jul 02 '21 at 19:14
  • Plus is Orderless. You don't get to pick the position of x. Perhaps you want something like TraditionalForm, but like TeXForm, it formats output display, not the expression itself. – Michael E2 Jul 02 '21 at 21:10
  • Do you mean something like a (Times @@ Subtract @@@ Expand@Flatten@Solve[f[x] == 0, x])?: image of full code & output – Michael E2 Jul 03 '21 at 04:33
  • @MichaelE2: Thank you for your very useful solution! – Display Name Jul 03 '21 at 05:31

5 Answers5

3
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)}    *)
Roman
  • 47,322
  • 2
  • 55
  • 121
1

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.

Michael Seifert
  • 15,208
  • 31
  • 68
1

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}} *)

JimB
  • 41,653
  • 3
  • 48
  • 106
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

Ulrich Neumann
  • 53,729
  • 2
  • 23
  • 55
1

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!

Alexei Boulbitch
  • 39,397
  • 2
  • 47
  • 96