2008-07-04 00:16:09 -04:00
|
|
|
USING: accessors alien alien.c-types arrays byte-arrays combinators
|
2008-12-04 16:40:55 -05:00
|
|
|
combinators.short-circuit fry kernel math math.blas.cblas
|
|
|
|
math.complex math.functions math.order sequences.complex
|
|
|
|
sequences.complex-components sequences sequences.private
|
2008-12-04 19:15:42 -05:00
|
|
|
functors words locals
|
2008-11-14 21:18:16 -05:00
|
|
|
specialized-arrays.float specialized-arrays.double
|
|
|
|
specialized-arrays.direct.float specialized-arrays.direct.double ;
|
2008-07-02 01:00:22 -04:00
|
|
|
IN: math.blas.vectors
|
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
TUPLE: blas-vector-base underlying length inc ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
INSTANCE: blas-vector-base virtual-sequence
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
GENERIC: element-type ( v -- type )
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-08-30 21:54:04 -04:00
|
|
|
GENERIC: n*V+V! ( alpha x y -- y=alpha*x+y )
|
|
|
|
GENERIC: n*V! ( alpha x -- x=alpha*x )
|
2008-07-05 14:24:01 -04:00
|
|
|
GENERIC: V. ( x y -- x.y )
|
|
|
|
GENERIC: V.conj ( x y -- xconj.y )
|
2008-07-05 14:30:42 -04:00
|
|
|
GENERIC: Vnorm ( x -- norm )
|
|
|
|
GENERIC: Vasum ( x -- sum )
|
2008-07-05 14:24:01 -04:00
|
|
|
GENERIC: Vswap ( x y -- x=y y=x )
|
2008-07-05 14:30:42 -04:00
|
|
|
GENERIC: Viamax ( x -- max-i )
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-07-04 00:16:09 -04:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
GENERIC: (blas-vector-like) ( data length inc exemplar -- vector )
|
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
GENERIC: (blas-direct-array) ( blas-vector -- direct-array )
|
|
|
|
|
|
|
|
: shorter-length ( v1 v2 -- length )
|
|
|
|
[ length>> ] bi@ min ; inline
|
|
|
|
: data-and-inc ( v -- data inc )
|
|
|
|
[ underlying>> ] [ inc>> ] bi ; inline
|
|
|
|
: datas-and-incs ( v1 v2 -- v1-data v1-inc v2-data v2-inc )
|
|
|
|
[ data-and-inc ] bi@ ; inline
|
|
|
|
|
|
|
|
:: (prepare-copy)
|
|
|
|
( v element-size -- length v-data v-inc v-dest-data v-dest-inc
|
|
|
|
copy-data copy-length copy-inc )
|
|
|
|
v [ length>> ] [ data-and-inc ] bi
|
|
|
|
v length>> element-size * <byte-array>
|
|
|
|
1
|
|
|
|
over v length>> 1 ;
|
|
|
|
|
|
|
|
: (prepare-swap)
|
|
|
|
( v1 v2 -- length v1-data v1-inc v2-data v2-inc
|
|
|
|
v1 v2 )
|
|
|
|
[ shorter-length ] [ datas-and-incs ] [ ] 2tri ;
|
|
|
|
|
|
|
|
:: (prepare-axpy)
|
|
|
|
( n v1 v2 -- length n v1-data v1-inc v2-data v2-inc
|
|
|
|
v2 )
|
|
|
|
v1 v2 shorter-length
|
|
|
|
n
|
|
|
|
v1 v2 datas-and-incs
|
|
|
|
v2 ;
|
|
|
|
|
|
|
|
:: (prepare-scal)
|
|
|
|
( n v -- length n v-data v-inc
|
|
|
|
v )
|
|
|
|
v length>>
|
|
|
|
n
|
|
|
|
v data-and-inc
|
|
|
|
v ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
|
|
|
: (prepare-dot) ( v1 v2 -- length v1-data v1-inc v2-data v2-inc )
|
2008-12-04 16:40:55 -05:00
|
|
|
[ shorter-length ] [ datas-and-incs ] 2bi ;
|
|
|
|
|
|
|
|
: (prepare-nrm2) ( v -- length data inc )
|
|
|
|
[ length>> ] [ data-and-inc ] bi ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
: n*V+V ( alpha x y -- alpha*x+y ) clone n*V+V! ; inline
|
|
|
|
: n*V ( alpha x -- alpha*x ) clone n*V! ; inline
|
|
|
|
|
|
|
|
: V+ ( x y -- x+y )
|
|
|
|
1.0 -rot n*V+V ; inline
|
|
|
|
: V- ( x y -- x-y )
|
|
|
|
-1.0 spin n*V+V ; inline
|
|
|
|
|
|
|
|
: Vneg ( x -- -x )
|
|
|
|
-1.0 swap n*V ; inline
|
|
|
|
|
|
|
|
: V*n ( x alpha -- x*alpha )
|
|
|
|
swap n*V ; inline
|
|
|
|
: V/n ( x alpha -- x/alpha )
|
|
|
|
recip swap n*V ; inline
|
|
|
|
|
|
|
|
: Vamax ( x -- max )
|
|
|
|
[ Viamax ] keep nth ; inline
|
|
|
|
|
|
|
|
:: Vsub ( v start length -- sub )
|
|
|
|
v inc>> start * v element-type heap-size *
|
|
|
|
v underlying>> <displaced-alien>
|
|
|
|
length v inc>> v (blas-vector-like) ;
|
|
|
|
|
2008-07-05 14:24:01 -04:00
|
|
|
: <zero-vector> ( exemplar -- zero )
|
2008-07-04 00:16:09 -04:00
|
|
|
[ element-type <c-object> ]
|
|
|
|
[ length>> 0 ]
|
|
|
|
[ (blas-vector-like) ] tri ;
|
|
|
|
|
2008-07-05 21:28:53 -04:00
|
|
|
: <empty-vector> ( length exemplar -- vector )
|
2008-07-04 23:57:22 -04:00
|
|
|
[ element-type <c-array> ]
|
|
|
|
[ 1 swap ] 2bi
|
|
|
|
(blas-vector-like) ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
M: blas-vector-base equal?
|
|
|
|
{
|
|
|
|
[ [ length ] bi@ = ]
|
|
|
|
[ [ = ] 2all? ]
|
|
|
|
} 2&& ;
|
|
|
|
|
|
|
|
M: blas-vector-base length
|
2008-07-02 01:00:22 -04:00
|
|
|
length>> ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: blas-vector-base virtual-seq
|
|
|
|
(blas-direct-array) ;
|
|
|
|
M: blas-vector-base virtual@
|
|
|
|
[ inc>> * ] [ nip (blas-direct-array) ] 2bi ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 19:08:01 -05:00
|
|
|
: float>arg ( f -- f ) ; inline
|
|
|
|
: double>arg ( f -- f ) ; inline
|
|
|
|
: arg>float ( f -- f ) ; inline
|
|
|
|
: arg>double ( f -- f ) ; inline
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
<<
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
FUNCTOR: (define-blas-vector) ( TYPE T -- )
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
<DIRECT-ARRAY> IS <direct-${TYPE}-array>
|
|
|
|
>ARRAY IS >${TYPE}-array
|
|
|
|
XCOPY IS cblas_${T}copy
|
|
|
|
XSWAP IS cblas_${T}swap
|
|
|
|
IXAMAX IS cblas_i${T}amax
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
VECTOR DEFINES ${TYPE}-blas-vector
|
|
|
|
<VECTOR> DEFINES <${TYPE}-blas-vector>
|
|
|
|
>VECTOR DEFINES >${TYPE}-blas-vector
|
2008-07-04 23:57:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
WHERE
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
TUPLE: VECTOR < blas-vector-base ;
|
|
|
|
: <VECTOR> ( underlying length inc -- vector ) VECTOR boa ; inline
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
: >VECTOR ( seq -- v )
|
2009-01-28 16:07:16 -05:00
|
|
|
[ >ARRAY underlying>> ] [ length ] bi 1 <VECTOR> ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR clone
|
|
|
|
TYPE heap-size (prepare-copy)
|
2009-01-28 16:07:16 -05:00
|
|
|
[ XCOPY ] 3dip <VECTOR> ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR element-type
|
|
|
|
drop TYPE ;
|
|
|
|
M: VECTOR Vswap
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-swap) [ XSWAP ] 2dip ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR Viamax
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-nrm2) IXAMAX ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR (blas-vector-like)
|
2009-01-28 16:07:16 -05:00
|
|
|
drop <VECTOR> ;
|
2008-07-02 01:00:22 -04:00
|
|
|
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR (blas-direct-array)
|
|
|
|
[ underlying>> ]
|
|
|
|
[ [ length>> ] [ inc>> ] bi * ] bi
|
2009-01-28 16:07:16 -05:00
|
|
|
<DIRECT-ARRAY> ;
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
;FUNCTOR
|
|
|
|
|
|
|
|
|
|
|
|
FUNCTOR: (define-real-blas-vector) ( TYPE T -- )
|
|
|
|
|
|
|
|
VECTOR IS ${TYPE}-blas-vector
|
|
|
|
XDOT IS cblas_${T}dot
|
|
|
|
XNRM2 IS cblas_${T}nrm2
|
|
|
|
XASUM IS cblas_${T}asum
|
2008-12-04 17:03:13 -05:00
|
|
|
XAXPY IS cblas_${T}axpy
|
|
|
|
XSCAL IS cblas_${T}scal
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
|
|
|
M: VECTOR V.
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-dot) XDOT ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR V.conj
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-dot) XDOT ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR Vnorm
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-nrm2) XNRM2 ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR Vasum
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-nrm2) XASUM ;
|
2008-12-04 17:03:13 -05:00
|
|
|
M: VECTOR n*V+V!
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-axpy) [ XAXPY ] dip ;
|
2008-12-04 17:03:13 -05:00
|
|
|
M: VECTOR n*V!
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-scal) [ XSCAL ] dip ;
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
;FUNCTOR
|
|
|
|
|
|
|
|
|
|
|
|
FUNCTOR: (define-complex-helpers) ( TYPE -- )
|
|
|
|
|
|
|
|
<DIRECT-COMPLEX-ARRAY> DEFINES <direct-${TYPE}-complex-array>
|
|
|
|
>COMPLEX-ARRAY DEFINES >${TYPE}-complex-array
|
2008-12-04 19:08:01 -05:00
|
|
|
ARG>COMPLEX DEFINES arg>${TYPE}-complex
|
|
|
|
COMPLEX>ARG DEFINES ${TYPE}-complex>arg
|
2008-12-04 16:40:55 -05:00
|
|
|
<DIRECT-ARRAY> IS <direct-${TYPE}-array>
|
|
|
|
>ARRAY IS >${TYPE}-array
|
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
|
|
|
: <DIRECT-COMPLEX-ARRAY> ( alien len -- sequence )
|
2009-01-28 16:07:16 -05:00
|
|
|
1 shift <DIRECT-ARRAY> <complex-sequence> ;
|
2008-12-04 16:40:55 -05:00
|
|
|
: >COMPLEX-ARRAY ( sequence -- sequence )
|
2009-01-28 16:07:16 -05:00
|
|
|
<complex-components> >ARRAY ;
|
2008-12-04 19:08:01 -05:00
|
|
|
: COMPLEX>ARG ( complex -- alien )
|
2009-01-28 16:07:16 -05:00
|
|
|
>rect 2array >ARRAY underlying>> ;
|
2008-12-04 19:08:01 -05:00
|
|
|
: ARG>COMPLEX ( alien -- complex )
|
2009-01-28 16:07:16 -05:00
|
|
|
2 <DIRECT-ARRAY> first2 rect> ;
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
;FUNCTOR
|
|
|
|
|
|
|
|
|
|
|
|
FUNCTOR: (define-complex-blas-vector) ( TYPE C S -- )
|
|
|
|
|
|
|
|
VECTOR IS ${TYPE}-blas-vector
|
|
|
|
XDOTU_SUB IS cblas_${C}dotu_sub
|
|
|
|
XDOTC_SUB IS cblas_${C}dotc_sub
|
|
|
|
XXNRM2 IS cblas_${S}${C}nrm2
|
|
|
|
XXASUM IS cblas_${S}${C}asum
|
2008-12-04 17:03:13 -05:00
|
|
|
XAXPY IS cblas_${C}axpy
|
|
|
|
XSCAL IS cblas_${C}scal
|
2008-12-04 19:08:01 -05:00
|
|
|
TYPE>ARG IS ${TYPE}>arg
|
|
|
|
ARG>TYPE IS arg>${TYPE}
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
WHERE
|
|
|
|
|
|
|
|
M: VECTOR V.
|
|
|
|
(prepare-dot) TYPE <c-object>
|
2009-01-28 16:07:16 -05:00
|
|
|
[ XDOTU_SUB ] keep
|
|
|
|
ARG>TYPE ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR V.conj
|
|
|
|
(prepare-dot) TYPE <c-object>
|
2009-01-28 16:07:16 -05:00
|
|
|
[ XDOTC_SUB ] keep
|
|
|
|
ARG>TYPE ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR Vnorm
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-nrm2) XXNRM2 ;
|
2008-12-04 16:40:55 -05:00
|
|
|
M: VECTOR Vasum
|
2009-01-28 16:07:16 -05:00
|
|
|
(prepare-nrm2) XXASUM ;
|
2008-12-04 17:03:13 -05:00
|
|
|
M: VECTOR n*V+V!
|
2009-01-28 16:07:16 -05:00
|
|
|
[ TYPE>ARG ] 2dip
|
|
|
|
(prepare-axpy) [ XAXPY ] dip ;
|
2008-12-04 17:03:13 -05:00
|
|
|
M: VECTOR n*V!
|
2009-01-28 16:07:16 -05:00
|
|
|
[ TYPE>ARG ] dip
|
|
|
|
(prepare-scal) [ XSCAL ] dip ;
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
;FUNCTOR
|
|
|
|
|
|
|
|
|
|
|
|
: define-real-blas-vector ( TYPE T -- )
|
|
|
|
[ (define-blas-vector) ]
|
|
|
|
[ (define-real-blas-vector) ] 2bi ;
|
|
|
|
:: define-complex-blas-vector ( TYPE C S -- )
|
|
|
|
TYPE (define-complex-helpers)
|
|
|
|
TYPE "-complex" append
|
|
|
|
[ C (define-blas-vector) ]
|
2008-12-04 17:03:13 -05:00
|
|
|
[ C S (define-complex-blas-vector) ] bi ;
|
2008-12-04 16:40:55 -05:00
|
|
|
|
|
|
|
"float" "s" define-real-blas-vector
|
|
|
|
"double" "d" define-real-blas-vector
|
|
|
|
"float" "c" "s" define-complex-blas-vector
|
|
|
|
"double" "z" "d" define-complex-blas-vector
|
|
|
|
|
|
|
|
>>
|
2008-07-05 21:28:53 -04:00
|
|
|
|