On Thu, May 23, 2013 at 4:02 AM, Linda Alvord <[email protected]> wrote:
> perm=: 3 : 0
>
> z=. i.1 0
> for. i.y do. z=.,/(0,.1+z){"2 1\:"1=i.>:{:$z end.
> )
...
> How does this work?
Yes, perm looks similar to the A. verb.
The initial value of z is i.1 0 which I interpret as "1 example of a
sequence of zero items".
The {:$z in the loop gets that trailing 0 from the shape the first
time through the loop, and it looks like that dimension grows by 1
each time through the loop. So i.>:{:$z is the number of elements
treated the next time through the loop, and =i.>:{:$z is the
corresponding identity (with this new number of rows and columns).
\:"1=i.>:{:$z gives us index vectors which can be used to bring that
diagonal element to the front of the list. For example:
\:"1=i.3
0 1 2
1 0 2
2 0 1
So if we have all permutations of two elements, and we stick a third
element in front of each of them, and then use these indices to select
from those "new value followed by all permutations" using {"1 2 we
will get all permutations, one item longer than our previous list of
permutations. Except the way it actually works is slightly different:
(0,.1+z) means we are sticking a 0 at the front and adjusting
everything else to fit - this means that the permutations stay sorted
from top to bottom. (This is different from ({:$z),.z which would
work but would also arrange things differently.)
I imagine you understand why it's using ,/ before defining the new z?
Thanks,
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm