0

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.

StarBucK
  • 2,164
  • 1
  • 10
  • 33
  • if you use a different name for Inactive[hh] (e.g. hh1 =Inactive[hh], Activate[hh1[2]]gives 4 as expected. – kglr Jun 27 '19 at 12:20
  • @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 – StarBucK Jun 27 '19 at 12:27
  • 2
    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. – Sjoerd Smit Jun 27 '19 at 13:18
  • @SjoerdSmit ok maybe I'm too much used to other programming language. In mathematica I should avoid to do it. Thanks ! – StarBucK Jun 27 '19 at 13:20

2 Answers2

5

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.

John Doty
  • 13,712
  • 1
  • 22
  • 42
0

I will try to give an example using related commands in an approach that has been useful to me. Specifically Inactivate and Activate.

Suppose you are working with a matrix expression and trying to understand what is going on. Or want to show work step-by-step as might be required in a homework problem or research derivation. This answer pertains to the original "basic question of how it works" posed by @StarBucK.

A = {{1,2,3},{4,5,6},{Pi, E, Sqrt[2]}}

and you want to form

A.Transpose[A].Inverse[A]

Actually here's a series of input lines to Mathematica

A = {{1, 2, 3}, {4, 5, 6}, {Pi, E, Sqrt[2]}}

A // TraditionalForm

Inactivate[A.Transpose[A].Inverse[A]] // TraditionalForm

Activate[%, Transpose] // TraditionalForm

Activate[%, Inverse] // TraditionalForm

Activate[%, Dot] // TraditionalForm

% // N // TraditionalForm

My output in Mathematica 13.3.0.0 is

enter image description here

PaulCommentary
  • 1,482
  • 7
  • 12