# Conjugating symbolic expression

I am trying to conjugate a symbolic expression, and I have explicitly stated the real terms. However, I simply can't get it to work:

Conjugate[
ComplexExpand[
I Cos[z] Sin[y] + Sin[z] +
A (Cos[z] - I Sin[y] Sin[z]), {z \[Element] Reals,
A \[Element] Reals, y \[Element] Reals}]]


What am I doing wrong here?

-

Just do the ComplexExpand after the Conjugate

ComplexExpand[Conjugate[I Cos[z] Sin[y] + Sin[z] + A (Cos[z] - I Sin[y] Sin[z])]]

(* A Cos[z] + Sin[z] - I (Cos[z] Sin[y] - A Sin[y] Sin[z]) *)

-

One straightforward way to take the conjugate of an expression is to replace I with -I

ComplexExpand[I Cos[z] Sin[y] + Sin[z] + A (Cos[z] - I Sin[y] Sin[z])] //. I -> (-I)

A Cos[z] + Sin[z] - I (Cos[z] Sin[y] - A Sin[y] Sin[z])


As Szabolcs points out in the comments, this solution can be problematic, so beware!

-
Unfortunately this is not a robust solution. It's a common but subtle error to try to do this. Try 1 + I /. I -> -I. It gives 1+I. The reason is that I does not in fact appear in 1+I anywhere. 1+I is an atomic object with FullForm Complex[1,1]. Note that the problem is not simply that it doesn't contain I, but that it's atomic, so 1+I /. 1 -> -1 also has no effect. –  Szabolcs Apr 9 at 22:19

The simple rule I to -I is not guaranteed to work, e.g.

Exp[3 I] /. I -> -I


and ComplexConjugate might be too slow (for lengthy expressions). Therefore, I rather define an alternative function to conjugate

ClearAll[AltConjugate]
AltConjugate[x_] := ReplaceAll[FullSimplify[x], Complex[a_, b_] -> Complex[a, -b]];


This functions looks for the pattern Complex[a_, b_] and replaces it by Complex[a, -b].

@celtschk - roots might be problematic, simple functions like f[x_]=Sqrt[-x^2] can be handle by simplifying the input function, i.e. adding FullSimplify in the definition of AltConjugate. Nevertheless, this will fail for functions including more general roots, such as f[x]=Sqrt[-x^2 +I b] where both x and b are reals.

Use this carefully and always test it.

Cheers.

-
An example where it doesn't work: Sqrt[-x^2]. ComplexExpand[Conjugate[Sqrt[-x^2]]] gives -I Sqrt[x^2], but ComplexExpand[AltConjugate[Sqrt[-x^2]]] gives I Sqrt[x^2]` –  celtschk Apr 13 at 12:09
Thanks @celtschk - Square roots are problematic, simplifying the initial expression solves a few simple cases, such the one you just describe. But, it fails for a more general function. –  fabio.hipolito Apr 14 at 3:12