1 USING: accessors alien alien.c-types arrays byte-arrays combinators
2 combinators.short-circuit fry kernel locals macros
3 math math.blas.ffi math.blas.vectors math.blas.vectors.private
4 math.complex math.functions math.order functors words
5 sequences sequences.merged sequences.private shuffle
6 parser prettyprint.backend prettyprint.custom ascii
8 SPECIALIZED-ARRAY: float
9 SPECIALIZED-ARRAY: double
10 SPECIALIZED-ARRAY: complex-float
11 SPECIALIZED-ARRAY: complex-double
12 IN: math.blas.matrices
14 TUPLE: blas-matrix-base underlying ld rows cols transpose ;
16 : Mtransposed? ( matrix -- ? )
18 : Mwidth ( matrix -- width )
19 dup Mtransposed? [ rows>> ] [ cols>> ] if ; inline
20 : Mheight ( matrix -- height )
21 dup Mtransposed? [ cols>> ] [ rows>> ] if ; inline
23 GENERIC: n*M.V+n*V! ( alpha A x beta y -- y=alpha*A.x+b*y )
24 GENERIC: n*V(*)V+M! ( alpha x y A -- A=alpha*x(*)y+A )
25 GENERIC: n*V(*)Vconj+M! ( alpha x y A -- A=alpha*x(*)yconj+A )
26 GENERIC: n*M.M+n*M! ( alpha A B beta C -- C=alpha*A.B+beta*C )
30 : (blas-transpose) ( matrix -- integer )
31 transpose>> [ "T" ] [ "N" ] if ;
33 GENERIC: (blas-matrix-like) ( data ld rows cols transpose exemplar -- matrix )
35 : (validate-gemv) ( A x y -- )
37 [ drop [ Mwidth ] [ length>> ] bi* = ]
38 [ nip [ Mheight ] [ length>> ] bi* = ]
40 [ "Mismatched matrix and vectors in matrix-vector multiplication" throw ]
44 ( alpha A x beta y -- A-trans m n alpha A-data A-ld x-data x-inc beta y-data y-inc
60 : (validate-ger) ( x y A -- )
62 [ [ length>> ] [ drop ] [ Mheight ] tri* = ]
63 [ [ drop ] [ length>> ] [ Mwidth ] tri* = ]
65 [ "Mismatched vertices and matrix in vector outer product" throw ]
69 ( alpha x y A -- m n alpha x-data x-inc y-data y-inc A-data A-ld
81 A f >>transpose ; inline
83 : (validate-gemm) ( A B C -- )
85 [ [ Mwidth ] [ Mheight ] [ drop ] tri* = ]
86 [ [ Mheight ] [ drop ] [ Mheight ] tri* = ]
87 [ [ drop ] [ Mwidth ] [ Mwidth ] tri* = ]
89 [ "Mismatched matrices in matrix multiplication" throw ]
93 ( alpha A B beta C -- A-trans B-trans m n k alpha A-data A-ld B-data B-ld beta C-data C-ld
109 C f >>transpose ; inline
111 : (>matrix) ( arrays >c-array -- c-array ld rows cols transpose )
112 '[ <merged> @ ] [ length dup ] [ first length ] tri f ; inline
116 ! XXX should do a dense clone
117 M: blas-matrix-base clone
123 [ element-type heap-size ]
124 } cleave * * memory>byte-array ]
132 ] keep (blas-matrix-like) ;
134 ! XXX try rounding stride to next 128 bit bound for better vectorizin'
135 : <empty-matrix> ( rows cols exemplar -- matrix )
136 [ element-type heap-size * * <byte-array> ]
138 [ f swap (blas-matrix-like) ] 3tri ;
140 : n*M.V+n*V ( alpha A x beta y -- alpha*A.x+b*y )
142 : n*V(*)V+M ( alpha x y A -- alpha*x(*)y+A )
144 : n*V(*)Vconj+M ( alpha x y A -- alpha*x(*)yconj+A )
145 clone n*V(*)Vconj+M! ;
146 : n*M.M+n*M ( alpha A B beta C -- alpha*A.B+beta*C )
149 : n*M.V ( alpha A x -- alpha*A.x )
150 1.0 2over [ Mheight ] dip <empty-vector>
154 1.0 -rot n*M.V ; inline
156 : n*V(*)V ( alpha x y -- alpha*x(*)y )
157 2dup [ length>> ] bi@ pick <empty-matrix>
159 : n*V(*)Vconj ( alpha x y -- alpha*x(*)yconj )
160 2dup [ length>> ] bi@ pick <empty-matrix>
163 : V(*) ( x y -- x(*)y )
164 1.0 -rot n*V(*)V ; inline
165 : V(*)conj ( x y -- x(*)yconj )
166 1.0 -rot n*V(*)Vconj ; inline
168 : n*M.M ( alpha A B -- alpha*A.B )
169 2dup [ Mheight ] [ Mwidth ] bi* pick <empty-matrix>
170 1.0 swap n*M.M+n*M! ;
173 1.0 -rot n*M.M ; inline
175 :: (Msub) ( matrix row col height width -- data ld rows cols )
176 matrix ld>> col * row + matrix element-type heap-size *
177 matrix underlying>> <displaced-alien>
182 :: Msub ( matrix row col height width -- sub )
183 matrix dup transpose>>
184 [ col row width height ]
185 [ row col height width ] if (Msub)
186 matrix transpose>> matrix (blas-matrix-like) ;
188 TUPLE: blas-matrix-rowcol-sequence
189 parent inc rowcol-length rowcol-jump length ;
190 C: <blas-matrix-rowcol-sequence> blas-matrix-rowcol-sequence
192 INSTANCE: blas-matrix-rowcol-sequence sequence
194 M: blas-matrix-rowcol-sequence length
196 M: blas-matrix-rowcol-sequence nth-unsafe
200 [ parent>> element-type heap-size ]
201 [ parent>> underlying>> ] tri
202 [ * * ] dip <displaced-alien>
207 } cleave (blas-vector-like) ;
209 : (Mcols) ( A -- columns )
210 { [ ] [ drop 1 ] [ rows>> ] [ ld>> ] [ cols>> ] }
211 cleave <blas-matrix-rowcol-sequence> ;
212 : (Mrows) ( A -- rows )
213 { [ ] [ ld>> ] [ cols>> ] [ drop 1 ] [ rows>> ] }
214 cleave <blas-matrix-rowcol-sequence> ;
216 : Mrows ( A -- rows )
217 dup transpose>> [ (Mcols) ] [ (Mrows) ] if ;
218 : Mcols ( A -- cols )
219 dup transpose>> [ (Mrows) ] [ (Mcols) ] if ;
221 : n*M! ( n A -- A=n*A )
222 [ (Mcols) [ n*V! drop ] with each ] keep ;
230 recip swap n*M ; inline
232 : Mtranspose ( matrix -- matrix^T )
238 } cleave ] keep (blas-matrix-like) ;
240 M: blas-matrix-base equal?
243 [ [ Mcols ] bi@ [ = ] 2all? ]
248 FUNCTOR: (define-blas-matrix) ( TYPE T U C -- )
250 VECTOR IS ${TYPE}-blas-vector
251 <VECTOR> IS <${TYPE}-blas-vector>
252 >ARRAY IS >${TYPE}-array
258 MATRIX DEFINES-CLASS ${TYPE}-blas-matrix
259 <MATRIX> DEFINES <${TYPE}-blas-matrix>
260 >MATRIX DEFINES >${TYPE}-blas-matrix
264 XMATRIX{ DEFINES ${t}matrix{
268 TUPLE: MATRIX < blas-matrix-base ;
269 : <MATRIX> ( underlying ld rows cols transpose -- matrix )
272 M: MATRIX element-type
274 M: MATRIX (blas-matrix-like)
276 M: VECTOR (blas-matrix-like)
278 M: MATRIX (blas-vector-like)
281 : >MATRIX ( arrays -- matrix )
282 [ >ARRAY underlying>> ] (>matrix) <MATRIX> ;
285 (prepare-gemv) [ XGEMV ] dip ;
287 (prepare-gemm) [ XGEMM ] dip ;
289 (prepare-ger) [ XGERU ] dip ;
290 M: MATRIX n*V(*)Vconj+M!
291 (prepare-ger) [ XGERC ] dip ;
293 SYNTAX: XMATRIX{ \ } [ >MATRIX ] parse-literal ;
295 M: MATRIX pprint-delims
296 drop \ XMATRIX{ \ } ;
301 : define-real-blas-matrix ( TYPE T -- )
302 "" "" (define-blas-matrix) ;
303 : define-complex-blas-matrix ( TYPE T -- )
304 "U" "C" (define-blas-matrix) ;
306 "float" "S" define-real-blas-matrix
307 "double" "D" define-real-blas-matrix
308 "complex-float" "C" define-complex-blas-matrix
309 "complex-double" "Z" define-complex-blas-matrix
313 M: blas-matrix-base >pprint-sequence Mrows ;
314 M: blas-matrix-base pprint* pprint-object ;