5
$\begingroup$

I have to simplyfy an expression involving Abs' This works:

FullSimplify[Abs'[1 - a], a ∈ Reals]

Sign[1 - a]

but this does not:

FullSimplify[Abs'[1. - a], a ∈ Reals]

Derivative[1][Abs][1. - a]

the problem seems to be the 1., how can i get around this ?

$\endgroup$
3
  • $\begingroup$ Something like: FullSimplify[Rationalize[Abs'[1. - a]], a \[Element] Reals]? $\endgroup$
    – Kuba
    Commented Dec 15, 2014 at 14:44
  • $\begingroup$ What about FullSimplify[D[Abs[c - a], a], Assumptions -> {a \[Element] Reals, c \[Element] Reals}] /. c -> 1. $\endgroup$ Commented Dec 15, 2014 at 15:01
  • $\begingroup$ Both works. Thank you! $\endgroup$ Commented Dec 15, 2014 at 15:52

4 Answers 4

10
$\begingroup$

Mathematica does not have a rule for the derivative of Abs. Assuming that the term arose from taking the derivative with respect to a then taking the derivative of Abs[1-a] results in

D[Abs[1 - a], a]

-Derivative[1][Abs][1 - a]

which would recurse forever if it evaluated. Note

Plot[{Abs[1 - a], -Sign[1 - a]}, {a, -5, 5},
 PlotLegends -> "Expressions"]

enter image description here

Graphically, the derivative is -Sign[1 - a] except when a == 1

If dealing with real functions, before taking the derivative of a function containing Abs the function should have the terms with Abs converted to the Sqrt of the square of the argument to Abs.

Simplify[D[Sqrt[(1 - a)^2], a], Element[a, Reals]]

Piecewise[{{-1, a < 1}}, 1]

Simplify[% == -Sign[1 - a], a != 1]

True

$\endgroup$
9
$\begingroup$

If you want to incorporate the realness assumption directly into the evaluation of the derivative, you can define it for all subsequent computations by setting

Derivative[1][Abs][x_] := Sign[x]

Abs'[1. - a]

(* ==> Sign[1. - a] *)

This works because Derivative is not a protected function. Of course, just as with the replacement by $\sqrt{x^2}$, this is only valid if you know you're dealing with purely real variables.

To undo the above definition, you can do this:

Derivative[1][Abs][x_] =.

Edit for more generality

If you want to go this route, then the issue might occur that you also want to define the derivative to give an answer at the vertex of the absolute-value function. The correct answer is the Dirac delta function, and to make this come out correctly one could define the general $n$-th derivative as follows:

Derivative[n_][Abs][x_] := 2 Derivative[n - 1][(HeavisideTheta[#] - 1/2)&][x]

The main advantage is that you also get the delta function in the second derivatitve that incorporates the special case where the derivative jumps:

Abs''[1. - a]

(* ==> 2 DiracDelta[1. - a] *)
$\endgroup$
5
$\begingroup$

It seems no one has mentioned ComplexExpand[expr], which "expands expr assuming that all variables are real," as a potential solution; it certainly applies in the example, since a is explicitly assumed to be real.

ComplexExpand will work on the derivative of Abs:

ComplexExpand[Abs'[1 - a]]
(*  1/Sqrt[(1 - a)^2] - a/Sqrt[(1 - a)^2]  *)

but in some cases, it might be more convenient to expand absolute values in the function:

D[ComplexExpand[-Abs[1 - a]], a]
(*  (1 - a)/Sqrt[(1 - a)^2]  *)
$\endgroup$
2
$\begingroup$

As of V11.1 there's RealAbs, whose derivative exists and evaluates automatically:

RealAbs'[1. - x]
(1. - x)/RealAbs[1. - x]
$\endgroup$

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.