3
$\begingroup$

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.

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

1 Answer 1

8
$\begingroup$

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).

$\endgroup$
4
  • 1
    $\begingroup$ "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) $\endgroup$ Jun 29, 2015 at 22:36
  • 1
    $\begingroup$ @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. $\endgroup$ Jun 29, 2015 at 22:45
  • $\begingroup$ @SimonRochester Hmmm... interesting. I'd never heard "jury-rig." Thanks. $\endgroup$ Jun 29, 2015 at 22:55
  • $\begingroup$ @DavidG.Stork I wasn't sure either...I had to look it up. $\endgroup$ Jun 29, 2015 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.