1 USING: accessors arrays io kernel math namespaces splitting
2 prettyprint sequences sorting vectors words inverse summary
3 shuffle math.functions sets ;
6 TUPLE: dimensioned value top bot ;
8 TUPLE: dimensions-not-equal ;
10 : dimensions-not-equal ( -- * )
11 \ dimensions-not-equal new throw ;
13 M: dimensions-not-equal summary drop "Dimensions do not match" ;
15 : remove-one ( seq obj -- seq )
16 1array split1 append ;
18 : 2remove-one ( seq seq obj -- seq seq )
19 [ remove-one ] curry bi@ ;
21 : symbolic-reduce ( seq seq -- seq seq )
23 [ first 2remove-one symbolic-reduce ] unless-empty ;
25 : <dimensioned> ( n top bot -- obj )
30 : >dimensioned< ( d -- n top bot )
31 [ value>> ] [ top>> ] [ bot>> ] tri ;
33 \ <dimensioned> [ >dimensioned< ] define-inverse
35 : dimensions ( dimensioned -- top bot )
36 [ top>> ] [ bot>> ] bi ;
38 : check-dimensions ( d d -- )
39 [ dimensions 2array ] bi@ =
40 [ dimensions-not-equal ] unless ;
42 : 2values ( dim dim -- val val ) [ value>> ] bi@ ;
44 : <dimension-op ( dim dim -- top bot val val )
45 2dup check-dimensions dup dimensions 2swap 2values ;
47 : dimension-op> ( top bot val -- dim )
50 : d+ ( d d -- d ) <dimension-op + dimension-op> ;
52 : d- ( d d -- d ) <dimension-op - dimension-op> ;
55 { } { } <dimensioned> ;
58 [ dup number? [ scalar ] when ] bi@
59 [ [ top>> ] bi@ append ] 2keep
60 [ [ bot>> ] bi@ append ] 2keep
61 2values * dimension-op> ;
63 : d-neg ( d -- d ) -1 d* ;
65 : d-sq ( d -- d ) dup d* ;
68 >dimensioned< spin recip dimension-op> ;
70 : d/ ( d d -- d ) d-recip d* ;
72 : comparison-op ( d d -- n n ) 2dup check-dimensions 2values ;
74 : d< ( d d -- ? ) comparison-op < ;
76 : d<= ( d d -- ? ) comparison-op <= ;
78 : d> ( d d -- ? ) comparison-op > ;
80 : d>= ( d d -- ? ) comparison-op >= ;
82 : d= ( d d -- ? ) comparison-op number= ;
84 : d~ ( d d delta -- ? ) [ comparison-op ] dip ~ ;
86 : d-min ( d d -- d ) [ d< ] most ;
88 : d-max ( d d -- d ) [ d> ] most ;
90 : d-product ( v -- d ) 1 scalar [ d* ] reduce ;
92 : d-sum ( v -- d ) unclip-slice [ d+ ] reduce ;
94 : d-infimum ( v -- d ) unclip-slice [ d-min ] reduce ;
96 : d-supremum ( v -- d ) unclip-slice [ d-max ] reduce ;
98 \ d+ [ d- ] [ d- ] define-math-inverse
99 \ d- [ d+ ] [ d- ] define-math-inverse
100 \ d* [ d/ ] [ d/ ] define-math-inverse
101 \ d/ [ d* ] [ d/ ] define-math-inverse
102 \ d-recip [ d-recip ] define-inverse