2006-07-14 03:29:42 -04:00
|
|
|
! Copyright (C) 2006 Slava Pestov.
|
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2006-07-14 06:36:50 -04:00
|
|
|
USING: arrays errors hashtables io kernel math matrices
|
|
|
|
|
namespaces parser prettyprint sequences topology words ;
|
2006-07-09 21:57:19 -04:00
|
|
|
IN: hopf
|
|
|
|
|
|
2006-07-14 03:29:42 -04:00
|
|
|
! Finitely generated Hopf algebras.
|
|
|
|
|
|
2006-07-09 21:57:19 -04:00
|
|
|
! An element is represented as a hashtable mapping basis
|
|
|
|
|
! elements to scalars.
|
|
|
|
|
|
2006-07-10 00:50:51 -04:00
|
|
|
! A basis element is a pair of arrays, odd/even generators.
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-10 00:50:51 -04:00
|
|
|
! Define degrees using deg=
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
! Add elements using l+
|
2006-07-10 00:50:51 -04:00
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
! Multiply elements using h*
|
2006-07-10 00:50:51 -04:00
|
|
|
|
|
|
|
|
! The co-unit is co1
|
|
|
|
|
|
|
|
|
|
! Print elements using h.
|
|
|
|
|
|
|
|
|
|
! Define the differential using d=
|
|
|
|
|
|
|
|
|
|
! Differentiate using d
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: ?set-hash ( value key hash/f -- hash )
|
|
|
|
|
[ [ set-hash ] keep ] [ associate ] if* ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
SYMBOL: degrees
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: deg= degrees [ ?set-hash ] change ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: deg degrees get ?hash [ 1 ] unless* ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
: h. ( vec -- )
|
|
|
|
|
hash>alist [ first2 >r concat r> 2array ] map (l.) ;
|
|
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: <basis-elt> ( generators -- pair )
|
|
|
|
|
#! Pair is { odd even }
|
2006-07-09 21:57:19 -04:00
|
|
|
V{ } clone V{ } clone
|
|
|
|
|
rot [
|
|
|
|
|
3dup deg odd? [ drop ] [ nip ] if push
|
|
|
|
|
] each [ >array ] 2apply 2array ;
|
|
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: [0] { { } { } } ;
|
|
|
|
|
|
2006-07-09 21:57:19 -04:00
|
|
|
: >h ( obj -- vec )
|
|
|
|
|
{
|
|
|
|
|
{ [ dup not ] [ drop 0 >h ] }
|
2006-08-18 16:15:08 -04:00
|
|
|
{ [ dup number? ] [ [0] associate ] }
|
2006-07-09 21:57:19 -04:00
|
|
|
{ [ dup array? ] [ <basis-elt> 1 swap associate ] }
|
|
|
|
|
{ [ dup hashtable? ] [ ] }
|
|
|
|
|
{ [ t ] [ 1array >h ] }
|
|
|
|
|
} cond ;
|
|
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: co1 ( vec -- n ) [0] swap hash [ 0 ] unless* ;
|
2006-07-10 00:50:51 -04:00
|
|
|
|
2006-07-09 21:57:19 -04:00
|
|
|
: permutation ( seq -- perm )
|
|
|
|
|
dup natural-sort [ swap index ] map-with ;
|
|
|
|
|
|
|
|
|
|
: (inversions) ( n seq -- n )
|
|
|
|
|
[ > ] subset-with length ;
|
|
|
|
|
|
|
|
|
|
: inversions ( seq -- n )
|
|
|
|
|
0 swap dup length [
|
2006-07-29 20:36:25 -04:00
|
|
|
swap [ nth ] 2keep swap 1+ tail-slice (inversions) +
|
2006-07-09 21:57:19 -04:00
|
|
|
] each-with ;
|
|
|
|
|
|
|
|
|
|
: duplicates? ( seq -- ? )
|
|
|
|
|
dup prune [ length ] 2apply > ;
|
|
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: (odd*) ( n terms -- n terms )
|
|
|
|
|
dup duplicates? [
|
2006-07-09 21:57:19 -04:00
|
|
|
2drop 0 { }
|
|
|
|
|
] [
|
|
|
|
|
dup permutation inversions -1^ rot *
|
|
|
|
|
swap natural-sort
|
|
|
|
|
] if ;
|
|
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: odd* ( n terms1 terms2 -- n terms )
|
|
|
|
|
append (odd*) ;
|
|
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
: even* ( terms1 terms2 -- terms )
|
2006-07-09 21:57:19 -04:00
|
|
|
append natural-sort ;
|
|
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
: (h*) ( n basis1 basis2 -- n basis )
|
2006-07-09 21:57:19 -04:00
|
|
|
[
|
2006-07-14 05:36:26 -04:00
|
|
|
[ first ] 2apply odd*
|
|
|
|
|
] 2keep [ second ] 2apply even* 2array ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
: h* ( x y -- x.y )
|
2006-07-09 21:57:19 -04:00
|
|
|
[ >h ] 2apply [
|
|
|
|
|
[
|
|
|
|
|
rot [
|
|
|
|
|
2swap [
|
2006-07-14 05:36:26 -04:00
|
|
|
swapd * -rot (h*) +@
|
2006-07-09 21:57:19 -04:00
|
|
|
] 2keep
|
|
|
|
|
] hash-each 2drop
|
|
|
|
|
] hash-each-with
|
|
|
|
|
] make-hash canonicalize ;
|
|
|
|
|
|
|
|
|
|
SYMBOL: boundaries
|
|
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: d= ( value basis -- )
|
|
|
|
|
boundaries [ ?set-hash ] change ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: ((d)) ( basis -- value ) boundaries get ?hash ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
: dx.y ( x y -- vec ) >r ((d)) r> h* ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
|
|
|
|
DEFER: (d)
|
|
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: x.dy ( x y -- vec ) >r [ deg -1^ ] keep r> (d) h* h* ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
|
|
|
|
: (d) ( product -- value )
|
2006-08-18 16:15:08 -04:00
|
|
|
#! d(x.y)=dx.y + (-1)^deg x x.dy
|
2006-07-09 21:57:19 -04:00
|
|
|
dup empty?
|
2006-07-14 06:36:50 -04:00
|
|
|
[ drop H{ } ] [ unclip swap [ x.dy ] 2keep dx.y l+ ] if ;
|
2006-07-09 21:57:19 -04:00
|
|
|
|
|
|
|
|
: d ( x -- dx )
|
2006-07-14 05:36:26 -04:00
|
|
|
>h [ concat (d) ] linear-op ;
|
2006-07-14 06:36:50 -04:00
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: d-matrix ( n seq -- matrix )
|
2006-07-14 06:36:50 -04:00
|
|
|
[ ?nth ] 2keep >r 1+ r> ?nth [ concat (d) ] op-matrix ;
|
|
|
|
|
|
|
|
|
|
: ker/im-d ( sim -- seq )
|
|
|
|
|
#! Dimension of kernel of C_{n+1} --> C_n, subsp. of C_{n+1}
|
|
|
|
|
#! Dimension of image C_{n+1} --> C_n, subsp. of C_n
|
|
|
|
|
dup length [ swap d-matrix null/rank 2array ] map-with ;
|
|
|
|
|
|
|
|
|
|
: nth-bit? ( m bit# -- ? )
|
|
|
|
|
1 swap shift bitand 0 > ;
|
|
|
|
|
|
|
|
|
|
: nth-basis-elt ( generators n -- elt )
|
|
|
|
|
over length [
|
|
|
|
|
3dup nth-bit? [ nth ] [ 2drop f ] if
|
|
|
|
|
] map [ ] subset 2nip ;
|
|
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
SYMBOL: generators
|
|
|
|
|
|
2006-07-14 06:36:50 -04:00
|
|
|
: basis ( generators -- seq )
|
|
|
|
|
[
|
|
|
|
|
dup length 1+ [ drop V{ } clone ] map \ basis set
|
|
|
|
|
1 over length shift [
|
|
|
|
|
nth-basis-elt dup length \ basis get nth push
|
|
|
|
|
] each-with
|
|
|
|
|
\ basis get [ [ { } 2array ] map ] map
|
|
|
|
|
] with-scope ;
|
|
|
|
|
|
2006-07-27 18:57:44 -04:00
|
|
|
: H* ( -- seq ) generators get basis ker/im-d (H*) ;
|