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 specialized-arrays.float specialized-arrays.double
7 specialized-arrays.complex-float specialized-arrays.complex-double
8 parser prettyprint.backend prettyprint.custom ascii ;
11 TUPLE: blas-matrix-base underlying ld rows cols transpose ;
13 : Mtransposed? ( matrix -- ? )
15 : Mwidth ( matrix -- width )
16 dup Mtransposed? [ rows>> ] [ cols>> ] if ; inline
17 : Mheight ( matrix -- height )
18 dup Mtransposed? [ cols>> ] [ rows>> ] if ; inline
20 GENERIC: n*M.V+n*V! ( alpha A x beta y -- y=alpha*A.x+b*y )
21 GENERIC: n*V(*)V+M! ( alpha x y A -- A=alpha*x(*)y+A )
22 GENERIC: n*V(*)Vconj+M! ( alpha x y A -- A=alpha*x(*)yconj+A )
23 GENERIC: n*M.M+n*M! ( alpha A B beta C -- C=alpha*A.B+beta*C )
27 : (blas-transpose) ( matrix -- integer )
28 transpose>> [ "T" ] [ "N" ] if ;
30 GENERIC: (blas-matrix-like) ( data ld rows cols transpose exemplar -- matrix )
32 : (validate-gemv) ( A x y -- )
34 [ drop [ Mwidth ] [ length>> ] bi* = ]
35 [ nip [ Mheight ] [ length>> ] bi* = ]
37 [ "Mismatched matrix and vectors in matrix-vector multiplication" throw ]
41 ( alpha A x beta y -- A-trans m n alpha A-data A-ld x-data x-inc beta y-data y-inc
57 : (validate-ger) ( x y A -- )
59 [ [ length>> ] [ drop ] [ Mheight ] tri* = ]
60 [ [ drop ] [ length>> ] [ Mwidth ] tri* = ]
62 [ "Mismatched vertices and matrix in vector outer product" throw ]
66 ( alpha x y A -- m n alpha x-data x-inc y-data y-inc A-data A-ld
78 A f >>transpose ; inline
80 : (validate-gemm) ( A B C -- )
82 [ [ Mwidth ] [ Mheight ] [ drop ] tri* = ]
83 [ [ Mheight ] [ drop ] [ Mheight ] tri* = ]
84 [ [ drop ] [ Mwidth ] [ Mwidth ] tri* = ]
86 [ "Mismatched matrices in matrix multiplication" throw ]
90 ( 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
106 C f >>transpose ; inline
108 : (>matrix) ( arrays >c-array -- c-array ld rows cols transpose )
109 '[ <merged> @ ] [ length dup ] [ first length ] tri f ; inline
113 ! XXX should do a dense clone
114 M: blas-matrix-base clone
120 [ element-type heap-size ]
121 } cleave * * memory>byte-array ]
129 ] keep (blas-matrix-like) ;
131 ! XXX try rounding stride to next 128 bit bound for better vectorizin'
132 : <empty-matrix> ( rows cols exemplar -- matrix )
133 [ element-type heap-size * * <byte-array> ]
135 [ f swap (blas-matrix-like) ] 3tri ;
137 : n*M.V+n*V ( alpha A x beta y -- alpha*A.x+b*y )
139 : n*V(*)V+M ( alpha x y A -- alpha*x(*)y+A )
141 : n*V(*)Vconj+M ( alpha x y A -- alpha*x(*)yconj+A )
142 clone n*V(*)Vconj+M! ;
143 : n*M.M+n*M ( alpha A B beta C -- alpha*A.B+beta*C )
146 : n*M.V ( alpha A x -- alpha*A.x )
147 1.0 2over [ Mheight ] dip <empty-vector>
151 1.0 -rot n*M.V ; inline
153 : n*V(*)V ( alpha x y -- alpha*x(*)y )
154 2dup [ length>> ] bi@ pick <empty-matrix>
156 : n*V(*)Vconj ( alpha x y -- alpha*x(*)yconj )
157 2dup [ length>> ] bi@ pick <empty-matrix>
160 : V(*) ( x y -- x(*)y )
161 1.0 -rot n*V(*)V ; inline
162 : V(*)conj ( x y -- x(*)yconj )
163 1.0 -rot n*V(*)Vconj ; inline
165 : n*M.M ( alpha A B -- alpha*A.B )
166 2dup [ Mheight ] [ Mwidth ] bi* pick <empty-matrix>
167 1.0 swap n*M.M+n*M! ;
170 1.0 -rot n*M.M ; inline
172 :: (Msub) ( matrix row col height width -- data ld rows cols )
173 matrix ld>> col * row + matrix element-type heap-size *
174 matrix underlying>> <displaced-alien>
179 :: Msub ( matrix row col height width -- sub )
180 matrix dup transpose>>
181 [ col row width height ]
182 [ row col height width ] if (Msub)
183 matrix transpose>> matrix (blas-matrix-like) ;
185 TUPLE: blas-matrix-rowcol-sequence
186 parent inc rowcol-length rowcol-jump length ;
187 C: <blas-matrix-rowcol-sequence> blas-matrix-rowcol-sequence
189 INSTANCE: blas-matrix-rowcol-sequence sequence
191 M: blas-matrix-rowcol-sequence length
193 M: blas-matrix-rowcol-sequence nth-unsafe
197 [ parent>> element-type heap-size ]
198 [ parent>> underlying>> ] tri
199 [ * * ] dip <displaced-alien>
204 } cleave (blas-vector-like) ;
206 : (Mcols) ( A -- columns )
207 { [ ] [ drop 1 ] [ rows>> ] [ ld>> ] [ cols>> ] }
208 cleave <blas-matrix-rowcol-sequence> ;
209 : (Mrows) ( A -- rows )
210 { [ ] [ ld>> ] [ cols>> ] [ drop 1 ] [ rows>> ] }
211 cleave <blas-matrix-rowcol-sequence> ;
213 : Mrows ( A -- rows )
214 dup transpose>> [ (Mcols) ] [ (Mrows) ] if ;
215 : Mcols ( A -- cols )
216 dup transpose>> [ (Mrows) ] [ (Mcols) ] if ;
218 : n*M! ( n A -- A=n*A )
219 [ (Mcols) [ n*V! drop ] with each ] keep ;
227 recip swap n*M ; inline
229 : Mtranspose ( matrix -- matrix^T )
235 } cleave ] keep (blas-matrix-like) ;
237 M: blas-matrix-base equal?
240 [ [ Mcols ] bi@ [ = ] 2all? ]
245 FUNCTOR: (define-blas-matrix) ( TYPE T U C -- )
247 VECTOR IS ${TYPE}-blas-vector
248 <VECTOR> IS <${TYPE}-blas-vector>
249 >ARRAY IS >${TYPE}-array
255 MATRIX DEFINES-CLASS ${TYPE}-blas-matrix
256 <MATRIX> DEFINES <${TYPE}-blas-matrix>
257 >MATRIX DEFINES >${TYPE}-blas-matrix
261 XMATRIX{ DEFINES ${t}matrix{
265 TUPLE: MATRIX < blas-matrix-base ;
266 : <MATRIX> ( underlying ld rows cols transpose -- matrix )
269 M: MATRIX element-type
271 M: MATRIX (blas-matrix-like)
273 M: VECTOR (blas-matrix-like)
275 M: MATRIX (blas-vector-like)
278 : >MATRIX ( arrays -- matrix )
279 [ >ARRAY underlying>> ] (>matrix) <MATRIX> ;
282 (prepare-gemv) [ XGEMV ] dip ;
284 (prepare-gemm) [ XGEMM ] dip ;
286 (prepare-ger) [ XGERU ] dip ;
287 M: MATRIX n*V(*)Vconj+M!
288 (prepare-ger) [ XGERC ] dip ;
290 SYNTAX: XMATRIX{ \ } [ >MATRIX ] parse-literal ;
292 M: MATRIX pprint-delims
293 drop \ XMATRIX{ \ } ;
298 : define-real-blas-matrix ( TYPE T -- )
299 "" "" (define-blas-matrix) ;
300 : define-complex-blas-matrix ( TYPE T -- )
301 "U" "C" (define-blas-matrix) ;
303 "float" "S" define-real-blas-matrix
304 "double" "D" define-real-blas-matrix
305 "complex-float" "C" define-complex-blas-matrix
306 "complex-double" "Z" define-complex-blas-matrix
310 M: blas-matrix-base >pprint-sequence Mrows ;
311 M: blas-matrix-base pprint* pprint-object ;