The SO duplicate Preventing evaluation of Mathematica expressions
does a nice job of explaining the differences among and use-cases for Defer
, Hold
, and Unevaluated
.
Here, I can give at least one use-case for Inactivate
for which the other functions are less useful (if not completely useless). There are plenty of situations in which I want to control the algebraic steps that Mathematica does automatically: sometimes I'm doing derivations---using Mathematica to get all of the negative signs right on the first try---where I want to see all of the steps in order that I can write them nicely in a paper later; sometimes I want to show the steps to some students; etc.
The problem is, of course, that Mathematica has all kinds of built-in automatic simplifications, and Simplify
does what it wants. So I use Inactivate
on some of the functions/Head
s in the expression in order to stop the unwanted automatic simplifications while still being able to use the rest of the automatic simplifications.
Here's a contrived example, taken from a previous answer of mine. We're trying to show that the expression
(1 - Tan[x])/(Sin[x] - Cos[x])
simplifies to
- 1/Cos[x]
or
- Sec[x]
but Sin[x]/Cos[x]
automatically simplifies to Tan[x]
, so we can't do a Replace
ment to show the step:
(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] :> Sin[x]/Cos[x]
(* (1 - Tan[x])/(-Cos[x] + Sin[x]) *)
Instead, we can Inactivate
all of the trigonometric functions:
expr = Inactivate[(1 - Tan[x])/(Sin[x] - Cos[x]) /. Tan[x] :> Sin[x]/Cos[x], Tan | Sin | Cos]
expr // Simplify
% // Activate
(* (1 - Inactive[Sin][x]/Inactive[Cos][x])/(-Inactive[Cos][x] + Inactive[Sin][x]) *)
(* -(1/Inactive[Cos][x]) *)
(* -Sec[x] *)
Of course, we could have Replace
d Head
s instead, using sin
, cos
, etc., but Activate
/Inactivate
is a nice programmatic way of doing that that doesn't require a whole bunch of replacement rules, and you have the power of Activate
ing in steps:
Activate[1 - Inactive[Sin][x]/Inactive[Cos][x], Cos]
(* 1 - Sec[x] Inactive[Sin][x] *)
which can be quite nice.
HoldForm[]
vs.Defer[]
, the output of the latter can be copied into a new cell and evaluated, while the former's output will still remain held after copying into a new cell. $\endgroup$Inactivate
). $\endgroup$Inactivate
fit? $\endgroup$