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

• Aug 12, 2012 at 7:14
• Aug 12, 2012 at 8:32
• – Kuba
Jan 19, 2018 at 6:57
• – Kuba
Jan 19, 2018 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. Sep 27, 2017 at 18:44
• Refine doesn't seem to be recognized by Wolfram Alfa - any suggestions on how to defines variables as Real there? Thank you. Nov 11, 2021 at 20:36

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]

EDIT: Cheng Tao found an example with undesired behavior:

Conj[a Exp[I b] + c Exp[I d]]


Conjugate[a $$e^{i b}$$ + c $$e^{i d}$$]

This seems to be an issue with Conjugate not automatically distributing over addition. I have a patch to fix this behavior, but I suspect there is still an underlying pathology:

ConjNew[x_] := Refine[
Conjugate[x] //. {Conjugate[sum_Plus] :> Conjugate /@ sum},
_Symbol ∈ Reals];
ConjNew[a Exp[I b] + c Exp[I d]]


a $$e^{-i b}$$ + c $$e^{-i d}$$

(I could not find a way to use Distribute to fix this. Note also the issues with using Plus in a pattern.)

### 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. (Edit: Interestingly, you can occasionally prevent ComplexExpand from converting Exp's to Cos's and Sin's using the option TargetFunctions -> Conjugate. This does't help with a E^(I b), but it does help with a E^(I b)+c E^(I d). Note that TargetFunctions -> Exp is not an allowed option.)

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[2].

• This method does not seem to be working on Conj[t1 Exp[I k1] + t1 Exp[I k2]] ? Jan 26 at 12:05
• Good find. This seems to be an issue with Conjugate not automatically distributing over addition. (I'd have hoped that Refine would fix this, but it doesn't.) I hacked together a patch in the answer above (see the edit). I'd welcome a better understanding of this issue. Jan 26 at 19:30

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]. Dec 6, 2016 at 16:33

Consider using Simplify and Assumptions

Simplify[Conjugate[a + I*b], Assumptions -> {{a, b} \[Element] Reals}]

Result

a - I b