0
$\begingroup$

I would like to understand how Inactive and Activate function works because I'm disturbed.

If I need to inactivate a function and then re activate it how to do it ? I tried the following which doesn't work :

hh[x_] := x^2

hh[2]

4

hh = Inactive[hh]

Inactive[hh]

hh[2]

Inactive[hh][2]

hh = Activate[hh]

Inactive[hh]

hh[2]

Inactive[hh][2]

As you can see the function isn't reactivated as I would like it to be. I would like to understand why because it would help me to understand better how activate/inactivate functions work.

$\endgroup$
  • $\begingroup$ if you use a different name for Inactive[hh] (e.g. hh1 =Inactive[hh], Activate[hh1[2]]gives 4 as expected. $\endgroup$ – kglr Jun 27 at 12:20
  • $\begingroup$ @kglr but why isn't it working as I wrote it ? Indeed in hh=Activate[hh] I would expexct mathematica to expand the rhs which would mean : hh=Activate[Inactive[hh]] thus "in a way:" hh[x]=Activate[Inactive[x^2]]=x^2. I am writing it roughly for you to see how I understand the code to give you an idea of where my misunderstanding would be $\endgroup$ – StarBucK Jun 27 at 12:27
  • 2
    $\begingroup$ Defining a thing in terms of itself is not a good idea if you don't want to do recursion. For example, try hh = List[hh]: you'll see that Mathematica complains about recursion depth. The only reason that doesn't happen with Inactive is because of the HoldFirst attribute, but it's still not a good idea to do self-referential definitions like the one you posted. The only time you can do that is when hh actually has a value, such as with x = 1; x = x + 1. In that case, the self-reference is broken because x will evaluate to its value on the r.h.s. $\endgroup$ – Sjoerd Smit Jun 27 at 13:18
  • $\begingroup$ @SjoerdSmit ok maybe I'm too much used to other programming language. In mathematica I should avoid to do it. Thanks ! $\endgroup$ – StarBucK Jun 27 at 13:20
4
$\begingroup$

When you say hh = Inactive[hh], it means "whenever you see hh, rewrite it as Inactive[hh]. Once you've made that definition, it becomes very hard to activate hh because when Activate yields hh, it immediately gets rewritten as Inactive[hh].

Look at Trace[hh = Activate[hh]]. The rewrite defeats your attempt to redefine it.

$\endgroup$

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.