# How to take a derivative of a function with a real part taken

Consider

f[x_]=Re[x^3]


I now take the derivative of this

D[f[x],x]


and the result is

3 x^2 Re'[x^3]


which is nonsense. How can I implement the derivative in such a function?

• Is x real or not? Apr 4, 2019 at 9:34
• Is this what you want? f[x_] := ComplexExpand@Re[x^3]; D[f[x], x] Apr 4, 2019 at 9:36
• @J.M.isslightlypensive no, of course not Apr 4, 2019 at 9:44
• @Nasser, that assumes x is real. Apr 4, 2019 at 9:54
• The question to be asked, I think, is how are you defining the derivative. Is it $\lim_{h\rightarrow 0} [f(x+h)-f(x)]/h$, for $h$ real or $h$ complex? Apr 5, 2019 at 2:20

I assume that you want to calculate the partial derivative with respect to $$x$$, $$\partial f/\partial x$$.

Define an explicit complex form

x = a + I b;


where $$a=\text{Re}(x)=(x+x^*)/2$$, $$b=\text{Im}(x)=(x-x^*)/(2i)$$, and $$x^*$$ is the complex conjugate of $$x$$. Your function f is then

f[a_, b_] = ComplexExpand[Re[x^3]]


a^3 - 3 a b^2

The derivative is now

$$\frac{\partial f}{\partial x} = \frac{\partial f}{\partial a}\frac{\partial a}{\partial x} + \frac{\partial f}{\partial b}\frac{\partial b}{\partial x} = \frac12\frac{\partial f}{\partial a} + \frac{1}{2i}\frac{\partial f}{\partial b}$$

In Mathematica, the partial derivative $$\partial f/\partial x$$ is thus

D[f[a, b], a]/2 + D[f[a, b], b]/(2 I) // FullSimplify


3/2 (a + I b)^2

or, a bit simpler,

D[f[a, b], {{a, b}}].{1, -I}/2 // FullSimplify


3/2 (a + I b)^2

which you recognize as $$\frac32x^2$$. From complex calculus it is easy to see that this is the correct answer: with $$x^*$$ denoting the complex conjugate of $$x$$, we have $$\text{Re}(x^3)=[x^3+(x^*)^3]/2$$ and therefore the partial derivative with respect to $$x$$ is $$\frac{\partial\text{Re}(x^3)}{\partial x} = \frac{\partial}{\partial x}\frac{x^3+(x^*)^3}{2}=\frac32x^2$$

Perhaps the definition, assuming the derivative the partial derivative with respect to the real part (h is treated as real, here :

f[x_] = Re[x^3];
Limit[(f[x + h] - f[x])/h, h -> 0]
(*  -3 Im[x]^2 + 3 Re[x]^2  *)


You could use myComplexD function,which I repeat here (slightly modified):

ComplexD[expr_, z__] := With[
{
nc = NonConstants -> Union @ Cases[{z},
s_Symbol | Conjugate[s_Symbol] | {s_Symbol | Conjugate[s_Symbol], _} :> s
],
old = OptionValue[
SystemOptions[],
"DifferentiationOptions" -> "ExcludedFunctions"]
},

InternalWithLocalSettings[
With[{new = Join[old, {Abs, Conjugate}]},
SetSystemOptions["DifferentiationOptions"->"ExcludedFunctions" -> new]
];
Unprotect[Conjugate, Abs];
Conjugate /: D[w_, Conjugate[w_], nc] := 0;
Conjugate /: D[Conjugate[f_], w_, nc] := Conjugate[D[f, Conjugate[w], nc]];
Abs /: D[Abs[f_], w_, nc] := D[Conjugate[f]f, w, nc]/(2 Abs[f]),

D[expr, z, nc],

SetSystemOptions["DifferentiationOptions" -> "ExcludedFunctions" -> old];
Conjugate /: D[w_, Conjugate[w_], nc] =.;
Conjugate /: D[Conjugate[f_], w_, nc] =.;
Abs /: D[Abs[f_], w_, nc] =.;
Protect[Conjugate, Abs];
]
]


ComplexD[(x^3 + Conjugate[x^3])/2, x]


(3 x^2)/2

It is also possible to use ComplexD to find the derivative with respect to the real part, as in Michael's answer:

expr = (x^3 + Conjugate[x^3])/2;

res = ComplexD[expr, x] + ComplexD[expr, Conjugate[x]]


(3 x^2)/2 + (3 Conjugate[x]^2)/2

ComplexExpand can be used to transform the above expression to the one given in Michael's answer:

ComplexExpand[res, x, TargetFunctions->{Re, Im}]
`

-3 Im[x]^2 + 3 Re[x]^2