]> gitweb.factorcode.org Git - factor.git/blob - basis/math/matrices/matrices.factor
math.matrices: Add m+n, m-n, n+m, n-m, words.
[factor.git] / basis / math / matrices / matrices.factor
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 ;
7 IN: math.matrices
8
9 ! Matrices
10 : make-matrix ( m n quot -- matrix )
11     '[ _ _ replicate ] replicate ; inline
12
13 : <matrix> ( m n element -- matrix )
14     '[ _ _ <array> ] replicate ; inline
15
16 : zero-matrix ( m n -- matrix )
17     0 <matrix> ; inline
18
19 : diagonal-matrix ( diagonal-seq -- matrix )
20     dup length dup zero-matrix
21     [ '[ dup _ nth set-nth ] each-index ] keep ; inline
22
23 : identity-matrix ( n -- matrix )
24     1 <repetition> diagonal-matrix ; inline
25
26 : eye ( m n k -- matrix )
27     [ [ iota ] bi@ ] dip neg '[ _ + = 1 0 ? ] cartesian-map ;
28
29 : hilbert-matrix ( m n -- matrix )
30     [ iota ] bi@ [ + 1 + recip ] cartesian-map ;
31
32 : toeplitz-matrix ( n -- matrix )
33     iota dup [ - abs 1 + ] cartesian-map ;
34
35 : hankel-matrix ( n -- matrix )
36     [ iota dup ] keep '[ + abs 1 + dup _ > [ drop 0 ] when ] cartesian-map ;
37
38 : box-matrix ( r -- matrix )
39     2 * 1 + dup '[ _ 1 <array> ] replicate ;
40
41 : vandermonde-matrix ( u n -- matrix )
42     iota [ v^n ] with map reverse flip ;
43
44 :: rotation-matrix3 ( axis theta -- matrix )
45     theta cos :> c
46     theta sin :> s
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
51     3array ;
52
53 :: rotation-matrix4 ( axis theta -- matrix )
54     theta cos :> c
55     theta sin :> s
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 ;
61
62 :: translation-matrix4 ( offset -- matrix )
63     offset first3 :> ( x y z )
64     {
65         { 1.0 0.0 0.0 x   }
66         { 0.0 1.0 0.0 y   }
67         { 0.0 0.0 1.0 z   }
68         { 0.0 0.0 0.0 1.0 }
69     } ;
70
71 : >scale-factors ( number/sequence -- x y z )
72     dup number? [ dup dup ] [ first3 ] if ;
73
74 :: scale-matrix3 ( factors -- matrix )
75     factors >scale-factors :> ( x y z )
76     {
77         { x   0.0 0.0 }
78         { 0.0 y   0.0 }
79         { 0.0 0.0 z   }
80     } ;
81
82 :: scale-matrix4 ( factors -- matrix )
83     factors >scale-factors :> ( x y z )
84     {
85         { x   0.0 0.0 0.0 }
86         { 0.0 y   0.0 0.0 }
87         { 0.0 0.0 z   0.0 }
88         { 0.0 0.0 0.0 1.0 }
89     } ;
90
91 : ortho-matrix4 ( dim -- matrix )
92     [ recip ] map scale-matrix4 ;
93
94 :: frustum-matrix4 ( xy-dim near far -- matrix )
95     xy-dim first2 :> ( x y )
96     near x /f :> xf
97     near y /f :> yf
98     near far + near far - /f :> zf
99     2 near far * * near far - /f :> wf
100
101     {
102         { xf  0.0  0.0 0.0 }
103         { 0.0 yf   0.0 0.0 }
104         { 0.0 0.0  zf  wf  }
105         { 0.0 0.0 -1.0 0.0 }
106     } ;
107
108 :: skew-matrix4 ( theta -- matrix )
109     theta tan :> zf
110
111     {
112         { 1.0 0.0 0.0 0.0 }
113         { 0.0 1.0 0.0 0.0 }
114         { 0.0 zf  1.0 0.0 }
115         { 0.0 0.0 0.0 1.0 }
116     } ;
117
118 ! Matrix operations
119 : mneg ( m -- m ) [ vneg ] map ;
120
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 ;
125 : n*m ( n m -- m ) [ n*v ] with map ;
126 : m*n ( m n -- m ) [ v*n ] curry map ;
127 : n/m ( n m -- m ) [ n/v ] with map ;
128 : m/n ( m n -- m ) [ v/n ] curry map ;
129
130 : m+   ( m m -- m ) [ v+ ] 2map ;
131 : m-   ( m m -- m ) [ v- ] 2map ;
132 : m*   ( m m -- m ) [ v* ] 2map ;
133 : m/   ( m m -- m ) [ v/ ] 2map ;
134
135 : v.m ( v m -- v ) flip [ v. ] with map ;
136 : m.v ( m v -- v ) [ v. ] curry map ;
137 : m.  ( m m -- m ) flip [ swap m.v ] curry map ;
138
139 : m~  ( m m epsilon -- ? ) [ v~ ] curry 2all? ;
140
141 : mmin ( m -- n ) [ 1/0. ] dip [ [ min ] each ] each ;
142 : mmax ( m -- n ) [ -1/0. ] dip [ [ max ] each ] each ;
143 : mnorm ( m -- n ) dup mmax abs m/n ;
144
145 : cross ( vec1 vec2 -- vec3 )
146     [ [ { 1 2 0 } vshuffle ] [ { 2 0 1 } vshuffle ] bi* v* ]
147     [ [ { 2 0 1 } vshuffle ] [ { 1 2 0 } vshuffle ] bi* v* ] 2bi v- ; inline
148
149 :: normal ( vec1 vec2 vec3 -- vec4 )
150     vec2 vec1 v- vec3 vec1 v- cross normalize ; inline
151
152 : proj ( v u -- w )
153     [ [ v. ] [ norm-sq ] bi / ] keep n*v ;
154
155 : perp ( v u -- w )
156     dupd proj v- ;
157
158 : angle-between ( v u -- a )
159     [ normalize ] bi@ h. acos ;
160
161 : (gram-schmidt) ( v seq -- newseq )
162     [ dupd proj v- ] each ;
163
164 : gram-schmidt ( seq -- orthogonal )
165     V{ } clone [ over (gram-schmidt) over push ] reduce ;
166
167 : norm-gram-schmidt ( seq -- orthonormal )
168     gram-schmidt [ normalize ] map ;
169
170 : m^n ( m n -- n ) 
171     make-bits over first length identity-matrix
172     [ [ dupd m. ] when [ dup m. ] dip ] reduce nip ;
173
174 : stitch ( m -- m' )
175     [ ] [ [ append ] 2map ] map-reduce ;
176
177 : kron ( m1 m2 -- m )
178     '[ [ _ n*m  ] map ] map stitch stitch ;
179
180 : outer ( u v -- m )
181     [ n*v ] curry map ;
182
183 : row ( n matrix -- col )
184     nth ; inline
185
186 : rows ( seq matrix -- cols )
187     '[ _ row ] map ; inline
188
189 : col ( n matrix -- col )
190     swap '[ _ swap nth ] map ; inline
191
192 : cols ( seq matrix -- cols )
193     '[ _ col ] map ; inline
194
195 : set-index ( object pair matrix -- )
196     [ first2 swap ] dip nth set-nth ; inline
197
198 : set-indices ( object sequence matrix -- )
199     '[ _ set-index ] with each ; inline
200     
201
202 : matrix-map ( matrix quot -- )
203     '[ _ map ] map ; inline
204
205 : column-map ( matrix quot -- seq )
206     [ [ first length iota ] keep ] dip '[ _ col @ ] map ; inline
207
208 : cartesian-square-indices ( n -- matrix )
209     iota dup cartesian-product ; inline
210
211 : cartesian-matrix-map ( matrix quot -- matrix' )
212     [ [ first length cartesian-square-indices ] keep ] dip
213     '[ _ @ ] matrix-map ; inline
214
215 : cartesian-matrix-column-map ( matrix quot -- matrix' )
216     [ cols first2 ] prepose cartesian-matrix-map ; inline
217
218 : cov-matrix-ddof ( matrix ddof -- cov )
219     '[ _ cov-ddof ] cartesian-matrix-column-map ; inline
220
221 : cov-matrix ( matrix -- cov ) 0 cov-matrix-ddof ; inline
222
223 : sample-cov-matrix ( matrix -- cov ) 1 cov-matrix-ddof ; inline
224
225 GENERIC: square-rows ( object -- matrix )
226 M: integer square-rows iota square-rows ;
227 M: sequence square-rows dup [ nip ] cartesian-map ; 
228
229 GENERIC: square-cols ( object -- matrix )
230 M: integer square-cols iota square-cols ;
231 M: sequence square-cols dup [ drop ] cartesian-map ; 
232
233 : make-matrix-with-indices ( m n quot -- matrix )
234     [ [ iota ] bi@ ] dip '[ @ ] cartesian-map ; inline
235
236 : null-matrix? ( matrix -- ? ) empty? ;
237
238 : well-formed-matrix? ( matrix -- ? )
239     dup null-matrix? [
240         drop t
241     ] [
242         [ ] [ first length ] bi
243         '[ length _ = ] all?
244     ] if ;
245
246 : dim ( matrix -- pair/f )
247     [ 2 0 <array> ]
248     [ [ length ] [ first length ] bi 2array ] if-empty ;
249
250 : square-matrix? ( matrix -- ? )
251     { [ well-formed-matrix? ] [ dim all-eq? ] } 1&& ;
252
253 : matrix-coordinates ( dim -- coordinates )
254     first2 [ iota ] bi@ cartesian-product ; inline
255
256 : dimension-range ( matrix -- dim range )
257     dim [ matrix-coordinates ] [ first [1,b] ] bi ;
258
259 : upper-matrix-indices ( matrix -- matrix' )
260     dimension-range <reversed> [ tail-slice* >array ] 2map concat ;
261
262 : lower-matrix-indices ( matrix -- matrix' )
263     dimension-range [ head-slice >array ] 2map concat ;
264
265
266 : make-lower-matrix ( object m n -- matrix )
267     zero-matrix [ lower-matrix-indices ] [ set-indices ] [ ] tri ;
268
269 : make-upper-matrix ( object m n -- matrix )
270     zero-matrix [ upper-matrix-indices ] [ set-indices ] [ ] tri ;