2006-07-14 05:36:26 -04:00
|
|
|
! Copyright (C) 2006 Slava Pestov.
|
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
|
IN: topology
|
|
|
|
|
USING: arrays hashtables hashtables io kernel math math
|
|
|
|
|
namespaces parser prettyprint sequences words ;
|
|
|
|
|
|
|
|
|
|
: SYMBOLS:
|
|
|
|
|
string-mode on
|
|
|
|
|
[ string-mode off [ create-in define-symbol ] each ] f ;
|
|
|
|
|
parsing
|
|
|
|
|
|
|
|
|
|
: canonicalize
|
|
|
|
|
[ nip zero? not ] hash-subset ;
|
|
|
|
|
|
2006-08-27 16:51:27 -04:00
|
|
|
SYMBOL: terms
|
|
|
|
|
|
|
|
|
|
: with-terms ( quot -- hash )
|
|
|
|
|
[ H{ } clone terms set call terms get ] with-scope ; inline
|
|
|
|
|
|
2006-07-14 05:36:26 -04:00
|
|
|
: (l+) ( x -- )
|
2006-08-27 16:51:27 -04:00
|
|
|
terms get [ [ swap +@ ] hash-each ] bind ;
|
2006-07-14 05:36:26 -04:00
|
|
|
|
|
|
|
|
: l+ ( x y -- x+y )
|
2006-08-27 16:51:27 -04:00
|
|
|
[ (l+) (l+) ] with-terms canonicalize ;
|
2006-07-14 05:36:26 -04:00
|
|
|
|
|
|
|
|
: l* ( vec n -- vec )
|
|
|
|
|
dup zero? [
|
|
|
|
|
2drop H{ }
|
|
|
|
|
] [
|
|
|
|
|
swap
|
|
|
|
|
hash>alist [ first2 rot * 2array ] map-with alist>hash
|
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
|
|
: num-l. ( n -- str )
|
|
|
|
|
{
|
|
|
|
|
{ [ dup 1 = ] [ drop " + " ] }
|
|
|
|
|
{ [ dup -1 = ] [ drop " - " ] }
|
|
|
|
|
{ [ t ] [ number>string " + " swap append ] }
|
|
|
|
|
} cond ;
|
|
|
|
|
|
|
|
|
|
: (l.) ( assoc -- )
|
|
|
|
|
dup empty? [
|
|
|
|
|
drop 0 .
|
|
|
|
|
] [
|
|
|
|
|
[
|
|
|
|
|
first2 num-l.
|
|
|
|
|
swap [ unparse ] map "." join
|
|
|
|
|
append
|
|
|
|
|
] map concat " + " ?head drop print
|
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
|
|
: l. ( vec -- ) hash>alist (l.) ;
|
|
|
|
|
|
|
|
|
|
: linear-op ( vec quot -- vec )
|
2006-08-27 16:51:27 -04:00
|
|
|
[
|
2006-07-14 05:36:26 -04:00
|
|
|
swap [
|
|
|
|
|
>r swap call r> l* (l+)
|
|
|
|
|
] hash-each-with
|
2006-08-27 16:51:27 -04:00
|
|
|
] with-terms canonicalize ; inline
|
2006-07-14 05:36:26 -04:00
|
|
|
|
|
|
|
|
: -1^ odd? -1 1 ? ;
|
|
|
|
|
|
|
|
|
|
: (op-matrix) ( range quot basis-elt -- row )
|
|
|
|
|
swap call swap [ swap hash [ 0 ] unless* ] map-with ; inline
|
|
|
|
|
|
|
|
|
|
: op-matrix ( domain range quot -- matrix )
|
2006-08-27 16:51:27 -04:00
|
|
|
rot [ >r 2dup r> (op-matrix) ] map 2nip ; inline
|
2006-07-14 06:36:50 -04:00
|
|
|
|
|
|
|
|
: rot-seq 1 swap cut swap append ;
|
|
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: (H) ( sim -- seq ) flip first2 rot-seq v- ;
|
2006-07-14 06:36:50 -04:00
|
|
|
|
|
|
|
|
: -rot-seq 1 swap cut* swap append ;
|
|
|
|
|
|
2006-08-18 16:15:08 -04:00
|
|
|
: (H*) ( sim -- seq ) flip first2 -rot-seq v- ;
|