$$\hat{X}(Y) = [X,Y] $$

I have known matrices $S_i$ and $V$. I am trying to use Mathematica to define a function which calculates

$$ \sum_{\substack{n_1, \ldots, n_k>1\\ n_1+\ldots n_k = m}} \hat{S}_{n_1}(\hat{S}_{n_2}(\ldots (\hat{S}_{n_k} (V))\ldots )) $$

for arbitrary integers $k$ and $m$ (with $k,m < 10$ or so). The matrices $S_i$ and $V$ are maybe up to $8 \times 8$ so I'm not too worried about speed or anything. Also note that I am able to access the $S_i$ by S[i].

I have a few questions.

1) Here the summation is over a somewhat complicated set of indices. There is the constraint that there must be exactly $k$ indices and that those indices must all add up $m$. I know that I can use

Select[Flatten[Permutations /@ IntegerPartitions[m], 1], Length[#] == k &]

to get a list of sets of indices which satisfy this constraint but I don't know how to sum over these indices other than using a loop and even then I'm not entirely sure how to do it.

2) Using the hat notation it is very easy to string together multiple commutators in writing. I'm not so sure how to string together a variable number of commutators with variable arguments. Again I feel there is a way I could do this with a loop but I'm not exactly sure.

At present I'm trying to construct loops to implement this summation but I'm not sure if it will work and even if it does it does not seem very elegant.

Could anyone provide me with a nice way to calculate this expression?

  • $\begingroup$ I'm confused about your sum. Is this a set of nested commutators? So really, you have something like $\hat{S}_{n_1} (\hat{S}_{n_2} ( \dots \hat{S}_{n_k} (V) \dots ) )$? $\endgroup$
    – march
    Dec 21, 2018 at 1:01
  • $\begingroup$ Yes that is correct. That is a more explicit expression. I'll update the question to reflect this. $\endgroup$
    – Jagerber48
    Dec 21, 2018 at 1:09

2 Answers 2


It's worth checking the following to make sure that it reproduces what you expect. If not, I can fix it.

First define the indices:

With[{m = 5, k = 3},
   indices = Flatten[Permutations /@ IntegerPartitions[5, {3}], 1]
(* {{3, 1, 1}, {1, 3, 1}, {1, 1, 3}, {2, 2, 1}, {2, 1, 2}, {1, 2, 2}} *)

We map s over these sets of indices:

ss = Append[#, v] & /@ Map[s, indices, {2}]
(* {{s[3], s[1], s[1], v}, {s[1], s[3], s[1], v}, {s[1], s[1], s[3], v},
   {s[2], s[2], s[1], v}, {s[2], s[1], s[2], v}, {s[1], s[2], s[2], v}} *)

We define a helper-function

comm[x_, y_] = y.x - x.y;

(The flipped definition here is intentional.) We then do:

tem = Fold[comm, Reverse@#] & /@ Map[s, indices, {2}];

which creates a list of commutators, one for each set of indices. Finally, take a take the Total:

Total @ tem;
  • $\begingroup$ Oooh nice this seems to get at what I am going for! I will try it out and let you know if there are any problems. Mapping $S$ over the set of indices seems like the key.. then I have a list of the things I need for the subsequent operations and I can just work with that list as needed. It will take me some time to understand all of the details. $\endgroup$
    – Jagerber48
    Dec 21, 2018 at 1:18

Here's an answer based on that of march above.

ad[X_, Y_] = X.Y - Y.X;

SS[k0_, m0_] :=
 Module[{k = k0, m = m0, inds},
  inds = Flatten[Permutations /@ IntegerPartitions[m, {k}], 1];
  ss = Map[S, inds, {2}];
  0*v+ Total@Map[Fold[ad[#2, #1] &], Prepend[#, v] & /@ ss]

The main difference with the other answer is the definition of ad (adjoint operator instead of comm) does not need to be reversed from the usual definition because of the ad[#2, #1]& construct in Fold.

inds is a list of all permutation of integers partitions of integer m of length k. ss is now a list of the $S$ matrices indexed the same way as inds. The next line in the module then prepends each item in ss with v. The fold iteratively applies the ad function to all of the elements of this new list, taking the left-most element of the list as the second argument and the 2nd-left most argument as the first. It then takes this result and uses it as the second argument and the next item in the list as the first etc.

inds = Flatten[Permutations /@ IntegerPartitions[4, {3}], 1]
(*{{2, 1, 1}, 
   {1, 2, 1}, 
   {1, 1, 2}}*)

ss = Map[S, inds, {2}]
(*{{S[2], S[1], S[1]}, 
   {S[1], S[2], S[1]}, 
   {S[1], S[1], S[2]}}*)

0*v + Total@Map[Fold[ad[#2, #1] &], Prepend[#, v] & /@ ss]
(*ad[S[1], ad[S[1], ad[S[2], v]]] + 
  ad[S[1], ad[S[2], ad[S[1], v]]] + 
  ad[S[2], ad[S[1], ad[S[1], v]]]*)

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.