1 ! Copyright (C) 2006, 2007 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators fry hashtables io
4 kernel locals make math math.matrices math.matrices.elimination
5 math.order math.parser math.vectors namespaces prettyprint
6 sequences sets shuffle sorting splitting ;
10 : -1^ ( m -- n ) odd? -1 1 ? ;
14 { [ dup not ] [ drop 0 >alt ] }
15 { [ dup number? ] [ { } associate ] }
16 { [ dup array? ] [ 1 swap associate ] }
17 { [ dup hashtable? ] [ ] }
21 : canonicalize ( assoc -- assoc' )
22 [ nip zero? ] assoc-reject ;
26 : with-terms ( quot -- hash )
28 H{ } clone terms namespaces:set call terms get canonicalize
32 : num-alt. ( n -- str )
36 [ number>string " + " prepend ]
39 : (alt.) ( basis n -- str )
44 swap [ name>> ] map "." join
52 [ (alt.) ] { } assoc>map concat " + " ?head drop print
57 terms get [ [ swap +@ ] assoc-each ] with-variables ;
60 [ >alt ] bi@ [ (alt+) (alt+) ] with-terms ;
63 : alt*n ( vec n -- vec )
70 : permutation ( seq -- perm )
71 [ natural-sort ] keep [ index ] curry map ;
73 : (inversions) ( n seq -- n )
76 : inversions ( seq -- n )
77 0 swap [ length <iota> ] keep [
78 [ nth ] 2keep swap 1 + tail-slice (inversions) +
81 : (wedge) ( n basis1 basis2 -- n basis )
82 append dup all-unique? not [
85 dup permutation inversions -1^ rot *
89 : wedge ( x y -- x.y )
94 swapd * -rot (wedge) _ at+
98 ] H{ } make canonicalize ;
103 : d= ( value basis -- )
104 boundaries [ ?set-at ] change ;
106 : get-boundary ( basis -- value ) boundaries get at ;
108 : dx.y ( x y -- vec ) [ get-boundary ] dip wedge ;
112 : x.dy ( x y -- vec ) (d) wedge -1 alt*n ;
114 : (d) ( product -- value )
115 [ H{ } ] [ unclip swap [ x.dy ] 2keep dx.y alt+ ] if-empty ;
117 : linear-op ( vec quot -- vec )
120 -rot [ swap call ] dip alt*n (alt+)
122 ] with-terms ; inline
125 >alt [ (d) ] linear-op ;
128 : (interior) ( y basis-elt -- i_y[basis-elt] )
130 -rot remove associate
135 : interior ( x y -- i_y[x] )
137 swap >alt [ dupd (interior) ] linear-op nip ;
140 : graded ( seq -- seq )
141 dup 0 [ length max ] reduce 1 + [ V{ } clone ] replicate
142 [ dup length pick nth push ] reduce ;
144 : nth-basis-elt ( generators n -- elt )
146 3dup bit? [ nth ] [ 2drop f ] if
149 : basis ( generators -- seq )
150 natural-sort dup length 2^ <iota> [ nth-basis-elt ] with map ;
152 : (tensor) ( seq1 seq2 -- seq )
154 [ prepend natural-sort ] curry map
157 : tensor ( graded-basis1 graded-basis2 -- bigraded-basis )
158 [ [ swap (tensor) ] curry map ] with map ;
160 ! Computing cohomology
161 : (op-matrix) ( range quot basis-elt -- row )
162 swap call [ at 0 or ] curry map ; inline
164 : op-matrix ( domain range quot -- matrix )
165 rot [ (op-matrix) ] 2with map ; inline
167 : d-matrix ( domain range -- matrix )
170 : dim-im/ker-d ( domain range -- null/rank )
171 d-matrix null/rank 2array ;
174 : (graded-ker/im-d) ( n seq -- null/rank )
175 ! d: C(n) ---> C(n+1)
176 [ ?nth ] [ [ 1 + ] dip ?nth ] 2bi
179 : graded-ker/im-d ( graded-basis -- seq )
180 [ length <iota> ] keep [ (graded-ker/im-d) ] curry map ;
182 : graded-betti ( generators -- seq )
183 basis graded graded-ker/im-d unzip but-last 0 prefix v- ;
185 ! Bi-graded for two-step complexes
186 : (bigraded-ker/im-d) ( u-deg z-deg bigraded-basis -- null/rank )
187 ! d: C(u,z) ---> C(u+2,z-1)
188 [ ?nth ?nth ] 3keep [ [ 2 + ] dip 1 - ] dip ?nth ?nth
191 :: bigraded-ker/im-d ( basis -- seq )
192 basis length <iota> [| z |
193 basis first length <iota> [| u |
194 u z basis (bigraded-ker/im-d)
198 : bigraded-betti ( u-generators z-generators -- seq )
199 [ basis graded ] bi@ tensor bigraded-ker/im-d
200 [ [ keys ] map ] keep
201 [ values 2 head* { 0 0 } prepend ] map
202 rest dup first length 0 <array> suffix
206 : m.m' ( matrix -- matrix' ) dup flip m. ;
207 : m'.m ( matrix -- matrix' ) dup flip swap m. ;
209 : empty-matrix? ( matrix -- ? )
210 [ t ] [ first empty? ] if-empty ;
212 : ?m+ ( m1 m2 -- m3 )
223 : laplacian-matrix ( basis1 basis2 basis3 -- matrix )
224 dupd d-matrix m.m' [ d-matrix m'.m ] dip ?m+ ;
226 : laplacian-betti ( basis1 basis2 basis3 -- n )
227 laplacian-matrix null/rank drop ;
229 :: laplacian-kernel ( basis1 basis2 basis3 -- basis )
230 basis1 basis2 basis3 laplacian-matrix :> lap
231 lap empty-matrix? [ f ] [
233 basis2 x [ [ wedge (alt+) ] 2each ] with-terms
237 : graded-triple ( seq n -- triple )
238 3 [ 1 - + ] with map swap [ ?nth ] curry map ;
240 : graded-triples ( seq -- triples )
241 dup length [ graded-triple ] with map ;
243 : graded-laplacian ( generators quot -- seq )
244 [ basis graded graded-triples [ first3 ] ] dip compose map ; inline
246 : graded-laplacian-betti ( generators -- seq )
247 [ laplacian-betti ] graded-laplacian ;
249 : graded-laplacian-kernel ( generators -- seq )
250 [ laplacian-kernel ] graded-laplacian ;
252 : graded-basis. ( seq -- )
254 "=== Degree " write pprint
255 ": dimension " write dup length .
259 : bigraded-triple ( u-deg z-deg bigraded-basis -- triple )
260 ! d: C(u,z) ---> C(u+2,z-1)
261 [ [ 2 - ] [ 1 + ] [ ] tri* ?nth ?nth ]
263 [ [ 2 + ] [ 1 - ] [ ] tri* ?nth ?nth ]
267 :: bigraded-triples ( grid -- triples )
268 grid length <iota> [| z |
269 grid first length <iota> [| u |
270 u z grid bigraded-triple
274 : bigraded-laplacian ( u-generators z-generators quot -- seq )
275 [ [ basis graded ] bi@ tensor bigraded-triples ] dip
276 [ [ first3 ] prepose map ] curry map ; inline
278 : bigraded-laplacian-betti ( u-generators z-generators -- seq )
279 [ laplacian-betti ] bigraded-laplacian ;
281 : bigraded-laplacian-kernel ( u-generators z-generators -- seq )
282 [ laplacian-kernel ] bigraded-laplacian ;
284 : bigraded-basis. ( seq -- )
286 "=== U-degree " write .
288 " === Z-degree " write pprint
289 ": dimension " write dup length .
290 [ " " write alt. ] each