4

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

Dr. Wolfgang Hintze
  • 13,039
  • 17
  • 47
  • 3
    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 08: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 08:51
  • 4
    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 09: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 09:09
  • @Marc: I know perfectly well what analyticity of functions means. But I was talking about MMA here. Please tell me where your assertion about D can be found in the documentation. – Dr. Wolfgang Hintze Sep 12 '14 at 10:03
  • Well, I guess it's not just D but the general fact that symbolic computation assumes symbols are complex. Many computations performed by Mathematica must be fully understood in this context - from Simplify[Abs[x^2]] to the apparent missing branch of the cube root in Plot[x^(1/3), {x,-1,1}]. There are exceptions, particularly the CubeRoot and Surd functions introduced in V9 but, generally, computations are done in the complex numbers and I assure you that this is the context in which you need to explore your question. – Mark McClure Sep 12 '14 at 11:28
  • Incidentally, Derivative is not protected, so you can easily define your own DownValues: Thus, Abs'[x_] := Sign[x]; Abs'[0.5] produces 1. – Mark McClure Sep 12 '14 at 11:30
  • This issue has been visited before http://mathematica.stackexchange.com/q/29329/193 – Dr. belisarius Sep 12 '14 at 11:59
  • @belisarius Your question seems to be exactly opposite of the one here - namely, Plot[Derivative[f]...] uses a finite difference scheme on discrete functions, such as IntegerPart, Round and several others. – Mark McClure Sep 12 '14 at 12:33
  • @MarkMcClure I haven't marked it as dup. I wanted to leave a link here for those researching problems with derivatives of non-analytic functions in the future – Dr. belisarius Sep 12 '14 at 12:51
  • @MarkMcClure My view, perhaps wrong, is that it is not D per se but Abs, Floor, etc. that are defined as functions of a complex variable. That D takes the complex derivative follows naturally. @Dr. Hintze, I recall reading somewhere that Mathematica makes no restriction on what the variables represent unless explicitly stated. So numeric functions are functions of a complex variable, unless the documentation or an error message states that it has to be real or an integer or whatever. – Michael E2 Sep 13 '14 at 13:31
  • @Mark: Thanks for your comprehensive replies and suggestions. I hope it is obvious that - as a year long user - I was just surprised to get rather unexpected result when I do things which are completely "legal". i.e. without giving rise to warnings. This can lead to errors more of less hidden (think of having Floor somewhere in a Long expression of which you need to take the derivative, and then have some numerical calculation do to: the spurious oscillations spoil the result). – Dr. Wolfgang Hintze Sep 13 '14 at 13:45
  • @Michael E2: I'm a bit confused. I gather from your comments to Mark and me that you consider it permitted to calculate D[Abs[z],z]: you state Abs[] is defined as a function of a complex variable, and there's no hint in the documentation of something else, and also there's no error message appearing when being used. – Dr. Wolfgang Hintze Sep 13 '14 at 13:56
  • @Dr.WolfgangHintze, Yes, D[Abs[z], z] is permitted. I believe one can take D of almost anything (early example: D[Plot[Sin[x], {x, 0, 2 Pi}]^2, Plot[Sin[x], {x, 0, 2 Pi}]] -- impressive and pointless at the same time). There are restrictions on what is allowed for a variable (second argument). However, the Derivative of Abs is undefined, but still permitted. There's some wisdom in that, since you can define it to agree with the real function as Mark points out. – Michael E2 Sep 13 '14 at 14:25
  • Here's another way to look at it: In cases where the complex and real derivative have the same formula, you should find that D returns the expected result. In cases where the complex and real derivatives do not agree, the Derivative should be undefined. – Michael E2 Sep 13 '14 at 14:27
  • I have studied the documentation for all these discontinous functions and shall present an overview as an edit to my question, as soon as I find the time. Here's just one positive example: "Possible issues for Conjugate: Differentiating Conjugate is not possible: D[Conjugate[t], t] -> Derivative[1][Conjugate][t]". I think this should be mentioned with Abs too. – Dr. Wolfgang Hintze Sep 13 '14 at 16:43
  • 2
    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
  • @ Jens: in order to find this out it might help to have a brief look at the fairly extensive comments here which are, as it were, kind of a "scattered" answer. I'm now fine with it. – Dr. Wolfgang Hintze Sep 17 '14 at 07:10

0 Answers0