12
$\begingroup$

Derivative of the Abs Function in Mathematica looks very different than the actual answer. Why? How to correct this?

For example I entered D[Abs[x - 1], x] and got the output Abs'[-1 + x]. I was hoping to get the output in this form: (x−1)/|x−1|.

Is there a syntax error in my input?

$\endgroup$
9
  • $\begingroup$ 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$ – ciao May 1 '14 at 7:29
  • 1
    $\begingroup$ How do I get the output in this form: (x−1)/|x−1| ? $\endgroup$ – user14056 May 1 '14 at 7:34
  • 1
    $\begingroup$ Throw a rule and traditionalform onto end of simplify, /. Sign[z_ - 1] :> (z - 1)/Abs[z - 1] // TraditionalForm or investigate ComplexityFunction and TransformationFunctions 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$ – ciao May 1 '14 at 7:41
  • 2
    $\begingroup$ You need ComplexExpand probably because you can't take the derivative of Abs in the complexes $\endgroup$ – Rojo May 1 '14 at 15:17
  • 1
    $\begingroup$ Well, Abs[x-1] is in fact not differentiable in the classical sense at x=1. So x-1/|x-1| would only be the right answer for x != 1. $\endgroup$ – Wizard May 1 '14 at 17:21
11
$\begingroup$

In version 11.1 the function RealAbs was introduced that give your desired result

D[RealAbs[x - 1], x]
RealAbs'[x - 1]

(*(-1 + x)/RealAbs[-1 + x]*)
(*(-1 + x)/RealAbs[-1 + x]*)

Alternatively, as suggested by @murray, you can use ComplexExpand

D[Abs[x - 1], x] // ComplexExpand // Together
(*(-1 + x)/Sqrt[(-1 + x)^2]*)

Or explicitly state that $x$ is real, as @Rojo said

D[Abs[x - 1], x] // FullSimplify[#, x \[Element] Reals] &

(*Sign[-1 + x]*)

However, FullSimplify changes the domain slightly because Sign[-1 + x] is defined at $x=1$

Sign[x - 1] /. x -> 1
(*0*)

RealAbs'[x - 1] /. x -> 1
(*Indeterminate*)
$\endgroup$
3
  • $\begingroup$ RealAbs'[x - 1]==Sign[x - 1] are equal.Check: Limit[{RealAbs'[x - 1], Sign[x - 1]}, x -> 1, Direction -> #] & /@ {-1, 1} $\endgroup$ – Mariusz Iwaniuk Jun 3 '18 at 17:17
  • $\begingroup$ @MariuszIwaniuk their limits are equal at x=1, but try this {FunctionDomain[RealAbs'[x - 1], x], FunctionDomain[Sign[x - 1], x]} $\endgroup$ – roman465 Jun 4 '18 at 1:54
  • $\begingroup$ {FunctionDomain[ Simplify[RealAbs'[x - 1], Assumptions -> {x \[Element] Reals}], x], FunctionDomain[Sign[x - 1], x]} ? $\endgroup$ – Mariusz Iwaniuk Jun 4 '18 at 9:14
6
$\begingroup$

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).

  1. Use Sqrt[z^2] instead of Abs[z]:

D[Sqrt[z^2], z]

z/Sqrt[z^2]

  1. 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];
    ]
]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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