1 USING: accessors alien alien.c-types alien.data arrays
2 byte-arrays combinators combinators.short-circuit fry
3 kernel locals macros math math.blas.ffi math.blas.vectors
4 math.blas.vectors.private math.complex math.functions
5 math.order functors words sequences sequences.merged
6 sequences.private shuffle parser prettyprint.backend
7 prettyprint.custom ascii specialized-arrays ;
8 FROM: alien.c-types => float ;
9 SPECIALIZED-ARRAY: float
10 SPECIALIZED-ARRAY: double
11 SPECIALIZED-ARRAY: complex-float
12 SPECIALIZED-ARRAY: complex-double
13 IN: math.blas.matrices
15 TUPLE: blas-matrix-base underlying ld rows cols transpose ;
17 : Mtransposed? ( matrix -- ? )
19 : Mwidth ( matrix -- width )
20 dup Mtransposed? [ rows>> ] [ cols>> ] if ; inline
21 : Mheight ( matrix -- height )
22 dup Mtransposed? [ cols>> ] [ rows>> ] if ; inline
24 GENERIC: n*M.V+n*V! ( alpha A x beta y -- y=alpha*A.x+b*y )
25 GENERIC: n*V(*)V+M! ( alpha x y A -- A=alpha*x(*)y+A )
26 GENERIC: n*V(*)Vconj+M! ( alpha x y A -- A=alpha*x(*)yconj+A )
27 GENERIC: n*M.M+n*M! ( alpha A B beta C -- C=alpha*A.B+beta*C )
31 : (blas-transpose) ( matrix -- integer )
32 transpose>> [ "T" ] [ "N" ] if ;
34 GENERIC: (blas-matrix-like) ( data ld rows cols transpose exemplar -- matrix )
36 : (validate-gemv) ( A x y -- )
38 [ drop [ Mwidth ] [ length>> ] bi* = ]
39 [ nip [ Mheight ] [ length>> ] bi* = ]
41 [ "Mismatched matrix and vectors in matrix-vector multiplication" throw ]
45 ( alpha A x beta y -- A-trans m n alpha A-data A-ld x-data x-inc beta y-data y-inc
61 : (validate-ger) ( x y A -- )
63 [ [ length>> ] [ drop ] [ Mheight ] tri* = ]
64 [ [ drop ] [ length>> ] [ Mwidth ] tri* = ]
66 [ "Mismatched vertices and matrix in vector outer product" throw ]
70 ( alpha x y A -- m n alpha x-data x-inc y-data y-inc A-data A-ld
82 A f >>transpose ; inline
84 : (validate-gemm) ( A B C -- )
86 [ [ Mwidth ] [ Mheight ] [ drop ] tri* = ]
87 [ [ Mheight ] [ drop ] [ Mheight ] tri* = ]
88 [ [ drop ] [ Mwidth ] [ Mwidth ] tri* = ]
90 [ "Mismatched matrices in matrix multiplication" throw ]
94 ( 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
110 C f >>transpose ; inline
112 : (>matrix) ( arrays >c-array -- c-array ld rows cols transpose )
113 '[ <merged> @ ] [ length dup ] [ first length ] tri f ; inline
117 ! XXX should do a dense clone
118 M: blas-matrix-base clone
124 [ element-type heap-size ]
125 } cleave * * memory>byte-array ]
133 ] keep (blas-matrix-like) ;
135 ! XXX try rounding stride to next 128 bit bound for better vectorizin'
136 : <empty-matrix> ( rows cols exemplar -- matrix )
137 [ element-type heap-size * * <byte-array> ]
139 [ f swap (blas-matrix-like) ] 3tri ;
141 : n*M.V+n*V ( alpha A x beta y -- alpha*A.x+b*y )
143 : n*V(*)V+M ( alpha x y A -- alpha*x(*)y+A )
145 : n*V(*)Vconj+M ( alpha x y A -- alpha*x(*)yconj+A )
146 clone n*V(*)Vconj+M! ;
147 : n*M.M+n*M ( alpha A B beta C -- alpha*A.B+beta*C )
150 : n*M.V ( alpha A x -- alpha*A.x )
151 1.0 2over [ Mheight ] dip <empty-vector>
155 1.0 -rot n*M.V ; inline
157 : n*V(*)V ( alpha x y -- alpha*x(*)y )
158 2dup [ length>> ] bi@ pick <empty-matrix>
160 : n*V(*)Vconj ( alpha x y -- alpha*x(*)yconj )
161 2dup [ length>> ] bi@ pick <empty-matrix>
164 : V(*) ( x y -- x(*)y )
165 1.0 -rot n*V(*)V ; inline
166 : V(*)conj ( x y -- x(*)yconj )
167 1.0 -rot n*V(*)Vconj ; inline
169 : n*M.M ( alpha A B -- alpha*A.B )
170 2dup [ Mheight ] [ Mwidth ] bi* pick <empty-matrix>
171 1.0 swap n*M.M+n*M! ;
174 1.0 -rot n*M.M ; inline
176 :: (Msub) ( matrix row col height width -- data ld rows cols )
177 matrix ld>> col * row + matrix element-type heap-size *
178 matrix underlying>> <displaced-alien>
183 :: Msub ( matrix row col height width -- sub )
184 matrix dup transpose>>
185 [ col row width height ]
186 [ row col height width ] if (Msub)
187 matrix transpose>> matrix (blas-matrix-like) ;
189 TUPLE: blas-matrix-rowcol-sequence
190 parent inc rowcol-length rowcol-jump length ;
191 C: <blas-matrix-rowcol-sequence> blas-matrix-rowcol-sequence
193 INSTANCE: blas-matrix-rowcol-sequence sequence
195 M: blas-matrix-rowcol-sequence length
197 M: blas-matrix-rowcol-sequence nth-unsafe
201 [ parent>> element-type heap-size ]
202 [ parent>> underlying>> ] tri
203 [ * * ] dip <displaced-alien>
208 } cleave (blas-vector-like) ;
210 : (Mcols) ( A -- columns )
211 { [ ] [ drop 1 ] [ rows>> ] [ ld>> ] [ cols>> ] }
212 cleave <blas-matrix-rowcol-sequence> ;
213 : (Mrows) ( A -- rows )
214 { [ ] [ ld>> ] [ cols>> ] [ drop 1 ] [ rows>> ] }
215 cleave <blas-matrix-rowcol-sequence> ;
217 : Mrows ( A -- rows )
218 dup transpose>> [ (Mcols) ] [ (Mrows) ] if ;
219 : Mcols ( A -- cols )
220 dup transpose>> [ (Mrows) ] [ (Mcols) ] if ;
222 : n*M! ( n A -- A=n*A )
223 [ (Mcols) [ n*V! drop ] with each ] keep ;
231 recip swap n*M ; inline
233 : Mtranspose ( matrix -- matrix^T )
239 } cleave ] keep (blas-matrix-like) ;
241 M: blas-matrix-base equal?
244 [ [ Mcols ] bi@ [ = ] 2all? ]
249 FUNCTOR: (define-blas-matrix) ( TYPE T U C -- )
251 VECTOR IS ${TYPE}-blas-vector
252 <VECTOR> IS <${TYPE}-blas-vector>
253 >ARRAY IS >${TYPE}-array
259 MATRIX DEFINES-CLASS ${TYPE}-blas-matrix
260 <MATRIX> DEFINES <${TYPE}-blas-matrix>
261 >MATRIX DEFINES >${TYPE}-blas-matrix
265 XMATRIX{ DEFINES ${t}matrix{
269 TUPLE: MATRIX < blas-matrix-base ;
270 : <MATRIX> ( underlying ld rows cols transpose -- matrix )
273 M: MATRIX element-type
275 M: MATRIX (blas-matrix-like)
277 M: VECTOR (blas-matrix-like)
279 M: MATRIX (blas-vector-like)
282 : >MATRIX ( arrays -- matrix )
283 [ >ARRAY underlying>> ] (>matrix) <MATRIX> ;
286 (prepare-gemv) [ XGEMV ] dip ;
288 (prepare-gemm) [ XGEMM ] dip ;
290 (prepare-ger) [ XGERU ] dip ;
291 M: MATRIX n*V(*)Vconj+M!
292 (prepare-ger) [ XGERC ] dip ;
294 SYNTAX: XMATRIX{ \ } [ >MATRIX ] parse-literal ;
296 M: MATRIX pprint-delims
297 drop \ XMATRIX{ \ } ;
302 : define-real-blas-matrix ( TYPE T -- )
303 "" "" (define-blas-matrix) ;
304 : define-complex-blas-matrix ( TYPE T -- )
305 "U" "C" (define-blas-matrix) ;
307 "float" "S" define-real-blas-matrix
308 "double" "D" define-real-blas-matrix
309 "complex-float" "C" define-complex-blas-matrix
310 "complex-double" "Z" define-complex-blas-matrix
314 M: blas-matrix-base >pprint-sequence Mrows ;
315 M: blas-matrix-base pprint* pprint-object ;