1 ! Copyright (C) 2005, 2010 Slava Pestov, Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays columns kernel locals math math.bits
4 math.functions math.order math.vectors sequences
5 sequences.private fry math.statistics grouping
6 combinators.short-circuit math.ranges combinators.smart ;
10 : make-matrix ( m n quot -- matrix )
11 '[ _ _ replicate ] replicate ; inline
13 : <matrix> ( m n element -- matrix )
14 '[ _ _ <array> ] replicate ; inline
16 : zero-matrix ( m n -- matrix )
19 : diagonal-matrix ( diagonal-seq -- matrix )
20 dup length dup zero-matrix
21 [ '[ dup _ nth set-nth ] each-index ] keep ; inline
23 : identity-matrix ( n -- matrix )
24 1 <repetition> diagonal-matrix ; inline
26 : eye ( m n k -- matrix )
27 [ [ iota ] bi@ ] dip neg '[ _ + = 1 0 ? ] cartesian-map ;
29 : hilbert-matrix ( m n -- matrix )
30 [ iota ] bi@ [ + 1 + recip ] cartesian-map ;
32 : toeplitz-matrix ( n -- matrix )
33 iota dup [ - abs 1 + ] cartesian-map ;
35 : hankel-matrix ( n -- matrix )
36 [ iota dup ] keep '[ + abs 1 + dup _ > [ drop 0 ] when ] cartesian-map ;
38 : box-matrix ( r -- matrix )
39 2 * 1 + dup '[ _ 1 <array> ] replicate ;
41 : vandermonde-matrix ( u n -- matrix )
42 iota [ v^n ] with map reverse flip ;
44 :: rotation-matrix3 ( axis theta -- matrix )
47 axis first3 :> ( x y z )
48 x sq 1.0 x sq - c * + x y * 1.0 c - * z s * - x z * 1.0 c - * y s * + 3array
49 x y * 1.0 c - * z s * + y sq 1.0 y sq - c * + y z * 1.0 c - * x s * - 3array
50 x z * 1.0 c - * y s * - y z * 1.0 c - * x s * + z sq 1.0 z sq - c * + 3array
53 :: rotation-matrix4 ( axis theta -- matrix )
56 axis first3 :> ( x y z )
57 x sq 1.0 x sq - c * + x y * 1.0 c - * z s * - x z * 1.0 c - * y s * + 0 4array
58 x y * 1.0 c - * z s * + y sq 1.0 y sq - c * + y z * 1.0 c - * x s * - 0 4array
59 x z * 1.0 c - * y s * - y z * 1.0 c - * x s * + z sq 1.0 z sq - c * + 0 4array
60 { 0.0 0.0 0.0 1.0 } 4array ;
62 :: translation-matrix4 ( offset -- matrix )
63 offset first3 :> ( x y z )
71 : >scale-factors ( number/sequence -- x y z )
72 dup number? [ dup dup ] [ first3 ] if ;
74 :: scale-matrix3 ( factors -- matrix )
75 factors >scale-factors :> ( x y z )
82 :: scale-matrix4 ( factors -- matrix )
83 factors >scale-factors :> ( x y z )
91 : ortho-matrix4 ( dim -- matrix )
92 [ recip ] map scale-matrix4 ;
94 :: frustum-matrix4 ( xy-dim near far -- matrix )
95 xy-dim first2 :> ( x y )
98 near far + near far - /f :> zf
99 2 near far * * near far - /f :> wf
108 :: skew-matrix4 ( theta -- matrix )
119 : mneg ( m -- m ) [ vneg ] map ;
121 : n*m ( n m -- m ) [ n*v ] with map ;
122 : m*n ( m n -- m ) [ v*n ] curry map ;
123 : n/m ( n m -- m ) [ n/v ] with map ;
124 : m/n ( m n -- m ) [ v/n ] curry map ;
126 : m+ ( m m -- m ) [ v+ ] 2map ;
127 : m- ( m m -- m ) [ v- ] 2map ;
128 : m* ( m m -- m ) [ v* ] 2map ;
129 : m/ ( m m -- m ) [ v/ ] 2map ;
131 : v.m ( v m -- v ) flip [ v. ] with map ;
132 : m.v ( m v -- v ) [ v. ] curry map ;
133 : m. ( m m -- m ) flip [ swap m.v ] curry map ;
135 : m~ ( m m epsilon -- ? ) [ v~ ] curry 2all? ;
137 : mmin ( m -- n ) [ 1/0. ] dip [ [ min ] each ] each ;
138 : mmax ( m -- n ) [ -1/0. ] dip [ [ max ] each ] each ;
139 : mnorm ( m -- n ) dup mmax abs m/n ;
141 : cross ( vec1 vec2 -- vec3 )
142 [ [ { 1 2 0 } vshuffle ] [ { 2 0 1 } vshuffle ] bi* v* ]
143 [ [ { 2 0 1 } vshuffle ] [ { 1 2 0 } vshuffle ] bi* v* ] 2bi v- ; inline
145 :: normal ( vec1 vec2 vec3 -- vec4 )
146 vec2 vec1 v- vec3 vec1 v- cross normalize ; inline
149 [ [ v. ] [ norm-sq ] bi / ] keep n*v ;
154 : angle-between ( v u -- a )
155 [ normalize ] bi@ h. acos ;
157 : (gram-schmidt) ( v seq -- newseq )
158 [ dupd proj v- ] each ;
160 : gram-schmidt ( seq -- orthogonal )
161 V{ } clone [ over (gram-schmidt) over push ] reduce ;
163 : norm-gram-schmidt ( seq -- orthonormal )
164 gram-schmidt [ normalize ] map ;
167 make-bits over first length identity-matrix
168 [ [ dupd m. ] when [ dup m. ] dip ] reduce nip ;
171 [ ] [ [ append ] 2map ] map-reduce ;
173 : kron ( m1 m2 -- m )
174 '[ [ _ n*m ] map ] map stitch stitch ;
179 : row ( n matrix -- col )
182 : rows ( seq matrix -- cols )
183 '[ _ row ] map ; inline
185 : col ( n matrix -- col )
186 swap '[ _ swap nth ] map ; inline
188 : cols ( seq matrix -- cols )
189 '[ _ col ] map ; inline
191 : set-index ( object pair matrix -- )
192 [ first2 swap ] dip nth set-nth ; inline
194 : set-indices ( object sequence matrix -- )
195 '[ _ set-index ] with each ; inline
198 : matrix-map ( matrix quot -- )
199 '[ _ map ] map ; inline
201 : column-map ( matrix quot -- seq )
202 [ [ first length iota ] keep ] dip '[ _ col @ ] map ; inline
204 : cartesian-square-indices ( n -- matrix )
205 iota dup cartesian-product ; inline
207 : cartesian-matrix-map ( matrix quot -- matrix' )
208 [ [ first length cartesian-square-indices ] keep ] dip
209 '[ _ @ ] matrix-map ; inline
211 : cartesian-matrix-column-map ( matrix quot -- matrix' )
212 [ cols first2 ] prepose cartesian-matrix-map ; inline
214 : cov-matrix-ddof ( matrix ddof -- cov )
215 '[ _ cov-ddof ] cartesian-matrix-column-map ; inline
217 : cov-matrix ( matrix -- cov ) 0 cov-matrix-ddof ; inline
219 : sample-cov-matrix ( matrix -- cov ) 1 cov-matrix-ddof ; inline
221 GENERIC: square-rows ( object -- matrix )
222 M: integer square-rows iota square-rows ;
223 M: sequence square-rows dup [ nip ] cartesian-map ;
225 GENERIC: square-cols ( object -- matrix )
226 M: integer square-cols iota square-cols ;
227 M: sequence square-cols dup [ drop ] cartesian-map ;
229 : make-matrix-with-indices ( m n quot -- matrix )
230 [ [ iota ] bi@ ] dip '[ @ ] cartesian-map ; inline
232 : null-matrix? ( matrix -- ? ) empty? ;
234 : well-formed-matrix? ( matrix -- ? )
238 [ ] [ first length ] bi
242 : dim ( matrix -- pair/f )
244 [ [ length ] [ first length ] bi 2array ] if-empty ;
246 : square-matrix? ( matrix -- ? )
247 { [ well-formed-matrix? ] [ dim all-eq? ] } 1&& ;
249 : matrix-coordinates ( dim -- coordinates )
250 first2 [ iota ] bi@ cartesian-product ; inline
252 : dimension-range ( matrix -- dim range )
253 dim [ matrix-coordinates ] [ first [1,b] ] bi ;
255 : upper-matrix-indices ( matrix -- matrix' )
256 dimension-range <reversed> [ tail-slice* >array ] 2map concat ;
258 : lower-matrix-indices ( matrix -- matrix' )
259 dimension-range [ head-slice >array ] 2map concat ;
262 : make-lower-matrix ( object m n -- matrix )
263 zero-matrix [ lower-matrix-indices ] [ set-indices ] [ ] tri ;
265 : make-upper-matrix ( object m n -- matrix )
266 zero-matrix [ upper-matrix-indices ] [ set-indices ] [ ] tri ;