Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am trying to apply InversePermutation on a list of sublists of selected permutation elements. First I am trying to see if I am able to reach every element,

temp = {{Cycles[{}]}, {Cycles[{}], Cycles[(12)]}, {Cycles[{}], 
   Cycles[(13)]}}

So the code I have tried to print all is

k = 1; i = 1; Do[Do[Apply[Print, i], {i, k}], {k, temp}]

which gives $\{\}\{\}\{1 2\}\{\}\{1 3\}....$ it pics only content of Cycle. So, I changed it a bit as,

k = 1; i = 1; Do[Do[Apply[Print, {i}], {i, k}], {k, temp}]

Surprisingly it started to take all the contents with Cycle prefix along, giving

$\{\text{Cycles}[\{\}]\}\\ \left\{\text{Cycles}[\{\}],\text{Cycles}\left[\left( \begin{array}{cc} 1 & 2 \\ \end{array} \right)\right]\right\}....$

What I could not understand is that putting {i} instead of i changed what ?
Edit: I am not able to replace this Print with InversePermutation.It doesn't respond if I replace Print with it. Can someone suggest what I am doing wrong?

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Note that your definitions k=1 and i=1 make little difference, as i and k are local in the do loops. To see how the code behaves we actually have to look at the range specifications for k first.

temp is a list, so values for k be taken from this list. But the values for k are then always lists as well, as temp is a list of lists. So also in the inner loop we have for every value of k a Do loop of the form Do[expr, {i, list}]. So i takes every element of this list as a value.

I am unsure if you meant this to happen. At least you are finding all the Cycles :). But it confuses me that you would set i and k to 1 then..

Suggestion

Evaluate

Clear[i, k]
Do[Print[i], {k, temp}, {i, k}]

Cycles[{}], Cycles[{}], Cycles[12]...

You see that you need no definitions for i and k? And that you can do with only one Do loop? And that you don't need Apply?

Also evaluate

Map[Print, Flatten[temp]];

Cycles[{}], Cycles[{}], Cycles[12]...

Further note

Apply is a function that changes the head of an expression. Apply[Print, i] is generally not good code in a Do loop where i is a variable, as in case i=1 for example, i has no proper head, as the integer 1 is a so called atom of Mathematica.

Apply[Print, {1}] is "good code", as the head of {1} is List and {1}.

Anyway, as it turns out, i is never 1 inside the Do loops so I guess this doesn't matter.

In your example, if you use Apply[Print, Cycles[{}]], that becomes Print[{}]. If you do Apply[Print, {Cycles[{}]}], that becomes Print[Cycles[{}]].

share|improve this answer
    
your explanation is very informative, thanks a lot. –  Rorschach Jun 1 '13 at 18:40
    
@rafiki Okido, no problem :) –  Jacob Akkerboom Jun 1 '13 at 18:43

Looking at your Do loops, you can combine them into a single Do expression; I will replace Do with Table to return all evaluations in nested lists, and I will use an arbitrary head label to show which parts are assigned to i in each loop:

Table[label[i], {k, temp}, {i, k}]
{{label[Cycles[{}]]}, {label[Cycles[{}]], label[Cycles[12]]},
 {label[Cycles[{}]], label[Cycles[13]]}}

Note that the same expression can be produced with:

Map[label, temp, {2}]
{{label[Cycles[{}]]}, {label[Cycles[{}]], label[Cycles[12]]},
 {label[Cycles[{}]], label[Cycles[13]]}}

Now we can address this part of your question:

What I could not understand is that putting {i} instead of i changed what?

The operation Apply[ff, expr] replaces the head of expr with ff. In the case of {stuff} the head is List, so you simply get ff[stuff]. Combined with the above, and using Scan in place of Map (which has similar syntax), with {i} you are therefore just doing the equivalent of:

Scan[Print, temp, {2}]

Depending on what you want to do you may find value in Level:

Level[temp, {2}]
{Cycles[{}], Cycles[{}], Cycles[12], Cycles[{}], Cycles[13]}
share|improve this answer
    
ya yesterday night after reading your other post on loops, I tried the same and could do it. Thanks for posting here in more refined way. –  Rorschach Jun 3 '13 at 4:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.