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 ;
7 FROM: namespaces => set ;
11 : -1^ ( m -- n ) odd? -1 1 ? ;
15 { [ dup not ] [ drop 0 >alt ] }
16 { [ dup number? ] [ { } associate ] }
17 { [ dup array? ] [ 1 swap associate ] }
18 { [ dup hashtable? ] [ ] }
22 : canonicalize ( assoc -- assoc' )
23 [ nip zero? ] assoc-reject ;
27 : with-terms ( quot -- hash )
29 H{ } clone terms set call terms get canonicalize
33 : num-alt. ( n -- str )
37 [ number>string " + " prepend ]
40 : (alt.) ( basis n -- str )
45 swap [ name>> ] map "." join
53 [ (alt.) ] { } assoc>map concat " + " ?head drop print
58 terms get [ [ swap +@ ] assoc-each ] with-variables ;
61 [ >alt ] bi@ [ (alt+) (alt+) ] with-terms ;
64 : alt*n ( vec n -- vec )
71 : permutation ( seq -- perm )
72 [ natural-sort ] keep [ index ] curry map ;
74 : (inversions) ( n seq -- n )
77 : inversions ( seq -- n )
78 0 swap [ length iota ] keep [
79 [ nth ] 2keep swap 1 + tail-slice (inversions) +
82 : (wedge) ( n basis1 basis2 -- n basis )
83 append dup all-unique? not [
86 dup permutation inversions -1^ rot *
90 : wedge ( x y -- x.y )
95 swapd * -rot (wedge) _ at+
99 ] H{ } make canonicalize ;
104 : d= ( value basis -- )
105 boundaries [ ?set-at ] change ;
107 : ((d)) ( basis -- value ) boundaries get at ;
109 : dx.y ( x y -- vec ) [ ((d)) ] dip wedge ;
113 : x.dy ( x y -- vec ) (d) wedge -1 alt*n ;
115 : (d) ( product -- value )
116 [ H{ } ] [ unclip swap [ x.dy ] 2keep dx.y alt+ ] if-empty ;
118 : linear-op ( vec quot -- vec )
121 -rot [ swap call ] dip alt*n (alt+)
123 ] with-terms ; inline
126 >alt [ (d) ] linear-op ;
129 : (interior) ( y basis-elt -- i_y[basis-elt] )
131 -rot remove associate
136 : interior ( x y -- i_y[x] )
138 swap >alt [ dupd (interior) ] linear-op nip ;
141 : graded ( seq -- seq )
142 dup 0 [ length max ] reduce 1 + [ V{ } clone ] replicate
143 [ dup length pick nth push ] reduce ;
145 : nth-basis-elt ( generators n -- elt )
147 3dup bit? [ nth ] [ 2drop f ] if
150 : basis ( generators -- seq )
151 natural-sort dup length 2^ iota [ nth-basis-elt ] with map ;
153 : (tensor) ( seq1 seq2 -- seq )
155 [ prepend natural-sort ] curry map
158 : tensor ( graded-basis1 graded-basis2 -- bigraded-basis )
159 [ [ swap (tensor) ] curry map ] with map ;
161 ! Computing cohomology
162 : (op-matrix) ( range quot basis-elt -- row )
163 swap call [ at 0 or ] curry map ; inline
165 : op-matrix ( domain range quot -- matrix )
166 rot [ (op-matrix) ] 2with map ; inline
168 : d-matrix ( domain range -- matrix )
171 : dim-im/ker-d ( domain range -- null/rank )
172 d-matrix null/rank 2array ;
175 : (graded-ker/im-d) ( n seq -- null/rank )
176 #! d: C(n) ---> C(n+1)
177 [ ?nth ] [ [ 1 + ] dip ?nth ] 2bi
180 : graded-ker/im-d ( graded-basis -- seq )
181 [ length iota ] keep [ (graded-ker/im-d) ] curry map ;
183 : graded-betti ( generators -- seq )
184 basis graded graded-ker/im-d unzip but-last 0 prefix v- ;
186 ! Bi-graded for two-step complexes
187 : (bigraded-ker/im-d) ( u-deg z-deg bigraded-basis -- null/rank )
188 #! d: C(u,z) ---> C(u+2,z-1)
189 [ ?nth ?nth ] 3keep [ [ 2 + ] dip 1 - ] dip ?nth ?nth
192 :: bigraded-ker/im-d ( basis -- seq )
193 basis length iota [| z |
194 basis first length iota [| u |
195 u z basis (bigraded-ker/im-d)
199 : bigraded-betti ( u-generators z-generators -- seq )
200 [ basis graded ] bi@ tensor bigraded-ker/im-d
201 [ [ keys ] map ] keep
202 [ values 2 head* { 0 0 } prepend ] map
203 rest dup first length 0 <array> suffix
207 : m.m' ( matrix -- matrix' ) dup flip m. ;
208 : m'.m ( matrix -- matrix' ) dup flip swap m. ;
210 : empty-matrix? ( matrix -- ? )
211 [ t ] [ first empty? ] if-empty ;
213 : ?m+ ( m1 m2 -- m3 )
224 : laplacian-matrix ( basis1 basis2 basis3 -- matrix )
225 dupd d-matrix m.m' [ d-matrix m'.m ] dip ?m+ ;
227 : laplacian-betti ( basis1 basis2 basis3 -- n )
228 laplacian-matrix null/rank drop ;
230 :: laplacian-kernel ( basis1 basis2 basis3 -- basis )
231 basis1 basis2 basis3 laplacian-matrix :> lap
232 lap empty-matrix? [ f ] [
234 basis2 x [ [ wedge (alt+) ] 2each ] with-terms
238 : graded-triple ( seq n -- triple )
239 3 [ 1 - + ] with map swap [ ?nth ] curry map ;
241 : graded-triples ( seq -- triples )
242 dup length [ graded-triple ] with map ;
244 : graded-laplacian ( generators quot -- seq )
245 [ basis graded graded-triples [ first3 ] ] dip compose map ; inline
247 : graded-laplacian-betti ( generators -- seq )
248 [ laplacian-betti ] graded-laplacian ;
250 : graded-laplacian-kernel ( generators -- seq )
251 [ laplacian-kernel ] graded-laplacian ;
253 : graded-basis. ( seq -- )
255 "=== Degree " write pprint
256 ": dimension " write dup length .
260 : bigraded-triple ( u-deg z-deg bigraded-basis -- triple )
261 #! d: C(u,z) ---> C(u+2,z-1)
262 [ [ 2 - ] [ 1 + ] [ ] tri* ?nth ?nth ]
264 [ [ 2 + ] [ 1 - ] [ ] tri* ?nth ?nth ]
268 :: bigraded-triples ( grid -- triples )
269 grid length iota [| z |
270 grid first length iota [| u |
271 u z grid bigraded-triple
275 : bigraded-laplacian ( u-generators z-generators quot -- seq )
276 [ [ basis graded ] bi@ tensor bigraded-triples ] dip
277 [ [ first3 ] prepose map ] curry map ; inline
279 : bigraded-laplacian-betti ( u-generators z-generators -- seq )
280 [ laplacian-betti ] bigraded-laplacian ;
282 : bigraded-laplacian-kernel ( u-generators z-generators -- seq )
283 [ laplacian-kernel ] bigraded-laplacian ;
285 : bigraded-basis. ( seq -- )
287 "=== U-degree " write .
289 " === Z-degree " write pprint
290 ": dimension " write dup length .
291 [ " " write alt. ] each