math.combinatorics: faster permutation (using slices).

db4
John Benediktsson 2013-04-05 07:28:22 -07:00
parent 4e2e8a21e0
commit a52ee59005
1 changed files with 6 additions and 3 deletions

View File

@ -34,11 +34,14 @@ MEMO: factorial ( n -- n! )
: factoradic ( n -- factoradic ) : factoradic ( n -- factoradic )
0 [ over 0 > ] [ 1 + [ /mod ] keep swap ] produce reverse! 2nip ; 0 [ over 0 > ] [ 1 + [ /mod ] keep swap ] produce reverse! 2nip ;
: (>permutation) ( seq n -- seq ) : bump-indices ( seq n -- )
[ '[ _ dupd >= [ 1 + ] when ] map! ] keep prefix ; '[ dup _ >= [ 1 + ] when ] map! drop ; inline
: (>permutation) ( seq n index -- seq )
swap [ dupd head-slice ] dip bump-indices ;
: >permutation ( factoradic -- permutation ) : >permutation ( factoradic -- permutation )
reverse! 1 cut [ (>permutation) ] each ; reverse! dup [ (>permutation) ] each-index reverse! ;
: permutation-indices ( n seq -- permutation ) : permutation-indices ( n seq -- permutation )
length [ factoradic ] dip 0 pad-head >permutation ; length [ factoradic ] dip 0 pad-head >permutation ;