1 USING: accessors alien alien.c-types arrays ascii byte-arrays combinators
2 combinators.short-circuit fry kernel math math.blas.ffi
3 math.complex math.functions math.order sequences sequences.private
4 functors words locals parser prettyprint.backend prettyprint.custom
5 specialized-arrays.float specialized-arrays.double
6 specialized-arrays.complex-float specialized-arrays.complex-double ;
9 TUPLE: blas-vector-base underlying length inc ;
11 INSTANCE: blas-vector-base virtual-sequence
13 GENERIC: element-type ( v -- type )
15 GENERIC: n*V+V! ( alpha x y -- y=alpha*x+y )
16 GENERIC: n*V! ( alpha x -- x=alpha*x )
17 GENERIC: V. ( x y -- x.y )
18 GENERIC: V.conj ( x y -- xconj.y )
19 GENERIC: Vnorm ( x -- norm )
20 GENERIC: Vasum ( x -- sum )
21 GENERIC: Vswap ( x y -- x=y y=x )
22 GENERIC: Viamax ( x -- max-i )
26 GENERIC: (blas-vector-like) ( data length inc exemplar -- vector )
28 GENERIC: (blas-direct-array) ( blas-vector -- direct-array )
30 : shorter-length ( v1 v2 -- length )
31 [ length>> ] bi@ min ; inline
32 : data-and-inc ( v -- data inc )
33 [ ] [ inc>> ] bi ; inline
34 : datas-and-incs ( v1 v2 -- v1-data v1-inc v2-data v2-inc )
35 [ data-and-inc ] bi@ ; inline
38 ( v element-size -- length v-data v-inc v-dest-data v-dest-inc
39 copy-data copy-length copy-inc )
40 v [ length>> ] [ data-and-inc ] bi
41 v length>> element-size * <byte-array>
46 ( v1 v2 -- length v1-data v1-inc v2-data v2-inc
48 [ shorter-length ] [ datas-and-incs ] [ ] 2tri ;
51 ( n v1 v2 -- length n v1-data v1-inc v2-data v2-inc
59 ( n v -- length n v-data v-inc
66 : (prepare-dot) ( v1 v2 -- length v1-data v1-inc v2-data v2-inc )
67 [ shorter-length ] [ datas-and-incs ] 2bi ;
69 : (prepare-nrm2) ( v -- length data inc )
70 [ length>> ] [ data-and-inc ] bi ;
74 : n*V+V ( alpha x y -- alpha*x+y ) clone n*V+V! ; inline
75 : n*V ( alpha x -- alpha*x ) clone n*V! ; inline
78 1.0 -rot n*V+V ; inline
80 -1.0 spin n*V+V ; inline
83 -1.0 swap n*V ; inline
85 : V*n ( x alpha -- x*alpha )
87 : V/n ( x alpha -- x/alpha )
88 recip swap n*V ; inline
91 [ Viamax ] keep nth ; inline
93 :: Vsub ( v start length -- sub )
94 v inc>> start * v element-type heap-size *
95 v underlying>> <displaced-alien>
96 length v inc>> v (blas-vector-like) ;
98 : <zero-vector> ( exemplar -- zero )
99 [ element-type heap-size <byte-array> ]
101 [ (blas-vector-like) ] tri ;
103 : <empty-vector> ( length exemplar -- vector )
104 [ element-type heap-size * <byte-array> ]
108 M: blas-vector-base equal?
114 M: blas-vector-base length
116 M: blas-vector-base virtual-seq
117 (blas-direct-array) ;
118 M: blas-vector-base virtual@
119 [ inc>> * ] [ nip (blas-direct-array) ] 2bi ;
121 : float>arg ( f -- f ) ; inline
122 : double>arg ( f -- f ) ; inline
123 : arg>float ( f -- f ) ; inline
124 : arg>double ( f -- f ) ; inline
128 FUNCTOR: (define-blas-vector) ( TYPE T -- )
130 <DIRECT-ARRAY> IS <direct-${TYPE}-array>
131 >ARRAY IS >${TYPE}-array
136 VECTOR DEFINES-CLASS ${TYPE}-blas-vector
137 <VECTOR> DEFINES <${TYPE}-blas-vector>
138 >VECTOR DEFINES >${TYPE}-blas-vector
142 XVECTOR{ DEFINES ${t}vector{
149 TUPLE: VECTOR < blas-vector-base ;
150 : <VECTOR> ( underlying length inc -- vector ) VECTOR boa ; inline
152 : >VECTOR ( seq -- v )
153 [ >ARRAY underlying>> ] [ length ] bi 1 <VECTOR> ;
156 TYPE heap-size (prepare-copy)
157 [ XCOPY ] 3dip <VECTOR> ;
159 M: VECTOR element-type
162 (prepare-swap) [ XSWAP ] 2dip ;
164 (prepare-nrm2) IXAMAX 1 - ;
166 M: VECTOR (blas-vector-like)
169 M: VECTOR (blas-direct-array)
171 [ [ length>> ] [ inc>> ] bi * ] bi
175 (prepare-axpy) [ XAXPY ] dip ;
177 (prepare-scal) [ XSCAL ] dip ;
179 SYNTAX: XVECTOR{ \ } [ >VECTOR ] parse-literal ;
181 M: VECTOR pprint-delims
182 drop \ XVECTOR{ \ } ;
187 FUNCTOR: (define-real-blas-vector) ( TYPE T -- )
189 VECTOR IS ${TYPE}-blas-vector
201 (prepare-nrm2) XNRM2 ;
203 (prepare-nrm2) XASUM ;
208 FUNCTOR: (define-complex-blas-vector) ( TYPE C S -- )
210 VECTOR IS ${TYPE}-blas-vector
213 XXNRM2 IS ${S}${C}NRM2
214 XXASUM IS ${S}${C}ASUM
219 (prepare-dot) XDOTU ;
221 (prepare-dot) XDOTC ;
223 (prepare-nrm2) XXNRM2 ;
225 (prepare-nrm2) XXASUM ;
230 : define-real-blas-vector ( TYPE T -- )
231 [ (define-blas-vector) ]
232 [ (define-real-blas-vector) ] 2bi ;
233 : define-complex-blas-vector ( TYPE C S -- )
234 [ drop (define-blas-vector) ]
235 [ (define-complex-blas-vector) ] 3bi ;
237 "float" "S" define-real-blas-vector
238 "double" "D" define-real-blas-vector
239 "complex-float" "C" "S" define-complex-blas-vector
240 "complex-double" "Z" "D" define-complex-blas-vector
244 M: blas-vector-base >pprint-sequence ;
245 M: blas-vector-base pprint* pprint-object ;