| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | USING: accessors alien alien.c-types arrays byte-arrays combinators | 
					
						
							|  |  |  | combinators.lib combinators.short-circuit fry kernel locals macros | 
					
						
							|  |  |  | math math.blas.cblas math.blas.vectors math.blas.vectors.private | 
					
						
							|  |  |  | math.complex math.functions math.order multi-methods qualified | 
					
						
							| 
									
										
										
										
											2008-07-07 20:36:33 -04:00
										 |  |  | sequences sequences.merged sequences.private generalizations | 
					
						
							|  |  |  | shuffle symbols ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | QUALIFIED: syntax | 
					
						
							|  |  |  | IN: math.blas.matrices | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: blas-matrix-base data ld rows cols transpose ;
 | 
					
						
							|  |  |  | TUPLE: float-blas-matrix < blas-matrix-base ;
 | 
					
						
							|  |  |  | TUPLE: double-blas-matrix < blas-matrix-base ;
 | 
					
						
							|  |  |  | TUPLE: float-complex-blas-matrix < blas-matrix-base ;
 | 
					
						
							|  |  |  | TUPLE: double-complex-blas-matrix < blas-matrix-base ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | C: <float-blas-matrix> float-blas-matrix | 
					
						
							|  |  |  | C: <double-blas-matrix> double-blas-matrix | 
					
						
							|  |  |  | C: <float-complex-blas-matrix> float-complex-blas-matrix | 
					
						
							|  |  |  | C: <double-complex-blas-matrix> double-complex-blas-matrix | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | METHOD: element-type { float-blas-matrix } | 
					
						
							|  |  |  |     drop "float" ;
 | 
					
						
							|  |  |  | METHOD: element-type { double-blas-matrix } | 
					
						
							|  |  |  |     drop "double" ;
 | 
					
						
							|  |  |  | METHOD: element-type { float-complex-blas-matrix } | 
					
						
							|  |  |  |     drop "CBLAS_C" ;
 | 
					
						
							|  |  |  | METHOD: element-type { double-complex-blas-matrix } | 
					
						
							|  |  |  |     drop "CBLAS_Z" ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : Mtransposed? ( matrix -- ? )
 | 
					
						
							|  |  |  |     transpose>> ; inline
 | 
					
						
							|  |  |  | : Mwidth ( matrix -- width )
 | 
					
						
							|  |  |  |     dup Mtransposed? [ rows>> ] [ cols>> ] if ; inline
 | 
					
						
							|  |  |  | : Mheight ( matrix -- height )
 | 
					
						
							|  |  |  |     dup Mtransposed? [ cols>> ] [ rows>> ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (blas-transpose) ( matrix -- integer )
 | 
					
						
							|  |  |  |     transpose>> [ CblasTrans ] [ CblasNoTrans ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | GENERIC: (blas-matrix-like) ( data ld rows cols transpose exemplar -- matrix )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object float-blas-matrix } | 
					
						
							|  |  |  |     drop <float-blas-matrix> ;
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object double-blas-matrix } | 
					
						
							|  |  |  |     drop <double-blas-matrix> ;
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object float-complex-blas-matrix } | 
					
						
							|  |  |  |     drop <float-complex-blas-matrix> ;
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object double-complex-blas-matrix } | 
					
						
							|  |  |  |     drop <double-complex-blas-matrix> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object float-blas-vector } | 
					
						
							|  |  |  |     drop <float-blas-matrix> ;
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object double-blas-vector } | 
					
						
							|  |  |  |     drop <double-blas-matrix> ;
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object float-complex-blas-vector } | 
					
						
							|  |  |  |     drop <float-complex-blas-matrix> ;
 | 
					
						
							|  |  |  | METHOD: (blas-matrix-like) { object object object object object double-complex-blas-vector } | 
					
						
							|  |  |  |     drop <double-complex-blas-matrix> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | METHOD: (blas-vector-like) { object object object float-blas-matrix } | 
					
						
							|  |  |  |     drop <float-blas-vector> ;
 | 
					
						
							|  |  |  | METHOD: (blas-vector-like) { object object object double-blas-matrix } | 
					
						
							|  |  |  |     drop <double-blas-vector> ;
 | 
					
						
							|  |  |  | METHOD: (blas-vector-like) { object object object float-complex-blas-matrix } | 
					
						
							|  |  |  |     drop <float-complex-blas-vector> ;
 | 
					
						
							|  |  |  | METHOD: (blas-vector-like) { object object object double-complex-blas-matrix } | 
					
						
							|  |  |  |     drop <double-complex-blas-vector> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (validate-gemv) ( A x y -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ drop [ Mwidth  ] [ length>> ] bi* = ] | 
					
						
							|  |  |  |         [ nip  [ Mheight ] [ length>> ] bi* = ] | 
					
						
							|  |  |  |     } 3&& | 
					
						
							|  |  |  |     [ "Mismatched matrix and vectors in matrix-vector multiplication" throw ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: (prepare-gemv) ( alpha A x beta y >c-arg -- order A-trans m n alpha A-data A-ld x-data x-inc beta y-data y-inc y )
 | 
					
						
							|  |  |  |     A x y (validate-gemv) | 
					
						
							|  |  |  |     CblasColMajor | 
					
						
							|  |  |  |     A (blas-transpose) | 
					
						
							|  |  |  |     A rows>> | 
					
						
							|  |  |  |     A cols>> | 
					
						
							|  |  |  |     alpha >c-arg call
 | 
					
						
							|  |  |  |     A data>> | 
					
						
							|  |  |  |     A ld>> | 
					
						
							|  |  |  |     x data>> | 
					
						
							|  |  |  |     x inc>> | 
					
						
							|  |  |  |     beta >c-arg call
 | 
					
						
							|  |  |  |     y data>> | 
					
						
							|  |  |  |     y inc>> | 
					
						
							|  |  |  |     y ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (validate-ger) ( x y A -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ nip  [ length>> ] [ Mheight ] bi* = ] | 
					
						
							|  |  |  |         [ nipd [ length>> ] [ Mwidth  ] bi* = ] | 
					
						
							|  |  |  |     } 3&& | 
					
						
							|  |  |  |     [ "Mismatched vertices and matrix in vector outer product" throw ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: (prepare-ger) ( alpha x y A >c-arg -- order m n alpha x-data x-inc y-data y-inc A-data A-ld A )
 | 
					
						
							|  |  |  |     x y A (validate-ger) | 
					
						
							|  |  |  |     CblasColMajor | 
					
						
							|  |  |  |     A rows>> | 
					
						
							|  |  |  |     A cols>> | 
					
						
							|  |  |  |     alpha >c-arg call
 | 
					
						
							|  |  |  |     x data>> | 
					
						
							|  |  |  |     x inc>> | 
					
						
							|  |  |  |     y data>> | 
					
						
							|  |  |  |     y inc>> | 
					
						
							|  |  |  |     A data>> | 
					
						
							|  |  |  |     A ld>> | 
					
						
							|  |  |  |     A f >>transpose ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (validate-gemm) ( A B C -- )
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ drop [ Mwidth  ] [ Mheight ] bi* = ] | 
					
						
							|  |  |  |         [ nip  [ Mheight ] bi@ = ] | 
					
						
							|  |  |  |         [ nipd [ Mwidth  ] bi@ = ] | 
					
						
							|  |  |  |     } 3&& [ "Mismatched matrices in matrix multiplication" throw ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: (prepare-gemm) ( alpha A B beta C >c-arg -- order A-trans B-trans m n k alpha A-data A-ld B-data B-ld beta C-data C-ld C )
 | 
					
						
							|  |  |  |     A B C (validate-gemm) | 
					
						
							|  |  |  |     CblasColMajor | 
					
						
							|  |  |  |     A (blas-transpose) | 
					
						
							|  |  |  |     B (blas-transpose) | 
					
						
							|  |  |  |     C rows>> | 
					
						
							|  |  |  |     C cols>> | 
					
						
							|  |  |  |     A Mwidth | 
					
						
							|  |  |  |     alpha >c-arg call
 | 
					
						
							|  |  |  |     A data>> | 
					
						
							|  |  |  |     A ld>> | 
					
						
							|  |  |  |     B data>> | 
					
						
							|  |  |  |     B ld>> | 
					
						
							|  |  |  |     beta >c-arg call
 | 
					
						
							|  |  |  |     C data>> | 
					
						
							|  |  |  |     C ld>> | 
					
						
							|  |  |  |     C f >>transpose ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (>matrix) ( arrays >c-array -- c-array ld rows cols transpose )
 | 
					
						
							| 
									
										
										
										
											2008-07-05 22:39:26 -04:00
										 |  |  |     '[ <merged> @ ] [ length dup ] [ first length ] tri f ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : >float-blas-matrix ( arrays -- matrix )
 | 
					
						
							|  |  |  |     [ >c-float-array ] (>matrix) <float-blas-matrix> ;
 | 
					
						
							|  |  |  | : >double-blas-matrix ( arrays -- matrix )
 | 
					
						
							|  |  |  |     [ >c-double-array ] (>matrix) <double-blas-matrix> ;
 | 
					
						
							|  |  |  | : >float-complex-blas-matrix ( arrays -- matrix )
 | 
					
						
							|  |  |  |     [ (flatten-complex-sequence) >c-float-array ] (>matrix) | 
					
						
							|  |  |  |     <float-complex-blas-matrix> ;
 | 
					
						
							|  |  |  | : >double-complex-blas-matrix ( arrays -- matrix )
 | 
					
						
							|  |  |  |     [ (flatten-complex-sequence) >c-double-array ] (>matrix) | 
					
						
							|  |  |  |     <double-complex-blas-matrix> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | GENERIC: n*M.V+n*V! ( alpha A x beta y -- y=alpha*A.x+b*y )
 | 
					
						
							|  |  |  | GENERIC: n*V(*)V+M! ( alpha x y A -- A=alpha*x(*)y+A )
 | 
					
						
							|  |  |  | GENERIC: n*V(*)Vconj+M! ( alpha x y A -- A=alpha*x(*)yconj+A )
 | 
					
						
							|  |  |  | GENERIC: n*M.M+n*M! ( alpha A B beta C -- C=alpha*A.B+beta*C )
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.V+n*V! { real float-blas-matrix float-blas-vector real float-blas-vector } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ ] (prepare-gemv) [ cblas_sgemv ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.V+n*V! { real double-blas-matrix double-blas-vector real double-blas-vector } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ ] (prepare-gemv) [ cblas_dgemv ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.V+n*V! { number float-complex-blas-matrix float-complex-blas-vector number float-complex-blas-vector } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>c-complex) ] (prepare-gemv) [ cblas_cgemv ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.V+n*V! { number double-complex-blas-matrix double-complex-blas-vector number double-complex-blas-vector } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>z-complex) ] (prepare-gemv) [ cblas_zgemv ] dip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*V(*)V+M! { real float-blas-vector float-blas-vector float-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ ] (prepare-ger) [ cblas_sger ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*V(*)V+M! { real double-blas-vector double-blas-vector double-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ ] (prepare-ger) [ cblas_dger ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*V(*)V+M! { number float-complex-blas-vector float-complex-blas-vector float-complex-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>c-complex) ] (prepare-ger) [ cblas_cgeru ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*V(*)V+M! { number double-complex-blas-vector double-complex-blas-vector double-complex-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>z-complex) ] (prepare-ger) [ cblas_zgeru ] dip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*V(*)Vconj+M! { real float-blas-vector float-blas-vector float-blas-matrix } | 
					
						
							|  |  |  |     [ ] (prepare-ger) [ cblas_sger ] dip ;
 | 
					
						
							|  |  |  | METHOD: n*V(*)Vconj+M! { real double-blas-vector double-blas-vector double-blas-matrix } | 
					
						
							|  |  |  |     [ ] (prepare-ger) [ cblas_dger ] dip ;
 | 
					
						
							|  |  |  | METHOD: n*V(*)Vconj+M! { number float-complex-blas-vector float-complex-blas-vector float-complex-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>c-complex) ] (prepare-ger) [ cblas_cgerc ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*V(*)Vconj+M! { number double-complex-blas-vector double-complex-blas-vector double-complex-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>z-complex) ] (prepare-ger) [ cblas_zgerc ] dip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.M+n*M! { real float-blas-matrix float-blas-matrix real float-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ ] (prepare-gemm) [ cblas_sgemm ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.M+n*M! { real double-blas-matrix double-blas-matrix real double-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ ] (prepare-gemm) [ cblas_dgemm ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.M+n*M! { number float-complex-blas-matrix float-complex-blas-matrix number float-complex-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>c-complex) ] (prepare-gemm) [ cblas_cgemm ] dip ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | METHOD: n*M.M+n*M! { number double-complex-blas-matrix double-complex-blas-matrix number double-complex-blas-matrix } | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ (>z-complex) ] (prepare-gemm) [ cblas_zgemm ] dip ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! XXX should do a dense clone | 
					
						
							|  |  |  | syntax:M: blas-matrix-base clone
 | 
					
						
							|  |  |  |     [  | 
					
						
							|  |  |  |         [ | 
					
						
							| 
									
										
										
										
											2008-08-27 17:24:04 -04:00
										 |  |  |             { [ data>> ] [ ld>> ] [ cols>> ] [ element-type heap-size ] } cleave
 | 
					
						
							|  |  |  |             * * memory>byte-array | 
					
						
							|  |  |  |         ] [ { [ ld>> ] [ rows>> ] [ cols>> ] [ transpose>> ] } cleave ] bi
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     ] keep (blas-matrix-like) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ! XXX try rounding stride to next 128 bit bound for better vectorizin' | 
					
						
							| 
									
										
										
										
											2008-07-05 14:24:01 -04:00
										 |  |  | : <empty-matrix> ( rows cols exemplar -- matrix )
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     [ element-type [ * ] dip <c-array> ] | 
					
						
							|  |  |  |     [ 2drop ] | 
					
						
							|  |  |  |     [ f swap (blas-matrix-like) ] 3tri ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : n*M.V+n*V ( alpha A x beta y -- alpha*A.x+b*y )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     clone n*M.V+n*V! ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | : n*V(*)V+M ( alpha x y A -- alpha*x(*)y+A )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     clone n*V(*)V+M! ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | : n*V(*)Vconj+M ( alpha x y A -- alpha*x(*)yconj+A )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     clone n*V(*)Vconj+M! ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | : n*M.M+n*M ( alpha A B beta C -- alpha*A.B+beta*C )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     clone n*M.M+n*M! ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : n*M.V ( alpha A x -- alpha*A.x )
 | 
					
						
							| 
									
										
										
										
											2008-07-05 18:13:48 -04:00
										 |  |  |     1.0 2over [ Mheight ] dip <empty-vector> | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     n*M.V+n*V! ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : M.V ( A x -- A.x )
 | 
					
						
							|  |  |  |     1.0 -rot n*M.V ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | : n*V(*)V ( alpha x y -- alpha*x(*)y )
 | 
					
						
							| 
									
										
										
										
											2008-07-05 14:24:01 -04:00
										 |  |  |     2dup [ length>> ] bi@ pick <empty-matrix> | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     n*V(*)V+M! ;
 | 
					
						
							|  |  |  | : n*V(*)Vconj ( alpha x y -- alpha*x(*)yconj )
 | 
					
						
							| 
									
										
										
										
											2008-07-05 14:24:01 -04:00
										 |  |  |     2dup [ length>> ] bi@ pick <empty-matrix> | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     n*V(*)Vconj+M! ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : V(*) ( x y -- x(*)y )
 | 
					
						
							|  |  |  |     1.0 -rot n*V(*)V ; inline
 | 
					
						
							|  |  |  | : V(*)conj ( x y -- x(*)yconj )
 | 
					
						
							|  |  |  |     1.0 -rot n*V(*)Vconj ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | : n*M.M ( alpha A B -- alpha*A.B )
 | 
					
						
							| 
									
										
										
										
											2008-07-05 14:24:01 -04:00
										 |  |  |     2dup [ Mheight ] [ Mwidth ] bi* pick <empty-matrix>  | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     1.0 swap n*M.M+n*M! ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : M. ( A B -- A.B )
 | 
					
						
							|  |  |  |     1.0 -rot n*M.M ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | :: (Msub) ( matrix row col height width -- data ld rows cols )
 | 
					
						
							|  |  |  |     matrix ld>> col * row + matrix element-type heap-size *
 | 
					
						
							|  |  |  |     matrix data>> <displaced-alien> | 
					
						
							|  |  |  |     matrix ld>> | 
					
						
							|  |  |  |     height | 
					
						
							|  |  |  |     width ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | : Msub ( matrix row col height width -- sub )
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     5 npick dup transpose>> | 
					
						
							|  |  |  |     [ nip [ [ swap ] 2dip swap ] when (Msub) ] 2keep
 | 
					
						
							|  |  |  |     swap (blas-matrix-like) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TUPLE: blas-matrix-rowcol-sequence parent inc rowcol-length rowcol-jump length ;
 | 
					
						
							|  |  |  | C: <blas-matrix-rowcol-sequence> blas-matrix-rowcol-sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | INSTANCE: blas-matrix-rowcol-sequence sequence | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | syntax:M: blas-matrix-rowcol-sequence length
 | 
					
						
							|  |  |  |     length>> ;
 | 
					
						
							|  |  |  | syntax:M: blas-matrix-rowcol-sequence nth-unsafe | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ | 
					
						
							|  |  |  |             [ rowcol-jump>> ] | 
					
						
							|  |  |  |             [ parent>> element-type heap-size ] | 
					
						
							|  |  |  |             [ parent>> data>> ] tri
 | 
					
						
							|  |  |  |             [ * * ] dip <displaced-alien> | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |         [ rowcol-length>> ] | 
					
						
							|  |  |  |         [ inc>> ] | 
					
						
							|  |  |  |         [ parent>> ] | 
					
						
							|  |  |  |     } cleave (blas-vector-like) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (Mcols) ( A -- columns )
 | 
					
						
							|  |  |  |     { [ ] [ drop 1 ] [ rows>> ] [ ld>> ] [ cols>> ] } cleave
 | 
					
						
							|  |  |  |     <blas-matrix-rowcol-sequence> ;
 | 
					
						
							|  |  |  | : (Mrows) ( A -- rows )
 | 
					
						
							|  |  |  |     { [ ] [ ld>> ] [ cols>> ] [ drop 1 ] [ rows>> ] } cleave
 | 
					
						
							|  |  |  |     <blas-matrix-rowcol-sequence> ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : Mrows ( A -- rows )
 | 
					
						
							|  |  |  |     dup transpose>> [ (Mcols) ] [ (Mrows) ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | : Mcols ( A -- cols )
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  |     dup transpose>> [ (Mrows) ] [ (Mcols) ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  | : n*M! ( n A -- A=n*A )
 | 
					
						
							|  |  |  |     [ (Mcols) [ n*V! drop ] with each ] keep ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : n*M ( n A -- n*A )
 | 
					
						
							| 
									
										
										
										
											2008-08-30 21:54:04 -04:00
										 |  |  |     clone n*M! ; inline
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : M*n ( A n -- A*n )
 | 
					
						
							|  |  |  |     swap n*M ; inline
 | 
					
						
							|  |  |  | : M/n ( A n -- A/n )
 | 
					
						
							|  |  |  |     recip swap n*M ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : Mtranspose ( matrix -- matrix^T )
 | 
					
						
							| 
									
										
										
										
											2008-08-27 17:24:04 -04:00
										 |  |  |     [ { [ data>> ] [ ld>> ] [ rows>> ] [ cols>> ] [ transpose>> not ] } cleave ] keep (blas-matrix-like) ;
 | 
					
						
							| 
									
										
										
										
											2008-07-04 23:57:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | syntax:M: blas-matrix-base equal?
 | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         [ [ Mwidth ] bi@ = ] | 
					
						
							|  |  |  |         [ [ Mcols ] bi@ [ = ] 2all? ] | 
					
						
							|  |  |  |     } 2&& ;
 | 
					
						
							|  |  |  | 
 |