# Complex number operations: telling Mathematica variables are real

I want to do Conjugate[a + b*I], but when I do that, the solution is Conjugate[a] - I*Conjugate[b]; when for me, a and b are reals.

I want to obtain the following expresion : a-b*I

The same problem exists with the function Abs.

• – Mr.Wizard Aug 12 '12 at 7:14
• – Artes Aug 12 '12 at 8:32
• – Kuba Jan 19 '18 at 6:57
• – Kuba Jan 19 '18 at 7:03

Conjugate[a + b*I]//ComplexExpand


or

Refine[Conjugate[a + b*I], {a, b} \[Element] Reals]

• Note that the first method will tend to convert exponentials to trig functions, which can cause very undesirable behavior if you have many exponentials (e.g., multiplying Gaussians together). The second method generally works well, but can arguably be improved by using _Symbol ∈ Reals as the assumption instead of {a, b} ∈ Reals since the former allows the method to be bundled into a function for re-use without passing the list of variables manually. See my alternate answer. – Jess Riedel Sep 27 '17 at 18:44

The most complete and extendable answer is to define

Conj[x_] := Refine[Conjugate[x], _Symbol ∈ Reals];


Then we get

Conj[a + I b]


a - i b

as expected. It is also possible to generalize so that Conj takes a second argument telling it which variables to treat as complex, recovering similar abilities of ComplexExpand

Conj[x_, exclu_:{}] :=
Refine[Conjugate[x], _?((Head[#] == Symbol && ! MemberQ[exclu, #]) &) ∈ Reals];
Conj[a + I b, {b}]


a - i Conjugate[b]

### Issues with other methods

This question and related versions have been asked many times before: 1,2,3,4,5,6,7,8,9,10,11. Here's a round up.

The basic issue is that using Conjugate alone doesn't work because Mathematica doesn't know your variables are real:

Conjugate[a + I b]


Conjugate[a] - i Conjugate[b]

Rojo's suggestion to deploy ComplexExpand is very common advice and it works in the simplest case

ComplexExpand[Conjugate[a + I b]]


a - i b

but it has the undesirable behavior of converting some exponentials functions to trig functions.

ComplexExpand[Conjugate[Exp[a + I b]]]


$e^a$ Cos[b] - i $e^a$ Sin[b]

This becomes terrible when you have multiple exponentials.

One can use Refine and assume everything is real

Refine[Conjugate[a + I b], _ ∈ Reals]


a - i b

but then you can run into trouble when it starts assuming every expressions is real (not just variables):

Refine[Conjugate[Sqrt[I*a]], _ ∈ Reals]


$\sqrt{i a}$

Like Rojo's alternate answer, you can assume that only the symbols in your expression are real

Refine[Conjugate[a + I b, {a,b} ∈ Reals}]


Conjugate[$\sqrt{i a}$]

but this becomes quite unwieldy if you have many variables or if you make variable substitutions. (If you forget one variable in a large expression, you may not notice.) Furthermore, if you want to encapsulate this by defining a function like Conj, you need to pass the list of variables manually or scrape them from the input expression.

Avoiding this issue by making the reality assumption for all things with the Head of Symbol gives the answer above.

Also note that you can use Simplify in place of refine, but this will muck around by simplifying your expression,

Simplify[Conjugate[I(x^2 + 2 x y + y^2)], _Symbol ∈ Reals]


-i $($x - y$)^2$

when you may really have wanted -I(x^2 + 2 x y + y^2).

The simple hack of flipping the sign on i is too fragile to rely on. This works

(a + I b) /. {I -> -I}


a - i b

but this doesn't

Exp[-I] /. {I -> -I}


$e^{-i}$

because

FullForm[Exp[-I]]


Power[E,Complex[0,-1]]

Even if you try the rule {Complex[re_, im_] :> Complex[re, -im]}, there are issues with complex-valued functions like ArcSin.

• Thanks Carl, done. I left the output (yellow) as lowercase i since copying and pasting 3D expressions like $e^{-i}$ is fraught anyways, but if you want to edit it yourself to fix that, please feel free. – Jess Riedel Sep 27 '17 at 19:59

And for the absolute value of z use Norm command instead of Abs:

Refine[Norm[a + b*I], {a, b} ∈ Reals]

• Or: norm[z_] := Sqrt[ComplexExpand[z Conjugate[z]]] and then just norm[a + b I]. – murray Dec 6 '16 at 16:33