scm2cpp-lasso 0.13.2__py3-none-win_amd64.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. scm2cpp_lasso/__init__.py +2808 -0
  2. scm2cpp_lasso/_classifiers.cp312-win_amd64.pyd +0 -0
  3. scm2cpp_lasso/_generated/__init__.py +0 -0
  4. scm2cpp_lasso/_generated/_loader.py +41 -0
  5. scm2cpp_lasso/_generated/_loader_accel_cublas.py +140 -0
  6. scm2cpp_lasso/_generated/_loader_classifiers.py +281 -0
  7. scm2cpp_lasso/_generated/_loader_derived_blas.py +29 -0
  8. scm2cpp_lasso/_generated/_loader_derived_cublas.py +29 -0
  9. scm2cpp_lasso/_generated/_loader_derived_loops.py +29 -0
  10. scm2cpp_lasso/_generated/_loader_gpu_cuda.py +294 -0
  11. scm2cpp_lasso/_generated/_loader_gramops_blas.py +496 -0
  12. scm2cpp_lasso/_generated/_loader_gramops_loops.py +496 -0
  13. scm2cpp_lasso/_generated/_loader_nmf_blas.py +29 -0
  14. scm2cpp_lasso/_generated/_loader_nmf_cublas.py +29 -0
  15. scm2cpp_lasso/_generated/_loader_nmf_loops.py +29 -0
  16. scm2cpp_lasso/_generated/accel_cublas/accel.cpp +20 -0
  17. scm2cpp_lasso/_generated/accel_cublas/accel.hpp +1023 -0
  18. scm2cpp_lasso/_generated/accel_cublas/accel_capi.cpp +64 -0
  19. scm2cpp_lasso/_generated/classifiers.cpp +34 -0
  20. scm2cpp_lasso/_generated/classifiers.hpp +2825 -0
  21. scm2cpp_lasso/_generated/classifiers_capi.cpp +106 -0
  22. scm2cpp_lasso/_generated/derived_blas/lasso_design.cpp +5 -0
  23. scm2cpp_lasso/_generated/derived_blas/lasso_design.hpp +153 -0
  24. scm2cpp_lasso/_generated/derived_blas/lasso_design_capi.cpp +19 -0
  25. scm2cpp_lasso/_generated/derived_cublas/lasso_design.cpp +5 -0
  26. scm2cpp_lasso/_generated/derived_cublas/lasso_design.hpp +155 -0
  27. scm2cpp_lasso/_generated/derived_cublas/lasso_design_capi.cpp +19 -0
  28. scm2cpp_lasso/_generated/derived_loops/lasso_design.cpp +5 -0
  29. scm2cpp_lasso/_generated/derived_loops/lasso_design.hpp +176 -0
  30. scm2cpp_lasso/_generated/derived_loops/lasso_design_capi.cpp +19 -0
  31. scm2cpp_lasso/_generated/gpu_cuda/gpu.cpp +35 -0
  32. scm2cpp_lasso/_generated/gpu_cuda/gpu.hpp +3715 -0
  33. scm2cpp_lasso/_generated/gpu_cuda/gpu_capi.cpp +289 -0
  34. scm2cpp_lasso/_generated/gramops_blas/gramops.cpp +57 -0
  35. scm2cpp_lasso/_generated/gramops_blas/gramops.hpp +3033 -0
  36. scm2cpp_lasso/_generated/gramops_blas/gramops_capi.cpp +175 -0
  37. scm2cpp_lasso/_generated/gramops_loops/gramops.cpp +57 -0
  38. scm2cpp_lasso/_generated/gramops_loops/gramops.hpp +3302 -0
  39. scm2cpp_lasso/_generated/gramops_loops/gramops_capi.cpp +175 -0
  40. scm2cpp_lasso/_generated/lasso_cov.cpp +7 -0
  41. scm2cpp_lasso/_generated/lasso_cov.hpp +152 -0
  42. scm2cpp_lasso/_generated/lasso_cov_capi.cpp +25 -0
  43. scm2cpp_lasso/_generated/nmf_blas/nmf_cd.cpp +5 -0
  44. scm2cpp_lasso/_generated/nmf_blas/nmf_cd.hpp +132 -0
  45. scm2cpp_lasso/_generated/nmf_blas/nmf_cd_capi.cpp +19 -0
  46. scm2cpp_lasso/_generated/nmf_cublas/nmf_cd.cpp +5 -0
  47. scm2cpp_lasso/_generated/nmf_cublas/nmf_cd.hpp +136 -0
  48. scm2cpp_lasso/_generated/nmf_cublas/nmf_cd_capi.cpp +19 -0
  49. scm2cpp_lasso/_generated/nmf_loops/nmf_cd.cpp +5 -0
  50. scm2cpp_lasso/_generated/nmf_loops/nmf_cd.hpp +167 -0
  51. scm2cpp_lasso/_generated/nmf_loops/nmf_cd_capi.cpp +19 -0
  52. scm2cpp_lasso/_generated/scm2cpp-blas.hpp +77 -0
  53. scm2cpp_lasso/_generated/scm2cpp-cublas.hpp +155 -0
  54. scm2cpp_lasso/_generated/scm2cpp-cuda.hpp +285 -0
  55. scm2cpp_lasso/_generated/scm2cpp.hpp +1627 -0
  56. scm2cpp_lasso/_gramops_loops.cp312-win_amd64.pyd +0 -0
  57. scm2cpp_lasso/_lasso_cov.cp312-win_amd64.pyd +0 -0
  58. scm2cpp_lasso/_lasso_derived_loops.cp312-win_amd64.pyd +0 -0
  59. scm2cpp_lasso/_libfind.py +149 -0
  60. scm2cpp_lasso/_nmf_loops.cp312-win_amd64.pyd +0 -0
  61. scm2cpp_lasso/batch_capi.cu +428 -0
  62. scm2cpp_lasso-0.13.2.dist-info/METADATA +920 -0
  63. scm2cpp_lasso-0.13.2.dist-info/RECORD +65 -0
  64. scm2cpp_lasso-0.13.2.dist-info/WHEEL +5 -0
  65. scm2cpp_lasso-0.13.2.dist-info/top_level.txt +1 -0
