# Factoring the imaginary unit

Suppose you have this:

Collect[2 u + I + 2 + I,I]


It gives the same.

Is it possible to factor I in elegant way (not manually) to get following:

I(-2 I (u+1) + 2 )


Doing in Block environment does not help either:

Block[I=i,Collect[2 u + I + 2 + I,I]]


because here I is still global, I guess.

• What, exactly, you are trying to achieve? What output you are expecting to see? Jun 29, 2015 at 20:46
• VERY inelegant, but works: (I (j /. Solve[2 u + I + 2 + I == I j, j]))[[1]] Jun 29, 2015 at 21:01
• perhaps a crime against codemanity: factorI[expr_] := I Inactive[Plus] @@ (ComplexExpand[ReIm[expr]] {-I, 1})
– chuy
Jun 29, 2015 at 21:09

Your direct method isn't working because neither Complex nor Collect works the way you are expecting. So depending on what you mean by "elegant", the answer to your question may be no.

You can jury-rig Collect to work using

expr = 2 u + I + 2 + I;
Expand[expr/I] /. Complex[x_, y_] -> x + i y
I Collect[%, i, Simplify] /. i -> I

(* 2 - 2 i - 2 i u *)
(* I (2 - 2 I (1 + u)) *)


You can also get most of the way there using HornerForm:

Expand[i expr/I] /. Complex[x_, y_] -> x + i y
HornerForm[%] /. i -> I

(* (2 - 2 i) i - 2 i^2 u *)
(* I (2 + I (-2 - 2 u)) *)


A useful general technique is to transform your expression into one where your idea of the simplest form coincides with Mathematica's, Simplify, and then transform back:

expr/I /. u -> x - 1
I Simplify[%] /. x -> u + 1

(* -I ((2 + 2 I) + 2 (-1 + x)) *)
(* I (2 - 2 I (1 + u)) *)


Also, I actually like chuy's "criminal" method from the comments. You can Simplify each term to get the form you were looking for:

factorI[expr_] := I Plus @@ Simplify[ComplexExpand[ReIm[expr]] {-I, 1}]
factorI[expr]

(* I (2 - 2 I (1 + u)) *)


(I removed the Inactive, assuming you don't care too much which term goes first).

• "Jerry-rig" (To fix an object (usually mechanical) to a working condition in a haphazard way. Also known as doing a MacGyver on it. This can apply to any non working thing, to fix it in a nonconventional way. This term was created during WW2, in reference to the Germans who were referred to as "Jerries" as slang. Allies often came across hastily repaired objects left by the Germans hence the term Jerry-Rig came to be urbandictionary.com/define.php?term=jerry-rig) Jun 29, 2015 at 22:36
• @DavidG.Stork Actually, apparently "Jury-rig" is the original term, dating to 1788: english.stackexchange.com/a/132919. It was a sailing term, referring to a mast built for temporary use. Jun 29, 2015 at 22:45
• @SimonRochester Hmmm... interesting. I'd never heard "jury-rig." Thanks. Jun 29, 2015 at 22:55
• @DavidG.Stork I wasn't sure either...I had to look it up. Jun 29, 2015 at 22:57