2

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?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
PhoenixPerson
  • 553
  • 2
  • 10

3 Answers3

6

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 $$

Roman
  • 47,322
  • 2
  • 55
  • 121
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"]
    },

    Internal`WithLocalSettings[
        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];
    ]
]

For your example:

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

(3 x^2)/2

Addendum

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
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  *)
Michael E2
  • 235,386
  • 17
  • 334
  • 747