@@ -0,0 +1,1023 @@
1
+
2
+ #ifndef ACCEL_HPP
3
+ #define ACCEL_HPP
4
+ #define SCM2CPP_MINIMAL
5
+ #include <cmath>
6
+ #include "scm2cpp-cublas.hpp"
7
+ #include <math.h>
8
+ #include <vector>
9
+ #include "scm2cpp.hpp"
10
+
11
+ int
12
+ f_regression( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> r, scm2cpp::span<double> f, int center, int n, int p, int q );
13
+ int
14
+ f_classif( scm2cpp::cspan<double> x, scm2cpp::cspan<int> lab, scm2cpp::span<double> f, int n, int p, int k );
15
+ SCM2CPP_FN double
16
+ min_pos( scm2cpp::cspan<double> v, int m );
17
+ SCM2CPP_FN double
18
+ round_even( double y );
19
+ SCM2CPP_FN int
20
+ cholesky_delete( scm2cpp::span<double> l, int go, int n, int k );
21
+ SCM2CPP_FN int
22
+ swap_rc( scm2cpp::span<double> g, int a, int b, int p );
23
+ int
24
+ lars( scm2cpp::span<double> g, scm2cpp::cspan<double> cov, scm2cpp::span<double> coef, scm2cpp::span<double> path, scm2cpp::span<double> alphas, int rec, double nsamples, double alphamin, int method, int maxiter, double eps, int p );
25
+ SCM2CPP_FN int
26
+ rfe_chol( scm2cpp::cspan<double> g, scm2cpp::cspan<int> idx, scm2cpp::span<double> l, double alpha, int k, int p );
27
+ SCM2CPP_FN int
28
+ rfe_solve( scm2cpp::cspan<double> c, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::cspan<double> l, int k, int p );
29
+ int
30
+ rfe( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> coef, scm2cpp::span<int> support, scm2cpp::span<int> ranking, double alpha, int nsel, int step, int n, int p );
31
+ SCM2CPP_FN double
32
+ eig_hypot( double a, double b );
33
+ SCM2CPP_FN int
34
+ sym_eigen( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m );
35
+ SCM2CPP_FN double
36
+ eig_abs( double a );
37
+ SCM2CPP_FN int
38
+ eig_order( scm2cpp::cspan<double> ev, scm2cpp::span<int> ord, int m );
39
+ int
40
+ pca( scm2cpp::cspan<double> x, scm2cpp::span<double> comps, scm2cpp::span<double> ev, scm2cpp::span<double> sv, scm2cpp::span<double> mean, int n, int p );
41
+ SCM2CPP_FN int
42
+ gram_xx( scm2cpp::cspan<double> x, scm2cpp::span<double> g, int n, int p );
43
+ SCM2CPP_FN int
44
+ gram_xy( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> c, int n, int p, int q );
45
+
46
+
47
+
48
+ inline int
49
+ f_regression( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> r, scm2cpp::span<double> f, int center, int n, int p, int q )
50
+ { std::vector<double> mx(p,0.0);
51
+ std::vector<double> sx(p,0.0);
52
+ std::vector<double> yc((n*q),0.0);
53
+ std::vector<double> yn(q,0.0);
54
+ double dof = double((n-( ( center == 1 ) ? (2) : (1) )));
55
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
56
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
57
+ double v = x[((i*p)+j)];
58
+ mx[ j ] = (mx[j]+v) ;
59
+ sx[ j ] = (sx[j]+(v*v)) ;
60
+ ;
61
+ } ;
62
+ } ;
63
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
64
+ mx[ j ] = (mx[j]/double(n)) ;
65
+ } ;
66
+ for( int t = 0 ; !(t == q) ; t = (t+1) ) {
67
+ double m = 0.0;
68
+ double s = 0.0;
69
+ if( center == 1 ) {
70
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
71
+ m = (m+y[((i*q)+t)]) ;
72
+ } ;
73
+ m = (m/double(n)) ;
74
+ } else {
75
+ 0 ;
76
+ } ;
77
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
78
+ double v = (y[((i*q)+t)]-m);
79
+ yc[ ((i*q)+t) ] = v ;
80
+ s = (s+(v*v)) ;
81
+ ;
82
+ } ;
83
+ yn[ t ] = sqrt(s) ;
84
+ ;
85
+ } ;
86
+ scm2cpp::device_matrix d24287 = scm2cpp::dmat_upload(x, (n*p), 1);
87
+ scm2cpp::device_matrix d24288 = scm2cpp::dmat_upload(yc, (n*q), 1);
88
+ scm2cpp::blas_gemm(r, d24287, d24288, 1.0, 0.0, 1, 0, p, q, n) ;
89
+ ;
90
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
91
+ double xn = sqrt(( ( center == 1 ) ? ((sx[j]-(double(n)*(mx[j]*mx[j])))) : (sx[j]) ));
92
+ for( int t = 0 ; !(t == q) ; t = (t+1) ) {
93
+ double rr = ((r[((j*q)+t)]/xn)/yn[t]);
94
+ r[ ((j*q)+t) ] = rr ;
95
+ f[ ((j*q)+t) ] = (((rr*rr)/(1.0-(rr*rr)))*dof) ;
96
+ ;
97
+ } ;
98
+ ;
99
+ } ;
100
+ return 0 ;
101
+ }
102
+
103
+
104
+ inline int
105
+ f_classif( scm2cpp::cspan<double> x, scm2cpp::cspan<int> lab, scm2cpp::span<double> f, int n, int p, int k )
106
+ { std::vector<double> oh((n*k),0.0);
107
+ std::vector<double> cs((k*p),0.0);
108
+ std::vector<double> cnt(k,0.0);
109
+ std::vector<double> ss(p,0.0);
110
+ std::vector<double> tot(p,0.0);
111
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
112
+ int c = lab[i];
113
+ oh[ ((i*k)+c) ] = 1.0 ;
114
+ cnt[ c ] = (cnt[c]+1.0) ;
115
+ ;
116
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
117
+ double v = x[((i*p)+j)];
118
+ ss[ j ] = (ss[j]+(v*v)) ;
119
+ ;
120
+ } ;
121
+ } ;
122
+ scm2cpp::device_matrix d24289 = scm2cpp::dmat_upload(oh, (n*k), 1);
123
+ scm2cpp::device_matrix d24290 = scm2cpp::dmat_upload(x, (n*p), 1);
124
+ scm2cpp::blas_gemm(cs, d24289, d24290, 1.0, 0.0, 1, 0, k, p, n) ;
125
+ ;
126
+ for( int c = 0 ; !(c == k) ; c = (c+1) ) {
127
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
128
+ tot[ j ] = (tot[j]+cs[((c*p)+j)]) ;
129
+ } ;
130
+ } ;
131
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
132
+ double sq = ((tot[j]*tot[j])/double(n));
133
+ double ssbn = 0.0;
134
+ for( int c = 0 ; !(c == k) ; c = (c+1) ) {
135
+ ssbn = (ssbn+((cs[((c*p)+j)]*cs[((c*p)+j)])/cnt[c])) ;
136
+ } ;
137
+ ssbn = (ssbn-sq) ;
138
+ double sswn = ((ss[j]-sq)-ssbn);
139
+ f[ j ] = ((ssbn/double((k-1)))/(sswn/double((n-k)))) ;
140
+ ;
141
+ ;
142
+ } ;
143
+ return 0 ;
144
+ }
145
+
146
+
147
+ SCM2CPP_FN inline double
148
+ min_pos( scm2cpp::cspan<double> v, int m )
149
+ { double r = 1.7976931348623157e+308;
150
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
151
+ double x = v[i];
152
+ if( (x > 0.0 && x < r) ) {
153
+ r = x ;
154
+ } else {
155
+ 0 ;
156
+ } ;
157
+ ;
158
+ } ;
159
+ return r ;
160
+ }
161
+
162
+
163
+ SCM2CPP_FN inline double
164
+ round_even( double y )
165
+ {
166
+ double r = std::floor(y);
167
+ double d = (y-r);
168
+ if( d > 0.5 ) {
169
+ return (r+1.0) ;
170
+ }
171
+ else {
172
+ if( d < 0.5 ) {
173
+ return r ;
174
+ }
175
+ else {
176
+ if( (2.0*std::floor((0.5*r))) == r ) {
177
+ return r ;
178
+ }
179
+ else {
180
+ return (r+1.0) ;
181
+ }
182
+ }
183
+ }
184
+ }
185
+
186
+
187
+ SCM2CPP_FN inline int
188
+ cholesky_delete( scm2cpp::span<double> l, int go, int n, int k )
189
+ { for( int i = go ; !(i >= (n-1)) ; i = (i+1) ) {
190
+ for( int j = 0 ; !(j == (i+2)) ; j = (j+1) ) {
191
+ l[ ((i*k)+j) ] = l[(((i+1)*k)+j)] ;
192
+ } ;
193
+ } ;
194
+ for( int i = go ; !(i >= (n-1)) ; i = (i+1) ) {
195
+ double a = l[((i*k)+i)];
196
+ double b = l[((i*k)+(i+1))];
197
+ double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
198
+ double ab = ( ( b < 0.0 ) ? ((0.0-b)) : (b) );
199
+ double roe = ( ( aa > ab ) ? (a) : (b) );
200
+ double scale = (aa+ab);
201
+ double r = ( ( scale == 0.0 ) ? (0.0) : ((( ( roe < 0.0 ) ? (-1.0) : (1.0) )*(scale*sqrt((((a/scale)*(a/scale))+((b/scale)*(b/scale))))))) );
202
+ double c = ( ( scale == 0.0 ) ? (1.0) : ((a/r)) );
203
+ double s = ( ( scale == 0.0 ) ? (0.0) : ((b/r)) );
204
+ double cc = ( ( r < 0.0 ) ? ((0.0-c)) : (c) );
205
+ double ss = ( ( r < 0.0 ) ? ((0.0-s)) : (s) );
206
+ l[ ((i*k)+i) ] = ( ( r < 0.0 ) ? ((0.0-r)) : (r) ) ;
207
+ l[ ((i*k)+(i+1)) ] = 0.0 ;
208
+ for( int t = (i+1) ; !(t >= n) ; t = (t+1) ) {
209
+ double x = l[((t*k)+i)];
210
+ double y = l[((t*k)+(i+1))];
211
+ l[ ((t*k)+i) ] = ((cc*x)+(ss*y)) ;
212
+ l[ ((t*k)+(i+1)) ] = ((cc*y)-(ss*x)) ;
213
+ ;
214
+ } ;
215
+ ;
216
+ ;
217
+ ;
218
+ ;
219
+ ;
220
+ ;
221
+ } ;
222
+ return 0 ;
223
+ }
224
+
225
+
226
+ SCM2CPP_FN inline int
227
+ swap_rc( scm2cpp::span<double> g, int a, int b, int p )
228
+ { for( int j = 0 ; !(j == p) ; j = (j+1) ) {
229
+ double rv = g[((a*p)+j)];
230
+ g[ ((a*p)+j) ] = g[((b*p)+j)] ;
231
+ g[ ((b*p)+j) ] = rv ;
232
+ ;
233
+ } ;
234
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
235
+ double cv = g[((j*p)+a)];
236
+ g[ ((j*p)+a) ] = g[((j*p)+b)] ;
237
+ g[ ((j*p)+b) ] = cv ;
238
+ ;
239
+ } ;
240
+ return 0 ;
241
+ }
242
+
243
+
244
+ inline int
245
+ lars( scm2cpp::span<double> g, scm2cpp::cspan<double> cov, scm2cpp::span<double> coef, scm2cpp::span<double> path, scm2cpp::span<double> alphas, int rec, double nsamples, double alphamin, int method, int maxiter, double eps, int p )
246
+ { std::vector<double> g0((p*p),0.0);
247
+ std::vector<double> cov0(p,0.0);
248
+ std::vector<double> cbuf(p,0.0);
249
+ std::vector<int> index(p,0);
250
+ std::vector<int> active(p,0);
251
+ std::vector<double> sign(p,0.0);
252
+ int kmax = ( ( maxiter < p ) ? (maxiter) : (p) );
253
+ std::vector<double> prev(p,0.0);
254
+ std::vector<double> dir(p,0.0);
255
+ std::vector<double> ls(p,0.0);
256
+ std::vector<double> zz(p,0.0);
257
+ std::vector<double> ratio(p,0.0);
258
+ std::vector<int> idx(p,0);
259
+ std::vector<int> dropv(p,0);
260
+ int cs = 0;
261
+ int na = 0;
262
+ int niter = 0;
263
+ int drop = 0;
264
+ int done = 0;
265
+ double alpha = 0.0;
266
+ double prevalpha = 0.0;
267
+ double tiny32 = 1.1754943508222875e-38;
268
+ double eqtol = 1.1920928955078125e-7;
269
+ std::vector<double> l((kmax*kmax),0.0);
270
+ for( int t = 0 ; !(t == (p*p)) ; t = (t+1) ) {
271
+ g0[ t ] = g[t] ;
272
+ } ;
273
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
274
+ cov0[ j ] = cov[j] ;
275
+ cbuf[ j ] = cov[j] ;
276
+ index[ j ] = j ;
277
+ coef[ j ] = 0.0 ;
278
+ if( rec == 1 ) {
279
+ path[ j ] = 0.0 ;
280
+ } else {
281
+ 0 ;
282
+ } ;
283
+ } ;
284
+ for( ; !(done == 1) ; ) {
285
+ int clen = (p-cs);
286
+ int cidx = 0;
287
+ double cval = 0.0;
288
+ double cabs = 0.0;
289
+ for( int t = 0 ; !(t == clen) ; t = (t+1) ) {
290
+ double v = cbuf[(cs+t)];
291
+ double av = ( ( v < 0.0 ) ? ((0.0-v)) : (v) );
292
+ if( av > cabs ) {
293
+ cabs = av ;
294
+ cidx = t ;
295
+ cval = v ;
296
+ } else {
297
+ 0 ;
298
+ } ;
299
+ ;
300
+ ;
301
+ } ;
302
+ alpha = (cabs/(1.0*nsamples)) ;
303
+ if( rec == 1 ) {
304
+ alphas[ niter ] = alpha ;
305
+ } else {
306
+ 0 ;
307
+ } ;
308
+ if( alpha <= (alphamin+eqtol) ) {
309
+ if( ( ( (alpha-alphamin) < 0.0 ) ? ((alphamin-alpha)) : ((alpha-alphamin)) ) > eqtol ) {
310
+ if( niter > 0 ) {
311
+ double ss = ((prevalpha-alphamin)/(prevalpha-alpha));
312
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
313
+ coef[ j ] = (prev[j]+(ss*(coef[j]-prev[j]))) ;
314
+ } ;
315
+ ;
316
+ } else {
317
+ 0 ;
318
+ } ;
319
+ alpha = alphamin ;
320
+ if( rec == 1 ) {
321
+ alphas[ niter ] = alphamin ;
322
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
323
+ path[ ((niter*p)+j) ] = coef[j] ;
324
+ } ;
325
+ } else {
326
+ 0 ;
327
+ } ;
328
+ } else {
329
+ 0 ;
330
+ } ;
331
+ done = 1 ;
332
+ } else {
333
+ if( (niter >= maxiter || na >= p) ) {
334
+ done = 1 ;
335
+ } else {
336
+ int degenerate = 0;
337
+ if( drop == 0 ) {
338
+ int m = na;
339
+ int nn = (cidx+na);
340
+ sign[ na ] = ( ( cval > 0.0 ) ? (1.0) : (( ( cval < 0.0 ) ? (-1.0) : (0.0) )) ) ;
341
+ double tc = cbuf[(cs+cidx)];
342
+ cbuf[ (cs+cidx) ] = cbuf[cs] ;
343
+ cbuf[ cs ] = tc ;
344
+ ;
345
+ int ti = index[nn];
346
+ index[ nn ] = index[m] ;
347
+ index[ m ] = ti ;
348
+ ;
349
+ cs = (cs+1) ;
350
+ swap_rc(g,m,nn,p) ;
351
+ double c = g[((na*p)+na)];
352
+ double v = 0.0;
353
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
354
+ double t = g[((na*p)+i)];
355
+ for( int s = 0 ; !(s == i) ; s = (s+1) ) {
356
+ t = (t-(l[((i*kmax)+s)]*l[((na*kmax)+s)])) ;
357
+ } ;
358
+ double li = (t/l[((i*kmax)+i)]);
359
+ l[ ((na*kmax)+i) ] = li ;
360
+ v = (v+(li*li)) ;
361
+ ;
362
+ ;
363
+ } ;
364
+ double dd = sqrt(( ( (c-v) < 0.0 ) ? ((v-c)) : ((c-v)) ));
365
+ double dg = ( ( dd > (1.0*eps) ) ? (dd) : ((1.0*eps)) );
366
+ l[ ((na*kmax)+na) ] = dg ;
367
+ if( dg < 1e-7 ) {
368
+ cs = (cs-1) ;
369
+ cbuf[ cs ] = 0.0 ;
370
+ double tc = cbuf[(cs+cidx)];
371
+ cbuf[ (cs+cidx) ] = cbuf[cs] ;
372
+ cbuf[ cs ] = tc ;
373
+ ;
374
+ degenerate = 1 ;
375
+ } else {
376
+ active[ na ] = index[na] ;
377
+ na = (na+1) ;
378
+ } ;
379
+ ;
380
+ ;
381
+ ;
382
+ ;
383
+ } else {
384
+ false ;
385
+ } ;
386
+ if( degenerate == 0 ) {
387
+ if( (method == 1 && niter > 0 && prevalpha < alpha) ) {
388
+ done = 1 ;
389
+ } else {
390
+ double aa = 0.0;
391
+ int clen2 = (p-cs);
392
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
393
+ double t = sign[i];
394
+ for( int s = 0 ; !(s == i) ; s = (s+1) ) {
395
+ t = (t-(l[((i*kmax)+s)]*zz[s])) ;
396
+ } ;
397
+ zz[ i ] = (t/l[((i*kmax)+i)]) ;
398
+ ;
399
+ } ;
400
+ for( int ii = 0 ; !(ii == na) ; ii = (ii+1) ) {
401
+ int i = ((na-1)-ii);
402
+ double t = zz[i];
403
+ for( int s = (i+1) ; !(s >= na) ; s = (s+1) ) {
404
+ t = (t-(l[((s*kmax)+i)]*ls[s])) ;
405
+ } ;
406
+ ls[ i ] = (t/l[((i*kmax)+i)]) ;
407
+ ;
408
+ ;
409
+ } ;
410
+ if( (na == 1 && ls[0] == 0.0) ) {
411
+ ls[ 0 ] = 1.0 ;
412
+ aa = 1.0 ;
413
+ } else {
414
+ double sm = 0.0;
415
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
416
+ sm = (sm+(ls[i]*sign[i])) ;
417
+ } ;
418
+ aa = (1.0/sqrt(sm)) ;
419
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
420
+ ls[ i ] = (aa*ls[i]) ;
421
+ } ;
422
+ ;
423
+ } ;
424
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
425
+ double acc = 0.0;
426
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
427
+ acc = (acc+(g[((i*p)+(na+t))]*ls[i])) ;
428
+ } ;
429
+ dir[ t ] = (round_even(double((acc*1e+15)))/1e+15) ;
430
+ ;
431
+ } ;
432
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
433
+ ratio[ t ] = ((cabs-cbuf[(cs+t)])/((aa-dir[t])+tiny32)) ;
434
+ } ;
435
+ double gfirst = min_pos(ratio,clen2);
436
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
437
+ ratio[ t ] = ((cabs+cbuf[(cs+t)])/((aa+dir[t])+tiny32)) ;
438
+ } ;
439
+ double gsecond = min_pos(ratio,clen2);
440
+ double gam = ( ( ( ( gfirst < gsecond ) ? (gfirst) : (gsecond) ) < (cabs/aa) ) ? (( ( gfirst < gsecond ) ? (gfirst) : (gsecond) )) : ((cabs/aa)) );
441
+ int nidx = 0;
442
+ drop = 0 ;
443
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
444
+ zz[ i ] = ((0.0-coef[active[i]])/(ls[i]+tiny32)) ;
445
+ } ;
446
+ double zpos = min_pos(zz,na);
447
+ if( zpos < gam ) {
448
+ for( int ii = 0 ; !(ii == na) ; ii = (ii+1) ) {
449
+ int i = ((na-1)-ii);
450
+ if( zz[i] == zpos ) {
451
+ idx[ nidx ] = i ;
452
+ nidx = (nidx+1) ;
453
+ } else {
454
+ 0 ;
455
+ } ;
456
+ ;
457
+ } ;
458
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
459
+ int i = idx[r];
460
+ sign[ i ] = (0.0-sign[i]) ;
461
+ ;
462
+ } ;
463
+ if( method == 1 ) {
464
+ gam = zpos ;
465
+ } else {
466
+ 0 ;
467
+ } ;
468
+ drop = 1 ;
469
+ } else {
470
+ 0 ;
471
+ } ;
472
+ ;
473
+ niter = (niter+1) ;
474
+ prevalpha = alpha ;
475
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
476
+ prev[ j ] = coef[j] ;
477
+ coef[ j ] = 0.0 ;
478
+ } ;
479
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
480
+ int a = active[i];
481
+ coef[ a ] = (prev[a]+(gam*ls[i])) ;
482
+ ;
483
+ } ;
484
+ if( rec == 1 ) {
485
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
486
+ path[ ((niter*p)+j) ] = coef[j] ;
487
+ } ;
488
+ } else {
489
+ 0 ;
490
+ } ;
491
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
492
+ cbuf[ (cs+t) ] = (cbuf[(cs+t)]-(gam*dir[t])) ;
493
+ } ;
494
+ if( (drop == 1 && method == 1) ) {
495
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
496
+ cholesky_delete(l,idx[r],na,kmax) ;
497
+ } ;
498
+ na = (na-1) ;
499
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
500
+ int ii = idx[r];
501
+ int dropped = active[ii];
502
+ for( int i = ii ; !(i >= na) ; i = (i+1) ) {
503
+ active[ i ] = active[(i+1)] ;
504
+ } ;
505
+ dropv[ r ] = dropped ;
506
+ ;
507
+ ;
508
+ } ;
509
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
510
+ int ii = idx[r];
511
+ for( int i = ii ; !(i >= na) ; i = (i+1) ) {
512
+ int ti = index[i];
513
+ index[ i ] = index[(i+1)] ;
514
+ index[ (i+1) ] = ti ;
515
+ ;
516
+ swap_rc(g,i,int((i+1)),p) ;
517
+ } ;
518
+ ;
519
+ } ;
520
+ for( int rr = 0 ; !(rr == nidx) ; rr = (rr+1) ) {
521
+ int r = ((nidx-1)-rr);
522
+ int d = dropv[r];
523
+ double t = cov0[d];
524
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
525
+ t = (t-(g0[((d*p)+j)]*coef[j])) ;
526
+ } ;
527
+ cs = (cs-1) ;
528
+ cbuf[ cs ] = t ;
529
+ ;
530
+ ;
531
+ ;
532
+ } ;
533
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
534
+ int ii = idx[r];
535
+ for( int i = ii ; !(i >= (p-1)) ; i = (i+1) ) {
536
+ sign[ i ] = sign[(i+1)] ;
537
+ } ;
538
+ sign[ (p-1) ] = 0.0 ;
539
+ ;
540
+ } ;
541
+ } else {
542
+ 0 ;
543
+ } ;
544
+ ;
545
+ ;
546
+ ;
547
+ ;
548
+ } ;
549
+ } else {
550
+ 0 ;
551
+ } ;
552
+ ;
553
+ } ;
554
+ } ;
555
+ ;
556
+ } ;
557
+ ;
558
+ return niter ;
559
+ }
560
+
561
+
562
+ SCM2CPP_FN inline int
563
+ rfe_chol( scm2cpp::cspan<double> g, scm2cpp::cspan<int> idx, scm2cpp::span<double> l, double alpha, int k, int p )
564
+ { for( int j = 0 ; !(j == k) ; j = (j+1) ) {
565
+ double s = (g[((idx[j]*p)+idx[j])]+alpha);
566
+ for( int m = 0 ; !(m == j) ; m = (m+1) ) {
567
+ s = (s-(l[((j*p)+m)]*l[((j*p)+m)])) ;
568
+ } ;
569
+ l[ ((j*p)+j) ] = sqrt(s) ;
570
+ for( int i = (j+1) ; !(i >= k) ; i = (i+1) ) {
571
+ double t = g[((idx[i]*p)+idx[j])];
572
+ for( int m = 0 ; !(m == j) ; m = (m+1) ) {
573
+ t = (t-(l[((i*p)+m)]*l[((j*p)+m)])) ;
574
+ } ;
575
+ l[ ((i*p)+j) ] = (t/l[((j*p)+j)]) ;
576
+ ;
577
+ } ;
578
+ ;
579
+ } ;
580
+ return 0 ;
581
+ }
582
+
583
+
584
+ SCM2CPP_FN inline int
585
+ rfe_solve( scm2cpp::cspan<double> c, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::cspan<double> l, int k, int p )
586
+ { for( int i = 0 ; !(i == k) ; i = (i+1) ) {
587
+ double t = c[idx[i]];
588
+ for( int m = 0 ; !(m == i) ; m = (m+1) ) {
589
+ t = (t-(l[((i*p)+m)]*w[m])) ;
590
+ } ;
591
+ w[ i ] = (t/l[((i*p)+i)]) ;
592
+ ;
593
+ } ;
594
+ for( int ii = 0 ; !(ii == k) ; ii = (ii+1) ) {
595
+ int i = ((k-1)-ii);
596
+ double t = w[i];
597
+ for( int m = (i+1) ; !(m >= k) ; m = (m+1) ) {
598
+ t = (t-(l[((m*p)+i)]*w[m])) ;
599
+ } ;
600
+ w[ i ] = (t/l[((i*p)+i)]) ;
601
+ ;
602
+ ;
603
+ } ;
604
+ return 0 ;
605
+ }
606
+
607
+
608
+ inline int
609
+ rfe( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> coef, scm2cpp::span<int> support, scm2cpp::span<int> ranking, double alpha, int nsel, int step, int n, int p )
610
+ { std::vector<double> xc((n*p),0.0);
611
+ std::vector<double> yc(n,0.0);
612
+ std::vector<double> mx(p,0.0);
613
+ std::vector<double> g((p*p),0.0);
614
+ std::vector<double> c(p,0.0);
615
+ std::vector<double> l((p*p),0.0);
616
+ std::vector<double> w(p,0.0);
617
+ std::vector<int> idx(p,0);
618
+ std::vector<int> ord(p,0);
619
+ std::vector<int> gone(p,0);
620
+ double ym = 0.0;
621
+ int rounds = 0;
622
+ int k = p;
623
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
624
+ ym = (ym+y[i]) ;
625
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
626
+ mx[ j ] = (mx[j]+x[((i*p)+j)]) ;
627
+ } ;
628
+ } ;
629
+ ym = (ym/double(n)) ;
630
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
631
+ mx[ j ] = (mx[j]/double(n)) ;
632
+ } ;
633
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
634
+ yc[ i ] = (y[i]-ym) ;
635
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
636
+ xc[ ((i*p)+j) ] = (x[((i*p)+j)]-mx[j]) ;
637
+ } ;
638
+ } ;
639
+ scm2cpp::device_matrix d24291 = scm2cpp::dmat_upload(xc, (n*p), 1);
640
+ scm2cpp::blas_syrk(g, d24291, 1.0, 0.0, 1, p, n) ;
641
+ ;
642
+ scm2cpp::device_matrix d24292 = scm2cpp::dmat_upload(xc, (n*p), 1);
643
+ scm2cpp::blas_gemv(c, d24292, yc, 1.0, 0.0, 1, p, n) ;
644
+ ;
645
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
646
+ support[ j ] = 1 ;
647
+ ranking[ j ] = 1 ;
648
+ idx[ j ] = j ;
649
+ } ;
650
+ rfe_chol(g,idx,l,alpha,p,p) ;
651
+ for( ; !(k <= nsel) ; ) {
652
+ rfe_solve(c,idx,w,l,k,p) ;
653
+ for( int i = 0 ; !(i == k) ; i = (i+1) ) {
654
+ double key = (w[i]*w[i]);
655
+ int jj = (i-1);
656
+ for( ; !((jj < 0 || (w[ord[jj]]*w[ord[jj]]) <= key)) ; ) {
657
+ ord[ (jj+1) ] = ord[jj] ;
658
+ jj = (jj-1) ;
659
+ } ;
660
+ ord[ (jj+1) ] = i ;
661
+ ;
662
+ } ;
663
+ int thr = ( ( step < (k-nsel) ) ? (step) : ((k-nsel)) );
664
+ for( int r = 0 ; !(r == thr) ; r = (r+1) ) {
665
+ support[ idx[ord[r]] ] = 0 ;
666
+ } ;
667
+ for( int t = 0 ; !(t == k) ; t = (t+1) ) {
668
+ gone[ t ] = 0 ;
669
+ } ;
670
+ for( int r = 0 ; !(r == thr) ; r = (r+1) ) {
671
+ gone[ ord[r] ] = 1 ;
672
+ } ;
673
+ int cur = k;
674
+ for( int tt = 0 ; !(tt == k) ; tt = (tt+1) ) {
675
+ int pos = ((k-1)-tt);
676
+ if( gone[pos] == 1 ) {
677
+ cholesky_delete(l,pos,cur,p) ;
678
+ for( int u = pos ; !(u >= (cur-1)) ; u = (u+1) ) {
679
+ idx[ u ] = idx[(u+1)] ;
680
+ } ;
681
+ cur = (cur-1) ;
682
+ } else {
683
+ 0 ;
684
+ } ;
685
+ ;
686
+ } ;
687
+ ;
688
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
689
+ if( support[j] == 0 ) {
690
+ ranking[ j ] = (ranking[j]+1) ;
691
+ } else {
692
+ 0 ;
693
+ } ;
694
+ } ;
695
+ k = (k-thr) ;
696
+ rounds = (rounds+1) ;
697
+ ;
698
+ } ;
699
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
700
+ coef[ j ] = 0.0 ;
701
+ } ;
702
+ rfe_solve(c,idx,w,l,k,p) ;
703
+ for( int i = 0 ; !(i == k) ; i = (i+1) ) {
704
+ coef[ idx[i] ] = w[i] ;
705
+ } ;
706
+ return rounds ;
707
+ }
708
+
709
+
710
+ SCM2CPP_FN inline double
711
+ eig_hypot( double a, double b )
712
+ {
713
+ double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
714
+ double bb = ( ( b < 0.0 ) ? ((0.0-b)) : (b) );
715
+ if( aa > bb ) {
716
+ return (aa*sqrt((1.0+((bb/aa)*(bb/aa))))) ;
717
+ }
718
+ else {
719
+ if( bb == 0.0 ) {
720
+ return 0.0 ;
721
+ }
722
+ else {
723
+ return (bb*sqrt((1.0+((aa/bb)*(aa/bb))))) ;
724
+ }
725
+ }
726
+ }
727
+
728
+
729
+ SCM2CPP_FN inline int
730
+ sym_eigen( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m )
731
+ { for( int ii = 0 ; !(ii == (m-1)) ; ii = (ii+1) ) {
732
+ int i = ((m-1)-ii);
733
+ int l = (i-1);
734
+ double h = 0.0;
735
+ double scale = 0.0;
736
+ if( l > 0 ) {
737
+ for( int k = 0 ; !(k > l) ; k = (k+1) ) {
738
+ double ak = a[((i*m)+k)];
739
+ scale = (scale+( ( ak < 0.0 ) ? ((0.0-ak)) : (ak) )) ;
740
+ ;
741
+ } ;
742
+ if( scale == 0.0 ) {
743
+ e[ i ] = a[((i*m)+l)] ;
744
+ } else {
745
+ for( int k = 0 ; !(k > l) ; k = (k+1) ) {
746
+ a[ ((i*m)+k) ] = (a[((i*m)+k)]/scale) ;
747
+ h = (h+(a[((i*m)+k)]*a[((i*m)+k)])) ;
748
+ } ;
749
+ double f = a[((i*m)+l)];
750
+ double g = ( ( f >= 0.0 ) ? ((0.0-sqrt(h))) : (sqrt(h)) );
751
+ e[ i ] = (scale*g) ;
752
+ h = (h-(f*g)) ;
753
+ a[ ((i*m)+l) ] = (f-g) ;
754
+ double ff = 0.0;
755
+ for( int j = 0 ; !(j > l) ; j = (j+1) ) {
756
+ a[ ((j*m)+i) ] = (a[((i*m)+j)]/h) ;
757
+ double gg = 0.0;
758
+ for( int k = 0 ; !(k > j) ; k = (k+1) ) {
759
+ gg = (gg+(a[((j*m)+k)]*a[((i*m)+k)])) ;
760
+ } ;
761
+ for( int k = (j+1) ; !(k > l) ; k = (k+1) ) {
762
+ gg = (gg+(a[((k*m)+j)]*a[((i*m)+k)])) ;
763
+ } ;
764
+ e[ j ] = (gg/h) ;
765
+ ff = (ff+(e[j]*a[((i*m)+j)])) ;
766
+ ;
767
+ } ;
768
+ double hh = (ff/(h+h));
769
+ for( int j = 0 ; !(j > l) ; j = (j+1) ) {
770
+ double f2 = a[((i*m)+j)];
771
+ double g2 = (e[j]-(hh*f2));
772
+ e[ j ] = g2 ;
773
+ for( int k = 0 ; !(k > j) ; k = (k+1) ) {
774
+ a[ ((j*m)+k) ] = (a[((j*m)+k)]-((f2*e[k])+(g2*a[((i*m)+k)]))) ;
775
+ } ;
776
+ ;
777
+ ;
778
+ } ;
779
+ ;
780
+ ;
781
+ ;
782
+ ;
783
+ } ;
784
+ } else {
785
+ e[ i ] = a[((i*m)+l)] ;
786
+ } ;
787
+ ev[ i ] = h ;
788
+ ;
789
+ ;
790
+ } ;
791
+ ev[ 0 ] = 0.0 ;
792
+ e[ 0 ] = 0.0 ;
793
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
794
+ if( !(ev[i] == 0.0) ) {
795
+ for( int j = 0 ; !(j == i) ; j = (j+1) ) {
796
+ double g3 = 0.0;
797
+ for( int k = 0 ; !(k == i) ; k = (k+1) ) {
798
+ g3 = (g3+(a[((i*m)+k)]*a[((k*m)+j)])) ;
799
+ } ;
800
+ for( int k = 0 ; !(k == i) ; k = (k+1) ) {
801
+ a[ ((k*m)+j) ] = (a[((k*m)+j)]-(g3*a[((k*m)+i)])) ;
802
+ } ;
803
+ ;
804
+ } ;
805
+ } else {
806
+ 0 ;
807
+ } ;
808
+ ev[ i ] = a[((i*m)+i)] ;
809
+ a[ ((i*m)+i) ] = 1.0 ;
810
+ for( int j = 0 ; !(j == i) ; j = (j+1) ) {
811
+ a[ ((j*m)+i) ] = 0.0 ;
812
+ a[ ((i*m)+j) ] = 0.0 ;
813
+ } ;
814
+ } ;
815
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
816
+ for( int j = 0 ; !(j == m) ; j = (j+1) ) {
817
+ v[ ((j*m)+i) ] = a[((i*m)+j)] ;
818
+ } ;
819
+ } ;
820
+ for( int i = 1 ; !(i == m) ; i = (i+1) ) {
821
+ e[ (i-1) ] = e[i] ;
822
+ } ;
823
+ e[ (m-1) ] = 0.0 ;
824
+ for( int l = 0 ; !(l == m) ; l = (l+1) ) {
825
+ int fin = 0;
826
+ int iter = 0;
827
+ for( ; !((fin == 1 || iter > 60)) ; ) {
828
+ int mm = l;
829
+ int found = 0;
830
+ for( ; !((found == 1 || mm >= (m-1))) ; ) {
831
+ double dd = (eig_abs(ev[mm])+eig_abs(ev[(mm+1)]));
832
+ if( (eig_abs(e[mm])+dd) == dd ) {
833
+ found = 1 ;
834
+ } else {
835
+ mm = (mm+1) ;
836
+ } ;
837
+ ;
838
+ } ;
839
+ if( mm == l ) {
840
+ fin = 1 ;
841
+ } else {
842
+ iter = (iter+1) ;
843
+ double g = ((ev[(l+1)]-ev[l])/(2.0*e[l]));
844
+ double r = eig_hypot(g,1.0);
845
+ double g4 = ((ev[mm]-ev[l])+(e[l]/(g+( ( g >= 0.0 ) ? (eig_abs(r)) : ((0.0-eig_abs(r))) ))));
846
+ double s = 1.0;
847
+ double c = 1.0;
848
+ double pp = 0.0;
849
+ double gq = g4;
850
+ int under = 0;
851
+ int i = (mm-1);
852
+ for( ; !((under == 1 || i < l)) ; ) {
853
+ double f = (s*e[i]);
854
+ double b = (c*e[i]);
855
+ double r2 = eig_hypot(f,gq);
856
+ e[ (i+1) ] = r2 ;
857
+ if( r2 == 0.0 ) {
858
+ ev[ (i+1) ] = (ev[(i+1)]-pp) ;
859
+ e[ mm ] = 0.0 ;
860
+ under = 1 ;
861
+ } else {
862
+ s = (f/r2) ;
863
+ c = (gq/r2) ;
864
+ double g5 = (ev[(i+1)]-pp);
865
+ double r3 = (((ev[i]-g5)*s)+(2.0*(c*b)));
866
+ pp = (s*r3) ;
867
+ ev[ (i+1) ] = (g5+pp) ;
868
+ gq = ((c*r3)-b) ;
869
+ ;
870
+ ;
871
+ for( int k = 0 ; !(k == m) ; k = (k+1) ) {
872
+ double zf = v[(((i+1)*m)+k)];
873
+ double zi = v[((i*m)+k)];
874
+ v[ (((i+1)*m)+k) ] = ((s*zi)+(c*zf)) ;
875
+ v[ ((i*m)+k) ] = ((c*zi)-(s*zf)) ;
876
+ ;
877
+ } ;
878
+ i = (i-1) ;
879
+ } ;
880
+ ;
881
+ ;
882
+ } ;
883
+ if( under == 0 ) {
884
+ ev[ l ] = (ev[l]-pp) ;
885
+ e[ l ] = gq ;
886
+ e[ mm ] = 0.0 ;
887
+ } else {
888
+ 0 ;
889
+ } ;
890
+ ;
891
+ ;
892
+ ;
893
+ ;
894
+ } ;
895
+ ;
896
+ } ;
897
+ ;
898
+ } ;
899
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
900
+ for( int j = (i+1) ; !(j >= m) ; j = (j+1) ) {
901
+ double tmp = v[((i*m)+j)];
902
+ v[ ((i*m)+j) ] = v[((j*m)+i)] ;
903
+ v[ ((j*m)+i) ] = tmp ;
904
+ ;
905
+ } ;
906
+ } ;
907
+ return 0 ;
908
+ }
909
+
910
+
911
+ SCM2CPP_FN inline double
912
+ eig_abs( double a )
913
+ {
914
+ if( a < 0.0 ) {
915
+ return (0.0-a) ;
916
+ }
917
+ else {
918
+ return a ;
919
+ }
920
+ }
921
+
922
+
923
+ SCM2CPP_FN inline int
924
+ eig_order( scm2cpp::cspan<double> ev, scm2cpp::span<int> ord, int m )
925
+ { for( int j = 0 ; !(j == m) ; j = (j+1) ) {
926
+ ord[ j ] = j ;
927
+ } ;
928
+ for( int j = 0 ; !(j == m) ; j = (j+1) ) {
929
+ int best = j;
930
+ for( int r = (j+1) ; !(r >= m) ; r = (r+1) ) {
931
+ if( ev[ord[r]] > ev[ord[best]] ) {
932
+ best = r ;
933
+ } else {
934
+ 0 ;
935
+ } ;
936
+ } ;
937
+ int tmp = ord[j];
938
+ ord[ j ] = ord[best] ;
939
+ ord[ best ] = tmp ;
940
+ ;
941
+ ;
942
+ } ;
943
+ return 0 ;
944
+ }
945
+
946
+
947
+ inline int
948
+ pca( scm2cpp::cspan<double> x, scm2cpp::span<double> comps, scm2cpp::span<double> ev, scm2cpp::span<double> sv, scm2cpp::span<double> mean, int n, int p )
949
+ { std::vector<double> c((p*p),0.0);
950
+ std::vector<double> v((p*p),0.0);
951
+ std::vector<double> e(p,0.0);
952
+ std::vector<double> sc(p,0.0);
953
+ std::vector<int> ord(p,0);
954
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
955
+ mean[ j ] = 0.0 ;
956
+ } ;
957
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
958
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
959
+ mean[ j ] = (mean[j]+x[((i*p)+j)]) ;
960
+ } ;
961
+ } ;
962
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
963
+ mean[ j ] = (mean[j]/double(n)) ;
964
+ } ;
965
+ scm2cpp::device_matrix d24293 = scm2cpp::dmat_upload(x, (n*p), 1);
966
+ scm2cpp::blas_syrk(c, d24293, 1.0, 0.0, 1, p, n) ;
967
+ ;
968
+ for( int i = 0 ; !(i == p) ; i = (i+1) ) {
969
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
970
+ c[ ((i*p)+j) ] = ((c[((i*p)+j)]-(double(n)*(mean[i]*mean[j])))/double((n-1))) ;
971
+ } ;
972
+ } ;
973
+ sym_eigen(c,v,e,sc,p) ;
974
+ eig_order(e,ord,p) ;
975
+ for( int r = 0 ; !(r == p) ; r = (r+1) ) {
976
+ int col = ord[r];
977
+ double lam = ( ( e[col] < 0.0 ) ? (0.0) : (e[col]) );
978
+ int big = 0;
979
+ double bv = -1.0;
980
+ ev[ r ] = lam ;
981
+ sv[ r ] = sqrt((lam*double((n-1)))) ;
982
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
983
+ double a = v[((j*p)+col)];
984
+ double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
985
+ if( aa > bv ) {
986
+ bv = aa ;
987
+ big = j ;
988
+ } else {
989
+ 0 ;
990
+ } ;
991
+ ;
992
+ ;
993
+ } ;
994
+ double sg = ( ( v[((big*p)+col)] < 0.0 ) ? (-1.0) : (1.0) );
995
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
996
+ comps[ ((r*p)+j) ] = (sg*v[((j*p)+col)]) ;
997
+ } ;
998
+ ;
999
+ ;
1000
+ ;
1001
+ } ;
1002
+ return 0 ;
1003
+ }
1004
+
1005
+
1006
+ SCM2CPP_FN inline int
1007
+ gram_xx( scm2cpp::cspan<double> x, scm2cpp::span<double> g, int n, int p )
1008
+ { scm2cpp::device_matrix d24294 = scm2cpp::dmat_upload(x, (n*p), 1);
1009
+ scm2cpp::blas_syrk(g, d24294, 1.0, 0.0, 1, p, n) ;
1010
+ ;
1011
+ return 0 ;
1012
+ }
1013
+
1014
+
1015
+ SCM2CPP_FN inline int
1016
+ gram_xy( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> c, int n, int p, int q )
1017
+ { scm2cpp::device_matrix d24295 = scm2cpp::dmat_upload(x, (n*p), 1);
1018
+ scm2cpp::device_matrix d24296 = scm2cpp::dmat_upload(y, (n*q), 1);
1019
+ scm2cpp::blas_gemm(c, d24295, d24296, 1.0, 0.0, 1, 0, p, q, n) ;
1020
+ ;
1021
+ return 0 ;
1022
+ }
1023
+ #endif // ACCEL_HPP