# HoldForm/Inactivate and exchange operators

I would like to know, how to change parts of a formula in HoldForm. I have the formula
$$\underbrace{\prod_{i=1}^N}_{\text{level 4}} \sum_{\sigma_i\in \{-1,1\}}\underbrace{\sum_{\lambda\in \{-1,1\}}}_{\text{level 2}}\underbrace{\frac{\partial}{\partial x_i}}_{\text{level 3}}\underbrace{\exp\left( -\lambda \sigma_i x_i^2\right)(x_i^2-\lambda)}_{\text{level 1}}$$ and I put it in HoldForm such that it is not evaluated.

1. I would like to work with the level 1 content first, change it by e.g. an expansion while the other parts remain fixed. After that I would like to exchange the level 2 and level 3 operators (if you regard summation as an operation as well) and evaluate only the level 2 - level 1 part with keeping the rest fixed. The result should read $$\underbrace{\prod_{i=1}^N}_{\text{level 4}} \sum_{\sigma_i\in \{-1,1\}}\underbrace{\frac{\partial}{\partial x_i}}_{\text{level 3}}\left(\underbrace{\exp\left(\sigma_i x_i^2\right)(x_i^2+1)}_{\text{level 1}}+\underbrace{\exp\left(-\sigma_i x_i^2\right)(x_i^2-1)}_{\text{level 1}}\right)$$

2. I would like to keep the order and evaluate the level 2 sum while the level 3 operator is not applied but remains in the HoldForm. The result for the second task should read $$\underbrace{\prod_{i=1}^N}_{\text{level 4}} \sum_{\sigma_i\in \{-1,1\}}\left(\underbrace{\frac{\partial}{\partial x_i}}_{\text{level 3}}\underbrace{\exp\left(\sigma_i x_i^2\right)(x_i^2+1)}_{\text{level 1}}+\underbrace{\frac{\partial}{\partial x_i}}_{\text{level 3}}\underbrace{\exp\left(-\sigma_i x_i^2\right)(x_i^2-1)}_{\text{level 1}}\right)$$ To summarize, there are two questions: How do I evaluate arbitrary things(different level combinations or maybe $N$ or the set of possible $\lambda$'s) and how do I change the operator order within the HoldForm of an overall summation or integration.

Edit
As recomended I add some code using a slightly simpler example. The code

J = Inactivate[Table[Subscript[j, i, j], {i, 0, N}, {j, 0, N}]];

Σ = Inactivate[Table[Subscript[σ, i], {i, 0, N}]];

H =
Inactivate[-Sum[Subscript[j,i,j] Subscript[σ,i] Subscript[σ,j], {i, 0, N}, {j, 0, N}]
-h*Sum[Subscript[σ, i], {i, 0, N}], Sum];

Z =
Inactivate[Product[Sum[Exp[-β*H], {Subscript[σ, i],-1,1}], {k,0, N}], Sum | Product]
D[Z, β]


yields $$\frac{\partial \left(\underset{k=0}{\overset{N}{\prod }}\underset{\sigma _i=-1}{\overset{1}{\sum }}\exp \left(-\beta \left(-h \underset{i=0}{\overset{N}{\sum }}\sigma _i-\underset{i=0}{\overset{N}{\sum }}\underset{j=0}{\overset{N}{\sum }}\sigma _i \sigma _j j_{i,j}\right)\right)\right)}{\partial \beta }$$ using Inactivate. How do I pass the $\beta$ derivative past the product and the $\sigma_i$ sum and evaluate it to $$\left(\underset{k=0}{\overset{N}{\prod }}\underset{\sigma _i=-1}{\overset{1}{\sum }}\left(h \underset{i=0}{\overset{N}{\sum }}\sigma _i+\underset{i=0}{\overset{N}{\sum }}\underset{j=0}{\overset{N}{\sum }}\sigma _i \sigma _j j_{i,j}\right)\exp \left(-\beta \left(-h \underset{i=0}{\overset{N}{\sum }}\sigma _i-\underset{i=0}{\overset{N}{\sum }}\underset{j=0}{\overset{N}{\sum }}\sigma _i \sigma _j j_{i,j}\right)\right)\right)$$

• Please, post Mathematica code. Withou concrete code to work with, this is very abstract and mo one wants to retype that. – Henrik Schumacher May 29 '18 at 13:57
• I think Inactive was developed for just this kind of thing. – Daniel W May 29 '18 at 14:30
• When working with such expressions, I find it useful to replace e.g. Sum with sum. This lets you manipulate the expression without it being evaluated. Obviously, when you have something you want to evaluate you can simply use expr /. sum -> Sum – mikado May 29 '18 at 21:02

MapAt[Activate[D[#, β]]&, Z, {1, 1}] % // TeXForm


$\small\underset{k=0}{\overset{N}{\prod }}\underset{\sigma_i=-1}{\overset{1}{\sum }}\left(\sum _{i=0}^N \sum _{j=0}^N \sigma _i \sigma _j j_{i,j}+h \sum _{i=0}^N \sigma _i\right) \exp \left(-\beta \left(-\sum _{i=0}^N \sum _{j=0}^N \sigma _i \sigma _j j_{i,j}-h \sum _{i=0}^N \sigma _i\right)\right)$

Also

ReplacePart[Z, {1, 1} -> Activate[Activate[D[Z[[1, 1]], β]]]] (* or *)
Z /. e: Exp[__] :> Activate[D[e, β]] (* or *)
W = Z; W[[1,1]] = Activate[D[W[[1, 1]], β]]; W

• Thank you very much for your answer. Could you quickly explain how you solved the problem. How did you get the position {1,1} and how would you do it if if you had to squeeze it in between the product and the sum? – Uwe.Schneider May 30 '18 at 19:40
• @Uwe.Schneider, try Z[], Z[], Z[[1,1]] etc to see what constitutes which Part of Z. To differentiate the sum term (which is Z[]) use MapAt[Activate @ D[#, β]&, Z, {1}] – kglr May 30 '18 at 21:23