11

Note to closers : This is a question about a Programming language (Mathematica), and not about a discipline/science (mathematics).

Why is

N[D[Sin[x], x] /. x -> Pi/4]
(*
Out -> 0.707107
*)

but

N[D[Abs[x], x] /. x -> Pi/4]
(*
Out -> Derivative[1][Abs][0.785398]
*)

?

And what is the better way to force a numerical result?

5
  • 1
    My bad, I read "mathematics". Ignore me.
    – Lekensteyn
    Jun 13, 2011 at 13:32
  • 1
    @Leke Also read meta.stackexchange.com/questions/81152/…. Jun 13, 2011 at 13:33
  • @Leke Don't mind, it is a common mistake. But the meta community refused to change the tag name to something more descriptive. Jun 13, 2011 at 13:34
  • 2
    yes Abs'[x] is not evaluated numerically; I've noticed that too in the past. I'd also like to know why.
    – acl
    Jun 13, 2011 at 13:35
  • 1
    @acl And that impacts on other functions like EuclideanDistance that use Abs[ ] Jun 13, 2011 at 13:37

3 Answers 3

11

Abs[z] is not a holomorphic function, so its derivative is not well defined on the complex plane (the default domain that Mathematica works with). This is in contradistinction to, e.g., Sin[z], whose complex derivative (i.e., with respect to its argument) is always defined.

More simply put, Abs[z] depends on both z and z*, so should really be thought as a two argument function. Sin[z] only depends on z, so makes sense with a single argument.

As pointed out by Leonid, once you restrict the domain to the reals, then the derivative is well defined (except maybe at x=0, where they've taken the average of the left and right derivatives)

In[1]:= FullSimplify[Abs'[x],x \[Element] Reals]
Out[1]= Sign[x]

As pointed out by Szabolcs (in a comment), FunctionExpand will simplify the numerical expressions, but "Some transformations used by FunctionExpand are only generically valid".

ComplexExpand also gives numeric results, but I don't trust it. It seems to take the derivative assuming the Abs is in the real domain, then substitutes in the numeric/complex arguments. That said, if you know that everything you're doing is in the reals, then ComplexExpand is your friend.

4
  • So, here is the reason. Thanks! Jun 13, 2011 at 23:05
  • but then what is the reason to not evaluate Abs'[.42]? I am not convinced that what you say justifies it (although it's true of course)
    – acl
    Jun 14, 2011 at 9:48
  • @acl: I think because Abs'[.42] = Limit[(Abs[z+.42] - Abs[.42])/z, z->0] and the limit is not the same in all complex directions. Consider the path z[t_] := 1-Exp[I t] As t->0, z[t]->0 and the limit/derivative in this direction is always zero. (This is basically reiterating my answer... oh well)
    – Simon
    Jun 14, 2011 at 10:07
  • Thanks. I realise that Abs[x] isn't analytic, since obviously the Cauchy-Riemann conditions can't be satisfied. My comment was based on believing that mma would pretend only the real line existed if one used reals as arguments in functions other than Abs, so this would be inconsistent. However I just tried Re'[.42] and so on and they also remain unevaluated (while I thought I remembered it being evaluated to 1), so I guess I simply remembered something else...
    – acl
    Jun 14, 2011 at 11:11
8

I refer you to this thread as possibly relevant - this issue has been discussed before. To summarize my answer there, Abs is defined generally on complex numbers. Once you specify that your argument is real, it works:

 In[1]:= FullSimplify[Abs'[x], Assumptions -> {Element[x, Reals]}] 

 Out[1]= Sign[x] 
10
  • 1
    @Leonid but as you point out in that thread, it does not work for specific real arguments. And for instance, Abs'[.23] does not evaluate while IntegerPart'[.23] does. Not terribly consistent, unless I am missing something.
    – acl
    Jun 13, 2011 at 13:50
  • 1
    @belisarius Oh, I see, you were mostly asking about numerics. Sorry, I wasn't reading your question carefully enough. It is interesting that while FunctionExpand seems to always work, FullSimplify works on integers but not reals. Jun 13, 2011 at 13:58
  • @acl Yes, I agree, does not look very consistent to me. I can understand that it may be desirable to not evaluate automatically on symbolic or integer arguments, but once they are real or N is applied, it should have given a numeric answer. Jun 13, 2011 at 14:00
  • 2
    @Leonid: Even with specific real values, the derivative is not well defined. The value only defines the point at which you examine the derivative, not the direction. FunctionExpand seems to be making an (almost reasonable) assumption about the domain.
    – Simon
    Jun 13, 2011 at 22:23
  • 1
    @Leonid: I assume that is what's happening, since nearly everywhere else, Mma assumes complex variables. And I agree that maybe a more sophisticated functionality or ability to change the default domain would be nice, but then would that be traded off against power/speed/consistency?
    – Simon
    Jun 14, 2011 at 10:14
7

You can use FunctionExpand to force getting a number as a result even when you are using exact quantities:

Abs'[Pi/4] // FunctionExpand
Abs'[-1] // FunctionExpand

I do not know the reason for the following though:

In:= Abs'[0] // FunctionExpand
Out= 0
6
  • @Szabolcs Thanks. Do you know why this is necessary? I mean ... why Mma does not evaluate this as usual? Jun 13, 2011 at 13:43
  • +1, had no idea about FunctionExpand! Curious why IntegerPart'[x] evaluates for numerical x but Abs'[x] does not though.
    – acl
    Jun 13, 2011 at 13:43
  • The last bit is probably due to convention (strictly the value is undefined because the derivative does not exist).
    – rubenvb
    Jun 13, 2011 at 13:45
  • 1
    @Szabolcs Why do you think that anything is wrong with the second answer? This is consistent with the definition of Sign[x], as long as we agree that Abs'[x] == Sign[x] for real x. Jun 13, 2011 at 13:49
  • @Leonid, the reason is what @rubenvb said. It's not mathematically precise. And maybe this is the reason why it's not evaluated automatically, @belisarius. Under "Possible issues" the doc page says: "Some transformations used by FunctionExpand are only generically valid." Leonid is right that the "imprecise" result appears because Abs'[x] is expanded to Sign[x]: FunctionExpand[Abs'[x], Assumptions -> x \[Element] Reals]
    – Szabolcs
    Jun 13, 2011 at 13:57

Your Answer

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

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