It has been mentioned before (for example, see this answer) that Abs
in Mathematica is defined for complex numbers. Since Abs
is not holomorphic over the complex numbers, its derivative is not well-defined. One way to see this is:
FullSimplify[Abs[z] == Sqrt[z Conjugate[z]]]
True
Here are a couple more ways to achieve what you want (besides those mentioned by @roman).
- Use
Sqrt[z^2]
instead of Abs[z]
:
D[Sqrt[z^2], z]
z/Sqrt[z^2]
- Use complex (Wirtinger) derivatives. Summarizing:
$$\frac{\partial}{\partial z} = \frac{1}{2} \left(\frac{\partial}{\partial x} - i \frac{\partial}{\partial y}\right), \quad\frac{\partial}{\partial \bar{z}} = \frac{1}{2} \left(\frac{\partial}{\partial x} + i \frac{\partial}{\partial y}\right)$$
This means that the "real" derivative, i.e., the derivative with respect to $x$ only, is given by:
$$\frac{\partial}{\partial x} = \frac{\partial}{\partial z} + \frac{\partial}{\partial \bar{z}}$$
Hence the "real" derivative of Abs[z]
is:
$$\frac{\partial}{\partial x}\left| z \right| = \frac{\partial}{\partial x} \sqrt{z \bar{z}} = \frac{z + \bar{z}}{2 \left| z \right|} = \frac{\Re{z}}{\left| z \right|}$$
which agrees with the previous answer when $z$ is real.
However, this exposition is not useful without an actual implementation of Wirtinger derivatives! On the other hand, in this answer I give such an implementation, and the function is called ComplexD
(I provide its definition at the end of this answer). So:
ComplexD[Abs[z], z] + ComplexD[Abs[z], Conjugate[z]]
% //FullSimplify
z/(2 Abs[z]) + Conjugate[z]/(2 Abs[z])
Re[z]/Abs[z]
in agreement with the above results. It would be possible to define a RealD
function:
RealD[expr_, z_] := ComplexD[expr, z] + ComplexD[expr, Conjugate[z]]
if one were so inclined. Here is the definition of ComplexD
(slightly edited from the original):
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];
]
]
FullSimplify[D[Abs[x - 1], x], x \[Element] Reals && x != 0]
Resolve[ForAll[x, x \[Element] Reals && x != 0, Sign[-1 + x] == Piecewise[{{1, x - 1 > 0}, {-1, x - 1 < 0}}]], Reals]
$\endgroup$/. Sign[z_ - 1] :> (z - 1)/Abs[z - 1] // TraditionalForm
or investigateComplexityFunction
andTransformationFunctions
for simplify. Bottom line, the form presented is what Mathematica deems the "simplest". If you need/want to transform it, you need to tell it so. $\endgroup$Abs[x-1]
is in fact not differentiable in the classical sense atx=1
. Sox-1/|x-1|
would only be the right answer forx != 1
. $\endgroup$