# Strange behaviour of MMA in derivatives of some standard functions

There are some peculiar things to be discovered in derivatives of some standard functions in MMA:

Strange behaviour

Example 1: Abs

We have

Abs[0.1]

(* Out[72]= 0.1 *)

But

Abs'[0.1]

(* Out[73]= Derivative[1][Abs][0.1] *)

Same thing if we avoid writing the prime:

D[Abs[x], x] /. x -> 0.1

(* Out[110]= Derivative[1][Abs][0.1] *)

That is, the derivative of Abs[] has no numerical value. When plotted, there's nothing to be seen.

The derivative should of course be, or at least behave like, Sign[x] .

Example 2: Sign

Sign[0.1]

(* Out[78]= 1 *)

Sign'[0.1]

(* Out[79]= Derivative[1][Sign][0.1] *)

That is, the derivative of Sign[] has no numerical value. When plotted, there's nothing to be seen.

Example 3: Floor

Floor[0.1]

(* Out[80]= 0 *)

But which value has the derivative at 0 ?

First possibility:

Floor'[0]

(* Out[89]= Derivative[1][Floor][0] *)

No value.

Second possibility:

Floor[0.]

(* Out[90]= 0 *)

Hence the numerical value seems to be 0.

No, bad luck, wrong guess! Look at that

Third possibility:

N[Floor'[0]]

(* Out[93]= 36.2120995105236639865598801739718338716778459859 *)

Furthermore:

Table[{x, N[Floor'[x], 5]}, {x, 0, 1/2, 0.05}]

(* Out[96]= {{0., 36.212}, {0.05, 12.5946}, {0.1, -4.29432}, {0.15,
1.61679}, {0.2, -0.532708}, {0.25, 0.13901}, {0.3, -0.0263362}, {0.35,
0.00318987}, {0.4, -0.000184538}, {0.45, 0.}, {0.5, 0.}} *)

Strange ocillatory behaviour for a quantity which should be = 0 throughout. It seems to be defined via a Fourier series.

Remedy ?: do it yourself !

Finally, let's create the derivative by ourselves as it is originally defined:

floorPrime[x_] := Limit[(1/h) (Floor[x + h] - Floor [x]), h -> 0]

Plot[0.1 + floorPrime[x], {x, -1, 1}, PlotRange -> {0, 0.2}]

(* Picture snipped *)

absPrime[x_] := Limit[(1/h) (Abs[x + h] - Abs[x]), h -> 0]

Plot[absPrime[x], {x, -1, 1}, PlotRange -> {-2, 2}]

(* Picture snipped *)

Ok, everything fine.

But why has MMA such problems with its own standard operation ' (or D[]) in this class of functions? Please explain.

Edit 15.09.14

There has been quite a lot of discussion here but no answer. I gather that the answers to similar topics referenced in the comments here are considered sufficient. These are:

Derivative of real functions including Re and Im

Symbolic derivatives are being calculated numerically

Because these references are pretty comprehensive I don't know if my question has contributed anything new, and the surprise was only on my side.

Let me nevertheless add some further observations which show that in some cases the documentation points out Possible Issues. But this is not done consistently. In one case WolframAlpha gives the expected result which MMA has refused to give.

1a) Abs'[0.1] is not evaluated.

But WolframAlpha "knows better":

WolframAlpha["Abs'[0.1]"];

(* -> 1 *)

1b) Trying to tell Mathematica that x is not complex but real (in which case the derivative is well defined)

Assuming[x \[Element] Reals, D[Abs[x], x]]

(* -> Derivative[1][Abs][x] *)

doesn't work either.

1c) Abs Possible Issues says: No series can be formed from Abs for complex arguments:

Series[Abs[x], {x, 0, 2}]

(* -> Abs[x] *)

For real arguments, a series can be found:

Series[Abs[x], {x, 0, 2}, Assumptions -> Element[x, Reals]]

$\begin{array}{ll} \{ & \begin{array}{ll} -x+O[x]^3 & x\leq 0 \\ x+O[x]^3 & \text{True} \\ \end{array} \\ \end{array}$

1d) UnitStep Possible Issues says: Differentiating Abs does not yield UnitStep:

D[Abs[t], t]

(* Derivative[1][Abs][t] *)

2) HeavisideTheta / DiracDelta Possible Issues say:

The functions UnitStep and HeavisideTheta are not mathematically equivalent:

{HeavisideTheta[x], UnitStep[x]}
Integrate[D[%, x], x]

(* -> {HeavisideTheta[x], UnitStep[x]} *)

Only HeavisideTheta gives DiracDelta after Differentiation.

{HeavisideTheta[x], UnitStep[x], (Sqrt[x^2]/x + 1)/2, (Abs[x]/x + 1)/2};

D[%, x] // Together

$\left\{\text{DiracDelta}[x], \begin{array}{ll} \{ & \begin{array}{ll} \text{Indeterminate} & x==0 \\ 0 & \text{True} \\ \end{array} \\ \end{array} ,0,\frac{-\text{Abs}[x]+x \text{Abs}'[x]}{2 x^2}\right\}$

3) Conjugate Possible Issues says: Differentiating Conjugate is not possible:

D[Conjugate[t], t]
(* Derivative[1][Conjugate][t] *)

A similar remark should be placed in the documentation under Possible Issues consistently.

4) Sign' behaves similar as Abs'

Sign'[0.1]

(* Derivative[1][Sign][0.1] *)

WolframAlpha["Sign[0.1]"];
(* -> 1 *)

Regards, Wolfgang

• The functions you explore are all non-analytic as complex functions, thus the derivative is undefined. You might explore the numerical derivative ND as defined the NumericalCalculus package. – Mark McClure Sep 12 '14 at 8:44
• @Marc: I don't agree. The derivative of these functions as real functions is well defined except for certain points. I have shown this in the section Remedy? But MMA has difficulties also outside these points. – Dr. Wolfgang Hintze Sep 12 '14 at 8:51
• It really doesn't matter if you agree or not - the basic fact is that D works in the complex domain and these functions are not differentiable in that context. That, quite simply, is the explanation of the behavior you see. Now, whether you would prefer different behavior and how you might implement it is a different question. – Mark McClure Sep 12 '14 at 9:05
• A similar thing happens with Re and Im. While those functions are obviously meant to work in the complex realm, I think an understanding of what is going on there is relevant. This discussion might help in that regard. – Mark McClure Sep 12 '14 at 9:09
• I tried to figure out what the question is about, but I honestly can't think of any way to answer here. – Jens Sep 15 '14 at 22:34