]> gitweb.factorcode.org Git - factor.git/blob - basis/math/blas/vectors/vectors.factor
Specialized array overhaul
[factor.git] / basis / math / blas / vectors / vectors.factor
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 ;
6 SPECIALIZED-ARRAY: float
7 SPECIALIZED-ARRAY: double
8 SPECIALIZED-ARRAY: complex-float
9 SPECIALIZED-ARRAY: complex-double
10 IN: math.blas.vectors
11
12 TUPLE: blas-vector-base underlying length inc ;
13
14 INSTANCE: blas-vector-base virtual-sequence
15
16 GENERIC: element-type ( v -- type )
17
18 GENERIC: n*V+V! ( alpha x y -- y=alpha*x+y )
19 GENERIC: n*V!   ( alpha x -- x=alpha*x )
20 GENERIC: V. ( x y -- x.y )
21 GENERIC: V.conj ( x y -- xconj.y )
22 GENERIC: Vnorm ( x -- norm )
23 GENERIC: Vasum ( x -- sum )
24 GENERIC: Vswap ( x y -- x=y y=x )
25 GENERIC: Viamax ( x -- max-i )
26
27 <PRIVATE
28
29 GENERIC: (blas-vector-like) ( data length inc exemplar -- vector )
30
31 GENERIC: (blas-direct-array) ( blas-vector -- direct-array )
32
33 : shorter-length ( v1 v2 -- length )
34     [ length>> ] bi@ min ; inline
35 : data-and-inc ( v -- data inc )
36     [ ] [ inc>> ] bi ; inline
37 : datas-and-incs ( v1 v2 -- v1-data v1-inc v2-data v2-inc )
38     [ data-and-inc ] bi@ ; inline
39
40 :: (prepare-copy)
41     ( v element-size -- length v-data v-inc v-dest-data v-dest-inc
42                         copy-data copy-length copy-inc )
43     v [ length>> ] [ data-and-inc ] bi
44     v length>> element-size * <byte-array>
45     1 
46     over v length>> 1 ;
47
48 : (prepare-swap)
49     ( v1 v2 -- length v1-data v1-inc v2-data v2-inc
50                v1 v2 )
51     [ shorter-length ] [ datas-and-incs ] [ ] 2tri ;
52
53 :: (prepare-axpy)
54     ( n v1 v2 -- length n v1-data v1-inc v2-data v2-inc
55                  v2 )
56     v1 v2 shorter-length
57     n
58     v1 v2 datas-and-incs
59     v2 ;
60
61 :: (prepare-scal)
62     ( n v -- length n v-data v-inc
63              v )
64     v length>>
65     n
66     v data-and-inc
67     v ;
68
69 : (prepare-dot) ( v1 v2 -- length v1-data v1-inc v2-data v2-inc )
70     [ shorter-length ] [ datas-and-incs ] 2bi ;
71
72 : (prepare-nrm2) ( v -- length data inc )
73     [ length>> ] [ data-and-inc ] bi ;
74
75 PRIVATE>
76
77 : n*V+V ( alpha x y -- alpha*x+y ) clone n*V+V! ; inline
78 : n*V ( alpha x -- alpha*x ) clone n*V! ; inline
79
80 : V+ ( x y -- x+y )
81     1.0 -rot n*V+V ; inline
82 : V- ( x y -- x-y )
83     -1.0 spin n*V+V ; inline
84
85 : Vneg ( x -- -x )
86     -1.0 swap n*V ; inline
87
88 : V*n ( x alpha -- x*alpha )
89     swap n*V ; inline
90 : V/n ( x alpha -- x/alpha )
91     recip swap n*V ; inline
92
93 : Vamax ( x -- max )
94     [ Viamax ] keep nth ; inline
95
96 :: Vsub ( v start length -- sub )
97     v inc>> start * v element-type heap-size *
98     v underlying>> <displaced-alien>
99     length v inc>> v (blas-vector-like) ;
100
101 : <zero-vector> ( exemplar -- zero )
102     [ element-type heap-size <byte-array> ]
103     [ length>> 0 ]
104     [ (blas-vector-like) ] tri ;
105
106 : <empty-vector> ( length exemplar -- vector )
107     [ element-type heap-size * <byte-array> ]
108     [ 1 swap ] 2bi
109     (blas-vector-like) ;
110
111 M: blas-vector-base equal?
112     {
113         [ [ length ] bi@ = ]
114         [ [ = ] 2all? ]
115     } 2&& ;
116
117 M: blas-vector-base length
118     length>> ;
119 M: blas-vector-base virtual-seq
120     (blas-direct-array) ;
121 M: blas-vector-base virtual@
122     [ inc>> * ] [ nip (blas-direct-array) ] 2bi ;
123
124 : float>arg ( f -- f ) ; inline
125 : double>arg ( f -- f ) ; inline
126 : arg>float ( f -- f ) ; inline
127 : arg>double ( f -- f ) ; inline
128
129 <<
130
131 FUNCTOR: (define-blas-vector) ( TYPE T -- )
132
133 <DIRECT-ARRAY> IS <direct-${TYPE}-array>
134 >ARRAY         IS >${TYPE}-array
135 XCOPY          IS ${T}COPY
136 XSWAP          IS ${T}SWAP
137 IXAMAX         IS I${T}AMAX
138
139 VECTOR         DEFINES-CLASS ${TYPE}-blas-vector
140 <VECTOR>       DEFINES <${TYPE}-blas-vector>
141 >VECTOR        DEFINES >${TYPE}-blas-vector
142
143 t              [ T >lower ]
144
145 XVECTOR{       DEFINES ${t}vector{
146
147 XAXPY          IS ${T}AXPY
148 XSCAL          IS ${T}SCAL
149
150 WHERE
151
152 TUPLE: VECTOR < blas-vector-base ;
153 : <VECTOR> ( underlying length inc -- vector ) VECTOR boa ; inline
154
155 : >VECTOR ( seq -- v )
156     [ >ARRAY underlying>> ] [ length ] bi 1 <VECTOR> ;
157
158 M: VECTOR clone
159     TYPE heap-size (prepare-copy)
160     [ XCOPY ] 3dip <VECTOR> ;
161
162 M: VECTOR element-type
163     drop TYPE ;
164 M: VECTOR Vswap
165     (prepare-swap) [ XSWAP ] 2dip ;
166 M: VECTOR Viamax
167     (prepare-nrm2) IXAMAX 1 - ;
168
169 M: VECTOR (blas-vector-like)
170     drop <VECTOR> ;
171
172 M: VECTOR (blas-direct-array)
173     [ underlying>> ]
174     [ [ length>> ] [ inc>> ] bi * ] bi
175     <DIRECT-ARRAY> ;
176
177 M: VECTOR n*V+V!
178     (prepare-axpy) [ XAXPY ] dip ;
179 M: VECTOR n*V!
180     (prepare-scal) [ XSCAL ] dip ;
181
182 SYNTAX: XVECTOR{ \ } [ >VECTOR ] parse-literal ;
183
184 M: VECTOR pprint-delims
185     drop \ XVECTOR{ \ } ;
186
187 ;FUNCTOR
188
189
190 FUNCTOR: (define-real-blas-vector) ( TYPE T -- )
191
192 VECTOR         IS ${TYPE}-blas-vector
193 XDOT           IS ${T}DOT
194 XNRM2          IS ${T}NRM2
195 XASUM          IS ${T}ASUM
196
197 WHERE
198
199 M: VECTOR V.
200     (prepare-dot) XDOT ;
201 M: VECTOR V.conj
202     (prepare-dot) XDOT ;
203 M: VECTOR Vnorm
204     (prepare-nrm2) XNRM2 ;
205 M: VECTOR Vasum
206     (prepare-nrm2) XASUM ;
207
208 ;FUNCTOR
209
210
211 FUNCTOR: (define-complex-blas-vector) ( TYPE C S -- )
212
213 VECTOR         IS ${TYPE}-blas-vector
214 XDOTU          IS ${C}DOTU
215 XDOTC          IS ${C}DOTC
216 XXNRM2         IS ${S}${C}NRM2
217 XXASUM         IS ${S}${C}ASUM
218
219 WHERE
220
221 M: VECTOR V.
222     (prepare-dot) XDOTU ;
223 M: VECTOR V.conj
224     (prepare-dot) XDOTC ;
225 M: VECTOR Vnorm
226     (prepare-nrm2) XXNRM2 ;
227 M: VECTOR Vasum
228     (prepare-nrm2) XXASUM ;
229
230 ;FUNCTOR
231
232
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 ;
239
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
244
245 >>
246
247 M: blas-vector-base >pprint-sequence ;
248 M: blas-vector-base pprint* pprint-object ;