math.combinatorics: faster permutation (using slices).
parent
4e2e8a21e0
commit
a52ee59005
|
@ -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 ;
|
||||||
|
|
Loading…
Reference in New Issue