7
$\begingroup$

I can activate an expression with Inactive objects such as

expr = Inactive[f][Inactive[f][x], y] + Inactive[f]

by applying Activate:

Activate[expr]
(*f + f[f[x],y]*)

But also, I can achieve the same task with ReplaceAll:

expr /. Inactive[f] :> f

Should I be aware of any differences between these two methods of activation? Or is Activate really just ReplaceAll under the hood? Or maybe it is the same as Replace[expr,Inactive[f] :> f,{0,Infinity},Heads->True due to similarities in options?

$\endgroup$

1 Answer 1

4
+50
$\begingroup$

There is at least one difference in evaluation between Activate and either of your proposed equivalents, for a malformed Inactive expression:

Inactive[1 + 1, 5] // Activate
Inactive[1 + 1, 5] /. Inactive[x_] :> x
Replace[Inactive[1 + 1, 5], Inactive[x_] :> x, {0, -1}, Heads -> True]
2

Inactive[1 + 1, 5]

Inactive[1 + 1, 5]

Therefore the replacement appears to be closer to:

Inactive[1 + 1, 5] /. Inactive[x_, ___] :> x
2

This however does not match with nested Inactives:

Inactive[Inactive[a]] // Activate
Inactive[Inactive[a]] /. Inactive[x_, ___] :> x
a

Inactive[a]

But the modified Replace form does:

Replace[Inactive[Inactive[a]], Inactive[x_, ___] :> x, {0, -1}, Heads -> True]
a

I have yet to make this behave differently from Activate.

$\endgroup$
2
  • $\begingroup$ So perhaps it is equivalent to ReplaceRepeated (//.) with your modified rule Inactive[x_, ___] :> x? $\endgroup$
    – QuantumDot
    Commented Mar 8, 2020 at 7:50
  • $\begingroup$ @QuantumDot That could be less efficient than the Replace form and I don't see the advantage of it, and ReplaceRepeated doesn't supports Heads does it? $\endgroup$
    – Mr.Wizard
    Commented Mar 8, 2020 at 8:46

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.