1
$\begingroup$

I was willing to understand the Coolwater's solution of the presented problem here. He proposed the below solution

 go[L_, m_] := Normal[SparseArray[Flatten[With[{R = Range[Length[L]]},
 MapIndexed[Thread[Thread[{First[#2],
 Join[#, Complement[R, #]]}] -> L] &, Subsets[R, {m}]]], 1]]]

 go[{1, 2, 3, 4, 5}, 2]

In a reverse procedure I have obtained

 L = {1, 2, 3, 4, 5}; R = Range[Length[L]]; m = 2;
 Subsets[R, {m}]
(**{{1, 2}, {1, 3}, {1, 4}, {1, 5}, {2, 3}, {2, 4}, {2, 5}, {3, 4}, {3, 5}, {4, 5}}**)

MapIndexed[
Thread[Thread[{First[#2], Join[#, Complement[R, #]]}] -> L] &, 
Subsets[R, {m}]]

(**{{{1, 1} -> 1, {1, 2} -> 2, {1, 3} -> 3, {1, 4} -> 4, {1, 5} -> 
 5}, {{2, 1} -> 1, {2, 3} -> 2, {2, 2} -> 3, {2, 4} -> 4, {2, 5} -> 
 5}, {{3, 1} -> 1, {3, 4} -> 2, {3, 2} -> 3, {3, 3} -> 4, {3, 5} -> 
 5}, {{4, 1} -> 1, {4, 5} -> 2, {4, 2} -> 3, {4, 3} -> 4, {4, 4} -> 
 5}, {{5, 2} -> 1, {5, 3} -> 2, {5, 1} -> 3, {5, 4} -> 4, {5, 5} -> 
 5}, {{6, 2} -> 1, {6, 4} -> 2, {6, 1} -> 3, {6, 3} -> 4, {6, 5} -> 
 5}, {{7, 2} -> 1, {7, 5} -> 2, {7, 1} -> 3, {7, 3} -> 4, {7, 4} -> 
 5}, {{8, 3} -> 1, {8, 4} -> 2, {8, 1} -> 3, {8, 2} -> 4, {8, 5} -> 
 5}, {{9, 3} -> 1, {9, 5} -> 2, {9, 1} -> 3, {9, 2} -> 4, {9, 4} -> 
 5}, {{10, 4} -> 1, {10, 5} -> 2, {10, 1} -> 3, {10, 2} -> 
 4, {10, 3} -> 5}}**)

Which are some results of the solution parties. But I have tried to understand what happen in the first Thread Thread[{First[#2], Join[#, Complement[R, #]]}] and the second Thread Thread[Thread[{First[#2], Join[#, Complement[R, #]]}] -> L].

Because in the first Thread have used # which returns to Subsets[R, {m}] and inside the second Thread have used ->. I cannot use of just Subsets[R, {m}] for investigating the Thread process also in the Mathematica help I cannot find a Thread with ->.

In fact what is the duties of two used Thread?!! Can anyone say in a clarified way?!

$\endgroup$
3
$\begingroup$

In fact this is more likely a extended comment which may gives you some idea of how these two Thread work.

To begin with, let's use Echo to conveniently get what's the input is:

go[L_, m_] := 
 Normal[SparseArray[
   Flatten[With[{R = Range[Length[L]]}, 
     MapIndexed[
      Thread[Thread[Echo@{First[#2], Join[#, Complement[R, #]]}] -> 
         Echo@L] &, Subsets[R, {m}]]], 1]]]

Let's take one out and check what's happening:

{1,{1,2,3,4,5}}
{1,2,3,4,5}

The first level of Thread is used in this form: Thread[{1,{1,2,3,4,5}}] which means the same as Thread[{{1,1,1,1,1},{1,2,3,4,5}}] and generate a result of {{1,1},{1,2},{1,3},{1,4},{1,5}}.

Then at the second level, it's the normal form of Thread working. Note that Thread may work on any Head and list1->list2 is actually Rule[list1,list2], thus Thread can work on this and change it into a list of rules. Thus Thread[{{1,1},{1,2},{1,3},{1,4},{1,5}}->{1,2,3,4,5}] will return {{1,1}->1,{1,2}->2,{1,3}->3,{1,4}->4,{1,5}->5}.

Will this explanation help? Is this what you need?

$\endgroup$
  • $\begingroup$ Your comment, and explanation is very useful. Echo which I don't know before is the keyword which clarifies the procedure. $\endgroup$ – Unbelievable Jul 20 '16 at 8:56

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.