Hi all,
I'm trying to understand how the ".permute" method works. Any hints on the algorithm behind it? Thanks a lot, Thomas 
Halfanswering my own question...
by looking at Array's Class definition, i saw that the ".permute" method calls the "_ArrayPermute" primitive. I have no C++ experience, but looking up the source code on GitHub (http://bit.ly/2rexD3a) i found the code snipet that is being called (pasted below). Could anybody explain the permutation bit somehow? Thanks and bests, Thomas int prArrayPermute(struct VMGlobals *g, int numArgsPushed) { PyrSlot *a, *b, *slots1, *slots2, temp; PyrObject *obj1, *obj2; int i, j, m, z, size; a = g>sp  1; b = g>sp; if (NotInt(b)) return errWrongType; obj1 = slotRawObject(a); size = obj1>size; obj2 = instantiateObject(g>gc, obj1>classptr, size, false, true); obj2>size = size; slots1 = obj1>slots; slots2 = obj2>slots; memcpy(slots2, slots1, size * sizeof(PyrSlot)); z = slotRawInt(b); for (i=0, m=size; i<size1; ++i, m) { j = i + sc_mod((int)z, (int)(sizei)); z = sc_div(z,sizei); slotCopy(&temp,&slots2[i]); slotCopy(&slots2[i],&slots2[j]); slotCopy(&slots2[j],&temp); } SetRaw(a, obj2); return errNone; } 
In reply to this post by pliens
An array of size N has factorial(N) permutations. The nthPermutation parameter is taken as an index into all possible permutations. For each position in the output list the corresponding factor of the factorial is used to determine which item to choose. The basic idea is explained here: https://en.wikipedia.org/wiki/Factorial_number_system On Wed, May 10, 2017 at 9:39 AM, <[hidden email]> wrote: Hi all,  james mccartney

interesting...
we had quite some discussion around this over here: https://github.com/supercollider/supercollider/issues/2687 :) Till > On 15. May 2017, at 05:40, [hidden email] wrote: > > An array of size N has factorial(N) permutations. > The nthPermutation parameter is taken as an index into all possible permutations. > For each position in the output list the corresponding factor of the factorial is used to determine which item to choose. > The basic idea is explained here: https://en.wikipedia.org/wiki/Factorial_number_system > > On Wed, May 10, 2017 at 9:39 AM, <[hidden email]> wrote: > Hi all, > > I'm trying to understand how the ".permute" method works. > Any hints on the algorithm behind it? > > Thanks a lot, > Thomas > > > >  > View this message in context: http://newsupercollidermailinglistsforumsusethese.2681727.n2.nabble.com/Howpermuteworkstp7632241.html > Sent from the SuperCollider Users New (Use this!!!!) mailing list archive at Nabble.com. > > _______________________________________________ > scusers mailing list > > info (subscription, etc.): http://www.birmingham.ac.uk/facilities/eastudios/research/supercollider/mailinglist.aspx > archive: http://www.listarc.bham.ac.uk/marchives/scusers/ > search: http://www.listarc.bham.ac.uk/lists/scusers/search/ > > > >  >  james mccartney _______________________________________________ scusers mailing list info (subscription, etc.): http://www.birmingham.ac.uk/facilities/eastudios/research/supercollider/mailinglist.aspx archive: http://www.listarc.bham.ac.uk/marchives/scusers/ search: http://www.listarc.bham.ac.uk/lists/scusers/search/ 
Thanks James and Till.
Seeing the simplified algorithm in the github discussion was very helpful. I have to say that it is a very difficulttograsp bit of logic there.. For sake of future reference, i post Julian's sclangversion of the algorithm here, (slightly corrected): ( p = { arr, n var j, size; size = arr.size; // i changed arr.lastIndex to arr.size size.do{ i j = i + (n % size); n = n div: size; arr.swap(i, j); }; arr } ); p.((0..6),24); permute((0..6),24) All the best, Thomas 
On Sun, May 21, 2017 at 10:40 AM, <[hidden email]> wrote: Thanks James and Till. Old thread... I don't think this is the same as the algorithm in the primitive. In the above it is doing modulo the same size each time. To implement factorial number system, the modulo needs to be by decreasing size each time.That is why it is sizei, it decreases each time through the loop. j = i + sc_mod((int)z, (int)(sizei));  james mccartney

On Tue, Jul 11, 2017 at 10:45 PM, <[hidden email]> wrote:
Oof.. I miscopied when I moved to pseudocode in the first comment of that GHub issue. The for loop includes the statement `size1`, and I mistyped 1 instead of i in the next two lines. That got passed down through the game of telephone to the block quoted above. Sorry about that.  brian 
Free forum by Nabble  Edit this page 