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,2825 @@
1
+
2
+ #ifndef CLASSIFIERS_HPP
3
+ #define CLASSIFIERS_HPP
4
+ #define SCM2CPP_MINIMAL
5
+ #include <cmath>
6
+ #include <math.h>
7
+ #include <array>
8
+ #include <vector>
9
+ #include "scm2cpp.hpp"
10
+
11
+ int
12
+ svm_liblinear( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, scm2cpp::span<double> alpha, scm2cpp::span<double> xnorm, double cost, int iters, double tol, int seed, int n, int p );
13
+ int
14
+ svm_smo( scm2cpp::cspan<double> k, scm2cpp::cspan<double> y, scm2cpp::span<double> alpha, scm2cpp::span<double> rho, double cost, int iters, double eps, int n );
15
+ int
16
+ smo_solve( scm2cpp::cspan<double> k, scm2cpp::cspan<double> y, scm2cpp::cspan<double> lin, scm2cpp::span<double> alpha, std::array<double,1> & rho, double cost, int iters, double eps, int n, int nk );
17
+ SCM2CPP_FN int
18
+ shrinkable_P( scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::cspan<double> g, double cost, double gmax1, double gmax2, int t );
19
+ SCM2CPP_FN int
20
+ swap_position( scm2cpp::span<int> index, scm2cpp::span<int> krow, scm2cpp::span<double> pp, scm2cpp::span<double> yy, scm2cpp::span<double> a, scm2cpp::span<double> g, scm2cpp::span<double> gbar, scm2cpp::span<double> qd, int s, int t );
21
+ SCM2CPP_FN int
22
+ reconstruct_gradient( scm2cpp::cspan<double> k, scm2cpp::cspan<int> krow, scm2cpp::cspan<double> pp, scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::span<double> g, scm2cpp::cspan<double> gbar, double cost, int active, int n, int nk );
23
+ SCM2CPP_FN int
24
+ select_working_set( scm2cpp::cspan<double> k, scm2cpp::cspan<int> krow, scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::cspan<double> g, scm2cpp::cspan<double> qd, double cost, double eps, int active, int nk, std::array<int,2> & wi );
25
+ int
26
+ svr_smo( scm2cpp::cspan<double> k, scm2cpp::cspan<double> z, scm2cpp::span<double> beta, scm2cpp::span<double> rho, double cost, double epsilon, int iters, double eps, int n );
27
+ int
28
+ logistic_l1( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, double cost, double tol, int maxnewton, int seed, int n, int p );
29
+ int
30
+ svr_liblinear( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, scm2cpp::span<double> beta, scm2cpp::span<double> xnorm, double cost, double epsilon, int iters, double tol, int seed, int n, int p );
31
+ SCM2CPP_FN double
32
+ ols_rows( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> rows, int m, scm2cpp::span<double> w, scm2cpp::span<double> g, scm2cpp::span<double> c, scm2cpp::span<double> l, scm2cpp::span<double> z, scm2cpp::span<double> xm, int n, int p );
33
+ int
34
+ ransac( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> subsets, scm2cpp::span<double> coef, scm2cpp::span<double> mask, scm2cpp::span<double> out, double maxtrials, int minsamples, double thresh, double stopprob, int n, int p );
35
+ SCM2CPP_FN double
36
+ sgd_loss( int loss, double y, double p, double epsilon );
37
+ SCM2CPP_FN double
38
+ sgd_grad( int loss, double y, double p, double epsilon );
39
+ SCM2CPP_FN int
40
+ sgd( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, scm2cpp::span<double> out, int loss, int lr, int pen, double alpha, double eta0, double powert, double epsilon, int maxiter, double tol, int niternochange, int fitint, int n, int p );
41
+ SCM2CPP_FN double
42
+ sgd_min( double a, double b );
43
+ SCM2CPP_FN double
44
+ min_pos( scm2cpp::cspan<double> v, int m );
45
+ SCM2CPP_FN double
46
+ round_even( double y );
47
+ SCM2CPP_FN int
48
+ cholesky_delete( scm2cpp::span<double> l, int go, int n, int k );
49
+ SCM2CPP_FN int
50
+ swap_rc( scm2cpp::span<double> g, int a, int b, int p );
51
+ int
52
+ 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 );
53
+ int
54
+ svm_l1( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, double cost, double tol, int maxiter, int seed, int n, int p );
55
+ SCM2CPP_FN double
56
+ svm_l1_abs( double a );
57
+ SCM2CPP_FN double
58
+ eig_hypot( double a, double b );
59
+ SCM2CPP_FN int
60
+ sym_eigen( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m );
61
+ SCM2CPP_FN double
62
+ eig_abs( double a );
63
+ SCM2CPP_FN int
64
+ eig_order( scm2cpp::cspan<double> ev, scm2cpp::span<int> ord, int m );
65
+ SCM2CPP_FN int
66
+ ts_minnorm( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::span<double> aa, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, scm2cpp::span<double> ab, int s, int ns, int fitint, int p, int d );
67
+ SCM2CPP_FN int
68
+ ts_fit( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::span<double> a, scm2cpp::span<double> b, scm2cpp::span<double> aa, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, scm2cpp::span<double> ab, int s, int ns, int fitint, int p, int d );
69
+ SCM2CPP_FN double
70
+ ts_abs( double v );
71
+ int
72
+ theilsen( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::span<double> coef, int nsub, int ns, int fitint, int maxiter, double tol, int p );
73
+
74
+
75
+
76
+ inline int
77
+ svm_liblinear( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, scm2cpp::span<double> alpha, scm2cpp::span<double> xnorm, double cost, int iters, double tol, int seed, int n, int p )
78
+ { for( int i = 0 ; !(i == n) ; i = (i+1) ) {
79
+ double acc = 0.0;
80
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
81
+ acc = (acc+(x[((i*p)+j)]*x[((i*p)+j)])) ;
82
+ } ;
83
+ xnorm[ i ] = acc ;
84
+ ;
85
+ } ;
86
+ std::vector<int> index(n,0);
87
+ int rng = seed;
88
+ int active = n;
89
+ double pgmaxold = 1e+300;
90
+ double pgminold = -1e+300;
91
+ int stop = 0;
92
+ int sweeps = 0;
93
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
94
+ index[ i ] = i ;
95
+ } ;
96
+ for( int sweep = 0 ; !((sweep == iters || stop == 1)) ; sweep = (sweep+1) ) {
97
+ double pgmax = -1e+300;
98
+ double pgmin = 1e+300;
99
+ for( int i = 0 ; !(i == active) ; i = (i+1) ) {
100
+ rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
101
+ if( rng < 0 ) {
102
+ rng = (rng+2147483647) ;
103
+ } else {
104
+ 0 ;
105
+ } ;
106
+ int j = (i+(rng % (active-i)));
107
+ int t = index[i];
108
+ index[ i ] = index[j] ;
109
+ index[ j ] = t ;
110
+ ;
111
+ } ;
112
+ int s = 0;
113
+ for( ; !(s >= active) ; ) {
114
+ int i = index[s];
115
+ double dot = 0.0;
116
+ double old = alpha[i];
117
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
118
+ dot = (dot+(x[((i*p)+j)]*w[j])) ;
119
+ } ;
120
+ double g = ((y[i]*dot)-1.0);
121
+ double pg = 0.0;
122
+ int shrunk = 0;
123
+ if( old == 0.0 ) {
124
+ if( g > pgmaxold ) {
125
+ shrunk = 1 ;
126
+ } else {
127
+ if( g < 0.0 ) {
128
+ pg = g ;
129
+ } else {
130
+ 0 ;
131
+ } ;
132
+ } ;
133
+ } else {
134
+ if( old == cost ) {
135
+ if( g < pgminold ) {
136
+ shrunk = 1 ;
137
+ } else {
138
+ if( g > 0.0 ) {
139
+ pg = g ;
140
+ } else {
141
+ 0 ;
142
+ } ;
143
+ } ;
144
+ } else {
145
+ pg = g ;
146
+ } ;
147
+ } ;
148
+ if( shrunk == 1 ) {
149
+ active = (active-1) ;
150
+ index[ s ] = index[active] ;
151
+ index[ active ] = i ;
152
+ } else {
153
+ if( pg > pgmax ) {
154
+ pgmax = pg ;
155
+ } else {
156
+ 0 ;
157
+ } ;
158
+ if( pg < pgmin ) {
159
+ pgmin = pg ;
160
+ } else {
161
+ 0 ;
162
+ } ;
163
+ if( (pg > 1e-12 || pg < -1e-12) ) {
164
+ double step = (old-(g/xnorm[i]));
165
+ double anew = ( ( step < 0.0 ) ? (0.0) : (( ( step > cost ) ? (cost) : (step) )) );
166
+ alpha[ i ] = anew ;
167
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
168
+ w[ j ] = (w[j]+(x[((i*p)+j)]*((anew-old)*y[i]))) ;
169
+ } ;
170
+ ;
171
+ ;
172
+ } else {
173
+ 0 ;
174
+ } ;
175
+ s = (s+1) ;
176
+ } ;
177
+ ;
178
+ ;
179
+ ;
180
+ } ;
181
+ ;
182
+ sweeps = (sweeps+1) ;
183
+ if( (pgmax-pgmin) <= tol ) {
184
+ if( active == n ) {
185
+ stop = 1 ;
186
+ } else {
187
+ active = n ;
188
+ pgmaxold = 1e+300 ;
189
+ pgminold = -1e+300 ;
190
+ } ;
191
+ } else {
192
+ pgmaxold = ( ( pgmax <= 0.0 ) ? (1e+300) : (pgmax) ) ;
193
+ pgminold = ( ( pgmin >= 0.0 ) ? (-1e+300) : (pgmin) ) ;
194
+ } ;
195
+ ;
196
+ } ;
197
+ return sweeps ;
198
+ }
199
+
200
+
201
+ inline int
202
+ svm_smo( scm2cpp::cspan<double> k, scm2cpp::cspan<double> y, scm2cpp::span<double> alpha, scm2cpp::span<double> rho, double cost, int iters, double eps, int n )
203
+ { std::vector<double> lin(n,-1.0);
204
+ std::vector<double> yv(n,0.0);
205
+ std::vector<double> av(n,0.0);
206
+ std::array<double,1> rv = scm2cpp::filled_array<double,1>(0.0);
207
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
208
+ yv[ i ] = y[i] ;
209
+ } ;
210
+ int it = smo_solve(k,yv,lin,av,rv,cost,iters,eps,n,n);
211
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
212
+ alpha[ i ] = av[i] ;
213
+ } ;
214
+ rho[ 0 ] = rv[0] ;
215
+ return it ;
216
+ }
217
+
218
+
219
+ inline int
220
+ smo_solve( scm2cpp::cspan<double> k, scm2cpp::cspan<double> y, scm2cpp::cspan<double> lin, scm2cpp::span<double> alpha, std::array<double,1> & rho, double cost, int iters, double eps, int n, int nk )
221
+ { std::vector<int> index(n,0);
222
+ std::vector<int> krow(n,0);
223
+ std::vector<double> pp(n,0.0);
224
+ std::vector<double> yy(n,0.0);
225
+ std::vector<double> a(n,0.0);
226
+ std::vector<double> g(n,0.0);
227
+ std::vector<double> gbar(n,0.0);
228
+ std::vector<double> qd(n,0.0);
229
+ int active = n;
230
+ int unshrink = 0;
231
+ int counter = (( ( n < 1000 ) ? (n) : (1000) )+1);
232
+ int iter = 0;
233
+ int done = 0;
234
+ for( int t = 0 ; !(t == n) ; t = (t+1) ) {
235
+ index[ t ] = t ;
236
+ krow[ t ] = (t % nk) ;
237
+ pp[ t ] = lin[t] ;
238
+ yy[ t ] = y[t] ;
239
+ g[ t ] = lin[t] ;
240
+ qd[ t ] = k[((krow[t]*nk)+krow[t])] ;
241
+ } ;
242
+ for( ; !((done == 1 || iter >= iters)) ; ) {
243
+ counter = (counter-1) ;
244
+ if( counter == 0 ) {
245
+ counter = ( ( n < 1000 ) ? (n) : (1000) ) ;
246
+ double gmax1 = -1e+300;
247
+ double gmax2 = -1e+300;
248
+ for( int t = 0 ; !(t == active) ; t = (t+1) ) {
249
+ double gt = g[t];
250
+ double at = a[t];
251
+ if( yy[t] > 0.0 ) {
252
+ if( (at < cost && (0.0-gt) > gmax1) ) {
253
+ gmax1 = (0.0-gt) ;
254
+ } else {
255
+ 0 ;
256
+ } ;
257
+ if( (at > 0.0 && gt > gmax2) ) {
258
+ gmax2 = gt ;
259
+ } else {
260
+ 0 ;
261
+ } ;
262
+ } else {
263
+ if( (at < cost && (0.0-gt) > gmax2) ) {
264
+ gmax2 = (0.0-gt) ;
265
+ } else {
266
+ 0 ;
267
+ } ;
268
+ if( (at > 0.0 && gt > gmax1) ) {
269
+ gmax1 = gt ;
270
+ } else {
271
+ 0 ;
272
+ } ;
273
+ } ;
274
+ ;
275
+ } ;
276
+ if( (unshrink == 0 && (gmax1+gmax2) <= (eps*10.0)) ) {
277
+ unshrink = 1 ;
278
+ reconstruct_gradient(k,krow,pp,yy,a,g,gbar,cost,active,n,nk) ;
279
+ active = n ;
280
+ } else {
281
+ 0 ;
282
+ } ;
283
+ int t = 0;
284
+ for( ; !(t >= active) ; ) {
285
+ if( shrinkable_P(yy,a,g,cost,gmax1,gmax2,t) == 1 ) {
286
+ active = (active-1) ;
287
+ int found = 0;
288
+ for( ; !((found == 1 || active <= t)) ; ) {
289
+ if( shrinkable_P(yy,a,g,cost,gmax1,gmax2,active) == 0 ) {
290
+ swap_position(index,krow,pp,yy,a,g,gbar,qd,t,active) ;
291
+ found = 1 ;
292
+ } else {
293
+ active = (active-1) ;
294
+ } ;
295
+ } ;
296
+ ;
297
+ t = (t+1) ;
298
+ } else {
299
+ t = (t+1) ;
300
+ } ;
301
+ } ;
302
+ ;
303
+ ;
304
+ } else {
305
+ 0 ;
306
+ } ;
307
+ std::array<int,2> wi = scm2cpp::filled_array<int,2>(0);
308
+ if( select_working_set(k,krow,yy,a,g,qd,cost,eps,active,nk,wi) == 1 ) {
309
+ reconstruct_gradient(k,krow,pp,yy,a,g,gbar,cost,active,n,nk) ;
310
+ active = n ;
311
+ if( select_working_set(k,krow,yy,a,g,qd,cost,eps,active,nk,wi) == 1 ) {
312
+ done = 1 ;
313
+ } else {
314
+ counter = 1 ;
315
+ } ;
316
+ } else {
317
+ 0 ;
318
+ } ;
319
+ if( done == 0 ) {
320
+ int i = wi[0];
321
+ int j = wi[1];
322
+ iter = (iter+1) ;
323
+ double ai = a[i];
324
+ double aj = a[j];
325
+ double yi = yy[i];
326
+ double yj = yy[j];
327
+ int pi = krow[i];
328
+ int pj = krow[j];
329
+ double qij = ((yi*yj)*k[((pi*nk)+pj)]);
330
+ int ui = ( ( ai >= cost ) ? (1) : (0) );
331
+ int uj = ( ( aj >= cost ) ? (1) : (0) );
332
+ double ni = ai;
333
+ double nj = aj;
334
+ if( !(yi == yj) ) {
335
+ double quad = ((qd[i]+qd[j])+(2.0*qij));
336
+ if( quad <= 0.0 ) {
337
+ quad = 1e-12 ;
338
+ } else {
339
+ 0 ;
340
+ } ;
341
+ double delta = (((0.0-g[i])-g[j])/quad);
342
+ double diff = (ai-aj);
343
+ ni = (ai+delta) ;
344
+ nj = (aj+delta) ;
345
+ if( diff > 0.0 ) {
346
+ if( nj < 0.0 ) {
347
+ nj = 0.0 ;
348
+ ni = diff ;
349
+ } else {
350
+ 0 ;
351
+ } ;
352
+ } else {
353
+ if( ni < 0.0 ) {
354
+ ni = 0.0 ;
355
+ nj = (0.0-diff) ;
356
+ } else {
357
+ 0 ;
358
+ } ;
359
+ } ;
360
+ if( diff > 0.0 ) {
361
+ if( ni > cost ) {
362
+ ni = cost ;
363
+ nj = (cost-diff) ;
364
+ } else {
365
+ 0 ;
366
+ } ;
367
+ } else {
368
+ if( nj > cost ) {
369
+ nj = cost ;
370
+ ni = (cost+diff) ;
371
+ } else {
372
+ 0 ;
373
+ } ;
374
+ } ;
375
+ ;
376
+ ;
377
+ } else {
378
+ double quad = ((qd[i]+qd[j])-(2.0*qij));
379
+ if( quad <= 0.0 ) {
380
+ quad = 1e-12 ;
381
+ } else {
382
+ 0 ;
383
+ } ;
384
+ double delta = ((g[i]-g[j])/quad);
385
+ double sum = (ai+aj);
386
+ ni = (ai-delta) ;
387
+ nj = (aj+delta) ;
388
+ if( sum > cost ) {
389
+ if( ni > cost ) {
390
+ ni = cost ;
391
+ nj = (sum-cost) ;
392
+ } else {
393
+ 0 ;
394
+ } ;
395
+ } else {
396
+ if( nj < 0.0 ) {
397
+ nj = 0.0 ;
398
+ ni = sum ;
399
+ } else {
400
+ 0 ;
401
+ } ;
402
+ } ;
403
+ if( sum > cost ) {
404
+ if( nj > cost ) {
405
+ nj = cost ;
406
+ ni = (sum-cost) ;
407
+ } else {
408
+ 0 ;
409
+ } ;
410
+ } else {
411
+ if( ni < 0.0 ) {
412
+ ni = 0.0 ;
413
+ nj = sum ;
414
+ } else {
415
+ 0 ;
416
+ } ;
417
+ } ;
418
+ ;
419
+ ;
420
+ } ;
421
+ a[ i ] = ni ;
422
+ a[ j ] = nj ;
423
+ double di = (ni-ai);
424
+ double dj = (nj-aj);
425
+ for( int t = 0 ; !(t == active) ; t = (t+1) ) {
426
+ int pt = krow[t];
427
+ double yt = yy[t];
428
+ g[ t ] = (g[t]+(yt*(((yi*di)*k[((pi*nk)+pt)])+((yj*dj)*k[((pj*nk)+pt)])))) ;
429
+ ;
430
+ } ;
431
+ if( !(ui == ( ( ni >= cost ) ? (1) : (0) )) ) {
432
+ for( int t = 0 ; !(t == n) ; t = (t+1) ) {
433
+ gbar[ t ] = (gbar[t]+(( ( ui == 1 ) ? ((0.0-cost)) : (cost) )*((yi*yy[t])*k[((pi*nk)+krow[t])]))) ;
434
+ } ;
435
+ } else {
436
+ 0 ;
437
+ } ;
438
+ if( !(uj == ( ( nj >= cost ) ? (1) : (0) )) ) {
439
+ for( int t = 0 ; !(t == n) ; t = (t+1) ) {
440
+ gbar[ t ] = (gbar[t]+(( ( uj == 1 ) ? ((0.0-cost)) : (cost) )*((yj*yy[t])*k[((pj*nk)+krow[t])]))) ;
441
+ } ;
442
+ } else {
443
+ 0 ;
444
+ } ;
445
+ ;
446
+ ;
447
+ ;
448
+ ;
449
+ } else {
450
+ 0 ;
451
+ } ;
452
+ ;
453
+ } ;
454
+ if( active < n ) {
455
+ reconstruct_gradient(k,krow,pp,yy,a,g,gbar,cost,active,n,nk) ;
456
+ active = n ;
457
+ } else {
458
+ 0 ;
459
+ } ;
460
+ int nfree = 0;
461
+ double sumfree = 0.0;
462
+ double ub = 1e+300;
463
+ double lb = -1e+300;
464
+ for( int t = 0 ; !(t == n) ; t = (t+1) ) {
465
+ double yg = (yy[t]*g[t]);
466
+ double at = a[t];
467
+ double yt = yy[t];
468
+ if( at >= cost ) {
469
+ if( yt < 0.0 ) {
470
+ if( yg < ub ) {
471
+ ub = yg ;
472
+ } else {
473
+ 0 ;
474
+ } ;
475
+ } else {
476
+ if( yg > lb ) {
477
+ lb = yg ;
478
+ } else {
479
+ 0 ;
480
+ } ;
481
+ } ;
482
+ } else {
483
+ if( at <= 0.0 ) {
484
+ if( yt > 0.0 ) {
485
+ if( yg < ub ) {
486
+ ub = yg ;
487
+ } else {
488
+ 0 ;
489
+ } ;
490
+ } else {
491
+ if( yg > lb ) {
492
+ lb = yg ;
493
+ } else {
494
+ 0 ;
495
+ } ;
496
+ } ;
497
+ } else {
498
+ nfree = (nfree+1) ;
499
+ sumfree = (sumfree+yg) ;
500
+ } ;
501
+ } ;
502
+ ;
503
+ } ;
504
+ rho[ 0 ] = ( ( nfree > 0 ) ? ((sumfree/(1.0*nfree))) : (((ub+lb)/2.0)) ) ;
505
+ ;
506
+ for( int t = 0 ; !(t == n) ; t = (t+1) ) {
507
+ alpha[ index[t] ] = a[t] ;
508
+ } ;
509
+ return iter ;
510
+ }
511
+
512
+
513
+ SCM2CPP_FN inline int
514
+ shrinkable_P( scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::cspan<double> g, double cost, double gmax1, double gmax2, int t )
515
+ {
516
+ double at = a[t];
517
+ double gt = g[t];
518
+ double yt = yy[t];
519
+ if( at >= cost ) {
520
+ if( yt > 0.0 ) {
521
+ if( (0.0-gt) > gmax1 ) {
522
+ return 1 ;
523
+ }
524
+ else {
525
+ return 0 ;
526
+ }
527
+ }
528
+ else {
529
+ if( (0.0-gt) > gmax2 ) {
530
+ return 1 ;
531
+ }
532
+ else {
533
+ return 0 ;
534
+ }
535
+ }
536
+ }
537
+ else {
538
+ if( at <= 0.0 ) {
539
+ if( yt > 0.0 ) {
540
+ if( gt > gmax2 ) {
541
+ return 1 ;
542
+ }
543
+ else {
544
+ return 0 ;
545
+ }
546
+ }
547
+ else {
548
+ if( gt > gmax1 ) {
549
+ return 1 ;
550
+ }
551
+ else {
552
+ return 0 ;
553
+ }
554
+ }
555
+ }
556
+ else {
557
+ return 0 ;
558
+ }
559
+ }
560
+ }
561
+
562
+
563
+ SCM2CPP_FN inline int
564
+ swap_position( scm2cpp::span<int> index, scm2cpp::span<int> krow, scm2cpp::span<double> pp, scm2cpp::span<double> yy, scm2cpp::span<double> a, scm2cpp::span<double> g, scm2cpp::span<double> gbar, scm2cpp::span<double> qd, int s, int t )
565
+ { int i0 = index[s];
566
+ double y0 = yy[s];
567
+ double a0 = a[s];
568
+ double g0 = g[s];
569
+ double b0 = gbar[s];
570
+ double q0 = qd[s];
571
+ int r0 = krow[s];
572
+ double p0 = pp[s];
573
+ index[ s ] = index[t] ;
574
+ index[ t ] = i0 ;
575
+ krow[ s ] = krow[t] ;
576
+ krow[ t ] = r0 ;
577
+ pp[ s ] = pp[t] ;
578
+ pp[ t ] = p0 ;
579
+ yy[ s ] = yy[t] ;
580
+ yy[ t ] = y0 ;
581
+ a[ s ] = a[t] ;
582
+ a[ t ] = a0 ;
583
+ g[ s ] = g[t] ;
584
+ g[ t ] = g0 ;
585
+ gbar[ s ] = gbar[t] ;
586
+ gbar[ t ] = b0 ;
587
+ qd[ s ] = qd[t] ;
588
+ qd[ t ] = q0 ;
589
+ return 0 ;
590
+ }
591
+
592
+
593
+ SCM2CPP_FN inline int
594
+ reconstruct_gradient( scm2cpp::cspan<double> k, scm2cpp::cspan<int> krow, scm2cpp::cspan<double> pp, scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::span<double> g, scm2cpp::cspan<double> gbar, double cost, int active, int n, int nk )
595
+ { for( int t = active ; !(t >= n) ; t = (t+1) ) {
596
+ g[ t ] = (gbar[t]+pp[t]) ;
597
+ } ;
598
+ for( int s = 0 ; !(s >= active) ; s = (s+1) ) {
599
+ double as = a[s];
600
+ if( (as > 0.0 && as < cost) ) {
601
+ int ps = krow[s];
602
+ double ys = yy[s];
603
+ for( int t = active ; !(t >= n) ; t = (t+1) ) {
604
+ g[ t ] = (g[t]+(as*((ys*yy[t])*k[((ps*nk)+krow[t])]))) ;
605
+ } ;
606
+ ;
607
+ } else {
608
+ 0 ;
609
+ } ;
610
+ ;
611
+ } ;
612
+ return 0 ;
613
+ }
614
+
615
+
616
+ SCM2CPP_FN inline int
617
+ select_working_set( scm2cpp::cspan<double> k, scm2cpp::cspan<int> krow, scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::cspan<double> g, scm2cpp::cspan<double> qd, double cost, double eps, int active, int nk, std::array<int,2> & wi )
618
+ { double gmax = -1e+300;
619
+ int gmaxi = -1;
620
+ double gmax2 = -1e+300;
621
+ int gmini = -1;
622
+ double objmin = 1e+300;
623
+ for( int t = 0 ; !(t == active) ; t = (t+1) ) {
624
+ double gt = g[t];
625
+ double at = a[t];
626
+ if( yy[t] > 0.0 ) {
627
+ if( (at < cost && (0.0-gt) >= gmax) ) {
628
+ gmax = (0.0-gt) ;
629
+ gmaxi = t ;
630
+ } else {
631
+ 0 ;
632
+ } ;
633
+ } else {
634
+ if( (at > 0.0 && gt >= gmax) ) {
635
+ gmax = gt ;
636
+ gmaxi = t ;
637
+ } else {
638
+ 0 ;
639
+ } ;
640
+ } ;
641
+ ;
642
+ } ;
643
+ if( gmaxi >= 0 ) {
644
+ int pi = krow[gmaxi];
645
+ double yi = yy[gmaxi];
646
+ double qdi = qd[gmaxi];
647
+ for( int t = 0 ; !(t == active) ; t = (t+1) ) {
648
+ double gt = g[t];
649
+ double at = a[t];
650
+ double yt = yy[t];
651
+ if( yt > 0.0 ) {
652
+ if( at > 0.0 ) {
653
+ double gd = (gmax+gt);
654
+ if( gt >= gmax2 ) {
655
+ gmax2 = gt ;
656
+ } else {
657
+ 0 ;
658
+ } ;
659
+ if( gd > 0.0 ) {
660
+ double quad = ((qdi+qd[t])-(2.0*(yi*(yi*(yt*k[((pi*nk)+krow[t])])))));
661
+ double obj = ((0.0-(gd*gd))/( ( quad > 0.0 ) ? (quad) : (1e-12) ));
662
+ if( obj <= objmin ) {
663
+ objmin = obj ;
664
+ gmini = t ;
665
+ } else {
666
+ 0 ;
667
+ } ;
668
+ ;
669
+ ;
670
+ } else {
671
+ 0 ;
672
+ } ;
673
+ ;
674
+ } else {
675
+ 0 ;
676
+ } ;
677
+ } else {
678
+ if( at < cost ) {
679
+ double gd = (gmax-gt);
680
+ if( (0.0-gt) >= gmax2 ) {
681
+ gmax2 = (0.0-gt) ;
682
+ } else {
683
+ 0 ;
684
+ } ;
685
+ if( gd > 0.0 ) {
686
+ double quad = ((qdi+qd[t])+(2.0*(yi*(yi*(yt*k[((pi*nk)+krow[t])])))));
687
+ double obj = ((0.0-(gd*gd))/( ( quad > 0.0 ) ? (quad) : (1e-12) ));
688
+ if( obj <= objmin ) {
689
+ objmin = obj ;
690
+ gmini = t ;
691
+ } else {
692
+ 0 ;
693
+ } ;
694
+ ;
695
+ ;
696
+ } else {
697
+ 0 ;
698
+ } ;
699
+ ;
700
+ } else {
701
+ 0 ;
702
+ } ;
703
+ } ;
704
+ ;
705
+ } ;
706
+ ;
707
+ } else {
708
+ 0 ;
709
+ } ;
710
+ wi[ 0 ] = gmaxi ;
711
+ wi[ 1 ] = gmini ;
712
+ if( ((gmax+gmax2) < eps || gmini < 0) ) {
713
+ return 1 ;
714
+ }
715
+ else {
716
+ return 0 ;
717
+ }
718
+ }
719
+
720
+
721
+ inline int
722
+ svr_smo( scm2cpp::cspan<double> k, scm2cpp::cspan<double> z, scm2cpp::span<double> beta, scm2cpp::span<double> rho, double cost, double epsilon, int iters, double eps, int n )
723
+ { std::vector<double> y2((2*n),0.0);
724
+ std::vector<double> lin((2*n),0.0);
725
+ std::vector<double> a2((2*n),0.0);
726
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
727
+ y2[ i ] = 1.0 ;
728
+ y2[ (i+n) ] = -1.0 ;
729
+ lin[ i ] = (epsilon-z[i]) ;
730
+ lin[ (i+n) ] = (epsilon+z[i]) ;
731
+ } ;
732
+ std::array<double,1> rv = scm2cpp::filled_array<double,1>(0.0);
733
+ int it = smo_solve(k,y2,lin,a2,rv,cost,iters,eps,int((2*n)),n);
734
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
735
+ beta[ i ] = (a2[i]-a2[(i+n)]) ;
736
+ } ;
737
+ rho[ 0 ] = rv[0] ;
738
+ return it ;
739
+ }
740
+
741
+
742
+ inline int
743
+ logistic_l1( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, double cost, double tol, int maxnewton, int seed, int n, int p )
744
+ { std::vector<int> index(p,0);
745
+ std::vector<double> hdiag(p,0.0);
746
+ std::vector<double> grad(p,0.0);
747
+ std::vector<double> wpd(p,0.0);
748
+ std::vector<double> xjneg(p,0.0);
749
+ std::vector<double> xtd(n,0.0);
750
+ std::vector<double> ewx(n,0.0);
751
+ std::vector<double> ewxnew(n,0.0);
752
+ std::vector<double> tau(n,0.0);
753
+ std::vector<double> d(n,0.0);
754
+ int rng = seed;
755
+ int npos = 0;
756
+ int newton = 0;
757
+ double wnorm = 0.0;
758
+ double ginit = -1.0;
759
+ double gmaxold = 1e+300;
760
+ int qpnochange = 0;
761
+ double innereps = 1.0;
762
+ int done = 0;
763
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
764
+ if( y[i] > 0.0 ) {
765
+ npos = (npos+1) ;
766
+ } else {
767
+ 0 ;
768
+ } ;
769
+ } ;
770
+
771
+ struct let24289 {
772
+ let24289() {}
773
+ int operator()( int m )
774
+ {
775
+ if( m < 1 ) {
776
+ return 1 ;
777
+ }
778
+ else {
779
+ return m ;
780
+ }
781
+ }
782
+ } ;
783
+ double eps = ((tol*(1.0*let24289()(( ( npos < (n-npos) ) ? (npos) : ((n-npos)) ))))/(1.0*n));
784
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
785
+ w[ j ] = 0.0 ;
786
+ wpd[ j ] = 0.0 ;
787
+ index[ j ] = j ;
788
+ double acc = 0.0;
789
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
790
+ if( y[i] < 0.0 ) {
791
+ acc = (acc+(cost*x[((j*n)+i)])) ;
792
+ } else {
793
+ 0 ;
794
+ } ;
795
+ } ;
796
+ xjneg[ j ] = acc ;
797
+ ;
798
+ } ;
799
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
800
+ ewx[ i ] = 1.0 ;
801
+ tau[ i ] = (cost*0.5) ;
802
+ d[ i ] = (cost*0.25) ;
803
+ } ;
804
+ for( ; !((done == 1 || newton >= maxnewton)) ; ) {
805
+ double gmaxnew = 0.0;
806
+ double gnorm1new = 0.0;
807
+ int active = p;
808
+ int s = 0;
809
+ for( ; !(s >= active) ; ) {
810
+ int j = index[s];
811
+ double h = 1e-12;
812
+ double tmp = 0.0;
813
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
814
+ double v = x[((j*n)+i)];
815
+ h = (h+((v*v)*d[i])) ;
816
+ tmp = (tmp+(v*tau[i])) ;
817
+ ;
818
+ } ;
819
+ hdiag[ j ] = h ;
820
+ grad[ j ] = ((0.0-tmp)+xjneg[j]) ;
821
+ double gp = (grad[j]+1.0);
822
+ double gn = (grad[j]-1.0);
823
+ double wj = w[j];
824
+ double viol = 0.0;
825
+ int shrunk = 0;
826
+ if( wj == 0.0 ) {
827
+ if( gp < 0.0 ) {
828
+ viol = (0.0-gp) ;
829
+ } else {
830
+ if( gn > 0.0 ) {
831
+ viol = gn ;
832
+ } else {
833
+ if( (gp > (gmaxold/(1.0*n)) && gn < (0.0-(gmaxold/(1.0*n)))) ) {
834
+ shrunk = 1 ;
835
+ } else {
836
+ 0 ;
837
+ } ;
838
+ } ;
839
+ } ;
840
+ } else {
841
+ if( wj > 0.0 ) {
842
+ viol = ( ( gp < 0.0 ) ? ((0.0-gp)) : (gp) ) ;
843
+ } else {
844
+ viol = ( ( gn < 0.0 ) ? ((0.0-gn)) : (gn) ) ;
845
+ } ;
846
+ } ;
847
+ if( shrunk == 1 ) {
848
+ active = (active-1) ;
849
+ index[ s ] = index[active] ;
850
+ index[ active ] = j ;
851
+ } else {
852
+ if( viol > gmaxnew ) {
853
+ gmaxnew = viol ;
854
+ } else {
855
+ 0 ;
856
+ } ;
857
+ gnorm1new = (gnorm1new+viol) ;
858
+ s = (s+1) ;
859
+ } ;
860
+ ;
861
+ ;
862
+ } ;
863
+ ;
864
+ if( newton == 0 ) {
865
+ ginit = gnorm1new ;
866
+ } else {
867
+ 0 ;
868
+ } ;
869
+ if( (gnorm1new <= (eps*ginit) || qpnochange >= 10) ) {
870
+ done = 1 ;
871
+ } else {
872
+ qpnochange = (qpnochange+1) ;
873
+ int iter = 0;
874
+ double qpgmaxold = 1e+300;
875
+ int qpactive = active;
876
+ int stopqp = 0;
877
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
878
+ xtd[ i ] = 0.0 ;
879
+ } ;
880
+ for( ; !((stopqp == 1 || iter >= 1000)) ; ) {
881
+ double qpgmaxnew = 0.0;
882
+ double qpgnorm1new = 0.0;
883
+ for( int t = 0 ; !(t == qpactive) ; t = (t+1) ) {
884
+ rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
885
+ if( rng < 0 ) {
886
+ rng = (rng+2147483647) ;
887
+ } else {
888
+ 0 ;
889
+ } ;
890
+ int k = (t+(rng % (qpactive-t)));
891
+ int tt = index[t];
892
+ index[ t ] = index[k] ;
893
+ index[ k ] = tt ;
894
+ ;
895
+ } ;
896
+ int s = 0;
897
+ for( ; !(s >= qpactive) ; ) {
898
+ int j = index[s];
899
+ double h = hdiag[j];
900
+ double g = (grad[j]+((wpd[j]-w[j])*1e-12));
901
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
902
+ g = (g+(x[((j*n)+i)]*(d[i]*xtd[i]))) ;
903
+ } ;
904
+ double gp = (g+1.0);
905
+ double gn = (g-1.0);
906
+ double wj = wpd[j];
907
+ double viol = 0.0;
908
+ int shrunk = 0;
909
+ if( wj == 0.0 ) {
910
+ if( gp < 0.0 ) {
911
+ viol = (0.0-gp) ;
912
+ } else {
913
+ if( gn > 0.0 ) {
914
+ viol = gn ;
915
+ } else {
916
+ if( (gp > (qpgmaxold/(1.0*n)) && gn < (0.0-(qpgmaxold/(1.0*n)))) ) {
917
+ shrunk = 1 ;
918
+ } else {
919
+ 0 ;
920
+ } ;
921
+ } ;
922
+ } ;
923
+ } else {
924
+ if( wj > 0.0 ) {
925
+ viol = ( ( gp < 0.0 ) ? ((0.0-gp)) : (gp) ) ;
926
+ } else {
927
+ viol = ( ( gn < 0.0 ) ? ((0.0-gn)) : (gn) ) ;
928
+ } ;
929
+ } ;
930
+ if( shrunk == 1 ) {
931
+ qpactive = (qpactive-1) ;
932
+ index[ s ] = index[qpactive] ;
933
+ index[ qpactive ] = j ;
934
+ } else {
935
+ double z = ( ( gp < (h*wj) ) ? (((0.0-gp)/h)) : (( ( gn > (h*wj) ) ? (((0.0-gn)/h)) : ((0.0-wj)) )) );
936
+ if( (z > 1e-12 || z < -1e-12) ) {
937
+ double zc = ( ( z < -10.0 ) ? (-10.0) : (( ( z > 10.0 ) ? (10.0) : (z) )) );
938
+ qpnochange = 0 ;
939
+ if( viol > qpgmaxnew ) {
940
+ qpgmaxnew = viol ;
941
+ } else {
942
+ 0 ;
943
+ } ;
944
+ qpgnorm1new = (qpgnorm1new+viol) ;
945
+ wpd[ j ] = (wj+zc) ;
946
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
947
+ xtd[ i ] = (xtd[i]+(x[((j*n)+i)]*zc)) ;
948
+ } ;
949
+ ;
950
+ } else {
951
+ 0 ;
952
+ } ;
953
+ ;
954
+ s = (s+1) ;
955
+ } ;
956
+ ;
957
+ ;
958
+ ;
959
+ } ;
960
+ ;
961
+ iter = (iter+1) ;
962
+ if( qpgnorm1new <= (innereps*ginit) ) {
963
+ if( qpactive == active ) {
964
+ stopqp = 1 ;
965
+ } else {
966
+ qpactive = active ;
967
+ qpgmaxold = 1e+300 ;
968
+ } ;
969
+ } else {
970
+ qpgmaxold = qpgmaxnew ;
971
+ } ;
972
+ ;
973
+ } ;
974
+ double delta = 0.0;
975
+ double wnormnew = 0.0;
976
+ double negsum = 0.0;
977
+ int found = 0;
978
+ int nls = 0;
979
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
980
+ delta = (delta+(grad[j]*(wpd[j]-w[j]))) ;
981
+ double v = wpd[j];
982
+ if( !(v == 0.0) ) {
983
+ wnormnew = (wnormnew+( ( v < 0.0 ) ? ((0.0-v)) : (v) )) ;
984
+ } else {
985
+ 0 ;
986
+ } ;
987
+ ;
988
+ } ;
989
+ delta = (delta+(wnormnew-wnorm)) ;
990
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
991
+ if( y[i] < 0.0 ) {
992
+ negsum = (negsum+(cost*xtd[i])) ;
993
+ } else {
994
+ 0 ;
995
+ } ;
996
+ } ;
997
+ for( ; !((found == 1 || nls >= 20)) ; ) {
998
+ double cond0 = (((wnormnew-wnorm)+negsum)-(0.01*delta));
999
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1000
+ double ex = exp(xtd[i]);
1001
+ ewxnew[ i ] = (ewx[i]*ex) ;
1002
+ cond0 = (cond0+(cost*log(((1.0+ewxnew[i])/(ex+ewxnew[i]))))) ;
1003
+ ;
1004
+ } ;
1005
+ if( cond0 <= 0.0 ) {
1006
+ found = 1 ;
1007
+ wnorm = wnormnew ;
1008
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1009
+ w[ j ] = wpd[j] ;
1010
+ } ;
1011
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1012
+ double e = ewxnew[i];
1013
+ ewx[ i ] = e ;
1014
+ double tt = (1.0/(1.0+e));
1015
+ tau[ i ] = (cost*tt) ;
1016
+ d[ i ] = (cost*(e*(tt*tt))) ;
1017
+ ;
1018
+ ;
1019
+ } ;
1020
+ } else {
1021
+ wnormnew = 0.0 ;
1022
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1023
+ double v = ((w[j]+wpd[j])*0.5);
1024
+ wpd[ j ] = v ;
1025
+ if( !(v == 0.0) ) {
1026
+ wnormnew = (wnormnew+( ( v < 0.0 ) ? ((0.0-v)) : (v) )) ;
1027
+ } else {
1028
+ 0 ;
1029
+ } ;
1030
+ ;
1031
+ } ;
1032
+ delta = (delta*0.5) ;
1033
+ negsum = (negsum*0.5) ;
1034
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1035
+ xtd[ i ] = (xtd[i]*0.5) ;
1036
+ } ;
1037
+ nls = (nls+1) ;
1038
+ } ;
1039
+ ;
1040
+ } ;
1041
+ if( nls >= 20 ) {
1042
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1043
+ double acc = 0.0;
1044
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1045
+ acc = (acc+(w[j]*x[((j*n)+i)])) ;
1046
+ } ;
1047
+ ewx[ i ] = exp(acc) ;
1048
+ ;
1049
+ } ;
1050
+ } else {
1051
+ 0 ;
1052
+ } ;
1053
+ ;
1054
+ if( iter == 1 ) {
1055
+ innereps = (innereps*0.25) ;
1056
+ } else {
1057
+ 0 ;
1058
+ } ;
1059
+ ;
1060
+ newton = (newton+1) ;
1061
+ gmaxold = gmaxnew ;
1062
+ } ;
1063
+ ;
1064
+ } ;
1065
+ return newton ;
1066
+ }
1067
+
1068
+
1069
+ inline int
1070
+ svr_liblinear( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, scm2cpp::span<double> beta, scm2cpp::span<double> xnorm, double cost, double epsilon, int iters, double tol, int seed, int n, int p )
1071
+ { std::vector<int> index(n,0);
1072
+ int rng = seed;
1073
+ int active = n;
1074
+ double gmaxold = 1e+300;
1075
+ double ginit = -1.0;
1076
+ int iter = 0;
1077
+ int done = 0;
1078
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1079
+ double acc = 0.0;
1080
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1081
+ acc = (acc+(x[((i*p)+j)]*x[((i*p)+j)])) ;
1082
+ } ;
1083
+ xnorm[ i ] = acc ;
1084
+ ;
1085
+ beta[ i ] = 0.0 ;
1086
+ index[ i ] = i ;
1087
+ } ;
1088
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1089
+ w[ j ] = 0.0 ;
1090
+ } ;
1091
+ for( ; !((done == 1 || iter >= iters)) ; ) {
1092
+ double gmaxnew = 0.0;
1093
+ double gnorm1new = 0.0;
1094
+ for( int t = 0 ; !(t == active) ; t = (t+1) ) {
1095
+ rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
1096
+ if( rng < 0 ) {
1097
+ rng = (rng+2147483647) ;
1098
+ } else {
1099
+ 0 ;
1100
+ } ;
1101
+ int k = (t+(rng % (active-t)));
1102
+ int tt = index[t];
1103
+ index[ t ] = index[k] ;
1104
+ index[ k ] = tt ;
1105
+ ;
1106
+ } ;
1107
+ int s = 0;
1108
+ for( ; !(s >= active) ; ) {
1109
+ int i = index[s];
1110
+ double g = (0.0-y[i]);
1111
+ double h = xnorm[i];
1112
+ double b = beta[i];
1113
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1114
+ g = (g+(x[((i*p)+j)]*w[j])) ;
1115
+ } ;
1116
+ double gp = (g+epsilon);
1117
+ double gn = (g-epsilon);
1118
+ double viol = 0.0;
1119
+ int shrunk = 0;
1120
+ if( b == 0.0 ) {
1121
+ if( gp < 0.0 ) {
1122
+ viol = (0.0-gp) ;
1123
+ } else {
1124
+ if( gn > 0.0 ) {
1125
+ viol = gn ;
1126
+ } else {
1127
+ if( (gp > gmaxold && gn < (0.0-gmaxold)) ) {
1128
+ shrunk = 1 ;
1129
+ } else {
1130
+ 0 ;
1131
+ } ;
1132
+ } ;
1133
+ } ;
1134
+ } else {
1135
+ if( b >= cost ) {
1136
+ if( gp > 0.0 ) {
1137
+ viol = gp ;
1138
+ } else {
1139
+ if( gp < (0.0-gmaxold) ) {
1140
+ shrunk = 1 ;
1141
+ } else {
1142
+ 0 ;
1143
+ } ;
1144
+ } ;
1145
+ } else {
1146
+ if( b <= (0.0-cost) ) {
1147
+ if( gn < 0.0 ) {
1148
+ viol = (0.0-gn) ;
1149
+ } else {
1150
+ if( gn > gmaxold ) {
1151
+ shrunk = 1 ;
1152
+ } else {
1153
+ 0 ;
1154
+ } ;
1155
+ } ;
1156
+ } else {
1157
+ if( b > 0.0 ) {
1158
+ viol = ( ( gp < 0.0 ) ? ((0.0-gp)) : (gp) ) ;
1159
+ } else {
1160
+ viol = ( ( gn < 0.0 ) ? ((0.0-gn)) : (gn) ) ;
1161
+ } ;
1162
+ } ;
1163
+ } ;
1164
+ } ;
1165
+ if( shrunk == 1 ) {
1166
+ active = (active-1) ;
1167
+ index[ s ] = index[active] ;
1168
+ index[ active ] = i ;
1169
+ } else {
1170
+ if( viol > gmaxnew ) {
1171
+ gmaxnew = viol ;
1172
+ } else {
1173
+ 0 ;
1174
+ } ;
1175
+ gnorm1new = (gnorm1new+viol) ;
1176
+ double d = ( ( gp < (h*b) ) ? (((0.0-gp)/h)) : (( ( gn > (h*b) ) ? (((0.0-gn)/h)) : ((0.0-b)) )) );
1177
+ if( (d > 1e-12 || d < -1e-12) ) {
1178
+ double bn = (b+d);
1179
+ double bc = ( ( bn < (0.0-cost) ) ? ((0.0-cost)) : (( ( bn > cost ) ? (cost) : (bn) )) );
1180
+ beta[ i ] = bc ;
1181
+ if( !(bc == b) ) {
1182
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1183
+ w[ j ] = (w[j]+((bc-b)*x[((i*p)+j)])) ;
1184
+ } ;
1185
+ } else {
1186
+ false ;
1187
+ } ;
1188
+ ;
1189
+ ;
1190
+ } else {
1191
+ 0 ;
1192
+ } ;
1193
+ ;
1194
+ s = (s+1) ;
1195
+ } ;
1196
+ ;
1197
+ ;
1198
+ ;
1199
+ } ;
1200
+ ;
1201
+ if( iter == 0 ) {
1202
+ ginit = gnorm1new ;
1203
+ } else {
1204
+ 0 ;
1205
+ } ;
1206
+ iter = (iter+1) ;
1207
+ if( gnorm1new <= (tol*ginit) ) {
1208
+ if( active == n ) {
1209
+ done = 1 ;
1210
+ } else {
1211
+ active = n ;
1212
+ gmaxold = 1e+300 ;
1213
+ } ;
1214
+ } else {
1215
+ gmaxold = gmaxnew ;
1216
+ } ;
1217
+ ;
1218
+ } ;
1219
+ return iter ;
1220
+ }
1221
+
1222
+
1223
+ SCM2CPP_FN inline double
1224
+ ols_rows( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> rows, int m, scm2cpp::span<double> w, scm2cpp::span<double> g, scm2cpp::span<double> c, scm2cpp::span<double> l, scm2cpp::span<double> z, scm2cpp::span<double> xm, int n, int p )
1225
+ { double ym = 0.0;
1226
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1227
+ xm[ j ] = 0.0 ;
1228
+ } ;
1229
+ for( int r = 0 ; !(r == m) ; r = (r+1) ) {
1230
+ int i = rows[r];
1231
+ ym = (ym+y[i]) ;
1232
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1233
+ xm[ j ] = (xm[j]+x[((i*p)+j)]) ;
1234
+ } ;
1235
+ ;
1236
+ } ;
1237
+ ym = (ym/(1.0*m)) ;
1238
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1239
+ xm[ j ] = (xm[j]/(1.0*m)) ;
1240
+ } ;
1241
+ for( int t = 0 ; !(t == (p*p)) ; t = (t+1) ) {
1242
+ g[ t ] = 0.0 ;
1243
+ } ;
1244
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1245
+ c[ j ] = 0.0 ;
1246
+ } ;
1247
+ for( int r = 0 ; !(r == m) ; r = (r+1) ) {
1248
+ int i = rows[r];
1249
+ double yc = (y[i]-ym);
1250
+ for( int a = 0 ; !(a == p) ; a = (a+1) ) {
1251
+ double xa = (x[((i*p)+a)]-xm[a]);
1252
+ c[ a ] = (c[a]+(xa*yc)) ;
1253
+ for( int b = 0 ; !(b > a) ; b = (b+1) ) {
1254
+ double xb = (x[((i*p)+b)]-xm[b]);
1255
+ g[ ((a*p)+b) ] = (g[((a*p)+b)]+(xa*xb)) ;
1256
+ ;
1257
+ } ;
1258
+ ;
1259
+ } ;
1260
+ ;
1261
+ ;
1262
+ } ;
1263
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1264
+ double s = g[((j*p)+j)];
1265
+ for( int k = 0 ; !(k == j) ; k = (k+1) ) {
1266
+ s = (s-(l[((j*p)+k)]*l[((j*p)+k)])) ;
1267
+ } ;
1268
+ l[ ((j*p)+j) ] = sqrt(( ( s > 0.0 ) ? (s) : (1e-300) )) ;
1269
+ for( int i = (j+1) ; !(i >= p) ; i = (i+1) ) {
1270
+ double t = g[((i*p)+j)];
1271
+ for( int k = 0 ; !(k == j) ; k = (k+1) ) {
1272
+ t = (t-(l[((i*p)+k)]*l[((j*p)+k)])) ;
1273
+ } ;
1274
+ l[ ((i*p)+j) ] = (t/l[((j*p)+j)]) ;
1275
+ ;
1276
+ } ;
1277
+ ;
1278
+ } ;
1279
+ for( int i = 0 ; !(i == p) ; i = (i+1) ) {
1280
+ double t = c[i];
1281
+ for( int k = 0 ; !(k == i) ; k = (k+1) ) {
1282
+ t = (t-(l[((i*p)+k)]*z[k])) ;
1283
+ } ;
1284
+ z[ i ] = (t/l[((i*p)+i)]) ;
1285
+ ;
1286
+ } ;
1287
+ for( int ii = 0 ; !(ii == p) ; ii = (ii+1) ) {
1288
+ int i = ((p-1)-ii);
1289
+ double t = z[i];
1290
+ for( int k = (i+1) ; !(k >= p) ; k = (k+1) ) {
1291
+ t = (t-(l[((k*p)+i)]*w[k])) ;
1292
+ } ;
1293
+ w[ i ] = (t/l[((i*p)+i)]) ;
1294
+ ;
1295
+ ;
1296
+ } ;
1297
+ double b = ym;
1298
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1299
+ b = (b-(xm[j]*w[j])) ;
1300
+ } ;
1301
+ return b ;
1302
+ }
1303
+
1304
+
1305
+ inline int
1306
+ ransac( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> subsets, scm2cpp::span<double> coef, scm2cpp::span<double> mask, scm2cpp::span<double> out, double maxtrials, int minsamples, double thresh, double stopprob, int n, int p )
1307
+ { std::vector<int> rows(n,0);
1308
+ std::vector<int> inl(n,0);
1309
+ std::vector<double> w(p,0.0);
1310
+ std::vector<double> g((p*p),0.0);
1311
+ std::vector<double> c(p,0.0);
1312
+ std::vector<double> l((p*p),0.0);
1313
+ std::vector<double> z(p,0.0);
1314
+ std::vector<double> xm(p,0.0);
1315
+ int nbest = 1;
1316
+ double scorebest = -1e+300;
1317
+ int found = 0;
1318
+ int trials = 0;
1319
+ double limit = (1.0*maxtrials);
1320
+ double eps = 2.220446049250313e-16;
1321
+ for( ; !((1.0*trials) >= limit) ; ) {
1322
+ int t = trials;
1323
+ trials = (trials+1) ;
1324
+ for( int r = 0 ; !(r == minsamples) ; r = (r+1) ) {
1325
+ rows[ r ] = subsets[((t*minsamples)+r)] ;
1326
+ } ;
1327
+ double b = ols_rows(x,y,rows,minsamples,w,g,c,l,z,xm,n,p);
1328
+ int ninl = 0;
1329
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1330
+ double pred = b;
1331
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1332
+ pred = (pred+(x[((i*p)+j)]*w[j])) ;
1333
+ } ;
1334
+ double res = (y[i]-pred);
1335
+ if( ( ( res < 0.0 ) ? ((0.0-res)) : (res) ) <= thresh ) {
1336
+ inl[ ninl ] = i ;
1337
+ ninl = (ninl+1) ;
1338
+ } else {
1339
+ 0 ;
1340
+ } ;
1341
+ ;
1342
+ ;
1343
+ } ;
1344
+ if( ninl >= nbest ) {
1345
+ double ym = 0.0;
1346
+ double sstot = 0.0;
1347
+ double ssres = 0.0;
1348
+ for( int r = 0 ; !(r == ninl) ; r = (r+1) ) {
1349
+ ym = (ym+y[inl[r]]) ;
1350
+ } ;
1351
+ ym = (ym/(1.0*ninl)) ;
1352
+ for( int r = 0 ; !(r == ninl) ; r = (r+1) ) {
1353
+ int i = inl[r];
1354
+ double pred = b;
1355
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1356
+ pred = (pred+(x[((i*p)+j)]*w[j])) ;
1357
+ } ;
1358
+ double dr = (y[i]-pred);
1359
+ double dt = (y[i]-ym);
1360
+ ssres = (ssres+(dr*dr)) ;
1361
+ sstot = (sstot+(dt*dt)) ;
1362
+ ;
1363
+ ;
1364
+ ;
1365
+ } ;
1366
+ double score = ( ( sstot > 0.0 ) ? ((1.0-(ssres/sstot))) : (( ( ssres > 0.0 ) ? (0.0) : (1.0) )) );
1367
+ if( !((ninl == nbest && score < scorebest)) ) {
1368
+ found = 1 ;
1369
+ nbest = ninl ;
1370
+ scorebest = score ;
1371
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1372
+ mask[ i ] = 0.0 ;
1373
+ } ;
1374
+ for( int r = 0 ; !(r == ninl) ; r = (r+1) ) {
1375
+ mask[ inl[r] ] = 1.0 ;
1376
+ } ;
1377
+ double ratio = ((1.0*nbest)/(1.0*n));
1378
+ double nom = ( ( (1.0-stopprob) > eps ) ? ((1.0-stopprob)) : (eps) );
1379
+ struct let24311 {
1380
+ double & ratio;
1381
+ int & minsamples;
1382
+ let24311( double & ratio, int & minsamples ):ratio(ratio),minsamples(minsamples) {}
1383
+ double operator()( double acc )
1384
+ { for( int s = 0 ; !(s == minsamples) ; s = (s+1) ) {
1385
+ acc = (acc*ratio) ;
1386
+ } ;
1387
+ return acc ;
1388
+ }
1389
+ } ;
1390
+ double pw = let24311(ratio,minsamples)(1.0);
1391
+ double denom = ( ( (1.0-pw) > eps ) ? ((1.0-pw)) : (eps) );
1392
+ struct let24339 {
1393
+ let24339() {}
1394
+ double operator()( double q )
1395
+ {
1396
+ double cq = (0.0-std::floor((0.0-q)));
1397
+ if( cq < 0.0 ) {
1398
+ return (0.0-cq) ;
1399
+ }
1400
+ else {
1401
+ return cq ;
1402
+ }
1403
+ }
1404
+ } ;
1405
+ double dyn = ( ( nom == 1.0 ) ? (0.0) : (( ( denom == 1.0 ) ? (1e+300) : (let24339()((log(nom)/log(denom)))) )) );
1406
+ if( dyn < limit ) {
1407
+ limit = dyn ;
1408
+ } else {
1409
+ 0 ;
1410
+ } ;
1411
+ ;
1412
+ ;
1413
+ ;
1414
+ ;
1415
+ } else {
1416
+ 0 ;
1417
+ } ;
1418
+ ;
1419
+ ;
1420
+ } else {
1421
+ 0 ;
1422
+ } ;
1423
+ ;
1424
+ ;
1425
+ } ;
1426
+ int m = 0;
1427
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1428
+ if( mask[i] == 1.0 ) {
1429
+ rows[ m ] = i ;
1430
+ m = (m+1) ;
1431
+ } else {
1432
+ 0 ;
1433
+ } ;
1434
+ } ;
1435
+ out[ 0 ] = ols_rows(x,y,rows,m,coef,g,c,l,z,xm,n,p) ;
1436
+ ;
1437
+ return trials ;
1438
+ }
1439
+
1440
+
1441
+ SCM2CPP_FN inline double
1442
+ sgd_loss( int loss, double y, double p, double epsilon )
1443
+ {
1444
+ if( loss == 1 ) {
1445
+ return (0.5*((p-y)*(p-y))) ;
1446
+ }
1447
+ else {
1448
+ if( loss == 3 ) {
1449
+ double r = (( ( (y-p) < 0.0 ) ? ((p-y)) : ((y-p)) )-epsilon);
1450
+ if( r > 0.0 ) {
1451
+ return r ;
1452
+ }
1453
+ else {
1454
+ return 0.0 ;
1455
+ }
1456
+ }
1457
+ else {
1458
+ double th = ( ( loss == 0 ) ? (1.0) : (0.0) );
1459
+ double z = (p*y);
1460
+ if( z <= th ) {
1461
+ return (th-z) ;
1462
+ }
1463
+ else {
1464
+ return 0.0 ;
1465
+ }
1466
+ }
1467
+ }
1468
+ }
1469
+
1470
+
1471
+ SCM2CPP_FN inline double
1472
+ sgd_grad( int loss, double y, double p, double epsilon )
1473
+ {
1474
+ if( loss == 1 ) {
1475
+ return (p-y) ;
1476
+ }
1477
+ else {
1478
+ if( loss == 3 ) {
1479
+ if( (y-p) > epsilon ) {
1480
+ return -1.0 ;
1481
+ }
1482
+ else {
1483
+ if( (p-y) > epsilon ) {
1484
+ return 1.0 ;
1485
+ }
1486
+ else {
1487
+ return 0.0 ;
1488
+ }
1489
+ }
1490
+ }
1491
+ else {
1492
+ double th2 = ( ( loss == 0 ) ? (1.0) : (0.0) );
1493
+ double z2 = (p*y);
1494
+ if( z2 <= th2 ) {
1495
+ return (0.0-y) ;
1496
+ }
1497
+ else {
1498
+ return 0.0 ;
1499
+ }
1500
+ }
1501
+ }
1502
+ }
1503
+
1504
+
1505
+ SCM2CPP_FN inline int
1506
+ sgd( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, scm2cpp::span<double> out, int loss, int lr, int pen, double alpha, double eta0, double powert, double epsilon, int maxiter, double tol, int niternochange, int fitint, int n, int p )
1507
+ { double wscale = 1.0;
1508
+ double sqnorm = 0.0;
1509
+ double b = 0.0;
1510
+ double t = 1.0;
1511
+ double optinit = 0.0;
1512
+ double best = 1e+300;
1513
+ int noimp = 0;
1514
+ int epoch = 0;
1515
+ int done = 0;
1516
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1517
+ sqnorm = (sqnorm+(w[j]*w[j])) ;
1518
+ } ;
1519
+ if( lr == 0 ) {
1520
+ double typw = sqrt((1.0/sqrt(alpha)));
1521
+ double g = sgd_grad(loss,1.0,double((0.0-typw)),epsilon);
1522
+ double eta00 = (typw/( ( g > 1.0 ) ? (g) : (1.0) ));
1523
+ optinit = (1.0/(eta00*alpha)) ;
1524
+ ;
1525
+ ;
1526
+ ;
1527
+ } else {
1528
+ 0 ;
1529
+ } ;
1530
+ for( ; !((done == 1 || epoch >= maxiter)) ; ) {
1531
+ double objsum = 0.0;
1532
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
1533
+ double dot = 0.0;
1534
+ double sx = 0.0;
1535
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1536
+ dot = (dot+(x[((i*p)+j)]*w[j])) ;
1537
+ } ;
1538
+ if( lr >= 3 ) {
1539
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1540
+ sx = (sx+(x[((i*p)+j)]*x[((i*p)+j)])) ;
1541
+ } ;
1542
+ } else {
1543
+ 0 ;
1544
+ } ;
1545
+ double pr = ((wscale*dot)+b);
1546
+ double yi = y[i];
1547
+ double eta = ( ( lr == 0 ) ? ((1.0/(alpha*((optinit+t)-1.0)))) : (( ( lr == 1 ) ? (((1.0*eta0)/std::pow(t, (1.0*powert)))) : (eta0) )) );
1548
+ double li = sgd_loss(loss,yi,pr,epsilon);
1549
+ double dl = ( ( lr < 3 ) ? (sgd_grad(loss,yi,pr,epsilon)) : (0.0) );
1550
+ objsum = (objsum+li) ;
1551
+ if( (lr < 3 && pen == 1) ) {
1552
+ objsum = (objsum+(alpha*(0.5*sqnorm))) ;
1553
+ } else {
1554
+ 0 ;
1555
+ } ;
1556
+ if( !((lr == 3 && sx == 0.0)) ) {
1557
+ double step = ( ( lr == 3 ) ? (sgd_min(eta0,double((li/sx)))) : (( ( lr == 4 ) ? ((li/(sx+(0.5/eta0)))) : (((0.0-eta)*( ( dl < -1000000000000.0 ) ? (-1000000000000.0) : (( ( dl > 1000000000000.0 ) ? (1000000000000.0) : (dl) )) ))) )) );
1558
+ double update = ( ( lr >= 3 ) ? (( ( (loss == 0 || loss == 2) ) ? ((step*yi)) : (( ( (yi-pr) < 0.0 ) ? ((0.0-step)) : (step) )) )) : (step) );
1559
+ if( pen == 1 ) {
1560
+ double c = (1.0-(eta*alpha));
1561
+ double cc = ( ( c > 0.0 ) ? (c) : (0.0) );
1562
+ wscale = (wscale*cc) ;
1563
+ sqnorm = (sqnorm*(cc*cc)) ;
1564
+ if( wscale < 1e-9 ) {
1565
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1566
+ w[ j ] = (w[j]*wscale) ;
1567
+ } ;
1568
+ wscale = 1.0 ;
1569
+ } else {
1570
+ 0 ;
1571
+ } ;
1572
+ ;
1573
+ ;
1574
+ } else {
1575
+ 0 ;
1576
+ } ;
1577
+ if( !(update == 0.0) ) {
1578
+ double acc = 0.0;
1579
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1580
+ double v = (w[j]+(x[((i*p)+j)]*(update/wscale)));
1581
+ w[ j ] = v ;
1582
+ acc = (acc+(v*v)) ;
1583
+ ;
1584
+ } ;
1585
+ sqnorm = (acc*(wscale*wscale)) ;
1586
+ ;
1587
+ } else {
1588
+ 0 ;
1589
+ } ;
1590
+ if( (fitint == 1 && !(update == 0.0)) ) {
1591
+ b = (b+update) ;
1592
+ } else {
1593
+ 0 ;
1594
+ } ;
1595
+ ;
1596
+ ;
1597
+ t = (t+1.0) ;
1598
+ } else {
1599
+ 0 ;
1600
+ } ;
1601
+ ;
1602
+ ;
1603
+ ;
1604
+ } ;
1605
+ epoch = (epoch+1) ;
1606
+ double mean = (objsum/(1.0*n));
1607
+ if( mean > (best-tol) ) {
1608
+ noimp = (noimp+1) ;
1609
+ } else {
1610
+ noimp = 0 ;
1611
+ } ;
1612
+ if( mean < best ) {
1613
+ best = mean ;
1614
+ } else {
1615
+ 0 ;
1616
+ } ;
1617
+ ;
1618
+ if( noimp >= niternochange ) {
1619
+ done = 1 ;
1620
+ } else {
1621
+ 0 ;
1622
+ } ;
1623
+ ;
1624
+ } ;
1625
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1626
+ w[ j ] = (w[j]*wscale) ;
1627
+ } ;
1628
+ out[ 0 ] = b ;
1629
+ return epoch ;
1630
+ }
1631
+
1632
+
1633
+ SCM2CPP_FN inline double
1634
+ sgd_min( double a, double b )
1635
+ {
1636
+ if( a < b ) {
1637
+ return a ;
1638
+ }
1639
+ else {
1640
+ return b ;
1641
+ }
1642
+ }
1643
+
1644
+
1645
+ SCM2CPP_FN inline double
1646
+ min_pos( scm2cpp::cspan<double> v, int m )
1647
+ { double r = 1.7976931348623157e+308;
1648
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
1649
+ double x = v[i];
1650
+ if( (x > 0.0 && x < r) ) {
1651
+ r = x ;
1652
+ } else {
1653
+ 0 ;
1654
+ } ;
1655
+ ;
1656
+ } ;
1657
+ return r ;
1658
+ }
1659
+
1660
+
1661
+ SCM2CPP_FN inline double
1662
+ round_even( double y )
1663
+ {
1664
+ double r = std::floor(y);
1665
+ double d = (y-r);
1666
+ if( d > 0.5 ) {
1667
+ return (r+1.0) ;
1668
+ }
1669
+ else {
1670
+ if( d < 0.5 ) {
1671
+ return r ;
1672
+ }
1673
+ else {
1674
+ if( (2.0*std::floor((0.5*r))) == r ) {
1675
+ return r ;
1676
+ }
1677
+ else {
1678
+ return (r+1.0) ;
1679
+ }
1680
+ }
1681
+ }
1682
+ }
1683
+
1684
+
1685
+ SCM2CPP_FN inline int
1686
+ cholesky_delete( scm2cpp::span<double> l, int go, int n, int k )
1687
+ { for( int i = go ; !(i >= (n-1)) ; i = (i+1) ) {
1688
+ for( int j = 0 ; !(j == (i+2)) ; j = (j+1) ) {
1689
+ l[ ((i*k)+j) ] = l[(((i+1)*k)+j)] ;
1690
+ } ;
1691
+ } ;
1692
+ for( int i = go ; !(i >= (n-1)) ; i = (i+1) ) {
1693
+ double a = l[((i*k)+i)];
1694
+ double b = l[((i*k)+(i+1))];
1695
+ double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
1696
+ double ab = ( ( b < 0.0 ) ? ((0.0-b)) : (b) );
1697
+ double roe = ( ( aa > ab ) ? (a) : (b) );
1698
+ double scale = (aa+ab);
1699
+ double r = ( ( scale == 0.0 ) ? (0.0) : ((( ( roe < 0.0 ) ? (-1.0) : (1.0) )*(scale*sqrt((((a/scale)*(a/scale))+((b/scale)*(b/scale))))))) );
1700
+ double c = ( ( scale == 0.0 ) ? (1.0) : ((a/r)) );
1701
+ double s = ( ( scale == 0.0 ) ? (0.0) : ((b/r)) );
1702
+ double cc = ( ( r < 0.0 ) ? ((0.0-c)) : (c) );
1703
+ double ss = ( ( r < 0.0 ) ? ((0.0-s)) : (s) );
1704
+ l[ ((i*k)+i) ] = ( ( r < 0.0 ) ? ((0.0-r)) : (r) ) ;
1705
+ l[ ((i*k)+(i+1)) ] = 0.0 ;
1706
+ for( int t = (i+1) ; !(t >= n) ; t = (t+1) ) {
1707
+ double x = l[((t*k)+i)];
1708
+ double y = l[((t*k)+(i+1))];
1709
+ l[ ((t*k)+i) ] = ((cc*x)+(ss*y)) ;
1710
+ l[ ((t*k)+(i+1)) ] = ((cc*y)-(ss*x)) ;
1711
+ ;
1712
+ } ;
1713
+ ;
1714
+ ;
1715
+ ;
1716
+ ;
1717
+ ;
1718
+ ;
1719
+ } ;
1720
+ return 0 ;
1721
+ }
1722
+
1723
+
1724
+ SCM2CPP_FN inline int
1725
+ swap_rc( scm2cpp::span<double> g, int a, int b, int p )
1726
+ { for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1727
+ double rv = g[((a*p)+j)];
1728
+ g[ ((a*p)+j) ] = g[((b*p)+j)] ;
1729
+ g[ ((b*p)+j) ] = rv ;
1730
+ ;
1731
+ } ;
1732
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1733
+ double cv = g[((j*p)+a)];
1734
+ g[ ((j*p)+a) ] = g[((j*p)+b)] ;
1735
+ g[ ((j*p)+b) ] = cv ;
1736
+ ;
1737
+ } ;
1738
+ return 0 ;
1739
+ }
1740
+
1741
+
1742
+ inline int
1743
+ 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 )
1744
+ { std::vector<double> g0((p*p),0.0);
1745
+ std::vector<double> cov0(p,0.0);
1746
+ std::vector<double> cbuf(p,0.0);
1747
+ std::vector<int> index(p,0);
1748
+ std::vector<int> active(p,0);
1749
+ std::vector<double> sign(p,0.0);
1750
+ int kmax = ( ( maxiter < p ) ? (maxiter) : (p) );
1751
+ std::vector<double> prev(p,0.0);
1752
+ std::vector<double> dir(p,0.0);
1753
+ std::vector<double> ls(p,0.0);
1754
+ std::vector<double> zz(p,0.0);
1755
+ std::vector<double> ratio(p,0.0);
1756
+ std::vector<int> idx(p,0);
1757
+ std::vector<int> dropv(p,0);
1758
+ int cs = 0;
1759
+ int na = 0;
1760
+ int niter = 0;
1761
+ int drop = 0;
1762
+ int done = 0;
1763
+ double alpha = 0.0;
1764
+ double prevalpha = 0.0;
1765
+ double tiny32 = 1.1754943508222875e-38;
1766
+ double eqtol = 1.1920928955078125e-7;
1767
+ std::vector<double> l((kmax*kmax),0.0);
1768
+ for( int t = 0 ; !(t == (p*p)) ; t = (t+1) ) {
1769
+ g0[ t ] = g[t] ;
1770
+ } ;
1771
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1772
+ cov0[ j ] = cov[j] ;
1773
+ cbuf[ j ] = cov[j] ;
1774
+ index[ j ] = j ;
1775
+ coef[ j ] = 0.0 ;
1776
+ if( rec == 1 ) {
1777
+ path[ j ] = 0.0 ;
1778
+ } else {
1779
+ 0 ;
1780
+ } ;
1781
+ } ;
1782
+ for( ; !(done == 1) ; ) {
1783
+ int clen = (p-cs);
1784
+ int cidx = 0;
1785
+ double cval = 0.0;
1786
+ double cabs = 0.0;
1787
+ for( int t = 0 ; !(t == clen) ; t = (t+1) ) {
1788
+ double v = cbuf[(cs+t)];
1789
+ double av = ( ( v < 0.0 ) ? ((0.0-v)) : (v) );
1790
+ if( av > cabs ) {
1791
+ cabs = av ;
1792
+ cidx = t ;
1793
+ cval = v ;
1794
+ } else {
1795
+ 0 ;
1796
+ } ;
1797
+ ;
1798
+ ;
1799
+ } ;
1800
+ alpha = (cabs/(1.0*nsamples)) ;
1801
+ if( rec == 1 ) {
1802
+ alphas[ niter ] = alpha ;
1803
+ } else {
1804
+ 0 ;
1805
+ } ;
1806
+ if( alpha <= (alphamin+eqtol) ) {
1807
+ if( ( ( (alpha-alphamin) < 0.0 ) ? ((alphamin-alpha)) : ((alpha-alphamin)) ) > eqtol ) {
1808
+ if( niter > 0 ) {
1809
+ double ss = ((prevalpha-alphamin)/(prevalpha-alpha));
1810
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1811
+ coef[ j ] = (prev[j]+(ss*(coef[j]-prev[j]))) ;
1812
+ } ;
1813
+ ;
1814
+ } else {
1815
+ 0 ;
1816
+ } ;
1817
+ alpha = alphamin ;
1818
+ if( rec == 1 ) {
1819
+ alphas[ niter ] = alphamin ;
1820
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1821
+ path[ ((niter*p)+j) ] = coef[j] ;
1822
+ } ;
1823
+ } else {
1824
+ 0 ;
1825
+ } ;
1826
+ } else {
1827
+ 0 ;
1828
+ } ;
1829
+ done = 1 ;
1830
+ } else {
1831
+ if( (niter >= maxiter || na >= p) ) {
1832
+ done = 1 ;
1833
+ } else {
1834
+ int degenerate = 0;
1835
+ if( drop == 0 ) {
1836
+ int m = na;
1837
+ int nn = (cidx+na);
1838
+ sign[ na ] = ( ( cval > 0.0 ) ? (1.0) : (( ( cval < 0.0 ) ? (-1.0) : (0.0) )) ) ;
1839
+ double tc = cbuf[(cs+cidx)];
1840
+ cbuf[ (cs+cidx) ] = cbuf[cs] ;
1841
+ cbuf[ cs ] = tc ;
1842
+ ;
1843
+ int ti = index[nn];
1844
+ index[ nn ] = index[m] ;
1845
+ index[ m ] = ti ;
1846
+ ;
1847
+ cs = (cs+1) ;
1848
+ swap_rc(g,m,nn,p) ;
1849
+ double c = g[((na*p)+na)];
1850
+ double v = 0.0;
1851
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1852
+ double t = g[((na*p)+i)];
1853
+ for( int s = 0 ; !(s == i) ; s = (s+1) ) {
1854
+ t = (t-(l[((i*kmax)+s)]*l[((na*kmax)+s)])) ;
1855
+ } ;
1856
+ double li = (t/l[((i*kmax)+i)]);
1857
+ l[ ((na*kmax)+i) ] = li ;
1858
+ v = (v+(li*li)) ;
1859
+ ;
1860
+ ;
1861
+ } ;
1862
+ double dd = sqrt(( ( (c-v) < 0.0 ) ? ((v-c)) : ((c-v)) ));
1863
+ double dg = ( ( dd > (1.0*eps) ) ? (dd) : ((1.0*eps)) );
1864
+ l[ ((na*kmax)+na) ] = dg ;
1865
+ if( dg < 1e-7 ) {
1866
+ cs = (cs-1) ;
1867
+ cbuf[ cs ] = 0.0 ;
1868
+ double tc = cbuf[(cs+cidx)];
1869
+ cbuf[ (cs+cidx) ] = cbuf[cs] ;
1870
+ cbuf[ cs ] = tc ;
1871
+ ;
1872
+ degenerate = 1 ;
1873
+ } else {
1874
+ active[ na ] = index[na] ;
1875
+ na = (na+1) ;
1876
+ } ;
1877
+ ;
1878
+ ;
1879
+ ;
1880
+ ;
1881
+ } else {
1882
+ false ;
1883
+ } ;
1884
+ if( degenerate == 0 ) {
1885
+ if( (method == 1 && niter > 0 && prevalpha < alpha) ) {
1886
+ done = 1 ;
1887
+ } else {
1888
+ double aa = 0.0;
1889
+ int clen2 = (p-cs);
1890
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1891
+ double t = sign[i];
1892
+ for( int s = 0 ; !(s == i) ; s = (s+1) ) {
1893
+ t = (t-(l[((i*kmax)+s)]*zz[s])) ;
1894
+ } ;
1895
+ zz[ i ] = (t/l[((i*kmax)+i)]) ;
1896
+ ;
1897
+ } ;
1898
+ for( int ii = 0 ; !(ii == na) ; ii = (ii+1) ) {
1899
+ int i = ((na-1)-ii);
1900
+ double t = zz[i];
1901
+ for( int s = (i+1) ; !(s >= na) ; s = (s+1) ) {
1902
+ t = (t-(l[((s*kmax)+i)]*ls[s])) ;
1903
+ } ;
1904
+ ls[ i ] = (t/l[((i*kmax)+i)]) ;
1905
+ ;
1906
+ ;
1907
+ } ;
1908
+ if( (na == 1 && ls[0] == 0.0) ) {
1909
+ ls[ 0 ] = 1.0 ;
1910
+ aa = 1.0 ;
1911
+ } else {
1912
+ double sm = 0.0;
1913
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1914
+ sm = (sm+(ls[i]*sign[i])) ;
1915
+ } ;
1916
+ aa = (1.0/sqrt(sm)) ;
1917
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1918
+ ls[ i ] = (aa*ls[i]) ;
1919
+ } ;
1920
+ ;
1921
+ } ;
1922
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
1923
+ double acc = 0.0;
1924
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1925
+ acc = (acc+(g[((i*p)+(na+t))]*ls[i])) ;
1926
+ } ;
1927
+ dir[ t ] = (round_even(double((acc*1e+15)))/1e+15) ;
1928
+ ;
1929
+ } ;
1930
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
1931
+ ratio[ t ] = ((cabs-cbuf[(cs+t)])/((aa-dir[t])+tiny32)) ;
1932
+ } ;
1933
+ double gfirst = min_pos(ratio,clen2);
1934
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
1935
+ ratio[ t ] = ((cabs+cbuf[(cs+t)])/((aa+dir[t])+tiny32)) ;
1936
+ } ;
1937
+ double gsecond = min_pos(ratio,clen2);
1938
+ double gam = ( ( ( ( gfirst < gsecond ) ? (gfirst) : (gsecond) ) < (cabs/aa) ) ? (( ( gfirst < gsecond ) ? (gfirst) : (gsecond) )) : ((cabs/aa)) );
1939
+ int nidx = 0;
1940
+ drop = 0 ;
1941
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1942
+ zz[ i ] = ((0.0-coef[active[i]])/(ls[i]+tiny32)) ;
1943
+ } ;
1944
+ double zpos = min_pos(zz,na);
1945
+ if( zpos < gam ) {
1946
+ for( int ii = 0 ; !(ii == na) ; ii = (ii+1) ) {
1947
+ int i = ((na-1)-ii);
1948
+ if( zz[i] == zpos ) {
1949
+ idx[ nidx ] = i ;
1950
+ nidx = (nidx+1) ;
1951
+ } else {
1952
+ 0 ;
1953
+ } ;
1954
+ ;
1955
+ } ;
1956
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
1957
+ int i = idx[r];
1958
+ sign[ i ] = (0.0-sign[i]) ;
1959
+ ;
1960
+ } ;
1961
+ if( method == 1 ) {
1962
+ gam = zpos ;
1963
+ } else {
1964
+ 0 ;
1965
+ } ;
1966
+ drop = 1 ;
1967
+ } else {
1968
+ 0 ;
1969
+ } ;
1970
+ ;
1971
+ niter = (niter+1) ;
1972
+ prevalpha = alpha ;
1973
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1974
+ prev[ j ] = coef[j] ;
1975
+ coef[ j ] = 0.0 ;
1976
+ } ;
1977
+ for( int i = 0 ; !(i == na) ; i = (i+1) ) {
1978
+ int a = active[i];
1979
+ coef[ a ] = (prev[a]+(gam*ls[i])) ;
1980
+ ;
1981
+ } ;
1982
+ if( rec == 1 ) {
1983
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
1984
+ path[ ((niter*p)+j) ] = coef[j] ;
1985
+ } ;
1986
+ } else {
1987
+ 0 ;
1988
+ } ;
1989
+ for( int t = 0 ; !(t == clen2) ; t = (t+1) ) {
1990
+ cbuf[ (cs+t) ] = (cbuf[(cs+t)]-(gam*dir[t])) ;
1991
+ } ;
1992
+ if( (drop == 1 && method == 1) ) {
1993
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
1994
+ cholesky_delete(l,idx[r],na,kmax) ;
1995
+ } ;
1996
+ na = (na-1) ;
1997
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
1998
+ int ii = idx[r];
1999
+ int dropped = active[ii];
2000
+ for( int i = ii ; !(i >= na) ; i = (i+1) ) {
2001
+ active[ i ] = active[(i+1)] ;
2002
+ } ;
2003
+ dropv[ r ] = dropped ;
2004
+ ;
2005
+ ;
2006
+ } ;
2007
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
2008
+ int ii = idx[r];
2009
+ for( int i = ii ; !(i >= na) ; i = (i+1) ) {
2010
+ int ti = index[i];
2011
+ index[ i ] = index[(i+1)] ;
2012
+ index[ (i+1) ] = ti ;
2013
+ ;
2014
+ swap_rc(g,i,int((i+1)),p) ;
2015
+ } ;
2016
+ ;
2017
+ } ;
2018
+ for( int rr = 0 ; !(rr == nidx) ; rr = (rr+1) ) {
2019
+ int r = ((nidx-1)-rr);
2020
+ int d = dropv[r];
2021
+ double t = cov0[d];
2022
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
2023
+ t = (t-(g0[((d*p)+j)]*coef[j])) ;
2024
+ } ;
2025
+ cs = (cs-1) ;
2026
+ cbuf[ cs ] = t ;
2027
+ ;
2028
+ ;
2029
+ ;
2030
+ } ;
2031
+ for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
2032
+ int ii = idx[r];
2033
+ for( int i = ii ; !(i >= (p-1)) ; i = (i+1) ) {
2034
+ sign[ i ] = sign[(i+1)] ;
2035
+ } ;
2036
+ sign[ (p-1) ] = 0.0 ;
2037
+ ;
2038
+ } ;
2039
+ } else {
2040
+ 0 ;
2041
+ } ;
2042
+ ;
2043
+ ;
2044
+ ;
2045
+ ;
2046
+ } ;
2047
+ } else {
2048
+ 0 ;
2049
+ } ;
2050
+ ;
2051
+ } ;
2052
+ } ;
2053
+ ;
2054
+ } ;
2055
+ ;
2056
+ return niter ;
2057
+ }
2058
+
2059
+
2060
+ inline int
2061
+ svm_l1( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::span<double> w, double cost, double tol, int maxiter, int seed, int n, int p )
2062
+ { std::vector<int> index(p,0);
2063
+ std::vector<double> b(n,1.0);
2064
+ std::vector<double> xjsq(p,0.0);
2065
+ int rng = seed;
2066
+ int npos = 0;
2067
+ int iter = 0;
2068
+ int active = p;
2069
+ double gmaxold = 1e+300;
2070
+ double ginit = -1.0;
2071
+ int done = 0;
2072
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2073
+ if( y[i] > 0.0 ) {
2074
+ npos = (npos+1) ;
2075
+ } else {
2076
+ 0 ;
2077
+ } ;
2078
+ } ;
2079
+
2080
+ struct let24389 {
2081
+ let24389() {}
2082
+ int operator()( int mn )
2083
+ {
2084
+ if( mn < 1 ) {
2085
+ return 1 ;
2086
+ }
2087
+ else {
2088
+ return mn ;
2089
+ }
2090
+ }
2091
+ } ;
2092
+ double eps = ((tol*double(let24389()(( ( npos < (n-npos) ) ? (npos) : ((n-npos)) ))))/double(n));
2093
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
2094
+ w[ j ] = 0.0 ;
2095
+ index[ j ] = j ;
2096
+ double acc = 0.0;
2097
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2098
+ double val = (x[((j*n)+i)]*y[i]);
2099
+ acc = (acc+(cost*(val*val))) ;
2100
+ ;
2101
+ } ;
2102
+ xjsq[ j ] = acc ;
2103
+ ;
2104
+ } ;
2105
+ for( ; !((done == 1 || iter >= maxiter)) ; ) {
2106
+ double gmaxnew = 0.0;
2107
+ double gnorm1new = 0.0;
2108
+ int s = 0;
2109
+ for( int j = 0 ; !(j == active) ; j = (j+1) ) {
2110
+ rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
2111
+ if( rng < 0 ) {
2112
+ rng = (rng+2147483647) ;
2113
+ } else {
2114
+ 0 ;
2115
+ } ;
2116
+ int k = (j+(rng % (active-j)));
2117
+ int tmp = index[k];
2118
+ index[ k ] = index[j] ;
2119
+ index[ j ] = tmp ;
2120
+ ;
2121
+ ;
2122
+ } ;
2123
+ for( ; !(s >= active) ; ) {
2124
+ int j = index[s];
2125
+ double gl = 0.0;
2126
+ double h = 0.0;
2127
+ int shrunk = 0;
2128
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2129
+ if( b[i] > 0.0 ) {
2130
+ double val = (x[((j*n)+i)]*y[i]);
2131
+ double tmp = (cost*val);
2132
+ gl = (gl-(tmp*b[i])) ;
2133
+ h = (h+(tmp*val)) ;
2134
+ ;
2135
+ ;
2136
+ } else {
2137
+ 0 ;
2138
+ } ;
2139
+ } ;
2140
+ gl = (2.0*gl) ;
2141
+ h = (2.0*h) ;
2142
+ if( h < 1e-12 ) {
2143
+ h = 1e-12 ;
2144
+ } else {
2145
+ 0 ;
2146
+ } ;
2147
+ double wj = w[j];
2148
+ double gp = (gl+1.0);
2149
+ double gn = (gl-1.0);
2150
+ double viol = 0.0;
2151
+ if( wj == 0.0 ) {
2152
+ if( gp < 0.0 ) {
2153
+ viol = (0.0-gp) ;
2154
+ } else {
2155
+ if( gn > 0.0 ) {
2156
+ viol = gn ;
2157
+ } else {
2158
+ if( (gp > (gmaxold/double(n)) && gn < (0.0-(gmaxold/double(n)))) ) {
2159
+ active = (active-1) ;
2160
+ int tmp2 = index[s];
2161
+ index[ s ] = index[active] ;
2162
+ index[ active ] = tmp2 ;
2163
+ ;
2164
+ shrunk = 1 ;
2165
+ } else {
2166
+ 0 ;
2167
+ } ;
2168
+ } ;
2169
+ } ;
2170
+ } else {
2171
+ viol = ( ( wj > 0.0 ) ? (svm_l1_abs(gp)) : (svm_l1_abs(gn)) ) ;
2172
+ } ;
2173
+ if( shrunk == 0 ) {
2174
+ if( viol > gmaxnew ) {
2175
+ gmaxnew = viol ;
2176
+ } else {
2177
+ 0 ;
2178
+ } ;
2179
+ gnorm1new = (gnorm1new+viol) ;
2180
+ double d = ( ( gp < (h*wj) ) ? (((0.0-gp)/h)) : (( ( gn > (h*wj) ) ? (((0.0-gn)/h)) : ((0.0-wj)) )) );
2181
+ if( svm_l1_abs(d) >= 1e-12 ) {
2182
+ double delta = ((svm_l1_abs(double((wj+d)))-svm_l1_abs(wj))+(gl*d));
2183
+ double dold = 0.0;
2184
+ int nls = 0;
2185
+ int fin = 0;
2186
+ double lossold = 0.0;
2187
+ for( ; !((fin == 1 || nls >= 20)) ; ) {
2188
+ double ddiff = (dold-d);
2189
+ double cnd = ((svm_l1_abs(double((wj+d)))-svm_l1_abs(wj))-(0.01*delta));
2190
+ if( (((xjsq[j]*(d*d))+(gl*d))+cnd) <= 0.0 ) {
2191
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2192
+ b[ i ] = (b[i]+(ddiff*(x[((j*n)+i)]*y[i]))) ;
2193
+ } ;
2194
+ fin = 1 ;
2195
+ } else {
2196
+ double lossnew = 0.0;
2197
+ if( nls == 0 ) {
2198
+ lossold = 0.0 ;
2199
+ } else {
2200
+ 0 ;
2201
+ } ;
2202
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2203
+ double bi = b[i];
2204
+ if( (nls == 0 && bi > 0.0) ) {
2205
+ lossold = (lossold+(cost*(bi*bi))) ;
2206
+ } else {
2207
+ 0 ;
2208
+ } ;
2209
+ double bnew = (bi+(ddiff*(x[((j*n)+i)]*y[i])));
2210
+ b[ i ] = bnew ;
2211
+ if( bnew > 0.0 ) {
2212
+ lossnew = (lossnew+(cost*(bnew*bnew))) ;
2213
+ } else {
2214
+ 0 ;
2215
+ } ;
2216
+ ;
2217
+ ;
2218
+ } ;
2219
+ if( (cnd+(lossnew-lossold)) <= 0.0 ) {
2220
+ fin = 1 ;
2221
+ } else {
2222
+ dold = d ;
2223
+ d = (0.5*d) ;
2224
+ delta = (0.5*delta) ;
2225
+ nls = (nls+1) ;
2226
+ } ;
2227
+ ;
2228
+ } ;
2229
+ ;
2230
+ } ;
2231
+ w[ j ] = (wj+d) ;
2232
+ if( nls >= 20 ) {
2233
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2234
+ b[ i ] = 1.0 ;
2235
+ } ;
2236
+ for( int jj = 0 ; !(jj == p) ; jj = (jj+1) ) {
2237
+ if( !(w[jj] == 0.0) ) {
2238
+ for( int i = 0 ; !(i == n) ; i = (i+1) ) {
2239
+ b[ i ] = (b[i]-(w[jj]*(x[((jj*n)+i)]*y[i]))) ;
2240
+ } ;
2241
+ } else {
2242
+ 0 ;
2243
+ } ;
2244
+ } ;
2245
+ } else {
2246
+ 0 ;
2247
+ } ;
2248
+ ;
2249
+ } else {
2250
+ 0 ;
2251
+ } ;
2252
+ ;
2253
+ s = (s+1) ;
2254
+ } else {
2255
+ 0 ;
2256
+ } ;
2257
+ ;
2258
+ ;
2259
+ } ;
2260
+ if( iter == 0 ) {
2261
+ ginit = gnorm1new ;
2262
+ } else {
2263
+ 0 ;
2264
+ } ;
2265
+ iter = (iter+1) ;
2266
+ if( gnorm1new <= (eps*ginit) ) {
2267
+ if( active == p ) {
2268
+ done = 1 ;
2269
+ } else {
2270
+ active = p ;
2271
+ gmaxold = 1e+300 ;
2272
+ } ;
2273
+ } else {
2274
+ gmaxold = gmaxnew ;
2275
+ } ;
2276
+ ;
2277
+ } ;
2278
+ ;
2279
+ return iter ;
2280
+ }
2281
+
2282
+
2283
+ SCM2CPP_FN inline double
2284
+ svm_l1_abs( double a )
2285
+ {
2286
+ if( a < 0.0 ) {
2287
+ return (0.0-a) ;
2288
+ }
2289
+ else {
2290
+ return a ;
2291
+ }
2292
+ }
2293
+
2294
+
2295
+ SCM2CPP_FN inline double
2296
+ eig_hypot( double a, double b )
2297
+ {
2298
+ double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
2299
+ double bb = ( ( b < 0.0 ) ? ((0.0-b)) : (b) );
2300
+ if( aa > bb ) {
2301
+ return (aa*sqrt((1.0+((bb/aa)*(bb/aa))))) ;
2302
+ }
2303
+ else {
2304
+ if( bb == 0.0 ) {
2305
+ return 0.0 ;
2306
+ }
2307
+ else {
2308
+ return (bb*sqrt((1.0+((aa/bb)*(aa/bb))))) ;
2309
+ }
2310
+ }
2311
+ }
2312
+
2313
+
2314
+ SCM2CPP_FN inline int
2315
+ sym_eigen( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m )
2316
+ { for( int ii = 0 ; !(ii == (m-1)) ; ii = (ii+1) ) {
2317
+ int i = ((m-1)-ii);
2318
+ int l = (i-1);
2319
+ double h = 0.0;
2320
+ double scale = 0.0;
2321
+ if( l > 0 ) {
2322
+ for( int k = 0 ; !(k > l) ; k = (k+1) ) {
2323
+ double ak = a[((i*m)+k)];
2324
+ scale = (scale+( ( ak < 0.0 ) ? ((0.0-ak)) : (ak) )) ;
2325
+ ;
2326
+ } ;
2327
+ if( scale == 0.0 ) {
2328
+ e[ i ] = a[((i*m)+l)] ;
2329
+ } else {
2330
+ for( int k = 0 ; !(k > l) ; k = (k+1) ) {
2331
+ a[ ((i*m)+k) ] = (a[((i*m)+k)]/scale) ;
2332
+ h = (h+(a[((i*m)+k)]*a[((i*m)+k)])) ;
2333
+ } ;
2334
+ double f = a[((i*m)+l)];
2335
+ double g = ( ( f >= 0.0 ) ? ((0.0-sqrt(h))) : (sqrt(h)) );
2336
+ e[ i ] = (scale*g) ;
2337
+ h = (h-(f*g)) ;
2338
+ a[ ((i*m)+l) ] = (f-g) ;
2339
+ double ff = 0.0;
2340
+ for( int j = 0 ; !(j > l) ; j = (j+1) ) {
2341
+ a[ ((j*m)+i) ] = (a[((i*m)+j)]/h) ;
2342
+ double gg = 0.0;
2343
+ for( int k = 0 ; !(k > j) ; k = (k+1) ) {
2344
+ gg = (gg+(a[((j*m)+k)]*a[((i*m)+k)])) ;
2345
+ } ;
2346
+ for( int k = (j+1) ; !(k > l) ; k = (k+1) ) {
2347
+ gg = (gg+(a[((k*m)+j)]*a[((i*m)+k)])) ;
2348
+ } ;
2349
+ e[ j ] = (gg/h) ;
2350
+ ff = (ff+(e[j]*a[((i*m)+j)])) ;
2351
+ ;
2352
+ } ;
2353
+ double hh = (ff/(h+h));
2354
+ for( int j = 0 ; !(j > l) ; j = (j+1) ) {
2355
+ double f2 = a[((i*m)+j)];
2356
+ double g2 = (e[j]-(hh*f2));
2357
+ e[ j ] = g2 ;
2358
+ for( int k = 0 ; !(k > j) ; k = (k+1) ) {
2359
+ a[ ((j*m)+k) ] = (a[((j*m)+k)]-((f2*e[k])+(g2*a[((i*m)+k)]))) ;
2360
+ } ;
2361
+ ;
2362
+ ;
2363
+ } ;
2364
+ ;
2365
+ ;
2366
+ ;
2367
+ ;
2368
+ } ;
2369
+ } else {
2370
+ e[ i ] = a[((i*m)+l)] ;
2371
+ } ;
2372
+ ev[ i ] = h ;
2373
+ ;
2374
+ ;
2375
+ } ;
2376
+ ev[ 0 ] = 0.0 ;
2377
+ e[ 0 ] = 0.0 ;
2378
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
2379
+ if( !(ev[i] == 0.0) ) {
2380
+ for( int j = 0 ; !(j == i) ; j = (j+1) ) {
2381
+ double g3 = 0.0;
2382
+ for( int k = 0 ; !(k == i) ; k = (k+1) ) {
2383
+ g3 = (g3+(a[((i*m)+k)]*a[((k*m)+j)])) ;
2384
+ } ;
2385
+ for( int k = 0 ; !(k == i) ; k = (k+1) ) {
2386
+ a[ ((k*m)+j) ] = (a[((k*m)+j)]-(g3*a[((k*m)+i)])) ;
2387
+ } ;
2388
+ ;
2389
+ } ;
2390
+ } else {
2391
+ 0 ;
2392
+ } ;
2393
+ ev[ i ] = a[((i*m)+i)] ;
2394
+ a[ ((i*m)+i) ] = 1.0 ;
2395
+ for( int j = 0 ; !(j == i) ; j = (j+1) ) {
2396
+ a[ ((j*m)+i) ] = 0.0 ;
2397
+ a[ ((i*m)+j) ] = 0.0 ;
2398
+ } ;
2399
+ } ;
2400
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
2401
+ for( int j = 0 ; !(j == m) ; j = (j+1) ) {
2402
+ v[ ((j*m)+i) ] = a[((i*m)+j)] ;
2403
+ } ;
2404
+ } ;
2405
+ for( int i = 1 ; !(i == m) ; i = (i+1) ) {
2406
+ e[ (i-1) ] = e[i] ;
2407
+ } ;
2408
+ e[ (m-1) ] = 0.0 ;
2409
+ for( int l = 0 ; !(l == m) ; l = (l+1) ) {
2410
+ int fin = 0;
2411
+ int iter = 0;
2412
+ for( ; !((fin == 1 || iter > 60)) ; ) {
2413
+ int mm = l;
2414
+ int found = 0;
2415
+ for( ; !((found == 1 || mm >= (m-1))) ; ) {
2416
+ double dd = (eig_abs(ev[mm])+eig_abs(ev[(mm+1)]));
2417
+ if( (eig_abs(e[mm])+dd) == dd ) {
2418
+ found = 1 ;
2419
+ } else {
2420
+ mm = (mm+1) ;
2421
+ } ;
2422
+ ;
2423
+ } ;
2424
+ if( mm == l ) {
2425
+ fin = 1 ;
2426
+ } else {
2427
+ iter = (iter+1) ;
2428
+ double g = ((ev[(l+1)]-ev[l])/(2.0*e[l]));
2429
+ double r = eig_hypot(g,1.0);
2430
+ double g4 = ((ev[mm]-ev[l])+(e[l]/(g+( ( g >= 0.0 ) ? (eig_abs(r)) : ((0.0-eig_abs(r))) ))));
2431
+ double s = 1.0;
2432
+ double c = 1.0;
2433
+ double pp = 0.0;
2434
+ double gq = g4;
2435
+ int under = 0;
2436
+ int i = (mm-1);
2437
+ for( ; !((under == 1 || i < l)) ; ) {
2438
+ double f = (s*e[i]);
2439
+ double b = (c*e[i]);
2440
+ double r2 = eig_hypot(f,gq);
2441
+ e[ (i+1) ] = r2 ;
2442
+ if( r2 == 0.0 ) {
2443
+ ev[ (i+1) ] = (ev[(i+1)]-pp) ;
2444
+ e[ mm ] = 0.0 ;
2445
+ under = 1 ;
2446
+ } else {
2447
+ s = (f/r2) ;
2448
+ c = (gq/r2) ;
2449
+ double g5 = (ev[(i+1)]-pp);
2450
+ double r3 = (((ev[i]-g5)*s)+(2.0*(c*b)));
2451
+ pp = (s*r3) ;
2452
+ ev[ (i+1) ] = (g5+pp) ;
2453
+ gq = ((c*r3)-b) ;
2454
+ ;
2455
+ ;
2456
+ for( int k = 0 ; !(k == m) ; k = (k+1) ) {
2457
+ double zf = v[(((i+1)*m)+k)];
2458
+ double zi = v[((i*m)+k)];
2459
+ v[ (((i+1)*m)+k) ] = ((s*zi)+(c*zf)) ;
2460
+ v[ ((i*m)+k) ] = ((c*zi)-(s*zf)) ;
2461
+ ;
2462
+ } ;
2463
+ i = (i-1) ;
2464
+ } ;
2465
+ ;
2466
+ ;
2467
+ } ;
2468
+ if( under == 0 ) {
2469
+ ev[ l ] = (ev[l]-pp) ;
2470
+ e[ l ] = gq ;
2471
+ e[ mm ] = 0.0 ;
2472
+ } else {
2473
+ 0 ;
2474
+ } ;
2475
+ ;
2476
+ ;
2477
+ ;
2478
+ ;
2479
+ } ;
2480
+ ;
2481
+ } ;
2482
+ ;
2483
+ } ;
2484
+ for( int i = 0 ; !(i == m) ; i = (i+1) ) {
2485
+ for( int j = (i+1) ; !(j >= m) ; j = (j+1) ) {
2486
+ double tmp = v[((i*m)+j)];
2487
+ v[ ((i*m)+j) ] = v[((j*m)+i)] ;
2488
+ v[ ((j*m)+i) ] = tmp ;
2489
+ ;
2490
+ } ;
2491
+ } ;
2492
+ return 0 ;
2493
+ }
2494
+
2495
+
2496
+ SCM2CPP_FN inline double
2497
+ eig_abs( double a )
2498
+ {
2499
+ if( a < 0.0 ) {
2500
+ return (0.0-a) ;
2501
+ }
2502
+ else {
2503
+ return a ;
2504
+ }
2505
+ }
2506
+
2507
+
2508
+ SCM2CPP_FN inline int
2509
+ eig_order( scm2cpp::cspan<double> ev, scm2cpp::span<int> ord, int m )
2510
+ { for( int j = 0 ; !(j == m) ; j = (j+1) ) {
2511
+ ord[ j ] = j ;
2512
+ } ;
2513
+ for( int j = 0 ; !(j == m) ; j = (j+1) ) {
2514
+ int best = j;
2515
+ for( int r = (j+1) ; !(r >= m) ; r = (r+1) ) {
2516
+ if( ev[ord[r]] > ev[ord[best]] ) {
2517
+ best = r ;
2518
+ } else {
2519
+ 0 ;
2520
+ } ;
2521
+ } ;
2522
+ int tmp = ord[j];
2523
+ ord[ j ] = ord[best] ;
2524
+ ord[ best ] = tmp ;
2525
+ ;
2526
+ ;
2527
+ } ;
2528
+ return 0 ;
2529
+ }
2530
+
2531
+
2532
+ SCM2CPP_FN inline int
2533
+ ts_minnorm( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::span<double> aa, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, scm2cpp::span<double> ab, int s, int ns, int fitint, int p, int d )
2534
+ { for( int t = 0 ; !(t == (d*d)) ; t = (t+1) ) {
2535
+ aa[ t ] = 0.0 ;
2536
+ } ;
2537
+ for( int t = 0 ; !(t == d) ; t = (t+1) ) {
2538
+ ab[ t ] = 0.0 ;
2539
+ } ;
2540
+ for( int r = 0 ; !(r == ns) ; r = (r+1) ) {
2541
+ int i = idx[((s*ns)+r)];
2542
+ for( int u = 0 ; !(u == d) ; u = (u+1) ) {
2543
+ double xu = ( ( u < fitint ) ? (1.0) : (x[((i*p)+(u-fitint))]) );
2544
+ ab[ u ] = (ab[u]+(xu*y[i])) ;
2545
+ for( int q = 0 ; !(q == d) ; q = (q+1) ) {
2546
+ double xq = ( ( q < fitint ) ? (1.0) : (x[((i*p)+(q-fitint))]) );
2547
+ aa[ ((u*d)+q) ] = (aa[((u*d)+q)]+(xu*xq)) ;
2548
+ ;
2549
+ } ;
2550
+ ;
2551
+ } ;
2552
+ ;
2553
+ } ;
2554
+ sym_eigen(aa,v,ev,e,d) ;
2555
+ double mx = 0.0;
2556
+ for( int r = 0 ; !(r == d) ; r = (r+1) ) {
2557
+ if( ts_abs(ev[r]) > mx ) {
2558
+ mx = ts_abs(ev[r]) ;
2559
+ } else {
2560
+ 0 ;
2561
+ } ;
2562
+ } ;
2563
+ double cut = (mx*(double(d)*2.220446049250313e-16));
2564
+ for( int u = 0 ; !(u == d) ; u = (u+1) ) {
2565
+ double acc = 0.0;
2566
+ for( int r = 0 ; !(r == d) ; r = (r+1) ) {
2567
+ if( ts_abs(ev[r]) > cut ) {
2568
+ double vab = 0.0;
2569
+ for( int q = 0 ; !(q == d) ; q = (q+1) ) {
2570
+ vab = (vab+(v[((q*d)+r)]*ab[q])) ;
2571
+ } ;
2572
+ acc = (acc+((v[((u*d)+r)]*vab)/ev[r])) ;
2573
+ ;
2574
+ } else {
2575
+ 0 ;
2576
+ } ;
2577
+ } ;
2578
+ w[ ((s*d)+u) ] = acc ;
2579
+ ;
2580
+ } ;
2581
+ ;
2582
+ ;
2583
+ return 0 ;
2584
+ }
2585
+
2586
+
2587
+ SCM2CPP_FN inline int
2588
+ ts_fit( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::span<double> a, scm2cpp::span<double> b, scm2cpp::span<double> aa, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, scm2cpp::span<double> ab, int s, int ns, int fitint, int p, int d )
2589
+ { int singular = 0;
2590
+ double scale = 0.0;
2591
+ if( ns == d ) {
2592
+ for( int r = 0 ; !(r == d) ; r = (r+1) ) {
2593
+ int i = idx[((s*ns)+r)];
2594
+ if( fitint == 1 ) {
2595
+ a[ (r*d) ] = 1.0 ;
2596
+ } else {
2597
+ 0 ;
2598
+ } ;
2599
+ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
2600
+ a[ ((r*d)+(j+fitint)) ] = x[((i*p)+j)] ;
2601
+ } ;
2602
+ b[ r ] = y[i] ;
2603
+ ;
2604
+ } ;
2605
+ for( int t = 0 ; !(t == (d*d)) ; t = (t+1) ) {
2606
+ if( ts_abs(a[t]) > scale ) {
2607
+ scale = ts_abs(a[t]) ;
2608
+ } else {
2609
+ 0 ;
2610
+ } ;
2611
+ } ;
2612
+ for( int c = 0 ; !(c == d) ; c = (c+1) ) {
2613
+ int piv = c;
2614
+ double big = ts_abs(a[((c*d)+c)]);
2615
+ for( int r = (c+1) ; !(r >= d) ; r = (r+1) ) {
2616
+ if( ts_abs(a[((r*d)+c)]) > big ) {
2617
+ big = ts_abs(a[((r*d)+c)]) ;
2618
+ piv = r ;
2619
+ } else {
2620
+ 0 ;
2621
+ } ;
2622
+ } ;
2623
+ if( big <= (scale*(double(d)*2.220446049250313e-16)) ) {
2624
+ singular = 1 ;
2625
+ } else {
2626
+ if( !(piv == c) ) {
2627
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2628
+ double tmp = a[((c*d)+j)];
2629
+ a[ ((c*d)+j) ] = a[((piv*d)+j)] ;
2630
+ a[ ((piv*d)+j) ] = tmp ;
2631
+ ;
2632
+ } ;
2633
+ double tb = b[c];
2634
+ b[ c ] = b[piv] ;
2635
+ b[ piv ] = tb ;
2636
+ ;
2637
+ } else {
2638
+ 0 ;
2639
+ } ;
2640
+ for( int r = (c+1) ; !(r >= d) ; r = (r+1) ) {
2641
+ double fct = (a[((r*d)+c)]/a[((c*d)+c)]);
2642
+ for( int j = c ; !(j == d) ; j = (j+1) ) {
2643
+ a[ ((r*d)+j) ] = (a[((r*d)+j)]-(fct*a[((c*d)+j)])) ;
2644
+ } ;
2645
+ b[ r ] = (b[r]-(fct*b[c])) ;
2646
+ ;
2647
+ } ;
2648
+ } ;
2649
+ ;
2650
+ } ;
2651
+ if( singular == 0 ) {
2652
+ for( int cc = 0 ; !(cc == d) ; cc = (cc+1) ) {
2653
+ int c = ((d-1)-cc);
2654
+ double t = b[c];
2655
+ for( int j = (c+1) ; !(j >= d) ; j = (j+1) ) {
2656
+ t = (t-(a[((c*d)+j)]*w[((s*d)+j)])) ;
2657
+ } ;
2658
+ w[ ((s*d)+c) ] = (t/a[((c*d)+c)]) ;
2659
+ ;
2660
+ ;
2661
+ } ;
2662
+ } else {
2663
+ 0 ;
2664
+ } ;
2665
+ } else {
2666
+ singular = 1 ;
2667
+ } ;
2668
+ if( singular == 1 ) {
2669
+ ts_minnorm(x,y,idx,w,aa,v,ev,e,ab,s,ns,fitint,p,d) ;
2670
+ } else {
2671
+ 0 ;
2672
+ } ;
2673
+ ;
2674
+ return 0 ;
2675
+ }
2676
+
2677
+
2678
+ SCM2CPP_FN inline double
2679
+ ts_abs( double v )
2680
+ {
2681
+ if( v < 0.0 ) {
2682
+ return (0.0-v) ;
2683
+ }
2684
+ else {
2685
+ return v ;
2686
+ }
2687
+ }
2688
+
2689
+
2690
+ inline int
2691
+ theilsen( scm2cpp::cspan<double> x, scm2cpp::cspan<double> y, scm2cpp::cspan<int> idx, scm2cpp::span<double> w, scm2cpp::span<double> coef, int nsub, int ns, int fitint, int maxiter, double tol, int p )
2692
+ { int d = (p+fitint);
2693
+ int it = 0;
2694
+ std::vector<double> a((d*d),0.0);
2695
+ std::vector<double> b(d,0.0);
2696
+ std::vector<double> aa((d*d),0.0);
2697
+ std::vector<double> v((d*d),0.0);
2698
+ std::vector<double> ev(d,0.0);
2699
+ std::vector<double> e(d,0.0);
2700
+ std::vector<double> ab(d,0.0);
2701
+ std::vector<double> old(d,0.0);
2702
+ std::vector<double> num(d,0.0);
2703
+ std::vector<double> qv(d,0.0);
2704
+ double eps = 2.220446049250313e-16;
2705
+ for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
2706
+ ts_fit(x,y,idx,w,a,b,aa,v,ev,e,ab,s,ns,fitint,p,d) ;
2707
+ } ;
2708
+ if( d == 1 ) {
2709
+ std::vector<double> v(nsub,0.0);
2710
+ for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
2711
+ v[ s ] = w[s] ;
2712
+ } ;
2713
+ for( int s = 1 ; !(s == nsub) ; s = (s+1) ) {
2714
+ double key = v[s];
2715
+ int j = (s-1);
2716
+ for( ; !((j < 0 || v[j] <= key)) ; ) {
2717
+ v[ (j+1) ] = v[j] ;
2718
+ j = (j-1) ;
2719
+ } ;
2720
+ v[ (j+1) ] = key ;
2721
+ ;
2722
+ } ;
2723
+ coef[ 0 ] = ( ( (nsub % 2) == 1 ) ? (v[(nsub / 2)]) : ((0.5*(v[((nsub / 2)-1)]+v[(nsub / 2)]))) ) ;
2724
+ it = 1 ;
2725
+ ;
2726
+ } else {
2727
+ double tol2 = (tol*tol);
2728
+ int done = 0;
2729
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2730
+ double acc = 0.0;
2731
+ for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
2732
+ acc = (acc+w[((s*d)+j)]) ;
2733
+ } ;
2734
+ old[ j ] = (acc/double(nsub)) ;
2735
+ ;
2736
+ } ;
2737
+ for( ; !((done == 1 || it >= maxiter)) ; ) {
2738
+ int inx = 0;
2739
+ double sinv = 0.0;
2740
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2741
+ num[ j ] = 0.0 ;
2742
+ qv[ j ] = 0.0 ;
2743
+ } ;
2744
+ for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
2745
+ double dn = 0.0;
2746
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2747
+ double df = (w[((s*d)+j)]-old[j]);
2748
+ dn = (dn+(df*df)) ;
2749
+ ;
2750
+ } ;
2751
+ dn = sqrt(dn) ;
2752
+ if( dn >= eps ) {
2753
+ sinv = (sinv+(1.0/dn)) ;
2754
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2755
+ qv[ j ] = (qv[j]+((w[((s*d)+j)]-old[j])/dn)) ;
2756
+ num[ j ] = (num[j]+(w[((s*d)+j)]/dn)) ;
2757
+ } ;
2758
+ } else {
2759
+ inx = 1 ;
2760
+ } ;
2761
+ ;
2762
+ } ;
2763
+ double qn = 0.0;
2764
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2765
+ qn = (qn+(qv[j]*qv[j])) ;
2766
+ } ;
2767
+ qn = sqrt(qn) ;
2768
+ int usedir = ( ( qn > eps ) ? (1) : (0) );
2769
+ double qn2 = ( ( usedir == 1 ) ? (qn) : (1.0) );
2770
+ double moved = 0.0;
2771
+ struct let24455 {
2772
+ let24455() {}
2773
+ double operator()( double t5 )
2774
+ {
2775
+ if( t5 > 0.0 ) {
2776
+ return t5 ;
2777
+ }
2778
+ else {
2779
+ return 0.0 ;
2780
+ }
2781
+ }
2782
+ } ;
2783
+
2784
+ struct let24477 {
2785
+ let24477() {}
2786
+ double operator()( double t6 )
2787
+ {
2788
+ if( t6 < 1.0 ) {
2789
+ return t6 ;
2790
+ }
2791
+ else {
2792
+ return 1.0 ;
2793
+ }
2794
+ }
2795
+ } ;
2796
+ double wa = let24455()((1.0-(double(inx)/qn2)));
2797
+ double wb = let24477()((double(inx)/qn2));
2798
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2799
+ double nd = ( ( usedir == 1 ) ? ((num[j]/sinv)) : (1.0) );
2800
+ double nw = ((wa*nd)+(wb*old[j]));
2801
+ moved = (moved+((old[j]-nw)*(old[j]-nw))) ;
2802
+ coef[ j ] = nw ;
2803
+ ;
2804
+ ;
2805
+ } ;
2806
+ ;
2807
+ if( moved < tol2 ) {
2808
+ done = 1 ;
2809
+ } else {
2810
+ for( int j = 0 ; !(j == d) ; j = (j+1) ) {
2811
+ old[ j ] = coef[j] ;
2812
+ } ;
2813
+ it = (it+1) ;
2814
+ } ;
2815
+ ;
2816
+ ;
2817
+ ;
2818
+ ;
2819
+ } ;
2820
+ ;
2821
+ } ;
2822
+ ;
2823
+ return it ;
2824
+ }
2825
+ #endif // CLASSIFIERS_HPP