1 USING: accessors alien alien.c-types alien.complex alien.data
2 arrays ascii byte-arrays combinators combinators.short-circuit
3 fry kernel math math.blas.ffi math.complex math.functions
4 math.order sequences sequences.private functors words locals
5 parser prettyprint.backend prettyprint.custom specialized-arrays ;
6 FROM: alien.c-types => float ;
7 SPECIALIZED-ARRAY: float
8 SPECIALIZED-ARRAY: double
9 SPECIALIZED-ARRAY: complex-float
10 SPECIALIZED-ARRAY: complex-double
13 TUPLE: blas-vector-base underlying length inc ;
15 INSTANCE: blas-vector-base virtual-sequence
17 GENERIC: element-type ( v -- type )
19 GENERIC: n*V+V! ( alpha x y -- y=alpha*x+y )
20 GENERIC: n*V! ( alpha x -- x=alpha*x )
21 GENERIC: V. ( x y -- x.y )
22 GENERIC: V.conj ( x y -- xconj.y )
23 GENERIC: Vnorm ( x -- norm )
24 GENERIC: Vasum ( x -- sum )
25 GENERIC: Vswap ( x y -- x=y y=x )
26 GENERIC: Viamax ( x -- max-i )
30 GENERIC: (blas-vector-like) ( data length inc exemplar -- vector )
32 GENERIC: (blas-direct-array) ( blas-vector -- direct-array )
34 : shorter-length ( v1 v2 -- length )
35 [ length>> ] bi@ min ; inline
36 : data-and-inc ( v -- data inc )
37 [ ] [ inc>> ] bi ; inline
38 : datas-and-incs ( v1 v2 -- v1-data v1-inc v2-data v2-inc )
39 [ data-and-inc ] bi@ ; inline
42 ( v element-size -- length v-data v-inc v-dest-data v-dest-inc
43 copy-data copy-length copy-inc )
44 v [ length>> ] [ data-and-inc ] bi
45 v length>> element-size * <byte-array>
50 ( v1 v2 -- length v1-data v1-inc v2-data v2-inc
52 [ shorter-length ] [ datas-and-incs ] [ ] 2tri ;
55 ( n v1 v2 -- length n v1-data v1-inc v2-data v2-inc
63 ( n v -- length n v-data v-inc
70 : (prepare-dot) ( v1 v2 -- length v1-data v1-inc v2-data v2-inc )
71 [ shorter-length ] [ datas-and-incs ] 2bi ;
73 : (prepare-nrm2) ( v -- length data inc )
74 [ length>> ] [ data-and-inc ] bi ;
78 : n*V+V ( alpha x y -- alpha*x+y ) clone n*V+V! ; inline
79 : n*V ( alpha x -- alpha*x ) clone n*V! ; inline
82 1.0 x y n*V+V ; inline
84 -1.0 y x n*V+V ; inline
87 -1.0 swap n*V ; inline
89 : V*n ( x alpha -- x*alpha )
91 : V/n ( x alpha -- x/alpha )
92 recip swap n*V ; inline
95 [ Viamax ] keep nth ; inline
97 :: Vsub ( v start length -- sub )
98 v inc>> start * v element-type heap-size *
99 v underlying>> <displaced-alien>
100 length v inc>> v (blas-vector-like) ;
102 : <zero-vector> ( exemplar -- zero )
103 [ element-type heap-size <byte-array> ]
105 [ (blas-vector-like) ] tri ;
107 : <empty-vector> ( length exemplar -- vector )
108 [ element-type heap-size * <byte-array> ]
112 M: blas-vector-base equal?
118 M: blas-vector-base length
120 M: blas-vector-base virtual-exemplar
121 (blas-direct-array) ;
122 M: blas-vector-base virtual@
123 [ inc>> * ] [ nip (blas-direct-array) ] 2bi ;
125 : float>arg ( f -- f ) ; inline
126 : double>arg ( f -- f ) ; inline
127 : arg>float ( f -- f ) ; inline
128 : arg>double ( f -- f ) ; inline
132 FUNCTOR: (define-blas-vector) ( TYPE T -- )
134 <DIRECT-ARRAY> IS <direct-${TYPE}-array>
139 VECTOR DEFINES-CLASS ${TYPE}-blas-vector
140 <VECTOR> DEFINES <${TYPE}-blas-vector>
141 >VECTOR DEFINES >${TYPE}-blas-vector
145 XVECTOR{ DEFINES ${t}vector{
152 TUPLE: VECTOR < blas-vector-base ;
153 : <VECTOR> ( underlying length inc -- vector ) VECTOR boa ; inline
155 : >VECTOR ( seq -- v )
156 [ TYPE >c-array underlying>> ] [ length ] bi 1 <VECTOR> ;
159 TYPE heap-size (prepare-copy)
160 [ XCOPY ] 3dip <VECTOR> ;
162 M: VECTOR element-type
165 (prepare-swap) [ XSWAP ] 2dip ;
167 (prepare-nrm2) IXAMAX 1 - ;
169 M: VECTOR (blas-vector-like)
172 M: VECTOR (blas-direct-array)
174 [ [ length>> ] [ inc>> ] bi * ] bi
178 (prepare-axpy) [ XAXPY ] dip ;
180 (prepare-scal) [ XSCAL ] dip ;
182 SYNTAX: XVECTOR{ \ } [ >VECTOR ] parse-literal ;
184 M: VECTOR pprint-delims
185 drop \ XVECTOR{ \ } ;
190 FUNCTOR: (define-real-blas-vector) ( TYPE T -- )
192 VECTOR IS ${TYPE}-blas-vector
204 (prepare-nrm2) XNRM2 ;
206 (prepare-nrm2) XASUM ;
211 FUNCTOR: (define-complex-blas-vector) ( TYPE C S -- )
213 VECTOR IS ${TYPE}-blas-vector
216 XXNRM2 IS ${S}${C}NRM2
217 XXASUM IS ${S}${C}ASUM
222 (prepare-dot) XDOTU ;
224 (prepare-dot) XDOTC ;
226 (prepare-nrm2) XXNRM2 ;
228 (prepare-nrm2) XXASUM ;
233 : define-real-blas-vector ( TYPE T -- )
234 [ (define-blas-vector) ]
235 [ (define-real-blas-vector) ] 2bi ;
236 : define-complex-blas-vector ( TYPE C S -- )
237 [ drop (define-blas-vector) ]
238 [ (define-complex-blas-vector) ] 3bi ;
240 float "S" define-real-blas-vector
241 double "D" define-real-blas-vector
242 complex-float "C" "S" define-complex-blas-vector
243 complex-double "Z" "D" define-complex-blas-vector
247 M: blas-vector-base >pprint-sequence ;
248 M: blas-vector-base pprint* pprint-object ;