I trying to modify the behaviour of the built-in Conjugate[]
operator on a particular function I have defined, to take into account that some of its variables are real.
ClearAll[f];
f /: Conjugate[f[k_]] := Conjugate[F[r]] Exp[I k r]
f[k_] := F[r] Exp[-I k r]
The problem with using UpValues
in this way is that the DownValues
for f[k]
are evaluated before, resulting in:
Conjugate[f[k]]=Exp[I Conjugate[k r]] Conjugate[F[r]]
Using non-standard evaluation seems to do the trick
Conjugate[Unevaluated[f[k]]]=Exp[I k r] Conjugate[F[r]]
However, I want to use my function inside expressions like
f[k1] + f[k2] + Conjugate[f[k3]]
without having to manually replace f[_]
by Unevaluated[f[_]]
.
Answer
One possibility would be to define a new Conjugate
function, myConjugate
, the behaves in the same way as Conjugate
, except when it encounters a phase of the type Exp[+(-)I k r]
, it transforms it to Exp[-(+)I k r]
, leaving k
and r
as real variables.
Another possibility (and the one I ended up using) is to go along the lines of this stack overflow answer and use UpValues
to explicitly define k
and r
as being real:
ClearAll[makeReal];
makeReal[a__Symbol] := (# /: Conjugate[#] := #) & /@ List[a]
makeReal[k, r]
Then one gets the expected
Conjugate[F[r] Exp[-I k r]]= Exp[I k r] Conjugate[F[r]]
Comments
Post a Comment