factor/basis/math/combinatorics/combinatorics.factor

212 lines
5.2 KiB
Factor
Raw Normal View History

! Copyright (c) 2007-2010 Slava Pestov, Doug Coleman, Aaron Schaefer, John Benediktsson.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors arrays assocs binary-search classes.tuple fry
kernel locals math math.order math.ranges namespaces sequences
sequences.private sorting ;
2007-09-20 18:09:08 -04:00
IN: math.combinatorics
<PRIVATE
2007-09-20 18:09:08 -04:00
: possible? ( n m -- ? )
0 rot between? ; inline
2007-09-20 18:09:08 -04:00
: twiddle ( n k -- n k )
2dup - dupd > [ dupd - ] when ; inline
2007-09-20 18:09:08 -04:00
PRIVATE>
: factorial ( n -- n! )
2010-01-14 10:10:13 -05:00
iota 1 [ 1 + * ] reduce ;
: nPk ( n k -- nPk )
2dup possible? [ dupd - [a,b) product ] [ 2drop 0 ] if ;
: nCk ( n k -- nCk )
twiddle [ nPk ] keep factorial / ;
! Factoradic-based permutation methodology
<PRIVATE
2007-09-20 18:09:08 -04:00
: factoradic ( n -- factoradic )
0 [ over 0 > ] [ 1 + [ /mod ] keep swap ] produce reverse 2nip ;
: (>permutation) ( seq n -- seq )
[ '[ _ dupd >= [ 1 + ] when ] map ] keep prefix ;
: >permutation ( factoradic -- permutation )
reverse 1 cut [ (>permutation) ] each ;
: permutation-indices ( n seq -- permutation )
length [ factoradic ] dip 0 pad-head >permutation ;
: permutation-iota ( seq -- iota )
length factorial iota ; inline
PRIVATE>
: permutation ( n seq -- seq' )
2008-10-03 03:19:03 -04:00
[ permutation-indices ] keep nths ;
2007-09-20 18:09:08 -04:00
TUPLE: permutations length seq ;
: <permutations> ( seq -- permutations )
[ length factorial ] keep permutations boa ;
M: permutations length length>> ; inline
M: permutations nth-unsafe seq>> permutation ;
M: permutations hashcode* tuple-hashcode ;
INSTANCE: permutations immutable-sequence
: each-permutation ( seq quot -- )
[ [ permutation-iota ] keep ] dip
'[ _ permutation @ ] each ; inline
: map-permutations ( seq quot -- seq' )
[ [ permutation-iota ] keep ] dip
2012-04-18 17:43:08 -04:00
'[ _ permutation @ ] map ; inline
: filter-permutations ( seq quot -- seq' )
selector [ each-permutation ] dip ; inline
: all-permutations ( seq -- seq' )
[ ] map-permutations ;
: find-permutation ( seq quot -- elt )
[ dup [ permutation-iota ] keep ] dip
'[ _ permutation @ ] find drop
[ swap permutation ] [ drop f ] if* ; inline
: reduce-permutations ( seq identity quot -- result )
swapd each-permutation ; inline
: inverse-permutation ( seq -- permutation )
<enum> sort-values keys ;
<PRIVATE
: cut-point ( seq -- n )
[ last ] keep [ [ > ] keep swap ] find-last drop nip ;
: greater-from-last ( n seq -- i )
[ nip ] [ nth ] 2bi [ > ] curry find-last drop ;
: reverse-tail! ( n seq -- seq )
[ swap 1 + tail-slice reverse! drop ] keep ;
: (next-permutation) ( seq -- seq )
dup cut-point [
swap [ greater-from-last ] 2keep
[ exchange ] [ reverse-tail! nip ] 3bi
] [ reverse! ] if* ;
PRIVATE>
: next-permutation ( seq -- seq )
dup [ ] [ drop (next-permutation) ] if-empty ;
! Combinadic-based combination methodology
<PRIVATE
TUPLE: combo
{ seq sequence }
{ k integer } ;
C: <combo> combo
2009-05-06 20:18:21 -04:00
: choose ( combo -- nCk )
[ seq>> length ] [ k>> ] bi nCk ;
: largest-value ( a b x -- v )
dup 0 = [
drop 1 - nip
] [
2010-01-14 14:05:50 -05:00
[ iota ] 2dip '[ _ nCk _ >=< ] search nip
] if ;
:: next-values ( a b x -- a' b' x' v )
a b x largest-value dup :> v ! a'
b 1 - ! b'
x v b nCk - ! x'
v ; ! v == a'
2009-05-06 20:18:21 -04:00
: dual-index ( m combo -- m' )
choose 1 - swap - ;
2009-05-06 20:18:21 -04:00
: initial-values ( combo m -- n k m )
[ [ seq>> length ] [ k>> ] bi ] dip ;
: combinadic ( combo m -- combinadic )
initial-values [ over 0 > ] [ next-values ] produce
[ 3drop ] dip ;
:: combination-indices ( m combo -- seq )
combo m combo dual-index combinadic
combo seq>> length 1 - swap [ - ] with map ;
: apply-combination ( m combo -- seq )
[ combination-indices ] keep seq>> nths ;
: combinations-quot ( seq k quot -- seq quot )
2010-01-14 14:05:50 -05:00
[ <combo> [ choose iota ] keep ] dip
'[ _ apply-combination @ ] ; inline
PRIVATE>
: combination ( m seq k -- seq' )
<combo> apply-combination ;
TUPLE: combinations combo length ;
: <combinations> ( seq k -- combinations )
[ <combo> ] 2keep [ length ] [ nCk ] bi* combinations boa ;
M: combinations length length>> ; inline
M: combinations nth-unsafe combo>> apply-combination ;
M: combinations hashcode* tuple-hashcode ;
INSTANCE: combinations immutable-sequence
: each-combination ( seq k quot -- )
combinations-quot each ; inline
: map-combinations ( seq k quot -- seq' )
combinations-quot map ; inline
: filter-combinations ( seq k quot -- seq' )
selector [ each-combination ] dip ; inline
: map>assoc-combinations ( seq k quot exemplar -- )
[ combinations-quot ] dip map>assoc ; inline
: all-combinations ( seq k -- seq' )
2011-10-15 22:19:44 -04:00
[ ] map-combinations ;
: find-combination ( seq k quot -- i elt )
[ combinations-quot find drop ]
[ drop pick [ combination ] [ 3drop f ] if ] 3bi ; inline
: reduce-combinations ( seq k identity quot -- result )
[ -rot ] dip each-combination ; inline
: all-subsets ( seq -- subsets )
dup length [0,b] [ all-combinations ] with map concat ;
<PRIVATE
: (selections) ( seq n -- selections )
[ [ 1array ] map dup ] [ 1 - ] bi* [
cartesian-product concat [ { } concat-as ] map
] with times ;
PRIVATE>
: selections ( seq n -- selections )
dup 0 > [ (selections) ] [ 2drop { } ] if ;