# Why does Mathematica simplify $x/x\to1$?

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

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


0

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]


1

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

• 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. Aug 16, 2014 at 16:58
• 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. Aug 16, 2014 at 17:31
• 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.
– Jens
Aug 16, 2014 at 17:35
• @Jens So how exactly do I use Assumptions for this? See update of the question. Aug 16, 2014 at 17:36
• For doing the derivative, I would perhaps approach it as shown in my answer.
– Jens
Aug 16, 2014 at 17:42

fun = Sqrt[x^2];

dd = D[fun, x, x];


With V10 we can explicitly define:

{dd, FunctionDomain[dd, x]} Or

{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 which prevents Together from evaluating to 0

di // Activate


0

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

• $\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). Aug 16, 2014 at 17:46
• @Ruslan You're right, I'm actually quite surprised after trying this a bit more. I'll update my answer.
– Jens
Aug 16, 2014 at 17:55
• Nice catch. The Sqrt bug is not present in my beta V10 (Mac), however. Aug 16, 2014 at 18:15
• My V10 (Win7-64) also returns Sign[x]. Perhaps you should restart. Aug 16, 2014 at 18:25
• @SjoerdC.deVries You're right, version 10 is working like 8 again - I had an uncleared assumption hanging around.
– Jens
Aug 16, 2014 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} 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} • (+1) This is what I was aiming for (before I messed up and wrongly guessed that there was a bug in version 10)...
– Jens
Aug 16, 2014 at 18:53