2
$\begingroup$

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?

$\endgroup$
5
  • $\begingroup$ Is x real or not? $\endgroup$ Apr 4, 2019 at 9:34
  • 1
    $\begingroup$ Is this what you want? f[x_] := ComplexExpand@Re[x^3]; D[f[x], x] $\endgroup$
    – Nasser
    Apr 4, 2019 at 9:36
  • $\begingroup$ @J.M.isslightlypensive no, of course not $\endgroup$ Apr 4, 2019 at 9:44
  • $\begingroup$ @Nasser, that assumes x is real. $\endgroup$ Apr 4, 2019 at 9:54
  • $\begingroup$ 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? $\endgroup$
    – Michael E2
    Apr 5, 2019 at 2:20

3 Answers 3

6
$\begingroup$

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

$\endgroup$
2
$\begingroup$

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  *)
$\endgroup$
2
$\begingroup$

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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.