If I enter x/x, I get 1. Such behavior leads to this:

Simplify[D[Sqrt[x^2], x, x]]


The same would be even if I use Together instead of Simplify.

One could then think that $\sqrt{x^2}$ is doubly differentiable at least $\forall x\in\mathbb R$, but if we remove Simplify call, we would reveal that it's not:

D[Sqrt[x^2], x, x]

-(x^2/(x^2)^(3/2)) + 1/Sqrt[x^2]

Even more ridiculous is this (which I guess is because x/x is simplified before feeding to Assuming):

Assuming[x == 0, x/x]


Why does Mathematica assume $x\ne0$? Is there a way to make it not cancel out such terms?

  • $\begingroup$ Well if you take a look at it, -(x^2/(x^2)^(3/2)) + 1/Sqrt[x^2] is exactly zero, except at x=0. But From a mathematical standpoint the problem is more like why does mathematicas differential operator D not exclude x=0 in this case. $\endgroup$ – Wizard Aug 16 '14 at 16:58
  • $\begingroup$ Here it's not a problem: D gives an expression, which is undefined for $x=0$. It's quite a correct result for function, which isn't differentiable at $x=0$. But then simplifying it to $0$ makes it defined at $x=0$ too, which is incorrect. $\endgroup$ – Ruslan Aug 16 '14 at 17:31
  • $\begingroup$ This is Mathematica's default behavior, so it's exactly the expected result. Any undefined symbol is assumed by Simplify to be a generic complex number. By generic, I mean not having any isolated special value, such as 0. You have to use Assumptions to change this default. $\endgroup$ – Jens Aug 16 '14 at 17:35
  • 2
    $\begingroup$ @Jens So how exactly do I use Assumptions for this? See update of the question. $\endgroup$ – Ruslan Aug 16 '14 at 17:36
  • $\begingroup$ For doing the derivative, I would perhaps approach it as shown in my answer. $\endgroup$ – Jens Aug 16 '14 at 17:42
fun = Sqrt[x^2];

dd = D[fun, x, x];

With V10 we can explicitly define:

{dd, FunctionDomain[dd, x]}

enter image description here


{Simplify @ dd, FunctionDomain[dd, x]}

{0, x < 0 || x > 0}

Also with V10 you might consider Inactivate:

di = D[Inactivate@Sqrt[x^2], x, x] // Together

enter image description here

which prevents Together from evaluating to 0

di // Activate


| improve this answer | |

Let's define

f[x_] := Sqrt[x^2]

FullSimplify[D[f[x],x], x ∈ Reals]

(* ==> Sign[x] *)

You can use f'[x] or D[f[x],x] interchangeably here. Then the second derivative is simply

FullSimplify[D[Sign[x], x], x ∈ Reals]

(* ==> Derivative[1][Sign][x] *)

So doing the second derivative in two steps yields a mathematically correct answer. The derivative of the Sign function is not defined, but we can define it in the sense of distributions by replacing Sign[x] with 2HeavisideTheta[x]-1. Then one would obtain this nice result for the second derivative:

FullSimplify[D[2 HeavisideTheta[x] - 1, x], 
 x ∈ Reals]

(* ==> 2 DiracDelta[x] *)

Here is my initial way to get a mathematically expected result by making the domain explicit over which the function is defined:

f[x_] := Piecewise[{{Sqrt[x^2], x > 0}, {Undefined, True}}]

Simplify[D[f[x], x, x]]

(* ==> ConditionalExpression[0, x > 0] *)

However, this is only a band-aid.

| improve this answer | |
  • $\begingroup$ $\sqrt{x^2}$ is also defined for $x\le0$. It's the whole point of it — to be defined for all x (it's $|x|$ in disguise). $\endgroup$ – Ruslan Aug 16 '14 at 17:46
  • $\begingroup$ @Ruslan You're right, I'm actually quite surprised after trying this a bit more. I'll update my answer. $\endgroup$ – Jens Aug 16 '14 at 17:55
  • $\begingroup$ Nice catch. The Sqrt bug is not present in my beta V10 (Mac), however. $\endgroup$ – Michael E2 Aug 16 '14 at 18:15
  • $\begingroup$ My V10 (Win7-64) also returns Sign[x]. Perhaps you should restart. $\endgroup$ – Sjoerd C. de Vries Aug 16 '14 at 18:25
  • $\begingroup$ @SjoerdC.deVries You're right, version 10 is working like 8 again - I had an uncleared assumption hanging around. $\endgroup$ – Jens Aug 16 '14 at 18:30

Here are three approaches to the function:

f1[x_] := Piecewise[{{Sqrt[x^2], x != 0}}, 0]

f2[x_] := Piecewise[{{Sqrt[x^2], x < 0}, {Sqrt[x^2], x > 0}}, 0]

f3[x_] := Piecewise[{{-x, x < 0}, {x, x > 0}}, 0]

Their second derivatives.

Simplify@D[#[x], x, x] & /@ {f1, f2, f3}

Mathematica graphics

It seems that f2 or f3 might be used, but Simplify[f2[x]] results in f1[x].

Simplify[f2[x]] // InputForm
(* Piecewise[{{Sqrt[x^2], x != 0}}, 0] *)

So if the functions are simplified, only f3 gives the correct result:

Simplify@D[Simplify@#[x], x, x] & /@ {f1, f2, f3}

Mathematica graphics

| improve this answer | |
  • $\begingroup$ (+1) This is what I was aiming for (before I messed up and wrongly guessed that there was a bug in version 10)... $\endgroup$ – Jens Aug 16 '14 at 18:53

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.