scm2cpp-learn 0.18.0__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.
- scm2cpp_learn/__init__.py +3836 -0
- scm2cpp_learn/_classifiers.cp312-win_amd64.pyd +0 -0
- scm2cpp_learn/_compat.py +284 -0
- scm2cpp_learn/_generated/__init__.py +0 -0
- scm2cpp_learn/_generated/_loader.py +41 -0
- scm2cpp_learn/_generated/_loader_accel_cublas.py +173 -0
- scm2cpp_learn/_generated/_loader_classifiers.py +308 -0
- scm2cpp_learn/_generated/_loader_derived_blas.py +29 -0
- scm2cpp_learn/_generated/_loader_derived_cublas.py +29 -0
- scm2cpp_learn/_generated/_loader_derived_loops.py +29 -0
- scm2cpp_learn/_generated/_loader_gpu_cuda.py +313 -0
- scm2cpp_learn/_generated/_loader_gramops_blas.py +692 -0
- scm2cpp_learn/_generated/_loader_gramops_loops.py +711 -0
- scm2cpp_learn/_generated/_loader_nmf_blas.py +29 -0
- scm2cpp_learn/_generated/_loader_nmf_cublas.py +29 -0
- scm2cpp_learn/_generated/_loader_nmf_loops.py +29 -0
- scm2cpp_learn/_generated/accel_cublas/accel.cpp +24 -0
- scm2cpp_learn/_generated/accel_cublas/accel.hpp +1178 -0
- scm2cpp_learn/_generated/accel_cublas/accel_capi.cpp +76 -0
- scm2cpp_learn/_generated/classifiers.cpp +37 -0
- scm2cpp_learn/_generated/classifiers.hpp +3745 -0
- scm2cpp_learn/_generated/classifiers_capi.cpp +115 -0
- scm2cpp_learn/_generated/derived_blas/lasso_design.cpp +5 -0
- scm2cpp_learn/_generated/derived_blas/lasso_design.hpp +156 -0
- scm2cpp_learn/_generated/derived_blas/lasso_design_capi.cpp +19 -0
- scm2cpp_learn/_generated/derived_cublas/lasso_design.cpp +5 -0
- scm2cpp_learn/_generated/derived_cublas/lasso_design.hpp +158 -0
- scm2cpp_learn/_generated/derived_cublas/lasso_design_capi.cpp +19 -0
- scm2cpp_learn/_generated/derived_loops/lasso_design.cpp +5 -0
- scm2cpp_learn/_generated/derived_loops/lasso_design.hpp +179 -0
- scm2cpp_learn/_generated/derived_loops/lasso_design_capi.cpp +19 -0
- scm2cpp_learn/_generated/gpu_cuda/gpu.cpp +37 -0
- scm2cpp_learn/_generated/gpu_cuda/gpu.hpp +3846 -0
- scm2cpp_learn/_generated/gpu_cuda/gpu_capi.cpp +308 -0
- scm2cpp_learn/_generated/gramops_blas/gramops.cpp +82 -0
- scm2cpp_learn/_generated/gramops_blas/gramops.hpp +5411 -0
- scm2cpp_learn/_generated/gramops_blas/gramops_capi.cpp +244 -0
- scm2cpp_learn/_generated/gramops_loops/gramops.cpp +82 -0
- scm2cpp_learn/_generated/gramops_loops/gramops.hpp +6395 -0
- scm2cpp_learn/_generated/gramops_loops/gramops_capi.cpp +250 -0
- scm2cpp_learn/_generated/lasso_cov.cpp +7 -0
- scm2cpp_learn/_generated/lasso_cov.hpp +152 -0
- scm2cpp_learn/_generated/lasso_cov_capi.cpp +25 -0
- scm2cpp_learn/_generated/nmf_blas/nmf_cd.cpp +5 -0
- scm2cpp_learn/_generated/nmf_blas/nmf_cd.hpp +132 -0
- scm2cpp_learn/_generated/nmf_blas/nmf_cd_capi.cpp +19 -0
- scm2cpp_learn/_generated/nmf_cublas/nmf_cd.cpp +5 -0
- scm2cpp_learn/_generated/nmf_cublas/nmf_cd.hpp +136 -0
- scm2cpp_learn/_generated/nmf_cublas/nmf_cd_capi.cpp +19 -0
- scm2cpp_learn/_generated/nmf_loops/nmf_cd.cpp +5 -0
- scm2cpp_learn/_generated/nmf_loops/nmf_cd.hpp +167 -0
- scm2cpp_learn/_generated/nmf_loops/nmf_cd_capi.cpp +19 -0
- scm2cpp_learn/_generated/scm2cpp-blas.hpp +158 -0
- scm2cpp_learn/_generated/scm2cpp-cublas.hpp +155 -0
- scm2cpp_learn/_generated/scm2cpp-cuda.hpp +285 -0
- scm2cpp_learn/_generated/scm2cpp.hpp +1627 -0
- scm2cpp_learn/_gramops_blas.cp312-win_amd64.pyd +0 -0
- scm2cpp_learn/_lasso_cov.cp312-win_amd64.pyd +0 -0
- scm2cpp_learn/_lasso_derived_blas.cp312-win_amd64.pyd +0 -0
- scm2cpp_learn/_libfind.py +153 -0
- scm2cpp_learn/_nmf_blas.cp312-win_amd64.pyd +0 -0
- scm2cpp_learn/batch_capi.cu +428 -0
- scm2cpp_learn-0.18.0.dist-info/DELVEWHEEL +2 -0
- scm2cpp_learn-0.18.0.dist-info/METADATA +1171 -0
- scm2cpp_learn-0.18.0.dist-info/RECORD +72 -0
- scm2cpp_learn-0.18.0.dist-info/WHEEL +5 -0
- scm2cpp_learn-0.18.0.dist-info/top_level.txt +1 -0
- scm2cpp_learn.libs/.load-order-scm2cpp_learn-0.18.0 +4 -0
- scm2cpp_learn.libs/libopenblas-0486735c359a67419d8832e3c40e2837.dll +4 -0
- scm2cpp_learn.libs/msvcp140-73438573e87f9a2fd944b01def13566d.dll +0 -0
- scm2cpp_learn.libs/vcruntime140-052ad6a20d375957e82aa6a3c441ea54.dll +0 -0
- scm2cpp_learn.libs/vcruntime140_1-a198370e11c6e7469d1cf0ef5b3f433d.dll +0 -0
|
@@ -0,0 +1,3745 @@
|
|
|
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
|
+
SCM2CPP_FN int
|
|
28
|
+
rbf_from_products( scm2cpp::span<double> g, scm2cpp::cspan<double> sa, scm2cpp::cspan<double> sb, double gamma, int na, int nb );
|
|
29
|
+
int
|
|
30
|
+
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 );
|
|
31
|
+
int
|
|
32
|
+
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 );
|
|
33
|
+
SCM2CPP_FN double
|
|
34
|
+
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 );
|
|
35
|
+
int
|
|
36
|
+
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 );
|
|
37
|
+
SCM2CPP_FN double
|
|
38
|
+
sgd_loss( int loss, double y, double p, double epsilon );
|
|
39
|
+
SCM2CPP_FN double
|
|
40
|
+
sgd_grad( int loss, double y, double p, double epsilon );
|
|
41
|
+
SCM2CPP_FN int
|
|
42
|
+
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 );
|
|
43
|
+
SCM2CPP_FN double
|
|
44
|
+
sgd_min( double a, double b );
|
|
45
|
+
SCM2CPP_FN double
|
|
46
|
+
min_pos( scm2cpp::cspan<double> v, int m );
|
|
47
|
+
SCM2CPP_FN double
|
|
48
|
+
round_even( double y );
|
|
49
|
+
SCM2CPP_FN int
|
|
50
|
+
cholesky_delete( scm2cpp::span<double> l, int go, int n, int k );
|
|
51
|
+
SCM2CPP_FN int
|
|
52
|
+
swap_rc( scm2cpp::span<double> g, int a, int b, int p );
|
|
53
|
+
int
|
|
54
|
+
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 );
|
|
55
|
+
int
|
|
56
|
+
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 );
|
|
57
|
+
SCM2CPP_FN double
|
|
58
|
+
svm_l1_abs( double a );
|
|
59
|
+
SCM2CPP_FN double
|
|
60
|
+
eig_hypot( double a, double b );
|
|
61
|
+
SCM2CPP_FN int
|
|
62
|
+
sym_eigen( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m );
|
|
63
|
+
SCM2CPP_FN double
|
|
64
|
+
eig_abs( double a );
|
|
65
|
+
SCM2CPP_FN int
|
|
66
|
+
eig_order( scm2cpp::cspan<double> ev, scm2cpp::span<int> ord, int m );
|
|
67
|
+
int
|
|
68
|
+
sym_eigen_top( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m, int k );
|
|
69
|
+
SCM2CPP_FN int
|
|
70
|
+
sym_eigen_gen( scm2cpp::span<double> a, scm2cpp::span<double> b, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m );
|
|
71
|
+
SCM2CPP_FN int
|
|
72
|
+
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 );
|
|
73
|
+
SCM2CPP_FN int
|
|
74
|
+
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 );
|
|
75
|
+
SCM2CPP_FN double
|
|
76
|
+
ts_abs( double v );
|
|
77
|
+
int
|
|
78
|
+
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 );
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
inline int
|
|
83
|
+
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 )
|
|
84
|
+
{ if( (n*p) > 65536 ) {
|
|
85
|
+
#pragma omp parallel for
|
|
86
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
87
|
+
double acc = 0.0;
|
|
88
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
89
|
+
acc = (acc+(x[((i*p)+j)]*x[((i*p)+j)])) ;
|
|
90
|
+
} ;
|
|
91
|
+
xnorm[ i ] = acc ;
|
|
92
|
+
;
|
|
93
|
+
}
|
|
94
|
+
} else {
|
|
95
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
96
|
+
double acc = 0.0;
|
|
97
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
98
|
+
acc = (acc+(x[((i*p)+j)]*x[((i*p)+j)])) ;
|
|
99
|
+
} ;
|
|
100
|
+
xnorm[ i ] = acc ;
|
|
101
|
+
;
|
|
102
|
+
}
|
|
103
|
+
} ;
|
|
104
|
+
std::vector<int> index(n,0);
|
|
105
|
+
int rng = seed;
|
|
106
|
+
int active = n;
|
|
107
|
+
double pgmaxold = 1e+300;
|
|
108
|
+
double pgminold = -1e+300;
|
|
109
|
+
int stop = 0;
|
|
110
|
+
int sweeps = 0;
|
|
111
|
+
if( n > 65536 ) {
|
|
112
|
+
#pragma omp parallel for
|
|
113
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
114
|
+
index[ i ] = i ;
|
|
115
|
+
}
|
|
116
|
+
} else {
|
|
117
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
118
|
+
index[ i ] = i ;
|
|
119
|
+
}
|
|
120
|
+
} ;
|
|
121
|
+
for( int sweep = 0 ; !((sweep == iters || stop == 1)) ; sweep = (sweep+1) ) {
|
|
122
|
+
double pgmax = -1e+300;
|
|
123
|
+
double pgmin = 1e+300;
|
|
124
|
+
for( int i = 0 ; !(i == active) ; i = (i+1) ) {
|
|
125
|
+
rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
|
|
126
|
+
if( rng < 0 ) {
|
|
127
|
+
rng = (rng+2147483647) ;
|
|
128
|
+
} else {
|
|
129
|
+
0 ;
|
|
130
|
+
} ;
|
|
131
|
+
int j = (i+(rng % (active-i)));
|
|
132
|
+
int t = index[i];
|
|
133
|
+
index[ i ] = index[j] ;
|
|
134
|
+
index[ j ] = t ;
|
|
135
|
+
;
|
|
136
|
+
} ;
|
|
137
|
+
int s = 0;
|
|
138
|
+
for( ; !(s >= active) ; ) {
|
|
139
|
+
int i = index[s];
|
|
140
|
+
double dot = 0.0;
|
|
141
|
+
double old = alpha[i];
|
|
142
|
+
if( p > 65536 ) {
|
|
143
|
+
#pragma omp parallel for reduction(+:dot)
|
|
144
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
145
|
+
dot = (dot+(x[((i*p)+j)]*w[j])) ;
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
149
|
+
dot = (dot+(x[((i*p)+j)]*w[j])) ;
|
|
150
|
+
}
|
|
151
|
+
} ;
|
|
152
|
+
double g = ((y[i]*dot)-1.0);
|
|
153
|
+
double pg = 0.0;
|
|
154
|
+
int shrunk = 0;
|
|
155
|
+
if( old == 0.0 ) {
|
|
156
|
+
if( g > pgmaxold ) {
|
|
157
|
+
shrunk = 1 ;
|
|
158
|
+
} else {
|
|
159
|
+
if( g < 0.0 ) {
|
|
160
|
+
pg = g ;
|
|
161
|
+
} else {
|
|
162
|
+
0 ;
|
|
163
|
+
} ;
|
|
164
|
+
} ;
|
|
165
|
+
} else {
|
|
166
|
+
if( old == cost ) {
|
|
167
|
+
if( g < pgminold ) {
|
|
168
|
+
shrunk = 1 ;
|
|
169
|
+
} else {
|
|
170
|
+
if( g > 0.0 ) {
|
|
171
|
+
pg = g ;
|
|
172
|
+
} else {
|
|
173
|
+
0 ;
|
|
174
|
+
} ;
|
|
175
|
+
} ;
|
|
176
|
+
} else {
|
|
177
|
+
pg = g ;
|
|
178
|
+
} ;
|
|
179
|
+
} ;
|
|
180
|
+
if( shrunk == 1 ) {
|
|
181
|
+
active = (active-1) ;
|
|
182
|
+
index[ s ] = index[active] ;
|
|
183
|
+
index[ active ] = i ;
|
|
184
|
+
} else {
|
|
185
|
+
if( pg > pgmax ) {
|
|
186
|
+
pgmax = pg ;
|
|
187
|
+
} else {
|
|
188
|
+
0 ;
|
|
189
|
+
} ;
|
|
190
|
+
if( pg < pgmin ) {
|
|
191
|
+
pgmin = pg ;
|
|
192
|
+
} else {
|
|
193
|
+
0 ;
|
|
194
|
+
} ;
|
|
195
|
+
if( (pg > 1e-12 || pg < -1e-12) ) {
|
|
196
|
+
double step = (old-(g/xnorm[i]));
|
|
197
|
+
double anew = ( ( step < 0.0 ) ? (0.0) : (( ( step > cost ) ? (cost) : (step) )) );
|
|
198
|
+
alpha[ i ] = anew ;
|
|
199
|
+
if( p > 65536 ) {
|
|
200
|
+
#pragma omp parallel for
|
|
201
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
202
|
+
w[ j ] = (w[j]+(x[((i*p)+j)]*((anew-old)*y[i]))) ;
|
|
203
|
+
}
|
|
204
|
+
} else {
|
|
205
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
206
|
+
w[ j ] = (w[j]+(x[((i*p)+j)]*((anew-old)*y[i]))) ;
|
|
207
|
+
}
|
|
208
|
+
} ;
|
|
209
|
+
;
|
|
210
|
+
;
|
|
211
|
+
} else {
|
|
212
|
+
0 ;
|
|
213
|
+
} ;
|
|
214
|
+
s = (s+1) ;
|
|
215
|
+
} ;
|
|
216
|
+
;
|
|
217
|
+
;
|
|
218
|
+
;
|
|
219
|
+
} ;
|
|
220
|
+
;
|
|
221
|
+
sweeps = (sweeps+1) ;
|
|
222
|
+
if( (pgmax-pgmin) <= tol ) {
|
|
223
|
+
if( active == n ) {
|
|
224
|
+
stop = 1 ;
|
|
225
|
+
} else {
|
|
226
|
+
active = n ;
|
|
227
|
+
pgmaxold = 1e+300 ;
|
|
228
|
+
pgminold = -1e+300 ;
|
|
229
|
+
} ;
|
|
230
|
+
} else {
|
|
231
|
+
pgmaxold = ( ( pgmax <= 0.0 ) ? (1e+300) : (pgmax) ) ;
|
|
232
|
+
pgminold = ( ( pgmin >= 0.0 ) ? (-1e+300) : (pgmin) ) ;
|
|
233
|
+
} ;
|
|
234
|
+
;
|
|
235
|
+
} ;
|
|
236
|
+
return sweeps ;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
inline int
|
|
241
|
+
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 )
|
|
242
|
+
{ std::vector<double> lin(n,-1.0);
|
|
243
|
+
std::vector<double> yv(n,0.0);
|
|
244
|
+
std::vector<double> av(n,0.0);
|
|
245
|
+
std::array<double,1> rv = scm2cpp::filled_array<double,1>(0.0);
|
|
246
|
+
if( n > 65536 ) {
|
|
247
|
+
#pragma omp parallel for
|
|
248
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
249
|
+
yv[ i ] = y[i] ;
|
|
250
|
+
}
|
|
251
|
+
} else {
|
|
252
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
253
|
+
yv[ i ] = y[i] ;
|
|
254
|
+
}
|
|
255
|
+
} ;
|
|
256
|
+
int it = smo_solve(k,yv,lin,av,rv,cost,iters,eps,n,n);
|
|
257
|
+
if( n > 65536 ) {
|
|
258
|
+
#pragma omp parallel for
|
|
259
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
260
|
+
alpha[ i ] = av[i] ;
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
264
|
+
alpha[ i ] = av[i] ;
|
|
265
|
+
}
|
|
266
|
+
} ;
|
|
267
|
+
rho[ 0 ] = rv[0] ;
|
|
268
|
+
return it ;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
inline int
|
|
273
|
+
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 )
|
|
274
|
+
{ std::vector<int> index(n,0);
|
|
275
|
+
std::vector<int> krow(n,0);
|
|
276
|
+
std::vector<double> pp(n,0.0);
|
|
277
|
+
std::vector<double> yy(n,0.0);
|
|
278
|
+
std::vector<double> a(n,0.0);
|
|
279
|
+
std::vector<double> g(n,0.0);
|
|
280
|
+
std::vector<double> gbar(n,0.0);
|
|
281
|
+
std::vector<double> qd(n,0.0);
|
|
282
|
+
int active = n;
|
|
283
|
+
int unshrink = 0;
|
|
284
|
+
int counter = (( ( n < 1000 ) ? (n) : (1000) )+1);
|
|
285
|
+
int iter = 0;
|
|
286
|
+
int done = 0;
|
|
287
|
+
if( n > 65536 ) {
|
|
288
|
+
#pragma omp parallel for
|
|
289
|
+
for( int t = 0 ; t < n ; t += 1 ) {
|
|
290
|
+
index[ t ] = t ;
|
|
291
|
+
krow[ t ] = (t % nk) ;
|
|
292
|
+
pp[ t ] = lin[t] ;
|
|
293
|
+
yy[ t ] = y[t] ;
|
|
294
|
+
g[ t ] = lin[t] ;
|
|
295
|
+
qd[ t ] = k[((krow[t]*nk)+krow[t])] ;
|
|
296
|
+
}
|
|
297
|
+
} else {
|
|
298
|
+
for( int t = 0 ; t < n ; t += 1 ) {
|
|
299
|
+
index[ t ] = t ;
|
|
300
|
+
krow[ t ] = (t % nk) ;
|
|
301
|
+
pp[ t ] = lin[t] ;
|
|
302
|
+
yy[ t ] = y[t] ;
|
|
303
|
+
g[ t ] = lin[t] ;
|
|
304
|
+
qd[ t ] = k[((krow[t]*nk)+krow[t])] ;
|
|
305
|
+
}
|
|
306
|
+
} ;
|
|
307
|
+
for( ; !((done == 1 || iter >= iters)) ; ) {
|
|
308
|
+
counter = (counter-1) ;
|
|
309
|
+
if( counter == 0 ) {
|
|
310
|
+
counter = ( ( n < 1000 ) ? (n) : (1000) ) ;
|
|
311
|
+
double gmax1 = -1e+300;
|
|
312
|
+
double gmax2 = -1e+300;
|
|
313
|
+
for( int t = 0 ; !(t == active) ; t = (t+1) ) {
|
|
314
|
+
double gt = g[t];
|
|
315
|
+
double at = a[t];
|
|
316
|
+
if( yy[t] > 0.0 ) {
|
|
317
|
+
if( (at < cost && (0.0-gt) > gmax1) ) {
|
|
318
|
+
gmax1 = (0.0-gt) ;
|
|
319
|
+
} else {
|
|
320
|
+
0 ;
|
|
321
|
+
} ;
|
|
322
|
+
if( (at > 0.0 && gt > gmax2) ) {
|
|
323
|
+
gmax2 = gt ;
|
|
324
|
+
} else {
|
|
325
|
+
0 ;
|
|
326
|
+
} ;
|
|
327
|
+
} else {
|
|
328
|
+
if( (at < cost && (0.0-gt) > gmax2) ) {
|
|
329
|
+
gmax2 = (0.0-gt) ;
|
|
330
|
+
} else {
|
|
331
|
+
0 ;
|
|
332
|
+
} ;
|
|
333
|
+
if( (at > 0.0 && gt > gmax1) ) {
|
|
334
|
+
gmax1 = gt ;
|
|
335
|
+
} else {
|
|
336
|
+
0 ;
|
|
337
|
+
} ;
|
|
338
|
+
} ;
|
|
339
|
+
;
|
|
340
|
+
} ;
|
|
341
|
+
if( (unshrink == 0 && (gmax1+gmax2) <= (eps*10.0)) ) {
|
|
342
|
+
unshrink = 1 ;
|
|
343
|
+
reconstruct_gradient(k,krow,pp,yy,a,g,gbar,cost,active,n,nk) ;
|
|
344
|
+
active = n ;
|
|
345
|
+
} else {
|
|
346
|
+
0 ;
|
|
347
|
+
} ;
|
|
348
|
+
int t = 0;
|
|
349
|
+
for( ; !(t >= active) ; ) {
|
|
350
|
+
if( shrinkable_P(yy,a,g,cost,gmax1,gmax2,t) == 1 ) {
|
|
351
|
+
active = (active-1) ;
|
|
352
|
+
int found = 0;
|
|
353
|
+
for( ; !((found == 1 || active <= t)) ; ) {
|
|
354
|
+
if( shrinkable_P(yy,a,g,cost,gmax1,gmax2,active) == 0 ) {
|
|
355
|
+
swap_position(index,krow,pp,yy,a,g,gbar,qd,t,active) ;
|
|
356
|
+
found = 1 ;
|
|
357
|
+
} else {
|
|
358
|
+
active = (active-1) ;
|
|
359
|
+
} ;
|
|
360
|
+
} ;
|
|
361
|
+
;
|
|
362
|
+
t = (t+1) ;
|
|
363
|
+
} else {
|
|
364
|
+
t = (t+1) ;
|
|
365
|
+
} ;
|
|
366
|
+
} ;
|
|
367
|
+
;
|
|
368
|
+
;
|
|
369
|
+
} else {
|
|
370
|
+
0 ;
|
|
371
|
+
} ;
|
|
372
|
+
std::array<int,2> wi = scm2cpp::filled_array<int,2>(0);
|
|
373
|
+
if( select_working_set(k,krow,yy,a,g,qd,cost,eps,active,nk,wi) == 1 ) {
|
|
374
|
+
reconstruct_gradient(k,krow,pp,yy,a,g,gbar,cost,active,n,nk) ;
|
|
375
|
+
active = n ;
|
|
376
|
+
if( select_working_set(k,krow,yy,a,g,qd,cost,eps,active,nk,wi) == 1 ) {
|
|
377
|
+
done = 1 ;
|
|
378
|
+
} else {
|
|
379
|
+
counter = 1 ;
|
|
380
|
+
} ;
|
|
381
|
+
} else {
|
|
382
|
+
0 ;
|
|
383
|
+
} ;
|
|
384
|
+
if( done == 0 ) {
|
|
385
|
+
int i = wi[0];
|
|
386
|
+
int j = wi[1];
|
|
387
|
+
iter = (iter+1) ;
|
|
388
|
+
double ai = a[i];
|
|
389
|
+
double aj = a[j];
|
|
390
|
+
double yi = yy[i];
|
|
391
|
+
double yj = yy[j];
|
|
392
|
+
int pi = krow[i];
|
|
393
|
+
int pj = krow[j];
|
|
394
|
+
double qij = ((yi*yj)*k[((pi*nk)+pj)]);
|
|
395
|
+
int ui = ( ( ai >= cost ) ? (1) : (0) );
|
|
396
|
+
int uj = ( ( aj >= cost ) ? (1) : (0) );
|
|
397
|
+
double ni = ai;
|
|
398
|
+
double nj = aj;
|
|
399
|
+
if( !(yi == yj) ) {
|
|
400
|
+
double quad = ((qd[i]+qd[j])+(2.0*qij));
|
|
401
|
+
if( quad <= 0.0 ) {
|
|
402
|
+
quad = 1e-12 ;
|
|
403
|
+
} else {
|
|
404
|
+
0 ;
|
|
405
|
+
} ;
|
|
406
|
+
double delta = (((0.0-g[i])-g[j])/quad);
|
|
407
|
+
double diff = (ai-aj);
|
|
408
|
+
ni = (ai+delta) ;
|
|
409
|
+
nj = (aj+delta) ;
|
|
410
|
+
if( diff > 0.0 ) {
|
|
411
|
+
if( nj < 0.0 ) {
|
|
412
|
+
nj = 0.0 ;
|
|
413
|
+
ni = diff ;
|
|
414
|
+
} else {
|
|
415
|
+
0 ;
|
|
416
|
+
} ;
|
|
417
|
+
} else {
|
|
418
|
+
if( ni < 0.0 ) {
|
|
419
|
+
ni = 0.0 ;
|
|
420
|
+
nj = (0.0-diff) ;
|
|
421
|
+
} else {
|
|
422
|
+
0 ;
|
|
423
|
+
} ;
|
|
424
|
+
} ;
|
|
425
|
+
if( diff > 0.0 ) {
|
|
426
|
+
if( ni > cost ) {
|
|
427
|
+
ni = cost ;
|
|
428
|
+
nj = (cost-diff) ;
|
|
429
|
+
} else {
|
|
430
|
+
0 ;
|
|
431
|
+
} ;
|
|
432
|
+
} else {
|
|
433
|
+
if( nj > cost ) {
|
|
434
|
+
nj = cost ;
|
|
435
|
+
ni = (cost+diff) ;
|
|
436
|
+
} else {
|
|
437
|
+
0 ;
|
|
438
|
+
} ;
|
|
439
|
+
} ;
|
|
440
|
+
;
|
|
441
|
+
;
|
|
442
|
+
} else {
|
|
443
|
+
double quad = ((qd[i]+qd[j])-(2.0*qij));
|
|
444
|
+
if( quad <= 0.0 ) {
|
|
445
|
+
quad = 1e-12 ;
|
|
446
|
+
} else {
|
|
447
|
+
0 ;
|
|
448
|
+
} ;
|
|
449
|
+
double delta = ((g[i]-g[j])/quad);
|
|
450
|
+
double sum = (ai+aj);
|
|
451
|
+
ni = (ai-delta) ;
|
|
452
|
+
nj = (aj+delta) ;
|
|
453
|
+
if( sum > cost ) {
|
|
454
|
+
if( ni > cost ) {
|
|
455
|
+
ni = cost ;
|
|
456
|
+
nj = (sum-cost) ;
|
|
457
|
+
} else {
|
|
458
|
+
0 ;
|
|
459
|
+
} ;
|
|
460
|
+
} else {
|
|
461
|
+
if( nj < 0.0 ) {
|
|
462
|
+
nj = 0.0 ;
|
|
463
|
+
ni = sum ;
|
|
464
|
+
} else {
|
|
465
|
+
0 ;
|
|
466
|
+
} ;
|
|
467
|
+
} ;
|
|
468
|
+
if( sum > cost ) {
|
|
469
|
+
if( nj > cost ) {
|
|
470
|
+
nj = cost ;
|
|
471
|
+
ni = (sum-cost) ;
|
|
472
|
+
} else {
|
|
473
|
+
0 ;
|
|
474
|
+
} ;
|
|
475
|
+
} else {
|
|
476
|
+
if( ni < 0.0 ) {
|
|
477
|
+
ni = 0.0 ;
|
|
478
|
+
nj = sum ;
|
|
479
|
+
} else {
|
|
480
|
+
0 ;
|
|
481
|
+
} ;
|
|
482
|
+
} ;
|
|
483
|
+
;
|
|
484
|
+
;
|
|
485
|
+
} ;
|
|
486
|
+
a[ i ] = ni ;
|
|
487
|
+
a[ j ] = nj ;
|
|
488
|
+
double di = (ni-ai);
|
|
489
|
+
double dj = (nj-aj);
|
|
490
|
+
if( active > 65536 ) {
|
|
491
|
+
#pragma omp parallel for
|
|
492
|
+
for( int t = 0 ; t < active ; t += 1 ) {
|
|
493
|
+
int pt = krow[t];
|
|
494
|
+
double yt = yy[t];
|
|
495
|
+
g[ t ] = (g[t]+(yt*(((yi*di)*k[((pi*nk)+pt)])+((yj*dj)*k[((pj*nk)+pt)])))) ;
|
|
496
|
+
;
|
|
497
|
+
}
|
|
498
|
+
} else {
|
|
499
|
+
for( int t = 0 ; t < active ; t += 1 ) {
|
|
500
|
+
int pt = krow[t];
|
|
501
|
+
double yt = yy[t];
|
|
502
|
+
g[ t ] = (g[t]+(yt*(((yi*di)*k[((pi*nk)+pt)])+((yj*dj)*k[((pj*nk)+pt)])))) ;
|
|
503
|
+
;
|
|
504
|
+
}
|
|
505
|
+
} ;
|
|
506
|
+
if( !(ui == ( ( ni >= cost ) ? (1) : (0) )) ) {
|
|
507
|
+
if( n > 65536 ) {
|
|
508
|
+
#pragma omp parallel for
|
|
509
|
+
for( int t = 0 ; t < n ; t += 1 ) {
|
|
510
|
+
gbar[ t ] = (gbar[t]+(( ( ui == 1 ) ? ((0.0-cost)) : (cost) )*((yi*yy[t])*k[((pi*nk)+krow[t])]))) ;
|
|
511
|
+
}
|
|
512
|
+
} else {
|
|
513
|
+
for( int t = 0 ; t < n ; t += 1 ) {
|
|
514
|
+
gbar[ t ] = (gbar[t]+(( ( ui == 1 ) ? ((0.0-cost)) : (cost) )*((yi*yy[t])*k[((pi*nk)+krow[t])]))) ;
|
|
515
|
+
}
|
|
516
|
+
} ;
|
|
517
|
+
} else {
|
|
518
|
+
0 ;
|
|
519
|
+
} ;
|
|
520
|
+
if( !(uj == ( ( nj >= cost ) ? (1) : (0) )) ) {
|
|
521
|
+
if( n > 65536 ) {
|
|
522
|
+
#pragma omp parallel for
|
|
523
|
+
for( int t = 0 ; t < n ; t += 1 ) {
|
|
524
|
+
gbar[ t ] = (gbar[t]+(( ( uj == 1 ) ? ((0.0-cost)) : (cost) )*((yj*yy[t])*k[((pj*nk)+krow[t])]))) ;
|
|
525
|
+
}
|
|
526
|
+
} else {
|
|
527
|
+
for( int t = 0 ; t < n ; t += 1 ) {
|
|
528
|
+
gbar[ t ] = (gbar[t]+(( ( uj == 1 ) ? ((0.0-cost)) : (cost) )*((yj*yy[t])*k[((pj*nk)+krow[t])]))) ;
|
|
529
|
+
}
|
|
530
|
+
} ;
|
|
531
|
+
} else {
|
|
532
|
+
0 ;
|
|
533
|
+
} ;
|
|
534
|
+
;
|
|
535
|
+
;
|
|
536
|
+
;
|
|
537
|
+
;
|
|
538
|
+
} else {
|
|
539
|
+
0 ;
|
|
540
|
+
} ;
|
|
541
|
+
;
|
|
542
|
+
} ;
|
|
543
|
+
if( active < n ) {
|
|
544
|
+
reconstruct_gradient(k,krow,pp,yy,a,g,gbar,cost,active,n,nk) ;
|
|
545
|
+
active = n ;
|
|
546
|
+
} else {
|
|
547
|
+
0 ;
|
|
548
|
+
} ;
|
|
549
|
+
int nfree = 0;
|
|
550
|
+
double sumfree = 0.0;
|
|
551
|
+
double ub = 1e+300;
|
|
552
|
+
double lb = -1e+300;
|
|
553
|
+
for( int t = 0 ; !(t == n) ; t = (t+1) ) {
|
|
554
|
+
double yg = (yy[t]*g[t]);
|
|
555
|
+
double at = a[t];
|
|
556
|
+
double yt = yy[t];
|
|
557
|
+
if( at >= cost ) {
|
|
558
|
+
if( yt < 0.0 ) {
|
|
559
|
+
if( yg < ub ) {
|
|
560
|
+
ub = yg ;
|
|
561
|
+
} else {
|
|
562
|
+
0 ;
|
|
563
|
+
} ;
|
|
564
|
+
} else {
|
|
565
|
+
if( yg > lb ) {
|
|
566
|
+
lb = yg ;
|
|
567
|
+
} else {
|
|
568
|
+
0 ;
|
|
569
|
+
} ;
|
|
570
|
+
} ;
|
|
571
|
+
} else {
|
|
572
|
+
if( at <= 0.0 ) {
|
|
573
|
+
if( yt > 0.0 ) {
|
|
574
|
+
if( yg < ub ) {
|
|
575
|
+
ub = yg ;
|
|
576
|
+
} else {
|
|
577
|
+
0 ;
|
|
578
|
+
} ;
|
|
579
|
+
} else {
|
|
580
|
+
if( yg > lb ) {
|
|
581
|
+
lb = yg ;
|
|
582
|
+
} else {
|
|
583
|
+
0 ;
|
|
584
|
+
} ;
|
|
585
|
+
} ;
|
|
586
|
+
} else {
|
|
587
|
+
nfree = (nfree+1) ;
|
|
588
|
+
sumfree = (sumfree+yg) ;
|
|
589
|
+
} ;
|
|
590
|
+
} ;
|
|
591
|
+
;
|
|
592
|
+
} ;
|
|
593
|
+
rho[ 0 ] = ( ( nfree > 0 ) ? ((sumfree/(1.0*nfree))) : (((ub+lb)/2.0)) ) ;
|
|
594
|
+
;
|
|
595
|
+
for( int t = 0 ; !(t == n) ; t = (t+1) ) {
|
|
596
|
+
alpha[ index[t] ] = a[t] ;
|
|
597
|
+
} ;
|
|
598
|
+
return iter ;
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
|
|
602
|
+
SCM2CPP_FN inline int
|
|
603
|
+
shrinkable_P( scm2cpp::cspan<double> yy, scm2cpp::cspan<double> a, scm2cpp::cspan<double> g, double cost, double gmax1, double gmax2, int t )
|
|
604
|
+
{
|
|
605
|
+
double at = a[t];
|
|
606
|
+
double gt = g[t];
|
|
607
|
+
double yt = yy[t];
|
|
608
|
+
if( at >= cost ) {
|
|
609
|
+
if( yt > 0.0 ) {
|
|
610
|
+
if( (0.0-gt) > gmax1 ) {
|
|
611
|
+
return 1 ;
|
|
612
|
+
}
|
|
613
|
+
else {
|
|
614
|
+
return 0 ;
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
if( (0.0-gt) > gmax2 ) {
|
|
619
|
+
return 1 ;
|
|
620
|
+
}
|
|
621
|
+
else {
|
|
622
|
+
return 0 ;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
else {
|
|
627
|
+
if( at <= 0.0 ) {
|
|
628
|
+
if( yt > 0.0 ) {
|
|
629
|
+
if( gt > gmax2 ) {
|
|
630
|
+
return 1 ;
|
|
631
|
+
}
|
|
632
|
+
else {
|
|
633
|
+
return 0 ;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
else {
|
|
637
|
+
if( gt > gmax1 ) {
|
|
638
|
+
return 1 ;
|
|
639
|
+
}
|
|
640
|
+
else {
|
|
641
|
+
return 0 ;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
else {
|
|
646
|
+
return 0 ;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
|
|
652
|
+
SCM2CPP_FN inline int
|
|
653
|
+
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 )
|
|
654
|
+
{ int i0 = index[s];
|
|
655
|
+
double y0 = yy[s];
|
|
656
|
+
double a0 = a[s];
|
|
657
|
+
double g0 = g[s];
|
|
658
|
+
double b0 = gbar[s];
|
|
659
|
+
double q0 = qd[s];
|
|
660
|
+
int r0 = krow[s];
|
|
661
|
+
double p0 = pp[s];
|
|
662
|
+
index[ s ] = index[t] ;
|
|
663
|
+
index[ t ] = i0 ;
|
|
664
|
+
krow[ s ] = krow[t] ;
|
|
665
|
+
krow[ t ] = r0 ;
|
|
666
|
+
pp[ s ] = pp[t] ;
|
|
667
|
+
pp[ t ] = p0 ;
|
|
668
|
+
yy[ s ] = yy[t] ;
|
|
669
|
+
yy[ t ] = y0 ;
|
|
670
|
+
a[ s ] = a[t] ;
|
|
671
|
+
a[ t ] = a0 ;
|
|
672
|
+
g[ s ] = g[t] ;
|
|
673
|
+
g[ t ] = g0 ;
|
|
674
|
+
gbar[ s ] = gbar[t] ;
|
|
675
|
+
gbar[ t ] = b0 ;
|
|
676
|
+
qd[ s ] = qd[t] ;
|
|
677
|
+
qd[ t ] = q0 ;
|
|
678
|
+
return 0 ;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
|
|
682
|
+
SCM2CPP_FN inline int
|
|
683
|
+
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 )
|
|
684
|
+
{ if( (n-active) > 65536 ) {
|
|
685
|
+
#pragma omp parallel for
|
|
686
|
+
for( int t = active ; t < n ; t += 1 ) {
|
|
687
|
+
g[ t ] = (gbar[t]+pp[t]) ;
|
|
688
|
+
}
|
|
689
|
+
} else {
|
|
690
|
+
for( int t = active ; t < n ; t += 1 ) {
|
|
691
|
+
g[ t ] = (gbar[t]+pp[t]) ;
|
|
692
|
+
}
|
|
693
|
+
} ;
|
|
694
|
+
for( int s = 0 ; !(s >= active) ; s = (s+1) ) {
|
|
695
|
+
double as = a[s];
|
|
696
|
+
if( (as > 0.0 && as < cost) ) {
|
|
697
|
+
int ps = krow[s];
|
|
698
|
+
double ys = yy[s];
|
|
699
|
+
if( (n-active) > 65536 ) {
|
|
700
|
+
#pragma omp parallel for
|
|
701
|
+
for( int t = active ; t < n ; t += 1 ) {
|
|
702
|
+
g[ t ] = (g[t]+(as*((ys*yy[t])*k[((ps*nk)+krow[t])]))) ;
|
|
703
|
+
}
|
|
704
|
+
} else {
|
|
705
|
+
for( int t = active ; t < n ; t += 1 ) {
|
|
706
|
+
g[ t ] = (g[t]+(as*((ys*yy[t])*k[((ps*nk)+krow[t])]))) ;
|
|
707
|
+
}
|
|
708
|
+
} ;
|
|
709
|
+
;
|
|
710
|
+
} else {
|
|
711
|
+
0 ;
|
|
712
|
+
} ;
|
|
713
|
+
;
|
|
714
|
+
} ;
|
|
715
|
+
return 0 ;
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
|
|
719
|
+
SCM2CPP_FN inline int
|
|
720
|
+
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 )
|
|
721
|
+
{ double gmax = -1e+300;
|
|
722
|
+
int gmaxi = -1;
|
|
723
|
+
double gmax2 = -1e+300;
|
|
724
|
+
int gmini = -1;
|
|
725
|
+
double objmin = 1e+300;
|
|
726
|
+
for( int t = 0 ; !(t == active) ; t = (t+1) ) {
|
|
727
|
+
double gt = g[t];
|
|
728
|
+
double at = a[t];
|
|
729
|
+
if( yy[t] > 0.0 ) {
|
|
730
|
+
if( (at < cost && (0.0-gt) >= gmax) ) {
|
|
731
|
+
gmax = (0.0-gt) ;
|
|
732
|
+
gmaxi = t ;
|
|
733
|
+
} else {
|
|
734
|
+
0 ;
|
|
735
|
+
} ;
|
|
736
|
+
} else {
|
|
737
|
+
if( (at > 0.0 && gt >= gmax) ) {
|
|
738
|
+
gmax = gt ;
|
|
739
|
+
gmaxi = t ;
|
|
740
|
+
} else {
|
|
741
|
+
0 ;
|
|
742
|
+
} ;
|
|
743
|
+
} ;
|
|
744
|
+
;
|
|
745
|
+
} ;
|
|
746
|
+
if( gmaxi >= 0 ) {
|
|
747
|
+
int pi = krow[gmaxi];
|
|
748
|
+
double yi = yy[gmaxi];
|
|
749
|
+
double qdi = qd[gmaxi];
|
|
750
|
+
for( int t = 0 ; !(t == active) ; t = (t+1) ) {
|
|
751
|
+
double gt = g[t];
|
|
752
|
+
double at = a[t];
|
|
753
|
+
double yt = yy[t];
|
|
754
|
+
if( yt > 0.0 ) {
|
|
755
|
+
if( at > 0.0 ) {
|
|
756
|
+
double gd = (gmax+gt);
|
|
757
|
+
if( gt >= gmax2 ) {
|
|
758
|
+
gmax2 = gt ;
|
|
759
|
+
} else {
|
|
760
|
+
0 ;
|
|
761
|
+
} ;
|
|
762
|
+
if( gd > 0.0 ) {
|
|
763
|
+
double quad = ((qdi+qd[t])-(2.0*(yi*(yi*(yt*k[((pi*nk)+krow[t])])))));
|
|
764
|
+
double obj = ((0.0-(gd*gd))/( ( quad > 0.0 ) ? (quad) : (1e-12) ));
|
|
765
|
+
if( obj <= objmin ) {
|
|
766
|
+
objmin = obj ;
|
|
767
|
+
gmini = t ;
|
|
768
|
+
} else {
|
|
769
|
+
0 ;
|
|
770
|
+
} ;
|
|
771
|
+
;
|
|
772
|
+
;
|
|
773
|
+
} else {
|
|
774
|
+
0 ;
|
|
775
|
+
} ;
|
|
776
|
+
;
|
|
777
|
+
} else {
|
|
778
|
+
0 ;
|
|
779
|
+
} ;
|
|
780
|
+
} else {
|
|
781
|
+
if( at < cost ) {
|
|
782
|
+
double gd = (gmax-gt);
|
|
783
|
+
if( (0.0-gt) >= gmax2 ) {
|
|
784
|
+
gmax2 = (0.0-gt) ;
|
|
785
|
+
} else {
|
|
786
|
+
0 ;
|
|
787
|
+
} ;
|
|
788
|
+
if( gd > 0.0 ) {
|
|
789
|
+
double quad = ((qdi+qd[t])+(2.0*(yi*(yi*(yt*k[((pi*nk)+krow[t])])))));
|
|
790
|
+
double obj = ((0.0-(gd*gd))/( ( quad > 0.0 ) ? (quad) : (1e-12) ));
|
|
791
|
+
if( obj <= objmin ) {
|
|
792
|
+
objmin = obj ;
|
|
793
|
+
gmini = t ;
|
|
794
|
+
} else {
|
|
795
|
+
0 ;
|
|
796
|
+
} ;
|
|
797
|
+
;
|
|
798
|
+
;
|
|
799
|
+
} else {
|
|
800
|
+
0 ;
|
|
801
|
+
} ;
|
|
802
|
+
;
|
|
803
|
+
} else {
|
|
804
|
+
0 ;
|
|
805
|
+
} ;
|
|
806
|
+
} ;
|
|
807
|
+
;
|
|
808
|
+
} ;
|
|
809
|
+
;
|
|
810
|
+
} else {
|
|
811
|
+
0 ;
|
|
812
|
+
} ;
|
|
813
|
+
wi[ 0 ] = gmaxi ;
|
|
814
|
+
wi[ 1 ] = gmini ;
|
|
815
|
+
if( ((gmax+gmax2) < eps || gmini < 0) ) {
|
|
816
|
+
return 1 ;
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
return 0 ;
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
|
|
824
|
+
inline int
|
|
825
|
+
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 )
|
|
826
|
+
{ std::vector<double> y2((2*n),0.0);
|
|
827
|
+
std::vector<double> lin((2*n),0.0);
|
|
828
|
+
std::vector<double> a2((2*n),0.0);
|
|
829
|
+
if( n > 65536 ) {
|
|
830
|
+
#pragma omp parallel for
|
|
831
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
832
|
+
y2[ i ] = 1.0 ;
|
|
833
|
+
y2[ (i+n) ] = -1.0 ;
|
|
834
|
+
lin[ i ] = (epsilon-z[i]) ;
|
|
835
|
+
lin[ (i+n) ] = (epsilon+z[i]) ;
|
|
836
|
+
}
|
|
837
|
+
} else {
|
|
838
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
839
|
+
y2[ i ] = 1.0 ;
|
|
840
|
+
y2[ (i+n) ] = -1.0 ;
|
|
841
|
+
lin[ i ] = (epsilon-z[i]) ;
|
|
842
|
+
lin[ (i+n) ] = (epsilon+z[i]) ;
|
|
843
|
+
}
|
|
844
|
+
} ;
|
|
845
|
+
std::array<double,1> rv = scm2cpp::filled_array<double,1>(0.0);
|
|
846
|
+
int it = smo_solve(k,y2,lin,a2,rv,cost,iters,eps,int((2*n)),n);
|
|
847
|
+
if( n > 65536 ) {
|
|
848
|
+
#pragma omp parallel for
|
|
849
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
850
|
+
beta[ i ] = (a2[i]-a2[(i+n)]) ;
|
|
851
|
+
}
|
|
852
|
+
} else {
|
|
853
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
854
|
+
beta[ i ] = (a2[i]-a2[(i+n)]) ;
|
|
855
|
+
}
|
|
856
|
+
} ;
|
|
857
|
+
rho[ 0 ] = rv[0] ;
|
|
858
|
+
return it ;
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
|
|
862
|
+
SCM2CPP_FN inline int
|
|
863
|
+
rbf_from_products( scm2cpp::span<double> g, scm2cpp::cspan<double> sa, scm2cpp::cspan<double> sb, double gamma, int na, int nb )
|
|
864
|
+
{ if( (na*nb) > 65536 ) {
|
|
865
|
+
#pragma omp parallel for
|
|
866
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
867
|
+
for( int j = 0 ; !(j == nb) ; j = (j+1) ) {
|
|
868
|
+
double d = ((sa[i]+sb[j])-(2.0*g[((i*nb)+j)]));
|
|
869
|
+
g[ ((i*nb)+j) ] = exp(((0.0-gamma)*( ( d < 0.0 ) ? (0.0) : (d) ))) ;
|
|
870
|
+
;
|
|
871
|
+
} ;
|
|
872
|
+
}
|
|
873
|
+
} else {
|
|
874
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
875
|
+
for( int j = 0 ; !(j == nb) ; j = (j+1) ) {
|
|
876
|
+
double d = ((sa[i]+sb[j])-(2.0*g[((i*nb)+j)]));
|
|
877
|
+
g[ ((i*nb)+j) ] = exp(((0.0-gamma)*( ( d < 0.0 ) ? (0.0) : (d) ))) ;
|
|
878
|
+
;
|
|
879
|
+
} ;
|
|
880
|
+
}
|
|
881
|
+
} ;
|
|
882
|
+
return 0 ;
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
|
|
886
|
+
inline int
|
|
887
|
+
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 )
|
|
888
|
+
{ std::vector<int> index(p,0);
|
|
889
|
+
std::vector<double> hdiag(p,0.0);
|
|
890
|
+
std::vector<double> grad(p,0.0);
|
|
891
|
+
std::vector<double> wpd(p,0.0);
|
|
892
|
+
std::vector<double> xjneg(p,0.0);
|
|
893
|
+
std::vector<double> xtd(n,0.0);
|
|
894
|
+
std::vector<double> ewx(n,0.0);
|
|
895
|
+
std::vector<double> ewxnew(n,0.0);
|
|
896
|
+
std::vector<double> tau(n,0.0);
|
|
897
|
+
std::vector<double> d(n,0.0);
|
|
898
|
+
int rng = seed;
|
|
899
|
+
int npos = 0;
|
|
900
|
+
int newton = 0;
|
|
901
|
+
double wnorm = 0.0;
|
|
902
|
+
double ginit = -1.0;
|
|
903
|
+
double gmaxold = 1e+300;
|
|
904
|
+
int qpnochange = 0;
|
|
905
|
+
double innereps = 1.0;
|
|
906
|
+
int done = 0;
|
|
907
|
+
if( n > 65536 ) {
|
|
908
|
+
#pragma omp parallel for reduction(+:npos)
|
|
909
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
910
|
+
if( y[i] > 0.0 ) {
|
|
911
|
+
npos = (npos+1) ;
|
|
912
|
+
} else {
|
|
913
|
+
0 ;
|
|
914
|
+
} ;
|
|
915
|
+
}
|
|
916
|
+
} else {
|
|
917
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
918
|
+
if( y[i] > 0.0 ) {
|
|
919
|
+
npos = (npos+1) ;
|
|
920
|
+
} else {
|
|
921
|
+
0 ;
|
|
922
|
+
} ;
|
|
923
|
+
}
|
|
924
|
+
} ;
|
|
925
|
+
|
|
926
|
+
struct let6087 {
|
|
927
|
+
let6087() {}
|
|
928
|
+
int operator()( int m )
|
|
929
|
+
{
|
|
930
|
+
if( m < 1 ) {
|
|
931
|
+
return 1 ;
|
|
932
|
+
}
|
|
933
|
+
else {
|
|
934
|
+
return m ;
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
} ;
|
|
938
|
+
double eps = ((tol*(1.0*let6087()(( ( npos < (n-npos) ) ? (npos) : ((n-npos)) ))))/(1.0*n));
|
|
939
|
+
if( (p*n) > 65536 ) {
|
|
940
|
+
#pragma omp parallel for
|
|
941
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
942
|
+
w[ j ] = 0.0 ;
|
|
943
|
+
wpd[ j ] = 0.0 ;
|
|
944
|
+
index[ j ] = j ;
|
|
945
|
+
double acc = 0.0;
|
|
946
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
947
|
+
if( y[i] < 0.0 ) {
|
|
948
|
+
acc = (acc+(cost*x[((j*n)+i)])) ;
|
|
949
|
+
} else {
|
|
950
|
+
0 ;
|
|
951
|
+
} ;
|
|
952
|
+
} ;
|
|
953
|
+
xjneg[ j ] = acc ;
|
|
954
|
+
;
|
|
955
|
+
}
|
|
956
|
+
} else {
|
|
957
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
958
|
+
w[ j ] = 0.0 ;
|
|
959
|
+
wpd[ j ] = 0.0 ;
|
|
960
|
+
index[ j ] = j ;
|
|
961
|
+
double acc = 0.0;
|
|
962
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
963
|
+
if( y[i] < 0.0 ) {
|
|
964
|
+
acc = (acc+(cost*x[((j*n)+i)])) ;
|
|
965
|
+
} else {
|
|
966
|
+
0 ;
|
|
967
|
+
} ;
|
|
968
|
+
} ;
|
|
969
|
+
xjneg[ j ] = acc ;
|
|
970
|
+
;
|
|
971
|
+
}
|
|
972
|
+
} ;
|
|
973
|
+
if( n > 65536 ) {
|
|
974
|
+
#pragma omp parallel for
|
|
975
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
976
|
+
ewx[ i ] = 1.0 ;
|
|
977
|
+
tau[ i ] = (cost*0.5) ;
|
|
978
|
+
d[ i ] = (cost*0.25) ;
|
|
979
|
+
}
|
|
980
|
+
} else {
|
|
981
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
982
|
+
ewx[ i ] = 1.0 ;
|
|
983
|
+
tau[ i ] = (cost*0.5) ;
|
|
984
|
+
d[ i ] = (cost*0.25) ;
|
|
985
|
+
}
|
|
986
|
+
} ;
|
|
987
|
+
for( ; !((done == 1 || newton >= maxnewton)) ; ) {
|
|
988
|
+
double gmaxnew = 0.0;
|
|
989
|
+
double gnorm1new = 0.0;
|
|
990
|
+
int active = p;
|
|
991
|
+
int s = 0;
|
|
992
|
+
for( ; !(s >= active) ; ) {
|
|
993
|
+
int j = index[s];
|
|
994
|
+
double h = 1e-12;
|
|
995
|
+
double tmp = 0.0;
|
|
996
|
+
if( n > 65536 ) {
|
|
997
|
+
#pragma omp parallel for reduction(+:h) reduction(+:tmp)
|
|
998
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
999
|
+
double v = x[((j*n)+i)];
|
|
1000
|
+
h = (h+((v*v)*d[i])) ;
|
|
1001
|
+
tmp = (tmp+(v*tau[i])) ;
|
|
1002
|
+
;
|
|
1003
|
+
}
|
|
1004
|
+
} else {
|
|
1005
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1006
|
+
double v = x[((j*n)+i)];
|
|
1007
|
+
h = (h+((v*v)*d[i])) ;
|
|
1008
|
+
tmp = (tmp+(v*tau[i])) ;
|
|
1009
|
+
;
|
|
1010
|
+
}
|
|
1011
|
+
} ;
|
|
1012
|
+
hdiag[ j ] = h ;
|
|
1013
|
+
grad[ j ] = ((0.0-tmp)+xjneg[j]) ;
|
|
1014
|
+
double gp = (grad[j]+1.0);
|
|
1015
|
+
double gn = (grad[j]-1.0);
|
|
1016
|
+
double wj = w[j];
|
|
1017
|
+
double viol = 0.0;
|
|
1018
|
+
int shrunk = 0;
|
|
1019
|
+
if( wj == 0.0 ) {
|
|
1020
|
+
if( gp < 0.0 ) {
|
|
1021
|
+
viol = (0.0-gp) ;
|
|
1022
|
+
} else {
|
|
1023
|
+
if( gn > 0.0 ) {
|
|
1024
|
+
viol = gn ;
|
|
1025
|
+
} else {
|
|
1026
|
+
if( (gp > (gmaxold/(1.0*n)) && gn < (0.0-(gmaxold/(1.0*n)))) ) {
|
|
1027
|
+
shrunk = 1 ;
|
|
1028
|
+
} else {
|
|
1029
|
+
0 ;
|
|
1030
|
+
} ;
|
|
1031
|
+
} ;
|
|
1032
|
+
} ;
|
|
1033
|
+
} else {
|
|
1034
|
+
if( wj > 0.0 ) {
|
|
1035
|
+
viol = ( ( gp < 0.0 ) ? ((0.0-gp)) : (gp) ) ;
|
|
1036
|
+
} else {
|
|
1037
|
+
viol = ( ( gn < 0.0 ) ? ((0.0-gn)) : (gn) ) ;
|
|
1038
|
+
} ;
|
|
1039
|
+
} ;
|
|
1040
|
+
if( shrunk == 1 ) {
|
|
1041
|
+
active = (active-1) ;
|
|
1042
|
+
index[ s ] = index[active] ;
|
|
1043
|
+
index[ active ] = j ;
|
|
1044
|
+
} else {
|
|
1045
|
+
if( viol > gmaxnew ) {
|
|
1046
|
+
gmaxnew = viol ;
|
|
1047
|
+
} else {
|
|
1048
|
+
0 ;
|
|
1049
|
+
} ;
|
|
1050
|
+
gnorm1new = (gnorm1new+viol) ;
|
|
1051
|
+
s = (s+1) ;
|
|
1052
|
+
} ;
|
|
1053
|
+
;
|
|
1054
|
+
;
|
|
1055
|
+
} ;
|
|
1056
|
+
;
|
|
1057
|
+
if( newton == 0 ) {
|
|
1058
|
+
ginit = gnorm1new ;
|
|
1059
|
+
} else {
|
|
1060
|
+
0 ;
|
|
1061
|
+
} ;
|
|
1062
|
+
if( (gnorm1new <= (eps*ginit) || qpnochange >= 10) ) {
|
|
1063
|
+
done = 1 ;
|
|
1064
|
+
} else {
|
|
1065
|
+
qpnochange = (qpnochange+1) ;
|
|
1066
|
+
int iter = 0;
|
|
1067
|
+
double qpgmaxold = 1e+300;
|
|
1068
|
+
int qpactive = active;
|
|
1069
|
+
int stopqp = 0;
|
|
1070
|
+
if( n > 65536 ) {
|
|
1071
|
+
#pragma omp parallel for
|
|
1072
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1073
|
+
xtd[ i ] = 0.0 ;
|
|
1074
|
+
}
|
|
1075
|
+
} else {
|
|
1076
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1077
|
+
xtd[ i ] = 0.0 ;
|
|
1078
|
+
}
|
|
1079
|
+
} ;
|
|
1080
|
+
for( ; !((stopqp == 1 || iter >= 1000)) ; ) {
|
|
1081
|
+
double qpgmaxnew = 0.0;
|
|
1082
|
+
double qpgnorm1new = 0.0;
|
|
1083
|
+
for( int t = 0 ; !(t == qpactive) ; t = (t+1) ) {
|
|
1084
|
+
rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
|
|
1085
|
+
if( rng < 0 ) {
|
|
1086
|
+
rng = (rng+2147483647) ;
|
|
1087
|
+
} else {
|
|
1088
|
+
0 ;
|
|
1089
|
+
} ;
|
|
1090
|
+
int k = (t+(rng % (qpactive-t)));
|
|
1091
|
+
int tt = index[t];
|
|
1092
|
+
index[ t ] = index[k] ;
|
|
1093
|
+
index[ k ] = tt ;
|
|
1094
|
+
;
|
|
1095
|
+
} ;
|
|
1096
|
+
int s = 0;
|
|
1097
|
+
for( ; !(s >= qpactive) ; ) {
|
|
1098
|
+
int j = index[s];
|
|
1099
|
+
double h = hdiag[j];
|
|
1100
|
+
double g = (grad[j]+((wpd[j]-w[j])*1e-12));
|
|
1101
|
+
if( n > 65536 ) {
|
|
1102
|
+
#pragma omp parallel for reduction(+:g)
|
|
1103
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1104
|
+
g = (g+(x[((j*n)+i)]*(d[i]*xtd[i]))) ;
|
|
1105
|
+
}
|
|
1106
|
+
} else {
|
|
1107
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1108
|
+
g = (g+(x[((j*n)+i)]*(d[i]*xtd[i]))) ;
|
|
1109
|
+
}
|
|
1110
|
+
} ;
|
|
1111
|
+
double gp = (g+1.0);
|
|
1112
|
+
double gn = (g-1.0);
|
|
1113
|
+
double wj = wpd[j];
|
|
1114
|
+
double viol = 0.0;
|
|
1115
|
+
int shrunk = 0;
|
|
1116
|
+
if( wj == 0.0 ) {
|
|
1117
|
+
if( gp < 0.0 ) {
|
|
1118
|
+
viol = (0.0-gp) ;
|
|
1119
|
+
} else {
|
|
1120
|
+
if( gn > 0.0 ) {
|
|
1121
|
+
viol = gn ;
|
|
1122
|
+
} else {
|
|
1123
|
+
if( (gp > (qpgmaxold/(1.0*n)) && gn < (0.0-(qpgmaxold/(1.0*n)))) ) {
|
|
1124
|
+
shrunk = 1 ;
|
|
1125
|
+
} else {
|
|
1126
|
+
0 ;
|
|
1127
|
+
} ;
|
|
1128
|
+
} ;
|
|
1129
|
+
} ;
|
|
1130
|
+
} else {
|
|
1131
|
+
if( wj > 0.0 ) {
|
|
1132
|
+
viol = ( ( gp < 0.0 ) ? ((0.0-gp)) : (gp) ) ;
|
|
1133
|
+
} else {
|
|
1134
|
+
viol = ( ( gn < 0.0 ) ? ((0.0-gn)) : (gn) ) ;
|
|
1135
|
+
} ;
|
|
1136
|
+
} ;
|
|
1137
|
+
if( shrunk == 1 ) {
|
|
1138
|
+
qpactive = (qpactive-1) ;
|
|
1139
|
+
index[ s ] = index[qpactive] ;
|
|
1140
|
+
index[ qpactive ] = j ;
|
|
1141
|
+
} else {
|
|
1142
|
+
double z = ( ( gp < (h*wj) ) ? (((0.0-gp)/h)) : (( ( gn > (h*wj) ) ? (((0.0-gn)/h)) : ((0.0-wj)) )) );
|
|
1143
|
+
if( (z > 1e-12 || z < -1e-12) ) {
|
|
1144
|
+
double zc = ( ( z < -10.0 ) ? (-10.0) : (( ( z > 10.0 ) ? (10.0) : (z) )) );
|
|
1145
|
+
qpnochange = 0 ;
|
|
1146
|
+
if( viol > qpgmaxnew ) {
|
|
1147
|
+
qpgmaxnew = viol ;
|
|
1148
|
+
} else {
|
|
1149
|
+
0 ;
|
|
1150
|
+
} ;
|
|
1151
|
+
qpgnorm1new = (qpgnorm1new+viol) ;
|
|
1152
|
+
wpd[ j ] = (wj+zc) ;
|
|
1153
|
+
if( n > 65536 ) {
|
|
1154
|
+
#pragma omp parallel for
|
|
1155
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1156
|
+
xtd[ i ] = (xtd[i]+(x[((j*n)+i)]*zc)) ;
|
|
1157
|
+
}
|
|
1158
|
+
} else {
|
|
1159
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1160
|
+
xtd[ i ] = (xtd[i]+(x[((j*n)+i)]*zc)) ;
|
|
1161
|
+
}
|
|
1162
|
+
} ;
|
|
1163
|
+
;
|
|
1164
|
+
} else {
|
|
1165
|
+
0 ;
|
|
1166
|
+
} ;
|
|
1167
|
+
;
|
|
1168
|
+
s = (s+1) ;
|
|
1169
|
+
} ;
|
|
1170
|
+
;
|
|
1171
|
+
;
|
|
1172
|
+
;
|
|
1173
|
+
} ;
|
|
1174
|
+
;
|
|
1175
|
+
iter = (iter+1) ;
|
|
1176
|
+
if( qpgnorm1new <= (innereps*ginit) ) {
|
|
1177
|
+
if( qpactive == active ) {
|
|
1178
|
+
stopqp = 1 ;
|
|
1179
|
+
} else {
|
|
1180
|
+
qpactive = active ;
|
|
1181
|
+
qpgmaxold = 1e+300 ;
|
|
1182
|
+
} ;
|
|
1183
|
+
} else {
|
|
1184
|
+
qpgmaxold = qpgmaxnew ;
|
|
1185
|
+
} ;
|
|
1186
|
+
;
|
|
1187
|
+
} ;
|
|
1188
|
+
double delta = 0.0;
|
|
1189
|
+
double wnormnew = 0.0;
|
|
1190
|
+
double negsum = 0.0;
|
|
1191
|
+
int found = 0;
|
|
1192
|
+
int nls = 0;
|
|
1193
|
+
if( p > 65536 ) {
|
|
1194
|
+
#pragma omp parallel for reduction(+:delta) reduction(+:wnormnew)
|
|
1195
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1196
|
+
delta = (delta+(grad[j]*(wpd[j]-w[j]))) ;
|
|
1197
|
+
double v = wpd[j];
|
|
1198
|
+
if( !(v == 0.0) ) {
|
|
1199
|
+
wnormnew = (wnormnew+( ( v < 0.0 ) ? ((0.0-v)) : (v) )) ;
|
|
1200
|
+
} else {
|
|
1201
|
+
0 ;
|
|
1202
|
+
} ;
|
|
1203
|
+
;
|
|
1204
|
+
}
|
|
1205
|
+
} else {
|
|
1206
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1207
|
+
delta = (delta+(grad[j]*(wpd[j]-w[j]))) ;
|
|
1208
|
+
double v = wpd[j];
|
|
1209
|
+
if( !(v == 0.0) ) {
|
|
1210
|
+
wnormnew = (wnormnew+( ( v < 0.0 ) ? ((0.0-v)) : (v) )) ;
|
|
1211
|
+
} else {
|
|
1212
|
+
0 ;
|
|
1213
|
+
} ;
|
|
1214
|
+
;
|
|
1215
|
+
}
|
|
1216
|
+
} ;
|
|
1217
|
+
delta = (delta+(wnormnew-wnorm)) ;
|
|
1218
|
+
if( n > 65536 ) {
|
|
1219
|
+
#pragma omp parallel for reduction(+:negsum)
|
|
1220
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1221
|
+
if( y[i] < 0.0 ) {
|
|
1222
|
+
negsum = (negsum+(cost*xtd[i])) ;
|
|
1223
|
+
} else {
|
|
1224
|
+
0 ;
|
|
1225
|
+
} ;
|
|
1226
|
+
}
|
|
1227
|
+
} else {
|
|
1228
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1229
|
+
if( y[i] < 0.0 ) {
|
|
1230
|
+
negsum = (negsum+(cost*xtd[i])) ;
|
|
1231
|
+
} else {
|
|
1232
|
+
0 ;
|
|
1233
|
+
} ;
|
|
1234
|
+
}
|
|
1235
|
+
} ;
|
|
1236
|
+
for( ; !((found == 1 || nls >= 20)) ; ) {
|
|
1237
|
+
double cond0 = (((wnormnew-wnorm)+negsum)-(0.01*delta));
|
|
1238
|
+
if( n > 65536 ) {
|
|
1239
|
+
#pragma omp parallel for reduction(+:cond0)
|
|
1240
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1241
|
+
double ex = exp(xtd[i]);
|
|
1242
|
+
ewxnew[ i ] = (ewx[i]*ex) ;
|
|
1243
|
+
cond0 = (cond0+(cost*log(((1.0+ewxnew[i])/(ex+ewxnew[i]))))) ;
|
|
1244
|
+
;
|
|
1245
|
+
}
|
|
1246
|
+
} else {
|
|
1247
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1248
|
+
double ex = exp(xtd[i]);
|
|
1249
|
+
ewxnew[ i ] = (ewx[i]*ex) ;
|
|
1250
|
+
cond0 = (cond0+(cost*log(((1.0+ewxnew[i])/(ex+ewxnew[i]))))) ;
|
|
1251
|
+
;
|
|
1252
|
+
}
|
|
1253
|
+
} ;
|
|
1254
|
+
if( cond0 <= 0.0 ) {
|
|
1255
|
+
found = 1 ;
|
|
1256
|
+
wnorm = wnormnew ;
|
|
1257
|
+
if( p > 65536 ) {
|
|
1258
|
+
#pragma omp parallel for
|
|
1259
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1260
|
+
w[ j ] = wpd[j] ;
|
|
1261
|
+
}
|
|
1262
|
+
} else {
|
|
1263
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1264
|
+
w[ j ] = wpd[j] ;
|
|
1265
|
+
}
|
|
1266
|
+
} ;
|
|
1267
|
+
if( n > 65536 ) {
|
|
1268
|
+
#pragma omp parallel for
|
|
1269
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1270
|
+
double e = ewxnew[i];
|
|
1271
|
+
ewx[ i ] = e ;
|
|
1272
|
+
double tt = (1.0/(1.0+e));
|
|
1273
|
+
tau[ i ] = (cost*tt) ;
|
|
1274
|
+
d[ i ] = (cost*(e*(tt*tt))) ;
|
|
1275
|
+
;
|
|
1276
|
+
;
|
|
1277
|
+
}
|
|
1278
|
+
} else {
|
|
1279
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1280
|
+
double e = ewxnew[i];
|
|
1281
|
+
ewx[ i ] = e ;
|
|
1282
|
+
double tt = (1.0/(1.0+e));
|
|
1283
|
+
tau[ i ] = (cost*tt) ;
|
|
1284
|
+
d[ i ] = (cost*(e*(tt*tt))) ;
|
|
1285
|
+
;
|
|
1286
|
+
;
|
|
1287
|
+
}
|
|
1288
|
+
} ;
|
|
1289
|
+
} else {
|
|
1290
|
+
wnormnew = 0.0 ;
|
|
1291
|
+
if( p > 65536 ) {
|
|
1292
|
+
#pragma omp parallel for reduction(+:wnormnew)
|
|
1293
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1294
|
+
double v = ((w[j]+wpd[j])*0.5);
|
|
1295
|
+
wpd[ j ] = v ;
|
|
1296
|
+
if( !(v == 0.0) ) {
|
|
1297
|
+
wnormnew = (wnormnew+( ( v < 0.0 ) ? ((0.0-v)) : (v) )) ;
|
|
1298
|
+
} else {
|
|
1299
|
+
0 ;
|
|
1300
|
+
} ;
|
|
1301
|
+
;
|
|
1302
|
+
}
|
|
1303
|
+
} else {
|
|
1304
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1305
|
+
double v = ((w[j]+wpd[j])*0.5);
|
|
1306
|
+
wpd[ j ] = v ;
|
|
1307
|
+
if( !(v == 0.0) ) {
|
|
1308
|
+
wnormnew = (wnormnew+( ( v < 0.0 ) ? ((0.0-v)) : (v) )) ;
|
|
1309
|
+
} else {
|
|
1310
|
+
0 ;
|
|
1311
|
+
} ;
|
|
1312
|
+
;
|
|
1313
|
+
}
|
|
1314
|
+
} ;
|
|
1315
|
+
delta = (delta*0.5) ;
|
|
1316
|
+
negsum = (negsum*0.5) ;
|
|
1317
|
+
if( n > 65536 ) {
|
|
1318
|
+
#pragma omp parallel for
|
|
1319
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1320
|
+
xtd[ i ] = (xtd[i]*0.5) ;
|
|
1321
|
+
}
|
|
1322
|
+
} else {
|
|
1323
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1324
|
+
xtd[ i ] = (xtd[i]*0.5) ;
|
|
1325
|
+
}
|
|
1326
|
+
} ;
|
|
1327
|
+
nls = (nls+1) ;
|
|
1328
|
+
} ;
|
|
1329
|
+
;
|
|
1330
|
+
} ;
|
|
1331
|
+
if( nls >= 20 ) {
|
|
1332
|
+
if( (n*p) > 65536 ) {
|
|
1333
|
+
#pragma omp parallel for
|
|
1334
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1335
|
+
double acc = 0.0;
|
|
1336
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1337
|
+
acc = (acc+(w[j]*x[((j*n)+i)])) ;
|
|
1338
|
+
} ;
|
|
1339
|
+
ewx[ i ] = exp(acc) ;
|
|
1340
|
+
;
|
|
1341
|
+
}
|
|
1342
|
+
} else {
|
|
1343
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1344
|
+
double acc = 0.0;
|
|
1345
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1346
|
+
acc = (acc+(w[j]*x[((j*n)+i)])) ;
|
|
1347
|
+
} ;
|
|
1348
|
+
ewx[ i ] = exp(acc) ;
|
|
1349
|
+
;
|
|
1350
|
+
}
|
|
1351
|
+
} ;
|
|
1352
|
+
} else {
|
|
1353
|
+
0 ;
|
|
1354
|
+
} ;
|
|
1355
|
+
;
|
|
1356
|
+
if( iter == 1 ) {
|
|
1357
|
+
innereps = (innereps*0.25) ;
|
|
1358
|
+
} else {
|
|
1359
|
+
0 ;
|
|
1360
|
+
} ;
|
|
1361
|
+
;
|
|
1362
|
+
newton = (newton+1) ;
|
|
1363
|
+
gmaxold = gmaxnew ;
|
|
1364
|
+
} ;
|
|
1365
|
+
;
|
|
1366
|
+
} ;
|
|
1367
|
+
return newton ;
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
|
|
1371
|
+
inline int
|
|
1372
|
+
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 )
|
|
1373
|
+
{ std::vector<int> index(n,0);
|
|
1374
|
+
int rng = seed;
|
|
1375
|
+
int active = n;
|
|
1376
|
+
double gmaxold = 1e+300;
|
|
1377
|
+
double ginit = -1.0;
|
|
1378
|
+
int iter = 0;
|
|
1379
|
+
int done = 0;
|
|
1380
|
+
if( (n*p) > 65536 ) {
|
|
1381
|
+
#pragma omp parallel for
|
|
1382
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1383
|
+
double acc = 0.0;
|
|
1384
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1385
|
+
acc = (acc+(x[((i*p)+j)]*x[((i*p)+j)])) ;
|
|
1386
|
+
} ;
|
|
1387
|
+
xnorm[ i ] = acc ;
|
|
1388
|
+
;
|
|
1389
|
+
beta[ i ] = 0.0 ;
|
|
1390
|
+
index[ i ] = i ;
|
|
1391
|
+
}
|
|
1392
|
+
} else {
|
|
1393
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1394
|
+
double acc = 0.0;
|
|
1395
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1396
|
+
acc = (acc+(x[((i*p)+j)]*x[((i*p)+j)])) ;
|
|
1397
|
+
} ;
|
|
1398
|
+
xnorm[ i ] = acc ;
|
|
1399
|
+
;
|
|
1400
|
+
beta[ i ] = 0.0 ;
|
|
1401
|
+
index[ i ] = i ;
|
|
1402
|
+
}
|
|
1403
|
+
} ;
|
|
1404
|
+
if( p > 65536 ) {
|
|
1405
|
+
#pragma omp parallel for
|
|
1406
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1407
|
+
w[ j ] = 0.0 ;
|
|
1408
|
+
}
|
|
1409
|
+
} else {
|
|
1410
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1411
|
+
w[ j ] = 0.0 ;
|
|
1412
|
+
}
|
|
1413
|
+
} ;
|
|
1414
|
+
for( ; !((done == 1 || iter >= iters)) ; ) {
|
|
1415
|
+
double gmaxnew = 0.0;
|
|
1416
|
+
double gnorm1new = 0.0;
|
|
1417
|
+
for( int t = 0 ; !(t == active) ; t = (t+1) ) {
|
|
1418
|
+
rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
|
|
1419
|
+
if( rng < 0 ) {
|
|
1420
|
+
rng = (rng+2147483647) ;
|
|
1421
|
+
} else {
|
|
1422
|
+
0 ;
|
|
1423
|
+
} ;
|
|
1424
|
+
int k = (t+(rng % (active-t)));
|
|
1425
|
+
int tt = index[t];
|
|
1426
|
+
index[ t ] = index[k] ;
|
|
1427
|
+
index[ k ] = tt ;
|
|
1428
|
+
;
|
|
1429
|
+
} ;
|
|
1430
|
+
int s = 0;
|
|
1431
|
+
for( ; !(s >= active) ; ) {
|
|
1432
|
+
int i = index[s];
|
|
1433
|
+
double g = (0.0-y[i]);
|
|
1434
|
+
double h = xnorm[i];
|
|
1435
|
+
double b = beta[i];
|
|
1436
|
+
if( p > 65536 ) {
|
|
1437
|
+
#pragma omp parallel for reduction(+:g)
|
|
1438
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1439
|
+
g = (g+(x[((i*p)+j)]*w[j])) ;
|
|
1440
|
+
}
|
|
1441
|
+
} else {
|
|
1442
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1443
|
+
g = (g+(x[((i*p)+j)]*w[j])) ;
|
|
1444
|
+
}
|
|
1445
|
+
} ;
|
|
1446
|
+
double gp = (g+epsilon);
|
|
1447
|
+
double gn = (g-epsilon);
|
|
1448
|
+
double viol = 0.0;
|
|
1449
|
+
int shrunk = 0;
|
|
1450
|
+
if( b == 0.0 ) {
|
|
1451
|
+
if( gp < 0.0 ) {
|
|
1452
|
+
viol = (0.0-gp) ;
|
|
1453
|
+
} else {
|
|
1454
|
+
if( gn > 0.0 ) {
|
|
1455
|
+
viol = gn ;
|
|
1456
|
+
} else {
|
|
1457
|
+
if( (gp > gmaxold && gn < (0.0-gmaxold)) ) {
|
|
1458
|
+
shrunk = 1 ;
|
|
1459
|
+
} else {
|
|
1460
|
+
0 ;
|
|
1461
|
+
} ;
|
|
1462
|
+
} ;
|
|
1463
|
+
} ;
|
|
1464
|
+
} else {
|
|
1465
|
+
if( b >= cost ) {
|
|
1466
|
+
if( gp > 0.0 ) {
|
|
1467
|
+
viol = gp ;
|
|
1468
|
+
} else {
|
|
1469
|
+
if( gp < (0.0-gmaxold) ) {
|
|
1470
|
+
shrunk = 1 ;
|
|
1471
|
+
} else {
|
|
1472
|
+
0 ;
|
|
1473
|
+
} ;
|
|
1474
|
+
} ;
|
|
1475
|
+
} else {
|
|
1476
|
+
if( b <= (0.0-cost) ) {
|
|
1477
|
+
if( gn < 0.0 ) {
|
|
1478
|
+
viol = (0.0-gn) ;
|
|
1479
|
+
} else {
|
|
1480
|
+
if( gn > gmaxold ) {
|
|
1481
|
+
shrunk = 1 ;
|
|
1482
|
+
} else {
|
|
1483
|
+
0 ;
|
|
1484
|
+
} ;
|
|
1485
|
+
} ;
|
|
1486
|
+
} else {
|
|
1487
|
+
if( b > 0.0 ) {
|
|
1488
|
+
viol = ( ( gp < 0.0 ) ? ((0.0-gp)) : (gp) ) ;
|
|
1489
|
+
} else {
|
|
1490
|
+
viol = ( ( gn < 0.0 ) ? ((0.0-gn)) : (gn) ) ;
|
|
1491
|
+
} ;
|
|
1492
|
+
} ;
|
|
1493
|
+
} ;
|
|
1494
|
+
} ;
|
|
1495
|
+
if( shrunk == 1 ) {
|
|
1496
|
+
active = (active-1) ;
|
|
1497
|
+
index[ s ] = index[active] ;
|
|
1498
|
+
index[ active ] = i ;
|
|
1499
|
+
} else {
|
|
1500
|
+
if( viol > gmaxnew ) {
|
|
1501
|
+
gmaxnew = viol ;
|
|
1502
|
+
} else {
|
|
1503
|
+
0 ;
|
|
1504
|
+
} ;
|
|
1505
|
+
gnorm1new = (gnorm1new+viol) ;
|
|
1506
|
+
double d = ( ( gp < (h*b) ) ? (((0.0-gp)/h)) : (( ( gn > (h*b) ) ? (((0.0-gn)/h)) : ((0.0-b)) )) );
|
|
1507
|
+
if( (d > 1e-12 || d < -1e-12) ) {
|
|
1508
|
+
double bn = (b+d);
|
|
1509
|
+
double bc = ( ( bn < (0.0-cost) ) ? ((0.0-cost)) : (( ( bn > cost ) ? (cost) : (bn) )) );
|
|
1510
|
+
beta[ i ] = bc ;
|
|
1511
|
+
if( !(bc == b) ) {
|
|
1512
|
+
if( p > 65536 ) {
|
|
1513
|
+
#pragma omp parallel for
|
|
1514
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1515
|
+
w[ j ] = (w[j]+((bc-b)*x[((i*p)+j)])) ;
|
|
1516
|
+
}
|
|
1517
|
+
} else {
|
|
1518
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1519
|
+
w[ j ] = (w[j]+((bc-b)*x[((i*p)+j)])) ;
|
|
1520
|
+
}
|
|
1521
|
+
} ;
|
|
1522
|
+
} else {
|
|
1523
|
+
false ;
|
|
1524
|
+
} ;
|
|
1525
|
+
;
|
|
1526
|
+
;
|
|
1527
|
+
} else {
|
|
1528
|
+
0 ;
|
|
1529
|
+
} ;
|
|
1530
|
+
;
|
|
1531
|
+
s = (s+1) ;
|
|
1532
|
+
} ;
|
|
1533
|
+
;
|
|
1534
|
+
;
|
|
1535
|
+
;
|
|
1536
|
+
} ;
|
|
1537
|
+
;
|
|
1538
|
+
if( iter == 0 ) {
|
|
1539
|
+
ginit = gnorm1new ;
|
|
1540
|
+
} else {
|
|
1541
|
+
0 ;
|
|
1542
|
+
} ;
|
|
1543
|
+
iter = (iter+1) ;
|
|
1544
|
+
if( gnorm1new <= (tol*ginit) ) {
|
|
1545
|
+
if( active == n ) {
|
|
1546
|
+
done = 1 ;
|
|
1547
|
+
} else {
|
|
1548
|
+
active = n ;
|
|
1549
|
+
gmaxold = 1e+300 ;
|
|
1550
|
+
} ;
|
|
1551
|
+
} else {
|
|
1552
|
+
gmaxold = gmaxnew ;
|
|
1553
|
+
} ;
|
|
1554
|
+
;
|
|
1555
|
+
} ;
|
|
1556
|
+
return iter ;
|
|
1557
|
+
}
|
|
1558
|
+
|
|
1559
|
+
|
|
1560
|
+
SCM2CPP_FN inline double
|
|
1561
|
+
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 )
|
|
1562
|
+
{ double ym = 0.0;
|
|
1563
|
+
if( p > 65536 ) {
|
|
1564
|
+
#pragma omp parallel for
|
|
1565
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1566
|
+
xm[ j ] = 0.0 ;
|
|
1567
|
+
}
|
|
1568
|
+
} else {
|
|
1569
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1570
|
+
xm[ j ] = 0.0 ;
|
|
1571
|
+
}
|
|
1572
|
+
} ;
|
|
1573
|
+
for( int r = 0 ; !(r == m) ; r = (r+1) ) {
|
|
1574
|
+
int i = rows[r];
|
|
1575
|
+
ym = (ym+y[i]) ;
|
|
1576
|
+
if( p > 65536 ) {
|
|
1577
|
+
#pragma omp parallel for
|
|
1578
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1579
|
+
xm[ j ] = (xm[j]+x[((i*p)+j)]) ;
|
|
1580
|
+
}
|
|
1581
|
+
} else {
|
|
1582
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1583
|
+
xm[ j ] = (xm[j]+x[((i*p)+j)]) ;
|
|
1584
|
+
}
|
|
1585
|
+
} ;
|
|
1586
|
+
;
|
|
1587
|
+
} ;
|
|
1588
|
+
ym = (ym/(1.0*m)) ;
|
|
1589
|
+
if( p > 65536 ) {
|
|
1590
|
+
#pragma omp parallel for
|
|
1591
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1592
|
+
xm[ j ] = (xm[j]/(1.0*m)) ;
|
|
1593
|
+
}
|
|
1594
|
+
} else {
|
|
1595
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1596
|
+
xm[ j ] = (xm[j]/(1.0*m)) ;
|
|
1597
|
+
}
|
|
1598
|
+
} ;
|
|
1599
|
+
if( (p*p) > 65536 ) {
|
|
1600
|
+
#pragma omp parallel for
|
|
1601
|
+
for( int t = 0 ; t < (p*p) ; t += 1 ) {
|
|
1602
|
+
g[ t ] = 0.0 ;
|
|
1603
|
+
}
|
|
1604
|
+
} else {
|
|
1605
|
+
for( int t = 0 ; t < (p*p) ; t += 1 ) {
|
|
1606
|
+
g[ t ] = 0.0 ;
|
|
1607
|
+
}
|
|
1608
|
+
} ;
|
|
1609
|
+
if( p > 65536 ) {
|
|
1610
|
+
#pragma omp parallel for
|
|
1611
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1612
|
+
c[ j ] = 0.0 ;
|
|
1613
|
+
}
|
|
1614
|
+
} else {
|
|
1615
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1616
|
+
c[ j ] = 0.0 ;
|
|
1617
|
+
}
|
|
1618
|
+
} ;
|
|
1619
|
+
for( int r = 0 ; !(r == m) ; r = (r+1) ) {
|
|
1620
|
+
int i = rows[r];
|
|
1621
|
+
double yc = (y[i]-ym);
|
|
1622
|
+
for( int a = 0 ; !(a == p) ; a = (a+1) ) {
|
|
1623
|
+
double xa = (x[((i*p)+a)]-xm[a]);
|
|
1624
|
+
c[ a ] = (c[a]+(xa*yc)) ;
|
|
1625
|
+
if( (a+1) > 65536 ) {
|
|
1626
|
+
#pragma omp parallel for
|
|
1627
|
+
for( int b = 0 ; b <= a ; b += 1 ) {
|
|
1628
|
+
double xb = (x[((i*p)+b)]-xm[b]);
|
|
1629
|
+
g[ ((a*p)+b) ] = (g[((a*p)+b)]+(xa*xb)) ;
|
|
1630
|
+
;
|
|
1631
|
+
}
|
|
1632
|
+
} else {
|
|
1633
|
+
for( int b = 0 ; b <= a ; b += 1 ) {
|
|
1634
|
+
double xb = (x[((i*p)+b)]-xm[b]);
|
|
1635
|
+
g[ ((a*p)+b) ] = (g[((a*p)+b)]+(xa*xb)) ;
|
|
1636
|
+
;
|
|
1637
|
+
}
|
|
1638
|
+
} ;
|
|
1639
|
+
;
|
|
1640
|
+
} ;
|
|
1641
|
+
;
|
|
1642
|
+
;
|
|
1643
|
+
} ;
|
|
1644
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1645
|
+
double s = g[((j*p)+j)];
|
|
1646
|
+
for( int k = 0 ; !(k == j) ; k = (k+1) ) {
|
|
1647
|
+
s = (s-(l[((j*p)+k)]*l[((j*p)+k)])) ;
|
|
1648
|
+
} ;
|
|
1649
|
+
l[ ((j*p)+j) ] = sqrt(( ( s > 0.0 ) ? (s) : (1e-300) )) ;
|
|
1650
|
+
for( int i = (j+1) ; !(i >= p) ; i = (i+1) ) {
|
|
1651
|
+
double t = g[((i*p)+j)];
|
|
1652
|
+
for( int k = 0 ; !(k == j) ; k = (k+1) ) {
|
|
1653
|
+
t = (t-(l[((i*p)+k)]*l[((j*p)+k)])) ;
|
|
1654
|
+
} ;
|
|
1655
|
+
l[ ((i*p)+j) ] = (t/l[((j*p)+j)]) ;
|
|
1656
|
+
;
|
|
1657
|
+
} ;
|
|
1658
|
+
;
|
|
1659
|
+
} ;
|
|
1660
|
+
for( int i = 0 ; !(i == p) ; i = (i+1) ) {
|
|
1661
|
+
double t = c[i];
|
|
1662
|
+
for( int k = 0 ; !(k == i) ; k = (k+1) ) {
|
|
1663
|
+
t = (t-(l[((i*p)+k)]*z[k])) ;
|
|
1664
|
+
} ;
|
|
1665
|
+
z[ i ] = (t/l[((i*p)+i)]) ;
|
|
1666
|
+
;
|
|
1667
|
+
} ;
|
|
1668
|
+
for( int ii = 0 ; !(ii == p) ; ii = (ii+1) ) {
|
|
1669
|
+
int i = ((p-1)-ii);
|
|
1670
|
+
double t = z[i];
|
|
1671
|
+
for( int k = (i+1) ; !(k >= p) ; k = (k+1) ) {
|
|
1672
|
+
t = (t-(l[((k*p)+i)]*w[k])) ;
|
|
1673
|
+
} ;
|
|
1674
|
+
w[ i ] = (t/l[((i*p)+i)]) ;
|
|
1675
|
+
;
|
|
1676
|
+
;
|
|
1677
|
+
} ;
|
|
1678
|
+
double b = ym;
|
|
1679
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1680
|
+
b = (b-(xm[j]*w[j])) ;
|
|
1681
|
+
} ;
|
|
1682
|
+
return b ;
|
|
1683
|
+
}
|
|
1684
|
+
|
|
1685
|
+
|
|
1686
|
+
inline int
|
|
1687
|
+
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 )
|
|
1688
|
+
{ std::vector<int> rows(n,0);
|
|
1689
|
+
std::vector<int> inl(n,0);
|
|
1690
|
+
std::vector<double> w(p,0.0);
|
|
1691
|
+
std::vector<double> g((p*p),0.0);
|
|
1692
|
+
std::vector<double> c(p,0.0);
|
|
1693
|
+
std::vector<double> l((p*p),0.0);
|
|
1694
|
+
std::vector<double> z(p,0.0);
|
|
1695
|
+
std::vector<double> xm(p,0.0);
|
|
1696
|
+
int nbest = 1;
|
|
1697
|
+
double scorebest = -1e+300;
|
|
1698
|
+
int found = 0;
|
|
1699
|
+
int trials = 0;
|
|
1700
|
+
double limit = (1.0*maxtrials);
|
|
1701
|
+
double eps = 2.220446049250313e-16;
|
|
1702
|
+
for( ; !((1.0*trials) >= limit) ; ) {
|
|
1703
|
+
int t = trials;
|
|
1704
|
+
trials = (trials+1) ;
|
|
1705
|
+
if( minsamples > 65536 ) {
|
|
1706
|
+
#pragma omp parallel for
|
|
1707
|
+
for( int r = 0 ; r < minsamples ; r += 1 ) {
|
|
1708
|
+
rows[ r ] = subsets[((t*minsamples)+r)] ;
|
|
1709
|
+
}
|
|
1710
|
+
} else {
|
|
1711
|
+
for( int r = 0 ; r < minsamples ; r += 1 ) {
|
|
1712
|
+
rows[ r ] = subsets[((t*minsamples)+r)] ;
|
|
1713
|
+
}
|
|
1714
|
+
} ;
|
|
1715
|
+
double b = ols_rows(x,y,rows,minsamples,w,g,c,l,z,xm,n,p);
|
|
1716
|
+
int ninl = 0;
|
|
1717
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
1718
|
+
double pred = b;
|
|
1719
|
+
if( p > 65536 ) {
|
|
1720
|
+
#pragma omp parallel for reduction(+:pred)
|
|
1721
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1722
|
+
pred = (pred+(x[((i*p)+j)]*w[j])) ;
|
|
1723
|
+
}
|
|
1724
|
+
} else {
|
|
1725
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1726
|
+
pred = (pred+(x[((i*p)+j)]*w[j])) ;
|
|
1727
|
+
}
|
|
1728
|
+
} ;
|
|
1729
|
+
double res = (y[i]-pred);
|
|
1730
|
+
if( ( ( res < 0.0 ) ? ((0.0-res)) : (res) ) <= thresh ) {
|
|
1731
|
+
inl[ ninl ] = i ;
|
|
1732
|
+
ninl = (ninl+1) ;
|
|
1733
|
+
} else {
|
|
1734
|
+
0 ;
|
|
1735
|
+
} ;
|
|
1736
|
+
;
|
|
1737
|
+
;
|
|
1738
|
+
} ;
|
|
1739
|
+
if( ninl >= nbest ) {
|
|
1740
|
+
double ym = 0.0;
|
|
1741
|
+
double sstot = 0.0;
|
|
1742
|
+
double ssres = 0.0;
|
|
1743
|
+
if( ninl > 65536 ) {
|
|
1744
|
+
#pragma omp parallel for reduction(+:ym)
|
|
1745
|
+
for( int r = 0 ; r < ninl ; r += 1 ) {
|
|
1746
|
+
ym = (ym+y[inl[r]]) ;
|
|
1747
|
+
}
|
|
1748
|
+
} else {
|
|
1749
|
+
for( int r = 0 ; r < ninl ; r += 1 ) {
|
|
1750
|
+
ym = (ym+y[inl[r]]) ;
|
|
1751
|
+
}
|
|
1752
|
+
} ;
|
|
1753
|
+
ym = (ym/(1.0*ninl)) ;
|
|
1754
|
+
if( (ninl*p) > 65536 ) {
|
|
1755
|
+
#pragma omp parallel for reduction(+:ssres) reduction(+:sstot)
|
|
1756
|
+
for( int r = 0 ; r < ninl ; r += 1 ) {
|
|
1757
|
+
int i = inl[r];
|
|
1758
|
+
double pred = b;
|
|
1759
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1760
|
+
pred = (pred+(x[((i*p)+j)]*w[j])) ;
|
|
1761
|
+
} ;
|
|
1762
|
+
double dr = (y[i]-pred);
|
|
1763
|
+
double dt = (y[i]-ym);
|
|
1764
|
+
ssres = (ssres+(dr*dr)) ;
|
|
1765
|
+
sstot = (sstot+(dt*dt)) ;
|
|
1766
|
+
;
|
|
1767
|
+
;
|
|
1768
|
+
;
|
|
1769
|
+
}
|
|
1770
|
+
} else {
|
|
1771
|
+
for( int r = 0 ; r < ninl ; r += 1 ) {
|
|
1772
|
+
int i = inl[r];
|
|
1773
|
+
double pred = b;
|
|
1774
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
1775
|
+
pred = (pred+(x[((i*p)+j)]*w[j])) ;
|
|
1776
|
+
} ;
|
|
1777
|
+
double dr = (y[i]-pred);
|
|
1778
|
+
double dt = (y[i]-ym);
|
|
1779
|
+
ssres = (ssres+(dr*dr)) ;
|
|
1780
|
+
sstot = (sstot+(dt*dt)) ;
|
|
1781
|
+
;
|
|
1782
|
+
;
|
|
1783
|
+
;
|
|
1784
|
+
}
|
|
1785
|
+
} ;
|
|
1786
|
+
double score = ( ( sstot > 0.0 ) ? ((1.0-(ssres/sstot))) : (( ( ssres > 0.0 ) ? (0.0) : (1.0) )) );
|
|
1787
|
+
if( !((ninl == nbest && score < scorebest)) ) {
|
|
1788
|
+
found = 1 ;
|
|
1789
|
+
nbest = ninl ;
|
|
1790
|
+
scorebest = score ;
|
|
1791
|
+
if( n > 65536 ) {
|
|
1792
|
+
#pragma omp parallel for
|
|
1793
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1794
|
+
mask[ i ] = 0.0 ;
|
|
1795
|
+
}
|
|
1796
|
+
} else {
|
|
1797
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
1798
|
+
mask[ i ] = 0.0 ;
|
|
1799
|
+
}
|
|
1800
|
+
} ;
|
|
1801
|
+
for( int r = 0 ; !(r == ninl) ; r = (r+1) ) {
|
|
1802
|
+
mask[ inl[r] ] = 1.0 ;
|
|
1803
|
+
} ;
|
|
1804
|
+
double ratio = ((1.0*nbest)/(1.0*n));
|
|
1805
|
+
double nom = ( ( (1.0-stopprob) > eps ) ? ((1.0-stopprob)) : (eps) );
|
|
1806
|
+
struct let6109 {
|
|
1807
|
+
double & ratio;
|
|
1808
|
+
int & minsamples;
|
|
1809
|
+
let6109( double & ratio, int & minsamples ):ratio(ratio),minsamples(minsamples) {}
|
|
1810
|
+
double operator()( double acc )
|
|
1811
|
+
{ if( minsamples > 65536 ) {
|
|
1812
|
+
#pragma omp parallel for reduction(*:acc)
|
|
1813
|
+
for( int s = 0 ; s < minsamples ; s += 1 ) {
|
|
1814
|
+
acc = (acc*ratio) ;
|
|
1815
|
+
}
|
|
1816
|
+
} else {
|
|
1817
|
+
for( int s = 0 ; s < minsamples ; s += 1 ) {
|
|
1818
|
+
acc = (acc*ratio) ;
|
|
1819
|
+
}
|
|
1820
|
+
} ;
|
|
1821
|
+
return acc ;
|
|
1822
|
+
}
|
|
1823
|
+
} ;
|
|
1824
|
+
double pw = let6109(ratio,minsamples)(1.0);
|
|
1825
|
+
double denom = ( ( (1.0-pw) > eps ) ? ((1.0-pw)) : (eps) );
|
|
1826
|
+
struct let6137 {
|
|
1827
|
+
let6137() {}
|
|
1828
|
+
double operator()( double q )
|
|
1829
|
+
{
|
|
1830
|
+
double cq = (0.0-std::floor((0.0-q)));
|
|
1831
|
+
if( cq < 0.0 ) {
|
|
1832
|
+
return (0.0-cq) ;
|
|
1833
|
+
}
|
|
1834
|
+
else {
|
|
1835
|
+
return cq ;
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
} ;
|
|
1839
|
+
double dyn = ( ( nom == 1.0 ) ? (0.0) : (( ( denom == 1.0 ) ? (1e+300) : (let6137()((log(nom)/log(denom)))) )) );
|
|
1840
|
+
if( dyn < limit ) {
|
|
1841
|
+
limit = dyn ;
|
|
1842
|
+
} else {
|
|
1843
|
+
0 ;
|
|
1844
|
+
} ;
|
|
1845
|
+
;
|
|
1846
|
+
;
|
|
1847
|
+
;
|
|
1848
|
+
;
|
|
1849
|
+
} else {
|
|
1850
|
+
0 ;
|
|
1851
|
+
} ;
|
|
1852
|
+
;
|
|
1853
|
+
;
|
|
1854
|
+
} else {
|
|
1855
|
+
0 ;
|
|
1856
|
+
} ;
|
|
1857
|
+
;
|
|
1858
|
+
;
|
|
1859
|
+
} ;
|
|
1860
|
+
int m = 0;
|
|
1861
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
1862
|
+
if( mask[i] == 1.0 ) {
|
|
1863
|
+
rows[ m ] = i ;
|
|
1864
|
+
m = (m+1) ;
|
|
1865
|
+
} else {
|
|
1866
|
+
0 ;
|
|
1867
|
+
} ;
|
|
1868
|
+
} ;
|
|
1869
|
+
out[ 0 ] = ols_rows(x,y,rows,m,coef,g,c,l,z,xm,n,p) ;
|
|
1870
|
+
;
|
|
1871
|
+
return trials ;
|
|
1872
|
+
}
|
|
1873
|
+
|
|
1874
|
+
|
|
1875
|
+
SCM2CPP_FN inline double
|
|
1876
|
+
sgd_loss( int loss, double y, double p, double epsilon )
|
|
1877
|
+
{
|
|
1878
|
+
if( loss == 1 ) {
|
|
1879
|
+
return (0.5*((p-y)*(p-y))) ;
|
|
1880
|
+
}
|
|
1881
|
+
else {
|
|
1882
|
+
if( loss == 3 ) {
|
|
1883
|
+
double r = (( ( (y-p) < 0.0 ) ? ((p-y)) : ((y-p)) )-epsilon);
|
|
1884
|
+
if( r > 0.0 ) {
|
|
1885
|
+
return r ;
|
|
1886
|
+
}
|
|
1887
|
+
else {
|
|
1888
|
+
return 0.0 ;
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
else {
|
|
1892
|
+
double th = ( ( loss == 0 ) ? (1.0) : (0.0) );
|
|
1893
|
+
double z = (p*y);
|
|
1894
|
+
if( z <= th ) {
|
|
1895
|
+
return (th-z) ;
|
|
1896
|
+
}
|
|
1897
|
+
else {
|
|
1898
|
+
return 0.0 ;
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
}
|
|
1903
|
+
|
|
1904
|
+
|
|
1905
|
+
SCM2CPP_FN inline double
|
|
1906
|
+
sgd_grad( int loss, double y, double p, double epsilon )
|
|
1907
|
+
{
|
|
1908
|
+
if( loss == 1 ) {
|
|
1909
|
+
return (p-y) ;
|
|
1910
|
+
}
|
|
1911
|
+
else {
|
|
1912
|
+
if( loss == 3 ) {
|
|
1913
|
+
if( (y-p) > epsilon ) {
|
|
1914
|
+
return -1.0 ;
|
|
1915
|
+
}
|
|
1916
|
+
else {
|
|
1917
|
+
if( (p-y) > epsilon ) {
|
|
1918
|
+
return 1.0 ;
|
|
1919
|
+
}
|
|
1920
|
+
else {
|
|
1921
|
+
return 0.0 ;
|
|
1922
|
+
}
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
else {
|
|
1926
|
+
double th2 = ( ( loss == 0 ) ? (1.0) : (0.0) );
|
|
1927
|
+
double z2 = (p*y);
|
|
1928
|
+
if( z2 <= th2 ) {
|
|
1929
|
+
return (0.0-y) ;
|
|
1930
|
+
}
|
|
1931
|
+
else {
|
|
1932
|
+
return 0.0 ;
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
}
|
|
1937
|
+
|
|
1938
|
+
|
|
1939
|
+
SCM2CPP_FN inline int
|
|
1940
|
+
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 )
|
|
1941
|
+
{ double wscale = 1.0;
|
|
1942
|
+
double sqnorm = 0.0;
|
|
1943
|
+
double b = 0.0;
|
|
1944
|
+
double t = 1.0;
|
|
1945
|
+
double optinit = 0.0;
|
|
1946
|
+
double best = 1e+300;
|
|
1947
|
+
int noimp = 0;
|
|
1948
|
+
int epoch = 0;
|
|
1949
|
+
int done = 0;
|
|
1950
|
+
if( p > 65536 ) {
|
|
1951
|
+
#pragma omp parallel for reduction(+:sqnorm)
|
|
1952
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1953
|
+
sqnorm = (sqnorm+(w[j]*w[j])) ;
|
|
1954
|
+
}
|
|
1955
|
+
} else {
|
|
1956
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1957
|
+
sqnorm = (sqnorm+(w[j]*w[j])) ;
|
|
1958
|
+
}
|
|
1959
|
+
} ;
|
|
1960
|
+
if( lr == 0 ) {
|
|
1961
|
+
double typw = sqrt((1.0/sqrt(alpha)));
|
|
1962
|
+
double g = sgd_grad(loss,1.0,double((0.0-typw)),epsilon);
|
|
1963
|
+
double eta00 = (typw/( ( g > 1.0 ) ? (g) : (1.0) ));
|
|
1964
|
+
optinit = (1.0/(eta00*alpha)) ;
|
|
1965
|
+
;
|
|
1966
|
+
;
|
|
1967
|
+
;
|
|
1968
|
+
} else {
|
|
1969
|
+
0 ;
|
|
1970
|
+
} ;
|
|
1971
|
+
for( ; !((done == 1 || epoch >= maxiter)) ; ) {
|
|
1972
|
+
double objsum = 0.0;
|
|
1973
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
1974
|
+
double dot = 0.0;
|
|
1975
|
+
double sx = 0.0;
|
|
1976
|
+
if( p > 65536 ) {
|
|
1977
|
+
#pragma omp parallel for reduction(+:dot)
|
|
1978
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1979
|
+
dot = (dot+(x[((i*p)+j)]*w[j])) ;
|
|
1980
|
+
}
|
|
1981
|
+
} else {
|
|
1982
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1983
|
+
dot = (dot+(x[((i*p)+j)]*w[j])) ;
|
|
1984
|
+
}
|
|
1985
|
+
} ;
|
|
1986
|
+
if( lr >= 3 ) {
|
|
1987
|
+
if( p > 65536 ) {
|
|
1988
|
+
#pragma omp parallel for reduction(+:sx)
|
|
1989
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1990
|
+
sx = (sx+(x[((i*p)+j)]*x[((i*p)+j)])) ;
|
|
1991
|
+
}
|
|
1992
|
+
} else {
|
|
1993
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
1994
|
+
sx = (sx+(x[((i*p)+j)]*x[((i*p)+j)])) ;
|
|
1995
|
+
}
|
|
1996
|
+
} ;
|
|
1997
|
+
} else {
|
|
1998
|
+
0 ;
|
|
1999
|
+
} ;
|
|
2000
|
+
double pr = ((wscale*dot)+b);
|
|
2001
|
+
double yi = y[i];
|
|
2002
|
+
double eta = ( ( lr == 0 ) ? ((1.0/(alpha*((optinit+t)-1.0)))) : (( ( lr == 1 ) ? (((1.0*eta0)/std::pow(t, (1.0*powert)))) : (eta0) )) );
|
|
2003
|
+
double li = sgd_loss(loss,yi,pr,epsilon);
|
|
2004
|
+
double dl = ( ( lr < 3 ) ? (sgd_grad(loss,yi,pr,epsilon)) : (0.0) );
|
|
2005
|
+
objsum = (objsum+li) ;
|
|
2006
|
+
if( (lr < 3 && pen == 1) ) {
|
|
2007
|
+
objsum = (objsum+(alpha*(0.5*sqnorm))) ;
|
|
2008
|
+
} else {
|
|
2009
|
+
0 ;
|
|
2010
|
+
} ;
|
|
2011
|
+
if( !((lr == 3 && sx == 0.0)) ) {
|
|
2012
|
+
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) )) ))) )) );
|
|
2013
|
+
double update = ( ( lr >= 3 ) ? (( ( (loss == 0 || loss == 2) ) ? ((step*yi)) : (( ( (yi-pr) < 0.0 ) ? ((0.0-step)) : (step) )) )) : (step) );
|
|
2014
|
+
if( pen == 1 ) {
|
|
2015
|
+
double c = (1.0-(eta*alpha));
|
|
2016
|
+
double cc = ( ( c > 0.0 ) ? (c) : (0.0) );
|
|
2017
|
+
wscale = (wscale*cc) ;
|
|
2018
|
+
sqnorm = (sqnorm*(cc*cc)) ;
|
|
2019
|
+
if( wscale < 1e-9 ) {
|
|
2020
|
+
if( p > 65536 ) {
|
|
2021
|
+
#pragma omp parallel for
|
|
2022
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2023
|
+
w[ j ] = (w[j]*wscale) ;
|
|
2024
|
+
}
|
|
2025
|
+
} else {
|
|
2026
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2027
|
+
w[ j ] = (w[j]*wscale) ;
|
|
2028
|
+
}
|
|
2029
|
+
} ;
|
|
2030
|
+
wscale = 1.0 ;
|
|
2031
|
+
} else {
|
|
2032
|
+
0 ;
|
|
2033
|
+
} ;
|
|
2034
|
+
;
|
|
2035
|
+
;
|
|
2036
|
+
} else {
|
|
2037
|
+
0 ;
|
|
2038
|
+
} ;
|
|
2039
|
+
if( !(update == 0.0) ) {
|
|
2040
|
+
double acc = 0.0;
|
|
2041
|
+
if( p > 65536 ) {
|
|
2042
|
+
#pragma omp parallel for reduction(+:acc)
|
|
2043
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2044
|
+
double v = (w[j]+(x[((i*p)+j)]*(update/wscale)));
|
|
2045
|
+
w[ j ] = v ;
|
|
2046
|
+
acc = (acc+(v*v)) ;
|
|
2047
|
+
;
|
|
2048
|
+
}
|
|
2049
|
+
} else {
|
|
2050
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2051
|
+
double v = (w[j]+(x[((i*p)+j)]*(update/wscale)));
|
|
2052
|
+
w[ j ] = v ;
|
|
2053
|
+
acc = (acc+(v*v)) ;
|
|
2054
|
+
;
|
|
2055
|
+
}
|
|
2056
|
+
} ;
|
|
2057
|
+
sqnorm = (acc*(wscale*wscale)) ;
|
|
2058
|
+
;
|
|
2059
|
+
} else {
|
|
2060
|
+
0 ;
|
|
2061
|
+
} ;
|
|
2062
|
+
if( (fitint == 1 && !(update == 0.0)) ) {
|
|
2063
|
+
b = (b+update) ;
|
|
2064
|
+
} else {
|
|
2065
|
+
0 ;
|
|
2066
|
+
} ;
|
|
2067
|
+
;
|
|
2068
|
+
;
|
|
2069
|
+
t = (t+1.0) ;
|
|
2070
|
+
} else {
|
|
2071
|
+
0 ;
|
|
2072
|
+
} ;
|
|
2073
|
+
;
|
|
2074
|
+
;
|
|
2075
|
+
;
|
|
2076
|
+
} ;
|
|
2077
|
+
epoch = (epoch+1) ;
|
|
2078
|
+
double mean = (objsum/(1.0*n));
|
|
2079
|
+
if( mean > (best-tol) ) {
|
|
2080
|
+
noimp = (noimp+1) ;
|
|
2081
|
+
} else {
|
|
2082
|
+
noimp = 0 ;
|
|
2083
|
+
} ;
|
|
2084
|
+
if( mean < best ) {
|
|
2085
|
+
best = mean ;
|
|
2086
|
+
} else {
|
|
2087
|
+
0 ;
|
|
2088
|
+
} ;
|
|
2089
|
+
;
|
|
2090
|
+
if( noimp >= niternochange ) {
|
|
2091
|
+
done = 1 ;
|
|
2092
|
+
} else {
|
|
2093
|
+
0 ;
|
|
2094
|
+
} ;
|
|
2095
|
+
;
|
|
2096
|
+
} ;
|
|
2097
|
+
if( p > 65536 ) {
|
|
2098
|
+
#pragma omp parallel for
|
|
2099
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2100
|
+
w[ j ] = (w[j]*wscale) ;
|
|
2101
|
+
}
|
|
2102
|
+
} else {
|
|
2103
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2104
|
+
w[ j ] = (w[j]*wscale) ;
|
|
2105
|
+
}
|
|
2106
|
+
} ;
|
|
2107
|
+
out[ 0 ] = b ;
|
|
2108
|
+
return epoch ;
|
|
2109
|
+
}
|
|
2110
|
+
|
|
2111
|
+
|
|
2112
|
+
SCM2CPP_FN inline double
|
|
2113
|
+
sgd_min( double a, double b )
|
|
2114
|
+
{
|
|
2115
|
+
if( a < b ) {
|
|
2116
|
+
return a ;
|
|
2117
|
+
}
|
|
2118
|
+
else {
|
|
2119
|
+
return b ;
|
|
2120
|
+
}
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
|
|
2124
|
+
SCM2CPP_FN inline double
|
|
2125
|
+
min_pos( scm2cpp::cspan<double> v, int m )
|
|
2126
|
+
{ double r = 1.7976931348623157e+308;
|
|
2127
|
+
for( int i = 0 ; !(i == m) ; i = (i+1) ) {
|
|
2128
|
+
double x = v[i];
|
|
2129
|
+
if( (x > 0.0 && x < r) ) {
|
|
2130
|
+
r = x ;
|
|
2131
|
+
} else {
|
|
2132
|
+
0 ;
|
|
2133
|
+
} ;
|
|
2134
|
+
;
|
|
2135
|
+
} ;
|
|
2136
|
+
return r ;
|
|
2137
|
+
}
|
|
2138
|
+
|
|
2139
|
+
|
|
2140
|
+
SCM2CPP_FN inline double
|
|
2141
|
+
round_even( double y )
|
|
2142
|
+
{
|
|
2143
|
+
double r = std::floor(y);
|
|
2144
|
+
double d = (y-r);
|
|
2145
|
+
if( d > 0.5 ) {
|
|
2146
|
+
return (r+1.0) ;
|
|
2147
|
+
}
|
|
2148
|
+
else {
|
|
2149
|
+
if( d < 0.5 ) {
|
|
2150
|
+
return r ;
|
|
2151
|
+
}
|
|
2152
|
+
else {
|
|
2153
|
+
if( (2.0*std::floor((0.5*r))) == r ) {
|
|
2154
|
+
return r ;
|
|
2155
|
+
}
|
|
2156
|
+
else {
|
|
2157
|
+
return (r+1.0) ;
|
|
2158
|
+
}
|
|
2159
|
+
}
|
|
2160
|
+
}
|
|
2161
|
+
}
|
|
2162
|
+
|
|
2163
|
+
|
|
2164
|
+
SCM2CPP_FN inline int
|
|
2165
|
+
cholesky_delete( scm2cpp::span<double> l, int go, int n, int k )
|
|
2166
|
+
{ for( int i = go ; !(i >= (n-1)) ; i = (i+1) ) {
|
|
2167
|
+
for( int j = 0 ; !(j == (i+2)) ; j = (j+1) ) {
|
|
2168
|
+
l[ ((i*k)+j) ] = l[(((i+1)*k)+j)] ;
|
|
2169
|
+
} ;
|
|
2170
|
+
} ;
|
|
2171
|
+
for( int i = go ; !(i >= (n-1)) ; i = (i+1) ) {
|
|
2172
|
+
double a = l[((i*k)+i)];
|
|
2173
|
+
double b = l[((i*k)+(i+1))];
|
|
2174
|
+
double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
|
|
2175
|
+
double ab = ( ( b < 0.0 ) ? ((0.0-b)) : (b) );
|
|
2176
|
+
double roe = ( ( aa > ab ) ? (a) : (b) );
|
|
2177
|
+
double scale = (aa+ab);
|
|
2178
|
+
double r = ( ( scale == 0.0 ) ? (0.0) : ((( ( roe < 0.0 ) ? (-1.0) : (1.0) )*(scale*sqrt((((a/scale)*(a/scale))+((b/scale)*(b/scale))))))) );
|
|
2179
|
+
double c = ( ( scale == 0.0 ) ? (1.0) : ((a/r)) );
|
|
2180
|
+
double s = ( ( scale == 0.0 ) ? (0.0) : ((b/r)) );
|
|
2181
|
+
double cc = ( ( r < 0.0 ) ? ((0.0-c)) : (c) );
|
|
2182
|
+
double ss = ( ( r < 0.0 ) ? ((0.0-s)) : (s) );
|
|
2183
|
+
l[ ((i*k)+i) ] = ( ( r < 0.0 ) ? ((0.0-r)) : (r) ) ;
|
|
2184
|
+
l[ ((i*k)+(i+1)) ] = 0.0 ;
|
|
2185
|
+
for( int t = (i+1) ; !(t >= n) ; t = (t+1) ) {
|
|
2186
|
+
double x = l[((t*k)+i)];
|
|
2187
|
+
double y = l[((t*k)+(i+1))];
|
|
2188
|
+
l[ ((t*k)+i) ] = ((cc*x)+(ss*y)) ;
|
|
2189
|
+
l[ ((t*k)+(i+1)) ] = ((cc*y)-(ss*x)) ;
|
|
2190
|
+
;
|
|
2191
|
+
} ;
|
|
2192
|
+
;
|
|
2193
|
+
;
|
|
2194
|
+
;
|
|
2195
|
+
;
|
|
2196
|
+
;
|
|
2197
|
+
;
|
|
2198
|
+
} ;
|
|
2199
|
+
return 0 ;
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
|
|
2203
|
+
SCM2CPP_FN inline int
|
|
2204
|
+
swap_rc( scm2cpp::span<double> g, int a, int b, int p )
|
|
2205
|
+
{ for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
2206
|
+
double rv = g[((a*p)+j)];
|
|
2207
|
+
g[ ((a*p)+j) ] = g[((b*p)+j)] ;
|
|
2208
|
+
g[ ((b*p)+j) ] = rv ;
|
|
2209
|
+
;
|
|
2210
|
+
} ;
|
|
2211
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
2212
|
+
double cv = g[((j*p)+a)];
|
|
2213
|
+
g[ ((j*p)+a) ] = g[((j*p)+b)] ;
|
|
2214
|
+
g[ ((j*p)+b) ] = cv ;
|
|
2215
|
+
;
|
|
2216
|
+
} ;
|
|
2217
|
+
return 0 ;
|
|
2218
|
+
}
|
|
2219
|
+
|
|
2220
|
+
|
|
2221
|
+
inline int
|
|
2222
|
+
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 )
|
|
2223
|
+
{ std::vector<double> g0((p*p),0.0);
|
|
2224
|
+
std::vector<double> cov0(p,0.0);
|
|
2225
|
+
std::vector<double> cbuf(p,0.0);
|
|
2226
|
+
std::vector<int> index(p,0);
|
|
2227
|
+
std::vector<int> active(p,0);
|
|
2228
|
+
std::vector<double> sign(p,0.0);
|
|
2229
|
+
int kmax = ( ( maxiter < p ) ? (maxiter) : (p) );
|
|
2230
|
+
std::vector<double> prev(p,0.0);
|
|
2231
|
+
std::vector<double> dir(p,0.0);
|
|
2232
|
+
std::vector<double> ls(p,0.0);
|
|
2233
|
+
std::vector<double> zz(p,0.0);
|
|
2234
|
+
std::vector<double> ratio(p,0.0);
|
|
2235
|
+
std::vector<int> idx(p,0);
|
|
2236
|
+
std::vector<int> dropv(p,0);
|
|
2237
|
+
int cs = 0;
|
|
2238
|
+
int na = 0;
|
|
2239
|
+
int niter = 0;
|
|
2240
|
+
int drop = 0;
|
|
2241
|
+
int done = 0;
|
|
2242
|
+
double alpha = 0.0;
|
|
2243
|
+
double prevalpha = 0.0;
|
|
2244
|
+
double tiny32 = 1.1754943508222875e-38;
|
|
2245
|
+
double eqtol = 1.1920928955078125e-7;
|
|
2246
|
+
std::vector<double> l((kmax*kmax),0.0);
|
|
2247
|
+
if( (p*p) > 65536 ) {
|
|
2248
|
+
#pragma omp parallel for
|
|
2249
|
+
for( int t = 0 ; t < (p*p) ; t += 1 ) {
|
|
2250
|
+
g0[ t ] = g[t] ;
|
|
2251
|
+
}
|
|
2252
|
+
} else {
|
|
2253
|
+
for( int t = 0 ; t < (p*p) ; t += 1 ) {
|
|
2254
|
+
g0[ t ] = g[t] ;
|
|
2255
|
+
}
|
|
2256
|
+
} ;
|
|
2257
|
+
if( p > 65536 ) {
|
|
2258
|
+
#pragma omp parallel for
|
|
2259
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2260
|
+
cov0[ j ] = cov[j] ;
|
|
2261
|
+
cbuf[ j ] = cov[j] ;
|
|
2262
|
+
index[ j ] = j ;
|
|
2263
|
+
coef[ j ] = 0.0 ;
|
|
2264
|
+
if( rec == 1 ) {
|
|
2265
|
+
path[ j ] = 0.0 ;
|
|
2266
|
+
} else {
|
|
2267
|
+
0 ;
|
|
2268
|
+
} ;
|
|
2269
|
+
}
|
|
2270
|
+
} else {
|
|
2271
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2272
|
+
cov0[ j ] = cov[j] ;
|
|
2273
|
+
cbuf[ j ] = cov[j] ;
|
|
2274
|
+
index[ j ] = j ;
|
|
2275
|
+
coef[ j ] = 0.0 ;
|
|
2276
|
+
if( rec == 1 ) {
|
|
2277
|
+
path[ j ] = 0.0 ;
|
|
2278
|
+
} else {
|
|
2279
|
+
0 ;
|
|
2280
|
+
} ;
|
|
2281
|
+
}
|
|
2282
|
+
} ;
|
|
2283
|
+
for( ; !(done == 1) ; ) {
|
|
2284
|
+
int clen = (p-cs);
|
|
2285
|
+
int cidx = 0;
|
|
2286
|
+
double cval = 0.0;
|
|
2287
|
+
double cabs = 0.0;
|
|
2288
|
+
for( int t = 0 ; !(t == clen) ; t = (t+1) ) {
|
|
2289
|
+
double v = cbuf[(cs+t)];
|
|
2290
|
+
double av = ( ( v < 0.0 ) ? ((0.0-v)) : (v) );
|
|
2291
|
+
if( av > cabs ) {
|
|
2292
|
+
cabs = av ;
|
|
2293
|
+
cidx = t ;
|
|
2294
|
+
cval = v ;
|
|
2295
|
+
} else {
|
|
2296
|
+
0 ;
|
|
2297
|
+
} ;
|
|
2298
|
+
;
|
|
2299
|
+
;
|
|
2300
|
+
} ;
|
|
2301
|
+
alpha = (cabs/(1.0*nsamples)) ;
|
|
2302
|
+
if( rec == 1 ) {
|
|
2303
|
+
alphas[ niter ] = alpha ;
|
|
2304
|
+
} else {
|
|
2305
|
+
0 ;
|
|
2306
|
+
} ;
|
|
2307
|
+
if( alpha <= (alphamin+eqtol) ) {
|
|
2308
|
+
if( ( ( (alpha-alphamin) < 0.0 ) ? ((alphamin-alpha)) : ((alpha-alphamin)) ) > eqtol ) {
|
|
2309
|
+
if( niter > 0 ) {
|
|
2310
|
+
double ss = ((prevalpha-alphamin)/(prevalpha-alpha));
|
|
2311
|
+
if( p > 65536 ) {
|
|
2312
|
+
#pragma omp parallel for
|
|
2313
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2314
|
+
coef[ j ] = (prev[j]+(ss*(coef[j]-prev[j]))) ;
|
|
2315
|
+
}
|
|
2316
|
+
} else {
|
|
2317
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2318
|
+
coef[ j ] = (prev[j]+(ss*(coef[j]-prev[j]))) ;
|
|
2319
|
+
}
|
|
2320
|
+
} ;
|
|
2321
|
+
;
|
|
2322
|
+
} else {
|
|
2323
|
+
0 ;
|
|
2324
|
+
} ;
|
|
2325
|
+
alpha = alphamin ;
|
|
2326
|
+
if( rec == 1 ) {
|
|
2327
|
+
alphas[ niter ] = alphamin ;
|
|
2328
|
+
if( p > 65536 ) {
|
|
2329
|
+
#pragma omp parallel for
|
|
2330
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2331
|
+
path[ ((niter*p)+j) ] = coef[j] ;
|
|
2332
|
+
}
|
|
2333
|
+
} else {
|
|
2334
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2335
|
+
path[ ((niter*p)+j) ] = coef[j] ;
|
|
2336
|
+
}
|
|
2337
|
+
} ;
|
|
2338
|
+
} else {
|
|
2339
|
+
0 ;
|
|
2340
|
+
} ;
|
|
2341
|
+
} else {
|
|
2342
|
+
0 ;
|
|
2343
|
+
} ;
|
|
2344
|
+
done = 1 ;
|
|
2345
|
+
} else {
|
|
2346
|
+
if( (niter >= maxiter || na >= p) ) {
|
|
2347
|
+
done = 1 ;
|
|
2348
|
+
} else {
|
|
2349
|
+
int degenerate = 0;
|
|
2350
|
+
if( drop == 0 ) {
|
|
2351
|
+
int m = na;
|
|
2352
|
+
int nn = (cidx+na);
|
|
2353
|
+
sign[ na ] = ( ( cval > 0.0 ) ? (1.0) : (( ( cval < 0.0 ) ? (-1.0) : (0.0) )) ) ;
|
|
2354
|
+
double tc = cbuf[(cs+cidx)];
|
|
2355
|
+
cbuf[ (cs+cidx) ] = cbuf[cs] ;
|
|
2356
|
+
cbuf[ cs ] = tc ;
|
|
2357
|
+
;
|
|
2358
|
+
int ti = index[nn];
|
|
2359
|
+
index[ nn ] = index[m] ;
|
|
2360
|
+
index[ m ] = ti ;
|
|
2361
|
+
;
|
|
2362
|
+
cs = (cs+1) ;
|
|
2363
|
+
swap_rc(g,m,nn,p) ;
|
|
2364
|
+
double c = g[((na*p)+na)];
|
|
2365
|
+
double v = 0.0;
|
|
2366
|
+
for( int i = 0 ; !(i == na) ; i = (i+1) ) {
|
|
2367
|
+
double t = g[((na*p)+i)];
|
|
2368
|
+
for( int s = 0 ; !(s == i) ; s = (s+1) ) {
|
|
2369
|
+
t = (t-(l[((i*kmax)+s)]*l[((na*kmax)+s)])) ;
|
|
2370
|
+
} ;
|
|
2371
|
+
double li = (t/l[((i*kmax)+i)]);
|
|
2372
|
+
l[ ((na*kmax)+i) ] = li ;
|
|
2373
|
+
v = (v+(li*li)) ;
|
|
2374
|
+
;
|
|
2375
|
+
;
|
|
2376
|
+
} ;
|
|
2377
|
+
double dd = sqrt(( ( (c-v) < 0.0 ) ? ((v-c)) : ((c-v)) ));
|
|
2378
|
+
double dg = ( ( dd > (1.0*eps) ) ? (dd) : ((1.0*eps)) );
|
|
2379
|
+
l[ ((na*kmax)+na) ] = dg ;
|
|
2380
|
+
if( dg < 1e-7 ) {
|
|
2381
|
+
cs = (cs-1) ;
|
|
2382
|
+
cbuf[ cs ] = 0.0 ;
|
|
2383
|
+
double tc = cbuf[(cs+cidx)];
|
|
2384
|
+
cbuf[ (cs+cidx) ] = cbuf[cs] ;
|
|
2385
|
+
cbuf[ cs ] = tc ;
|
|
2386
|
+
;
|
|
2387
|
+
degenerate = 1 ;
|
|
2388
|
+
} else {
|
|
2389
|
+
active[ na ] = index[na] ;
|
|
2390
|
+
na = (na+1) ;
|
|
2391
|
+
} ;
|
|
2392
|
+
;
|
|
2393
|
+
;
|
|
2394
|
+
;
|
|
2395
|
+
;
|
|
2396
|
+
} else {
|
|
2397
|
+
false ;
|
|
2398
|
+
} ;
|
|
2399
|
+
if( degenerate == 0 ) {
|
|
2400
|
+
if( (method == 1 && niter > 0 && prevalpha < alpha) ) {
|
|
2401
|
+
done = 1 ;
|
|
2402
|
+
} else {
|
|
2403
|
+
double aa = 0.0;
|
|
2404
|
+
int clen2 = (p-cs);
|
|
2405
|
+
for( int i = 0 ; !(i == na) ; i = (i+1) ) {
|
|
2406
|
+
double t = sign[i];
|
|
2407
|
+
for( int s = 0 ; !(s == i) ; s = (s+1) ) {
|
|
2408
|
+
t = (t-(l[((i*kmax)+s)]*zz[s])) ;
|
|
2409
|
+
} ;
|
|
2410
|
+
zz[ i ] = (t/l[((i*kmax)+i)]) ;
|
|
2411
|
+
;
|
|
2412
|
+
} ;
|
|
2413
|
+
for( int ii = 0 ; !(ii == na) ; ii = (ii+1) ) {
|
|
2414
|
+
int i = ((na-1)-ii);
|
|
2415
|
+
double t = zz[i];
|
|
2416
|
+
for( int s = (i+1) ; !(s >= na) ; s = (s+1) ) {
|
|
2417
|
+
t = (t-(l[((s*kmax)+i)]*ls[s])) ;
|
|
2418
|
+
} ;
|
|
2419
|
+
ls[ i ] = (t/l[((i*kmax)+i)]) ;
|
|
2420
|
+
;
|
|
2421
|
+
;
|
|
2422
|
+
} ;
|
|
2423
|
+
if( (na == 1 && ls[0] == 0.0) ) {
|
|
2424
|
+
ls[ 0 ] = 1.0 ;
|
|
2425
|
+
aa = 1.0 ;
|
|
2426
|
+
} else {
|
|
2427
|
+
double sm = 0.0;
|
|
2428
|
+
if( na > 65536 ) {
|
|
2429
|
+
#pragma omp parallel for reduction(+:sm)
|
|
2430
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
2431
|
+
sm = (sm+(ls[i]*sign[i])) ;
|
|
2432
|
+
}
|
|
2433
|
+
} else {
|
|
2434
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
2435
|
+
sm = (sm+(ls[i]*sign[i])) ;
|
|
2436
|
+
}
|
|
2437
|
+
} ;
|
|
2438
|
+
aa = (1.0/sqrt(sm)) ;
|
|
2439
|
+
if( na > 65536 ) {
|
|
2440
|
+
#pragma omp parallel for
|
|
2441
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
2442
|
+
ls[ i ] = (aa*ls[i]) ;
|
|
2443
|
+
}
|
|
2444
|
+
} else {
|
|
2445
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
2446
|
+
ls[ i ] = (aa*ls[i]) ;
|
|
2447
|
+
}
|
|
2448
|
+
} ;
|
|
2449
|
+
;
|
|
2450
|
+
} ;
|
|
2451
|
+
if( (clen2*na) > 65536 ) {
|
|
2452
|
+
#pragma omp parallel for
|
|
2453
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2454
|
+
double acc = 0.0;
|
|
2455
|
+
for( int i = 0 ; !(i == na) ; i = (i+1) ) {
|
|
2456
|
+
acc = (acc+(g[((i*p)+(na+t))]*ls[i])) ;
|
|
2457
|
+
} ;
|
|
2458
|
+
dir[ t ] = (round_even(double((acc*1e+15)))/1e+15) ;
|
|
2459
|
+
;
|
|
2460
|
+
}
|
|
2461
|
+
} else {
|
|
2462
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2463
|
+
double acc = 0.0;
|
|
2464
|
+
for( int i = 0 ; !(i == na) ; i = (i+1) ) {
|
|
2465
|
+
acc = (acc+(g[((i*p)+(na+t))]*ls[i])) ;
|
|
2466
|
+
} ;
|
|
2467
|
+
dir[ t ] = (round_even(double((acc*1e+15)))/1e+15) ;
|
|
2468
|
+
;
|
|
2469
|
+
}
|
|
2470
|
+
} ;
|
|
2471
|
+
if( clen2 > 65536 ) {
|
|
2472
|
+
#pragma omp parallel for
|
|
2473
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2474
|
+
ratio[ t ] = ((cabs-cbuf[(cs+t)])/((aa-dir[t])+tiny32)) ;
|
|
2475
|
+
}
|
|
2476
|
+
} else {
|
|
2477
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2478
|
+
ratio[ t ] = ((cabs-cbuf[(cs+t)])/((aa-dir[t])+tiny32)) ;
|
|
2479
|
+
}
|
|
2480
|
+
} ;
|
|
2481
|
+
double gfirst = min_pos(ratio,clen2);
|
|
2482
|
+
if( clen2 > 65536 ) {
|
|
2483
|
+
#pragma omp parallel for
|
|
2484
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2485
|
+
ratio[ t ] = ((cabs+cbuf[(cs+t)])/((aa+dir[t])+tiny32)) ;
|
|
2486
|
+
}
|
|
2487
|
+
} else {
|
|
2488
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2489
|
+
ratio[ t ] = ((cabs+cbuf[(cs+t)])/((aa+dir[t])+tiny32)) ;
|
|
2490
|
+
}
|
|
2491
|
+
} ;
|
|
2492
|
+
double gsecond = min_pos(ratio,clen2);
|
|
2493
|
+
double gam = ( ( ( ( gfirst < gsecond ) ? (gfirst) : (gsecond) ) < (cabs/aa) ) ? (( ( gfirst < gsecond ) ? (gfirst) : (gsecond) )) : ((cabs/aa)) );
|
|
2494
|
+
int nidx = 0;
|
|
2495
|
+
drop = 0 ;
|
|
2496
|
+
if( na > 65536 ) {
|
|
2497
|
+
#pragma omp parallel for
|
|
2498
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
2499
|
+
zz[ i ] = ((0.0-coef[active[i]])/(ls[i]+tiny32)) ;
|
|
2500
|
+
}
|
|
2501
|
+
} else {
|
|
2502
|
+
for( int i = 0 ; i < na ; i += 1 ) {
|
|
2503
|
+
zz[ i ] = ((0.0-coef[active[i]])/(ls[i]+tiny32)) ;
|
|
2504
|
+
}
|
|
2505
|
+
} ;
|
|
2506
|
+
double zpos = min_pos(zz,na);
|
|
2507
|
+
if( zpos < gam ) {
|
|
2508
|
+
for( int ii = 0 ; !(ii == na) ; ii = (ii+1) ) {
|
|
2509
|
+
int i = ((na-1)-ii);
|
|
2510
|
+
if( zz[i] == zpos ) {
|
|
2511
|
+
idx[ nidx ] = i ;
|
|
2512
|
+
nidx = (nidx+1) ;
|
|
2513
|
+
} else {
|
|
2514
|
+
0 ;
|
|
2515
|
+
} ;
|
|
2516
|
+
;
|
|
2517
|
+
} ;
|
|
2518
|
+
for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
|
|
2519
|
+
int i = idx[r];
|
|
2520
|
+
sign[ i ] = (0.0-sign[i]) ;
|
|
2521
|
+
;
|
|
2522
|
+
} ;
|
|
2523
|
+
if( method == 1 ) {
|
|
2524
|
+
gam = zpos ;
|
|
2525
|
+
} else {
|
|
2526
|
+
0 ;
|
|
2527
|
+
} ;
|
|
2528
|
+
drop = 1 ;
|
|
2529
|
+
} else {
|
|
2530
|
+
0 ;
|
|
2531
|
+
} ;
|
|
2532
|
+
;
|
|
2533
|
+
niter = (niter+1) ;
|
|
2534
|
+
prevalpha = alpha ;
|
|
2535
|
+
if( p > 65536 ) {
|
|
2536
|
+
#pragma omp parallel for
|
|
2537
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2538
|
+
prev[ j ] = coef[j] ;
|
|
2539
|
+
coef[ j ] = 0.0 ;
|
|
2540
|
+
}
|
|
2541
|
+
} else {
|
|
2542
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2543
|
+
prev[ j ] = coef[j] ;
|
|
2544
|
+
coef[ j ] = 0.0 ;
|
|
2545
|
+
}
|
|
2546
|
+
} ;
|
|
2547
|
+
for( int i = 0 ; !(i == na) ; i = (i+1) ) {
|
|
2548
|
+
int a = active[i];
|
|
2549
|
+
coef[ a ] = (prev[a]+(gam*ls[i])) ;
|
|
2550
|
+
;
|
|
2551
|
+
} ;
|
|
2552
|
+
if( rec == 1 ) {
|
|
2553
|
+
if( p > 65536 ) {
|
|
2554
|
+
#pragma omp parallel for
|
|
2555
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2556
|
+
path[ ((niter*p)+j) ] = coef[j] ;
|
|
2557
|
+
}
|
|
2558
|
+
} else {
|
|
2559
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2560
|
+
path[ ((niter*p)+j) ] = coef[j] ;
|
|
2561
|
+
}
|
|
2562
|
+
} ;
|
|
2563
|
+
} else {
|
|
2564
|
+
0 ;
|
|
2565
|
+
} ;
|
|
2566
|
+
if( clen2 > 65536 ) {
|
|
2567
|
+
#pragma omp parallel for
|
|
2568
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2569
|
+
cbuf[ (cs+t) ] = (cbuf[(cs+t)]-(gam*dir[t])) ;
|
|
2570
|
+
}
|
|
2571
|
+
} else {
|
|
2572
|
+
for( int t = 0 ; t < clen2 ; t += 1 ) {
|
|
2573
|
+
cbuf[ (cs+t) ] = (cbuf[(cs+t)]-(gam*dir[t])) ;
|
|
2574
|
+
}
|
|
2575
|
+
} ;
|
|
2576
|
+
if( (drop == 1 && method == 1) ) {
|
|
2577
|
+
for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
|
|
2578
|
+
cholesky_delete(l,idx[r],na,kmax) ;
|
|
2579
|
+
} ;
|
|
2580
|
+
na = (na-1) ;
|
|
2581
|
+
for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
|
|
2582
|
+
int ii = idx[r];
|
|
2583
|
+
int dropped = active[ii];
|
|
2584
|
+
for( int i = ii ; !(i >= na) ; i = (i+1) ) {
|
|
2585
|
+
active[ i ] = active[(i+1)] ;
|
|
2586
|
+
} ;
|
|
2587
|
+
dropv[ r ] = dropped ;
|
|
2588
|
+
;
|
|
2589
|
+
;
|
|
2590
|
+
} ;
|
|
2591
|
+
for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
|
|
2592
|
+
int ii = idx[r];
|
|
2593
|
+
for( int i = ii ; !(i >= na) ; i = (i+1) ) {
|
|
2594
|
+
int ti = index[i];
|
|
2595
|
+
index[ i ] = index[(i+1)] ;
|
|
2596
|
+
index[ (i+1) ] = ti ;
|
|
2597
|
+
;
|
|
2598
|
+
swap_rc(g,i,int((i+1)),p) ;
|
|
2599
|
+
} ;
|
|
2600
|
+
;
|
|
2601
|
+
} ;
|
|
2602
|
+
for( int rr = 0 ; !(rr == nidx) ; rr = (rr+1) ) {
|
|
2603
|
+
int r = ((nidx-1)-rr);
|
|
2604
|
+
int d = dropv[r];
|
|
2605
|
+
double t = cov0[d];
|
|
2606
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
2607
|
+
t = (t-(g0[((d*p)+j)]*coef[j])) ;
|
|
2608
|
+
} ;
|
|
2609
|
+
cs = (cs-1) ;
|
|
2610
|
+
cbuf[ cs ] = t ;
|
|
2611
|
+
;
|
|
2612
|
+
;
|
|
2613
|
+
;
|
|
2614
|
+
} ;
|
|
2615
|
+
for( int r = 0 ; !(r == nidx) ; r = (r+1) ) {
|
|
2616
|
+
int ii = idx[r];
|
|
2617
|
+
for( int i = ii ; !(i >= (p-1)) ; i = (i+1) ) {
|
|
2618
|
+
sign[ i ] = sign[(i+1)] ;
|
|
2619
|
+
} ;
|
|
2620
|
+
sign[ (p-1) ] = 0.0 ;
|
|
2621
|
+
;
|
|
2622
|
+
} ;
|
|
2623
|
+
} else {
|
|
2624
|
+
0 ;
|
|
2625
|
+
} ;
|
|
2626
|
+
;
|
|
2627
|
+
;
|
|
2628
|
+
;
|
|
2629
|
+
;
|
|
2630
|
+
} ;
|
|
2631
|
+
} else {
|
|
2632
|
+
0 ;
|
|
2633
|
+
} ;
|
|
2634
|
+
;
|
|
2635
|
+
} ;
|
|
2636
|
+
} ;
|
|
2637
|
+
;
|
|
2638
|
+
} ;
|
|
2639
|
+
;
|
|
2640
|
+
return niter ;
|
|
2641
|
+
}
|
|
2642
|
+
|
|
2643
|
+
|
|
2644
|
+
inline int
|
|
2645
|
+
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 )
|
|
2646
|
+
{ std::vector<int> index(p,0);
|
|
2647
|
+
std::vector<double> b(n,1.0);
|
|
2648
|
+
std::vector<double> xjsq(p,0.0);
|
|
2649
|
+
int rng = seed;
|
|
2650
|
+
int npos = 0;
|
|
2651
|
+
int iter = 0;
|
|
2652
|
+
int active = p;
|
|
2653
|
+
double gmaxold = 1e+300;
|
|
2654
|
+
double ginit = -1.0;
|
|
2655
|
+
int done = 0;
|
|
2656
|
+
if( n > 65536 ) {
|
|
2657
|
+
#pragma omp parallel for reduction(+:npos)
|
|
2658
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2659
|
+
if( y[i] > 0.0 ) {
|
|
2660
|
+
npos = (npos+1) ;
|
|
2661
|
+
} else {
|
|
2662
|
+
0 ;
|
|
2663
|
+
} ;
|
|
2664
|
+
}
|
|
2665
|
+
} else {
|
|
2666
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2667
|
+
if( y[i] > 0.0 ) {
|
|
2668
|
+
npos = (npos+1) ;
|
|
2669
|
+
} else {
|
|
2670
|
+
0 ;
|
|
2671
|
+
} ;
|
|
2672
|
+
}
|
|
2673
|
+
} ;
|
|
2674
|
+
|
|
2675
|
+
struct let6187 {
|
|
2676
|
+
let6187() {}
|
|
2677
|
+
int operator()( int mn )
|
|
2678
|
+
{
|
|
2679
|
+
if( mn < 1 ) {
|
|
2680
|
+
return 1 ;
|
|
2681
|
+
}
|
|
2682
|
+
else {
|
|
2683
|
+
return mn ;
|
|
2684
|
+
}
|
|
2685
|
+
}
|
|
2686
|
+
} ;
|
|
2687
|
+
double eps = ((tol*double(let6187()(( ( npos < (n-npos) ) ? (npos) : ((n-npos)) ))))/double(n));
|
|
2688
|
+
if( (p*n) > 65536 ) {
|
|
2689
|
+
#pragma omp parallel for
|
|
2690
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2691
|
+
w[ j ] = 0.0 ;
|
|
2692
|
+
index[ j ] = j ;
|
|
2693
|
+
double acc = 0.0;
|
|
2694
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
2695
|
+
double val = (x[((j*n)+i)]*y[i]);
|
|
2696
|
+
acc = (acc+(cost*(val*val))) ;
|
|
2697
|
+
;
|
|
2698
|
+
} ;
|
|
2699
|
+
xjsq[ j ] = acc ;
|
|
2700
|
+
;
|
|
2701
|
+
}
|
|
2702
|
+
} else {
|
|
2703
|
+
for( int j = 0 ; j < p ; j += 1 ) {
|
|
2704
|
+
w[ j ] = 0.0 ;
|
|
2705
|
+
index[ j ] = j ;
|
|
2706
|
+
double acc = 0.0;
|
|
2707
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
2708
|
+
double val = (x[((j*n)+i)]*y[i]);
|
|
2709
|
+
acc = (acc+(cost*(val*val))) ;
|
|
2710
|
+
;
|
|
2711
|
+
} ;
|
|
2712
|
+
xjsq[ j ] = acc ;
|
|
2713
|
+
;
|
|
2714
|
+
}
|
|
2715
|
+
} ;
|
|
2716
|
+
for( ; !((done == 1 || iter >= maxiter)) ; ) {
|
|
2717
|
+
double gmaxnew = 0.0;
|
|
2718
|
+
double gnorm1new = 0.0;
|
|
2719
|
+
int s = 0;
|
|
2720
|
+
for( int j = 0 ; !(j == active) ; j = (j+1) ) {
|
|
2721
|
+
rng = ((16807*(rng % 127773))-(2836*(rng / 127773))) ;
|
|
2722
|
+
if( rng < 0 ) {
|
|
2723
|
+
rng = (rng+2147483647) ;
|
|
2724
|
+
} else {
|
|
2725
|
+
0 ;
|
|
2726
|
+
} ;
|
|
2727
|
+
int k = (j+(rng % (active-j)));
|
|
2728
|
+
int tmp = index[k];
|
|
2729
|
+
index[ k ] = index[j] ;
|
|
2730
|
+
index[ j ] = tmp ;
|
|
2731
|
+
;
|
|
2732
|
+
;
|
|
2733
|
+
} ;
|
|
2734
|
+
for( ; !(s >= active) ; ) {
|
|
2735
|
+
int j = index[s];
|
|
2736
|
+
double gl = 0.0;
|
|
2737
|
+
double h = 0.0;
|
|
2738
|
+
int shrunk = 0;
|
|
2739
|
+
for( int i = 0 ; !(i == n) ; i = (i+1) ) {
|
|
2740
|
+
if( b[i] > 0.0 ) {
|
|
2741
|
+
double val = (x[((j*n)+i)]*y[i]);
|
|
2742
|
+
double tmp = (cost*val);
|
|
2743
|
+
gl = (gl-(tmp*b[i])) ;
|
|
2744
|
+
h = (h+(tmp*val)) ;
|
|
2745
|
+
;
|
|
2746
|
+
;
|
|
2747
|
+
} else {
|
|
2748
|
+
0 ;
|
|
2749
|
+
} ;
|
|
2750
|
+
} ;
|
|
2751
|
+
gl = (2.0*gl) ;
|
|
2752
|
+
h = (2.0*h) ;
|
|
2753
|
+
if( h < 1e-12 ) {
|
|
2754
|
+
h = 1e-12 ;
|
|
2755
|
+
} else {
|
|
2756
|
+
0 ;
|
|
2757
|
+
} ;
|
|
2758
|
+
double wj = w[j];
|
|
2759
|
+
double gp = (gl+1.0);
|
|
2760
|
+
double gn = (gl-1.0);
|
|
2761
|
+
double viol = 0.0;
|
|
2762
|
+
if( wj == 0.0 ) {
|
|
2763
|
+
if( gp < 0.0 ) {
|
|
2764
|
+
viol = (0.0-gp) ;
|
|
2765
|
+
} else {
|
|
2766
|
+
if( gn > 0.0 ) {
|
|
2767
|
+
viol = gn ;
|
|
2768
|
+
} else {
|
|
2769
|
+
if( (gp > (gmaxold/double(n)) && gn < (0.0-(gmaxold/double(n)))) ) {
|
|
2770
|
+
active = (active-1) ;
|
|
2771
|
+
int tmp2 = index[s];
|
|
2772
|
+
index[ s ] = index[active] ;
|
|
2773
|
+
index[ active ] = tmp2 ;
|
|
2774
|
+
;
|
|
2775
|
+
shrunk = 1 ;
|
|
2776
|
+
} else {
|
|
2777
|
+
0 ;
|
|
2778
|
+
} ;
|
|
2779
|
+
} ;
|
|
2780
|
+
} ;
|
|
2781
|
+
} else {
|
|
2782
|
+
viol = ( ( wj > 0.0 ) ? (svm_l1_abs(gp)) : (svm_l1_abs(gn)) ) ;
|
|
2783
|
+
} ;
|
|
2784
|
+
if( shrunk == 0 ) {
|
|
2785
|
+
if( viol > gmaxnew ) {
|
|
2786
|
+
gmaxnew = viol ;
|
|
2787
|
+
} else {
|
|
2788
|
+
0 ;
|
|
2789
|
+
} ;
|
|
2790
|
+
gnorm1new = (gnorm1new+viol) ;
|
|
2791
|
+
double d = ( ( gp < (h*wj) ) ? (((0.0-gp)/h)) : (( ( gn > (h*wj) ) ? (((0.0-gn)/h)) : ((0.0-wj)) )) );
|
|
2792
|
+
if( svm_l1_abs(d) >= 1e-12 ) {
|
|
2793
|
+
double delta = ((svm_l1_abs(double((wj+d)))-svm_l1_abs(wj))+(gl*d));
|
|
2794
|
+
double dold = 0.0;
|
|
2795
|
+
int nls = 0;
|
|
2796
|
+
int fin = 0;
|
|
2797
|
+
double lossold = 0.0;
|
|
2798
|
+
for( ; !((fin == 1 || nls >= 20)) ; ) {
|
|
2799
|
+
double ddiff = (dold-d);
|
|
2800
|
+
double cnd = ((svm_l1_abs(double((wj+d)))-svm_l1_abs(wj))-(0.01*delta));
|
|
2801
|
+
if( (((xjsq[j]*(d*d))+(gl*d))+cnd) <= 0.0 ) {
|
|
2802
|
+
if( n > 65536 ) {
|
|
2803
|
+
#pragma omp parallel for
|
|
2804
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2805
|
+
b[ i ] = (b[i]+(ddiff*(x[((j*n)+i)]*y[i]))) ;
|
|
2806
|
+
}
|
|
2807
|
+
} else {
|
|
2808
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2809
|
+
b[ i ] = (b[i]+(ddiff*(x[((j*n)+i)]*y[i]))) ;
|
|
2810
|
+
}
|
|
2811
|
+
} ;
|
|
2812
|
+
fin = 1 ;
|
|
2813
|
+
} else {
|
|
2814
|
+
double lossnew = 0.0;
|
|
2815
|
+
if( nls == 0 ) {
|
|
2816
|
+
lossold = 0.0 ;
|
|
2817
|
+
} else {
|
|
2818
|
+
0 ;
|
|
2819
|
+
} ;
|
|
2820
|
+
if( n > 65536 ) {
|
|
2821
|
+
#pragma omp parallel for reduction(+:lossold) reduction(+:lossnew)
|
|
2822
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2823
|
+
double bi = b[i];
|
|
2824
|
+
if( (nls == 0 && bi > 0.0) ) {
|
|
2825
|
+
lossold = (lossold+(cost*(bi*bi))) ;
|
|
2826
|
+
} else {
|
|
2827
|
+
0 ;
|
|
2828
|
+
} ;
|
|
2829
|
+
double bnew = (bi+(ddiff*(x[((j*n)+i)]*y[i])));
|
|
2830
|
+
b[ i ] = bnew ;
|
|
2831
|
+
if( bnew > 0.0 ) {
|
|
2832
|
+
lossnew = (lossnew+(cost*(bnew*bnew))) ;
|
|
2833
|
+
} else {
|
|
2834
|
+
0 ;
|
|
2835
|
+
} ;
|
|
2836
|
+
;
|
|
2837
|
+
;
|
|
2838
|
+
}
|
|
2839
|
+
} else {
|
|
2840
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2841
|
+
double bi = b[i];
|
|
2842
|
+
if( (nls == 0 && bi > 0.0) ) {
|
|
2843
|
+
lossold = (lossold+(cost*(bi*bi))) ;
|
|
2844
|
+
} else {
|
|
2845
|
+
0 ;
|
|
2846
|
+
} ;
|
|
2847
|
+
double bnew = (bi+(ddiff*(x[((j*n)+i)]*y[i])));
|
|
2848
|
+
b[ i ] = bnew ;
|
|
2849
|
+
if( bnew > 0.0 ) {
|
|
2850
|
+
lossnew = (lossnew+(cost*(bnew*bnew))) ;
|
|
2851
|
+
} else {
|
|
2852
|
+
0 ;
|
|
2853
|
+
} ;
|
|
2854
|
+
;
|
|
2855
|
+
;
|
|
2856
|
+
}
|
|
2857
|
+
} ;
|
|
2858
|
+
if( (cnd+(lossnew-lossold)) <= 0.0 ) {
|
|
2859
|
+
fin = 1 ;
|
|
2860
|
+
} else {
|
|
2861
|
+
dold = d ;
|
|
2862
|
+
d = (0.5*d) ;
|
|
2863
|
+
delta = (0.5*delta) ;
|
|
2864
|
+
nls = (nls+1) ;
|
|
2865
|
+
} ;
|
|
2866
|
+
;
|
|
2867
|
+
} ;
|
|
2868
|
+
;
|
|
2869
|
+
} ;
|
|
2870
|
+
w[ j ] = (wj+d) ;
|
|
2871
|
+
if( nls >= 20 ) {
|
|
2872
|
+
if( n > 65536 ) {
|
|
2873
|
+
#pragma omp parallel for
|
|
2874
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2875
|
+
b[ i ] = 1.0 ;
|
|
2876
|
+
}
|
|
2877
|
+
} else {
|
|
2878
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2879
|
+
b[ i ] = 1.0 ;
|
|
2880
|
+
}
|
|
2881
|
+
} ;
|
|
2882
|
+
for( int jj = 0 ; !(jj == p) ; jj = (jj+1) ) {
|
|
2883
|
+
if( !(w[jj] == 0.0) ) {
|
|
2884
|
+
if( n > 65536 ) {
|
|
2885
|
+
#pragma omp parallel for
|
|
2886
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2887
|
+
b[ i ] = (b[i]-(w[jj]*(x[((jj*n)+i)]*y[i]))) ;
|
|
2888
|
+
}
|
|
2889
|
+
} else {
|
|
2890
|
+
for( int i = 0 ; i < n ; i += 1 ) {
|
|
2891
|
+
b[ i ] = (b[i]-(w[jj]*(x[((jj*n)+i)]*y[i]))) ;
|
|
2892
|
+
}
|
|
2893
|
+
} ;
|
|
2894
|
+
} else {
|
|
2895
|
+
0 ;
|
|
2896
|
+
} ;
|
|
2897
|
+
} ;
|
|
2898
|
+
} else {
|
|
2899
|
+
0 ;
|
|
2900
|
+
} ;
|
|
2901
|
+
;
|
|
2902
|
+
} else {
|
|
2903
|
+
0 ;
|
|
2904
|
+
} ;
|
|
2905
|
+
;
|
|
2906
|
+
s = (s+1) ;
|
|
2907
|
+
} else {
|
|
2908
|
+
0 ;
|
|
2909
|
+
} ;
|
|
2910
|
+
;
|
|
2911
|
+
;
|
|
2912
|
+
} ;
|
|
2913
|
+
if( iter == 0 ) {
|
|
2914
|
+
ginit = gnorm1new ;
|
|
2915
|
+
} else {
|
|
2916
|
+
0 ;
|
|
2917
|
+
} ;
|
|
2918
|
+
iter = (iter+1) ;
|
|
2919
|
+
if( gnorm1new <= (eps*ginit) ) {
|
|
2920
|
+
if( active == p ) {
|
|
2921
|
+
done = 1 ;
|
|
2922
|
+
} else {
|
|
2923
|
+
active = p ;
|
|
2924
|
+
gmaxold = 1e+300 ;
|
|
2925
|
+
} ;
|
|
2926
|
+
} else {
|
|
2927
|
+
gmaxold = gmaxnew ;
|
|
2928
|
+
} ;
|
|
2929
|
+
;
|
|
2930
|
+
} ;
|
|
2931
|
+
;
|
|
2932
|
+
return iter ;
|
|
2933
|
+
}
|
|
2934
|
+
|
|
2935
|
+
|
|
2936
|
+
SCM2CPP_FN inline double
|
|
2937
|
+
svm_l1_abs( double a )
|
|
2938
|
+
{
|
|
2939
|
+
if( a < 0.0 ) {
|
|
2940
|
+
return (0.0-a) ;
|
|
2941
|
+
}
|
|
2942
|
+
else {
|
|
2943
|
+
return a ;
|
|
2944
|
+
}
|
|
2945
|
+
}
|
|
2946
|
+
|
|
2947
|
+
|
|
2948
|
+
SCM2CPP_FN inline double
|
|
2949
|
+
eig_hypot( double a, double b )
|
|
2950
|
+
{
|
|
2951
|
+
double aa = ( ( a < 0.0 ) ? ((0.0-a)) : (a) );
|
|
2952
|
+
double bb = ( ( b < 0.0 ) ? ((0.0-b)) : (b) );
|
|
2953
|
+
if( aa > bb ) {
|
|
2954
|
+
return (aa*sqrt((1.0+((bb/aa)*(bb/aa))))) ;
|
|
2955
|
+
}
|
|
2956
|
+
else {
|
|
2957
|
+
if( bb == 0.0 ) {
|
|
2958
|
+
return 0.0 ;
|
|
2959
|
+
}
|
|
2960
|
+
else {
|
|
2961
|
+
return (bb*sqrt((1.0+((aa/bb)*(aa/bb))))) ;
|
|
2962
|
+
}
|
|
2963
|
+
}
|
|
2964
|
+
}
|
|
2965
|
+
|
|
2966
|
+
|
|
2967
|
+
SCM2CPP_FN inline int
|
|
2968
|
+
sym_eigen( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m )
|
|
2969
|
+
{ for( int ii = 0 ; !(ii == (m-1)) ; ii = (ii+1) ) {
|
|
2970
|
+
int i = ((m-1)-ii);
|
|
2971
|
+
int l = (i-1);
|
|
2972
|
+
double h = 0.0;
|
|
2973
|
+
double scale = 0.0;
|
|
2974
|
+
if( l > 0 ) {
|
|
2975
|
+
if( (l+1) > 65536 ) {
|
|
2976
|
+
#pragma omp parallel for reduction(+:scale)
|
|
2977
|
+
for( int k = 0 ; k <= l ; k += 1 ) {
|
|
2978
|
+
double ak = a[((i*m)+k)];
|
|
2979
|
+
scale = (scale+( ( ak < 0.0 ) ? ((0.0-ak)) : (ak) )) ;
|
|
2980
|
+
;
|
|
2981
|
+
}
|
|
2982
|
+
} else {
|
|
2983
|
+
for( int k = 0 ; k <= l ; k += 1 ) {
|
|
2984
|
+
double ak = a[((i*m)+k)];
|
|
2985
|
+
scale = (scale+( ( ak < 0.0 ) ? ((0.0-ak)) : (ak) )) ;
|
|
2986
|
+
;
|
|
2987
|
+
}
|
|
2988
|
+
} ;
|
|
2989
|
+
if( scale == 0.0 ) {
|
|
2990
|
+
e[ i ] = a[((i*m)+l)] ;
|
|
2991
|
+
} else {
|
|
2992
|
+
if( (l+1) > 65536 ) {
|
|
2993
|
+
#pragma omp parallel for reduction(+:h)
|
|
2994
|
+
for( int k = 0 ; k <= l ; k += 1 ) {
|
|
2995
|
+
a[ ((i*m)+k) ] = (a[((i*m)+k)]/scale) ;
|
|
2996
|
+
h = (h+(a[((i*m)+k)]*a[((i*m)+k)])) ;
|
|
2997
|
+
}
|
|
2998
|
+
} else {
|
|
2999
|
+
for( int k = 0 ; k <= l ; k += 1 ) {
|
|
3000
|
+
a[ ((i*m)+k) ] = (a[((i*m)+k)]/scale) ;
|
|
3001
|
+
h = (h+(a[((i*m)+k)]*a[((i*m)+k)])) ;
|
|
3002
|
+
}
|
|
3003
|
+
} ;
|
|
3004
|
+
double f = a[((i*m)+l)];
|
|
3005
|
+
double g = ( ( f >= 0.0 ) ? ((0.0-sqrt(h))) : (sqrt(h)) );
|
|
3006
|
+
e[ i ] = (scale*g) ;
|
|
3007
|
+
h = (h-(f*g)) ;
|
|
3008
|
+
a[ ((i*m)+l) ] = (f-g) ;
|
|
3009
|
+
double ff = 0.0;
|
|
3010
|
+
for( int j = 0 ; !(j > l) ; j = (j+1) ) {
|
|
3011
|
+
a[ ((j*m)+i) ] = (a[((i*m)+j)]/h) ;
|
|
3012
|
+
double gg = 0.0;
|
|
3013
|
+
if( (j+1) > 65536 ) {
|
|
3014
|
+
#pragma omp parallel for reduction(+:gg)
|
|
3015
|
+
for( int k = 0 ; k <= j ; k += 1 ) {
|
|
3016
|
+
gg = (gg+(a[((j*m)+k)]*a[((i*m)+k)])) ;
|
|
3017
|
+
}
|
|
3018
|
+
} else {
|
|
3019
|
+
for( int k = 0 ; k <= j ; k += 1 ) {
|
|
3020
|
+
gg = (gg+(a[((j*m)+k)]*a[((i*m)+k)])) ;
|
|
3021
|
+
}
|
|
3022
|
+
} ;
|
|
3023
|
+
if( ((l+1)-(j+1)) > 65536 ) {
|
|
3024
|
+
#pragma omp parallel for reduction(+:gg)
|
|
3025
|
+
for( int k = (j+1) ; k <= l ; k += 1 ) {
|
|
3026
|
+
gg = (gg+(a[((k*m)+j)]*a[((i*m)+k)])) ;
|
|
3027
|
+
}
|
|
3028
|
+
} else {
|
|
3029
|
+
for( int k = (j+1) ; k <= l ; k += 1 ) {
|
|
3030
|
+
gg = (gg+(a[((k*m)+j)]*a[((i*m)+k)])) ;
|
|
3031
|
+
}
|
|
3032
|
+
} ;
|
|
3033
|
+
e[ j ] = (gg/h) ;
|
|
3034
|
+
ff = (ff+(e[j]*a[((i*m)+j)])) ;
|
|
3035
|
+
;
|
|
3036
|
+
} ;
|
|
3037
|
+
double hh = (ff/(h+h));
|
|
3038
|
+
for( int j = 0 ; !(j > l) ; j = (j+1) ) {
|
|
3039
|
+
double f2 = a[((i*m)+j)];
|
|
3040
|
+
double g2 = (e[j]-(hh*f2));
|
|
3041
|
+
e[ j ] = g2 ;
|
|
3042
|
+
for( int k = 0 ; !(k > j) ; k = (k+1) ) {
|
|
3043
|
+
a[ ((j*m)+k) ] = (a[((j*m)+k)]-((f2*e[k])+(g2*a[((i*m)+k)]))) ;
|
|
3044
|
+
} ;
|
|
3045
|
+
;
|
|
3046
|
+
;
|
|
3047
|
+
} ;
|
|
3048
|
+
;
|
|
3049
|
+
;
|
|
3050
|
+
;
|
|
3051
|
+
;
|
|
3052
|
+
} ;
|
|
3053
|
+
} else {
|
|
3054
|
+
e[ i ] = a[((i*m)+l)] ;
|
|
3055
|
+
} ;
|
|
3056
|
+
ev[ i ] = h ;
|
|
3057
|
+
;
|
|
3058
|
+
;
|
|
3059
|
+
} ;
|
|
3060
|
+
ev[ 0 ] = 0.0 ;
|
|
3061
|
+
e[ 0 ] = 0.0 ;
|
|
3062
|
+
for( int i = 0 ; !(i == m) ; i = (i+1) ) {
|
|
3063
|
+
if( !(ev[i] == 0.0) ) {
|
|
3064
|
+
for( int j = 0 ; !(j == i) ; j = (j+1) ) {
|
|
3065
|
+
double g3 = 0.0;
|
|
3066
|
+
if( i > 65536 ) {
|
|
3067
|
+
#pragma omp parallel for reduction(+:g3)
|
|
3068
|
+
for( int k = 0 ; k < i ; k += 1 ) {
|
|
3069
|
+
g3 = (g3+(a[((i*m)+k)]*a[((k*m)+j)])) ;
|
|
3070
|
+
}
|
|
3071
|
+
} else {
|
|
3072
|
+
for( int k = 0 ; k < i ; k += 1 ) {
|
|
3073
|
+
g3 = (g3+(a[((i*m)+k)]*a[((k*m)+j)])) ;
|
|
3074
|
+
}
|
|
3075
|
+
} ;
|
|
3076
|
+
for( int k = 0 ; !(k == i) ; k = (k+1) ) {
|
|
3077
|
+
a[ ((k*m)+j) ] = (a[((k*m)+j)]-(g3*a[((k*m)+i)])) ;
|
|
3078
|
+
} ;
|
|
3079
|
+
;
|
|
3080
|
+
} ;
|
|
3081
|
+
} else {
|
|
3082
|
+
0 ;
|
|
3083
|
+
} ;
|
|
3084
|
+
ev[ i ] = a[((i*m)+i)] ;
|
|
3085
|
+
a[ ((i*m)+i) ] = 1.0 ;
|
|
3086
|
+
for( int j = 0 ; !(j == i) ; j = (j+1) ) {
|
|
3087
|
+
a[ ((j*m)+i) ] = 0.0 ;
|
|
3088
|
+
a[ ((i*m)+j) ] = 0.0 ;
|
|
3089
|
+
} ;
|
|
3090
|
+
} ;
|
|
3091
|
+
if( (m*m) > 65536 ) {
|
|
3092
|
+
#pragma omp parallel for
|
|
3093
|
+
for( int i = 0 ; i < m ; i += 1 ) {
|
|
3094
|
+
for( int j = 0 ; !(j == m) ; j = (j+1) ) {
|
|
3095
|
+
v[ ((j*m)+i) ] = a[((i*m)+j)] ;
|
|
3096
|
+
} ;
|
|
3097
|
+
}
|
|
3098
|
+
} else {
|
|
3099
|
+
for( int i = 0 ; i < m ; i += 1 ) {
|
|
3100
|
+
for( int j = 0 ; !(j == m) ; j = (j+1) ) {
|
|
3101
|
+
v[ ((j*m)+i) ] = a[((i*m)+j)] ;
|
|
3102
|
+
} ;
|
|
3103
|
+
}
|
|
3104
|
+
} ;
|
|
3105
|
+
for( int i = 1 ; !(i == m) ; i = (i+1) ) {
|
|
3106
|
+
e[ (i-1) ] = e[i] ;
|
|
3107
|
+
} ;
|
|
3108
|
+
e[ (m-1) ] = 0.0 ;
|
|
3109
|
+
for( int l = 0 ; !(l == m) ; l = (l+1) ) {
|
|
3110
|
+
int fin = 0;
|
|
3111
|
+
int iter = 0;
|
|
3112
|
+
for( ; !((fin == 1 || iter > 60)) ; ) {
|
|
3113
|
+
int mm = l;
|
|
3114
|
+
int found = 0;
|
|
3115
|
+
for( ; !((found == 1 || mm >= (m-1))) ; ) {
|
|
3116
|
+
double dd = (eig_abs(ev[mm])+eig_abs(ev[(mm+1)]));
|
|
3117
|
+
if( (eig_abs(e[mm])+dd) == dd ) {
|
|
3118
|
+
found = 1 ;
|
|
3119
|
+
} else {
|
|
3120
|
+
mm = (mm+1) ;
|
|
3121
|
+
} ;
|
|
3122
|
+
;
|
|
3123
|
+
} ;
|
|
3124
|
+
if( mm == l ) {
|
|
3125
|
+
fin = 1 ;
|
|
3126
|
+
} else {
|
|
3127
|
+
iter = (iter+1) ;
|
|
3128
|
+
double g = ((ev[(l+1)]-ev[l])/(2.0*e[l]));
|
|
3129
|
+
double r = eig_hypot(g,1.0);
|
|
3130
|
+
double g4 = ((ev[mm]-ev[l])+(e[l]/(g+( ( g >= 0.0 ) ? (eig_abs(r)) : ((0.0-eig_abs(r))) ))));
|
|
3131
|
+
double s = 1.0;
|
|
3132
|
+
double c = 1.0;
|
|
3133
|
+
double pp = 0.0;
|
|
3134
|
+
double gq = g4;
|
|
3135
|
+
int under = 0;
|
|
3136
|
+
int i = (mm-1);
|
|
3137
|
+
for( ; !((under == 1 || i < l)) ; ) {
|
|
3138
|
+
double f = (s*e[i]);
|
|
3139
|
+
double b = (c*e[i]);
|
|
3140
|
+
double r2 = eig_hypot(f,gq);
|
|
3141
|
+
e[ (i+1) ] = r2 ;
|
|
3142
|
+
if( r2 == 0.0 ) {
|
|
3143
|
+
ev[ (i+1) ] = (ev[(i+1)]-pp) ;
|
|
3144
|
+
e[ mm ] = 0.0 ;
|
|
3145
|
+
under = 1 ;
|
|
3146
|
+
} else {
|
|
3147
|
+
s = (f/r2) ;
|
|
3148
|
+
c = (gq/r2) ;
|
|
3149
|
+
double g5 = (ev[(i+1)]-pp);
|
|
3150
|
+
double r3 = (((ev[i]-g5)*s)+(2.0*(c*b)));
|
|
3151
|
+
pp = (s*r3) ;
|
|
3152
|
+
ev[ (i+1) ] = (g5+pp) ;
|
|
3153
|
+
gq = ((c*r3)-b) ;
|
|
3154
|
+
;
|
|
3155
|
+
;
|
|
3156
|
+
for( int k = 0 ; !(k == m) ; k = (k+1) ) {
|
|
3157
|
+
double zf = v[(((i+1)*m)+k)];
|
|
3158
|
+
double zi = v[((i*m)+k)];
|
|
3159
|
+
v[ (((i+1)*m)+k) ] = ((s*zi)+(c*zf)) ;
|
|
3160
|
+
v[ ((i*m)+k) ] = ((c*zi)-(s*zf)) ;
|
|
3161
|
+
;
|
|
3162
|
+
} ;
|
|
3163
|
+
i = (i-1) ;
|
|
3164
|
+
} ;
|
|
3165
|
+
;
|
|
3166
|
+
;
|
|
3167
|
+
} ;
|
|
3168
|
+
if( under == 0 ) {
|
|
3169
|
+
ev[ l ] = (ev[l]-pp) ;
|
|
3170
|
+
e[ l ] = gq ;
|
|
3171
|
+
e[ mm ] = 0.0 ;
|
|
3172
|
+
} else {
|
|
3173
|
+
0 ;
|
|
3174
|
+
} ;
|
|
3175
|
+
;
|
|
3176
|
+
;
|
|
3177
|
+
;
|
|
3178
|
+
;
|
|
3179
|
+
} ;
|
|
3180
|
+
;
|
|
3181
|
+
} ;
|
|
3182
|
+
;
|
|
3183
|
+
} ;
|
|
3184
|
+
for( int i = 0 ; !(i == m) ; i = (i+1) ) {
|
|
3185
|
+
for( int j = (i+1) ; !(j >= m) ; j = (j+1) ) {
|
|
3186
|
+
double tmp = v[((i*m)+j)];
|
|
3187
|
+
v[ ((i*m)+j) ] = v[((j*m)+i)] ;
|
|
3188
|
+
v[ ((j*m)+i) ] = tmp ;
|
|
3189
|
+
;
|
|
3190
|
+
} ;
|
|
3191
|
+
} ;
|
|
3192
|
+
return 0 ;
|
|
3193
|
+
}
|
|
3194
|
+
|
|
3195
|
+
|
|
3196
|
+
SCM2CPP_FN inline double
|
|
3197
|
+
eig_abs( double a )
|
|
3198
|
+
{
|
|
3199
|
+
if( a < 0.0 ) {
|
|
3200
|
+
return (0.0-a) ;
|
|
3201
|
+
}
|
|
3202
|
+
else {
|
|
3203
|
+
return a ;
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
|
|
3207
|
+
|
|
3208
|
+
SCM2CPP_FN inline int
|
|
3209
|
+
eig_order( scm2cpp::cspan<double> ev, scm2cpp::span<int> ord, int m )
|
|
3210
|
+
{ if( m > 65536 ) {
|
|
3211
|
+
#pragma omp parallel for
|
|
3212
|
+
for( int j = 0 ; j < m ; j += 1 ) {
|
|
3213
|
+
ord[ j ] = j ;
|
|
3214
|
+
}
|
|
3215
|
+
} else {
|
|
3216
|
+
for( int j = 0 ; j < m ; j += 1 ) {
|
|
3217
|
+
ord[ j ] = j ;
|
|
3218
|
+
}
|
|
3219
|
+
} ;
|
|
3220
|
+
for( int j = 0 ; !(j == m) ; j = (j+1) ) {
|
|
3221
|
+
int best = j;
|
|
3222
|
+
for( int r = (j+1) ; !(r >= m) ; r = (r+1) ) {
|
|
3223
|
+
if( ev[ord[r]] > ev[ord[best]] ) {
|
|
3224
|
+
best = r ;
|
|
3225
|
+
} else {
|
|
3226
|
+
0 ;
|
|
3227
|
+
} ;
|
|
3228
|
+
} ;
|
|
3229
|
+
int tmp = ord[j];
|
|
3230
|
+
ord[ j ] = ord[best] ;
|
|
3231
|
+
ord[ best ] = tmp ;
|
|
3232
|
+
;
|
|
3233
|
+
;
|
|
3234
|
+
} ;
|
|
3235
|
+
return 0 ;
|
|
3236
|
+
}
|
|
3237
|
+
|
|
3238
|
+
|
|
3239
|
+
inline int
|
|
3240
|
+
sym_eigen_top( scm2cpp::span<double> a, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m, int k )
|
|
3241
|
+
{ std::vector<double> vf((m*m),0.0);
|
|
3242
|
+
std::vector<double> evf(m,0.0);
|
|
3243
|
+
std::vector<int> ord(m,0);
|
|
3244
|
+
sym_eigen(a,vf,evf,e,m) ;
|
|
3245
|
+
eig_order(evf,ord,m) ;
|
|
3246
|
+
if( (k*m) > 65536 ) {
|
|
3247
|
+
#pragma omp parallel for
|
|
3248
|
+
for( int c = 0 ; c < k ; c += 1 ) {
|
|
3249
|
+
int col = ord[c];
|
|
3250
|
+
ev[ c ] = evf[col] ;
|
|
3251
|
+
for( int j = 0 ; !(j == m) ; j = (j+1) ) {
|
|
3252
|
+
v[ ((j*k)+c) ] = vf[((j*m)+col)] ;
|
|
3253
|
+
} ;
|
|
3254
|
+
;
|
|
3255
|
+
}
|
|
3256
|
+
} else {
|
|
3257
|
+
for( int c = 0 ; c < k ; c += 1 ) {
|
|
3258
|
+
int col = ord[c];
|
|
3259
|
+
ev[ c ] = evf[col] ;
|
|
3260
|
+
for( int j = 0 ; !(j == m) ; j = (j+1) ) {
|
|
3261
|
+
v[ ((j*k)+c) ] = vf[((j*m)+col)] ;
|
|
3262
|
+
} ;
|
|
3263
|
+
;
|
|
3264
|
+
}
|
|
3265
|
+
} ;
|
|
3266
|
+
return 0 ;
|
|
3267
|
+
}
|
|
3268
|
+
|
|
3269
|
+
|
|
3270
|
+
SCM2CPP_FN inline int
|
|
3271
|
+
sym_eigen_gen( scm2cpp::span<double> a, scm2cpp::span<double> b, scm2cpp::span<double> v, scm2cpp::span<double> ev, scm2cpp::span<double> e, int m )
|
|
3272
|
+
{ for( int j = 0 ; !(j == m) ; j = (j+1) ) {
|
|
3273
|
+
double d = b[((j*m)+j)];
|
|
3274
|
+
for( int l = 0 ; !(l == j) ; l = (l+1) ) {
|
|
3275
|
+
d = (d-(b[((j*m)+l)]*b[((j*m)+l)])) ;
|
|
3276
|
+
} ;
|
|
3277
|
+
b[ ((j*m)+j) ] = sqrt(d) ;
|
|
3278
|
+
for( int i = (j+1) ; !(i >= m) ; i = (i+1) ) {
|
|
3279
|
+
double s = b[((i*m)+j)];
|
|
3280
|
+
for( int l = 0 ; !(l == j) ; l = (l+1) ) {
|
|
3281
|
+
s = (s-(b[((i*m)+l)]*b[((j*m)+l)])) ;
|
|
3282
|
+
} ;
|
|
3283
|
+
b[ ((i*m)+j) ] = (s/b[((j*m)+j)]) ;
|
|
3284
|
+
;
|
|
3285
|
+
} ;
|
|
3286
|
+
;
|
|
3287
|
+
} ;
|
|
3288
|
+
for( int c = 0 ; !(c == m) ; c = (c+1) ) {
|
|
3289
|
+
for( int i = 0 ; !(i == m) ; i = (i+1) ) {
|
|
3290
|
+
double s = a[((i*m)+c)];
|
|
3291
|
+
for( int l = 0 ; !(l == i) ; l = (l+1) ) {
|
|
3292
|
+
s = (s-(b[((i*m)+l)]*a[((l*m)+c)])) ;
|
|
3293
|
+
} ;
|
|
3294
|
+
a[ ((i*m)+c) ] = (s/b[((i*m)+i)]) ;
|
|
3295
|
+
;
|
|
3296
|
+
} ;
|
|
3297
|
+
} ;
|
|
3298
|
+
for( int r = 0 ; !(r == m) ; r = (r+1) ) {
|
|
3299
|
+
for( int i = 0 ; !(i == m) ; i = (i+1) ) {
|
|
3300
|
+
double s = a[((r*m)+i)];
|
|
3301
|
+
for( int l = 0 ; !(l == i) ; l = (l+1) ) {
|
|
3302
|
+
s = (s-(b[((i*m)+l)]*a[((r*m)+l)])) ;
|
|
3303
|
+
} ;
|
|
3304
|
+
a[ ((r*m)+i) ] = (s/b[((i*m)+i)]) ;
|
|
3305
|
+
;
|
|
3306
|
+
} ;
|
|
3307
|
+
} ;
|
|
3308
|
+
for( int i = 0 ; !(i == m) ; i = (i+1) ) {
|
|
3309
|
+
for( int j = 0 ; !(j == i) ; j = (j+1) ) {
|
|
3310
|
+
double s = (0.5*(a[((i*m)+j)]+a[((j*m)+i)]));
|
|
3311
|
+
a[ ((i*m)+j) ] = s ;
|
|
3312
|
+
a[ ((j*m)+i) ] = s ;
|
|
3313
|
+
;
|
|
3314
|
+
} ;
|
|
3315
|
+
} ;
|
|
3316
|
+
sym_eigen(a,v,ev,e,m) ;
|
|
3317
|
+
for( int c = 0 ; !(c == m) ; c = (c+1) ) {
|
|
3318
|
+
for( int ii = 0 ; !(ii == m) ; ii = (ii+1) ) {
|
|
3319
|
+
int i = ((m-1)-ii);
|
|
3320
|
+
double s = v[((i*m)+c)];
|
|
3321
|
+
for( int l = (i+1) ; !(l >= m) ; l = (l+1) ) {
|
|
3322
|
+
s = (s-(b[((l*m)+i)]*v[((l*m)+c)])) ;
|
|
3323
|
+
} ;
|
|
3324
|
+
v[ ((i*m)+c) ] = (s/b[((i*m)+i)]) ;
|
|
3325
|
+
;
|
|
3326
|
+
;
|
|
3327
|
+
} ;
|
|
3328
|
+
} ;
|
|
3329
|
+
return 0 ;
|
|
3330
|
+
}
|
|
3331
|
+
|
|
3332
|
+
|
|
3333
|
+
SCM2CPP_FN inline int
|
|
3334
|
+
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 )
|
|
3335
|
+
{ if( (d*d) > 65536 ) {
|
|
3336
|
+
#pragma omp parallel for
|
|
3337
|
+
for( int t = 0 ; t < (d*d) ; t += 1 ) {
|
|
3338
|
+
aa[ t ] = 0.0 ;
|
|
3339
|
+
}
|
|
3340
|
+
} else {
|
|
3341
|
+
for( int t = 0 ; t < (d*d) ; t += 1 ) {
|
|
3342
|
+
aa[ t ] = 0.0 ;
|
|
3343
|
+
}
|
|
3344
|
+
} ;
|
|
3345
|
+
if( d > 65536 ) {
|
|
3346
|
+
#pragma omp parallel for
|
|
3347
|
+
for( int t = 0 ; t < d ; t += 1 ) {
|
|
3348
|
+
ab[ t ] = 0.0 ;
|
|
3349
|
+
}
|
|
3350
|
+
} else {
|
|
3351
|
+
for( int t = 0 ; t < d ; t += 1 ) {
|
|
3352
|
+
ab[ t ] = 0.0 ;
|
|
3353
|
+
}
|
|
3354
|
+
} ;
|
|
3355
|
+
for( int r = 0 ; !(r == ns) ; r = (r+1) ) {
|
|
3356
|
+
int i = idx[((s*ns)+r)];
|
|
3357
|
+
if( (d*d) > 65536 ) {
|
|
3358
|
+
#pragma omp parallel for
|
|
3359
|
+
for( int u = 0 ; u < d ; u += 1 ) {
|
|
3360
|
+
double xu = ( ( u < fitint ) ? (1.0) : (x[((i*p)+(u-fitint))]) );
|
|
3361
|
+
ab[ u ] = (ab[u]+(xu*y[i])) ;
|
|
3362
|
+
for( int q = 0 ; !(q == d) ; q = (q+1) ) {
|
|
3363
|
+
double xq = ( ( q < fitint ) ? (1.0) : (x[((i*p)+(q-fitint))]) );
|
|
3364
|
+
aa[ ((u*d)+q) ] = (aa[((u*d)+q)]+(xu*xq)) ;
|
|
3365
|
+
;
|
|
3366
|
+
} ;
|
|
3367
|
+
;
|
|
3368
|
+
}
|
|
3369
|
+
} else {
|
|
3370
|
+
for( int u = 0 ; u < d ; u += 1 ) {
|
|
3371
|
+
double xu = ( ( u < fitint ) ? (1.0) : (x[((i*p)+(u-fitint))]) );
|
|
3372
|
+
ab[ u ] = (ab[u]+(xu*y[i])) ;
|
|
3373
|
+
for( int q = 0 ; !(q == d) ; q = (q+1) ) {
|
|
3374
|
+
double xq = ( ( q < fitint ) ? (1.0) : (x[((i*p)+(q-fitint))]) );
|
|
3375
|
+
aa[ ((u*d)+q) ] = (aa[((u*d)+q)]+(xu*xq)) ;
|
|
3376
|
+
;
|
|
3377
|
+
} ;
|
|
3378
|
+
;
|
|
3379
|
+
}
|
|
3380
|
+
} ;
|
|
3381
|
+
;
|
|
3382
|
+
} ;
|
|
3383
|
+
sym_eigen(aa,v,ev,e,d) ;
|
|
3384
|
+
double mx = 0.0;
|
|
3385
|
+
for( int r = 0 ; !(r == d) ; r = (r+1) ) {
|
|
3386
|
+
if( ts_abs(ev[r]) > mx ) {
|
|
3387
|
+
mx = ts_abs(ev[r]) ;
|
|
3388
|
+
} else {
|
|
3389
|
+
0 ;
|
|
3390
|
+
} ;
|
|
3391
|
+
} ;
|
|
3392
|
+
double cut = (mx*(double(d)*2.220446049250313e-16));
|
|
3393
|
+
if( (d*d) > 65536 ) {
|
|
3394
|
+
#pragma omp parallel for
|
|
3395
|
+
for( int u = 0 ; u < d ; u += 1 ) {
|
|
3396
|
+
double acc = 0.0;
|
|
3397
|
+
for( int r = 0 ; !(r == d) ; r = (r+1) ) {
|
|
3398
|
+
if( ts_abs(ev[r]) > cut ) {
|
|
3399
|
+
double vab = 0.0;
|
|
3400
|
+
for( int q = 0 ; !(q == d) ; q = (q+1) ) {
|
|
3401
|
+
vab = (vab+(v[((q*d)+r)]*ab[q])) ;
|
|
3402
|
+
} ;
|
|
3403
|
+
acc = (acc+((v[((u*d)+r)]*vab)/ev[r])) ;
|
|
3404
|
+
;
|
|
3405
|
+
} else {
|
|
3406
|
+
0 ;
|
|
3407
|
+
} ;
|
|
3408
|
+
} ;
|
|
3409
|
+
w[ ((s*d)+u) ] = acc ;
|
|
3410
|
+
;
|
|
3411
|
+
}
|
|
3412
|
+
} else {
|
|
3413
|
+
for( int u = 0 ; u < d ; u += 1 ) {
|
|
3414
|
+
double acc = 0.0;
|
|
3415
|
+
for( int r = 0 ; !(r == d) ; r = (r+1) ) {
|
|
3416
|
+
if( ts_abs(ev[r]) > cut ) {
|
|
3417
|
+
double vab = 0.0;
|
|
3418
|
+
for( int q = 0 ; !(q == d) ; q = (q+1) ) {
|
|
3419
|
+
vab = (vab+(v[((q*d)+r)]*ab[q])) ;
|
|
3420
|
+
} ;
|
|
3421
|
+
acc = (acc+((v[((u*d)+r)]*vab)/ev[r])) ;
|
|
3422
|
+
;
|
|
3423
|
+
} else {
|
|
3424
|
+
0 ;
|
|
3425
|
+
} ;
|
|
3426
|
+
} ;
|
|
3427
|
+
w[ ((s*d)+u) ] = acc ;
|
|
3428
|
+
;
|
|
3429
|
+
}
|
|
3430
|
+
} ;
|
|
3431
|
+
;
|
|
3432
|
+
;
|
|
3433
|
+
return 0 ;
|
|
3434
|
+
}
|
|
3435
|
+
|
|
3436
|
+
|
|
3437
|
+
SCM2CPP_FN inline int
|
|
3438
|
+
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 )
|
|
3439
|
+
{ int singular = 0;
|
|
3440
|
+
double scale = 0.0;
|
|
3441
|
+
if( ns == d ) {
|
|
3442
|
+
for( int r = 0 ; !(r == d) ; r = (r+1) ) {
|
|
3443
|
+
int i = idx[((s*ns)+r)];
|
|
3444
|
+
if( fitint == 1 ) {
|
|
3445
|
+
a[ (r*d) ] = 1.0 ;
|
|
3446
|
+
} else {
|
|
3447
|
+
0 ;
|
|
3448
|
+
} ;
|
|
3449
|
+
for( int j = 0 ; !(j == p) ; j = (j+1) ) {
|
|
3450
|
+
a[ ((r*d)+(j+fitint)) ] = x[((i*p)+j)] ;
|
|
3451
|
+
} ;
|
|
3452
|
+
b[ r ] = y[i] ;
|
|
3453
|
+
;
|
|
3454
|
+
} ;
|
|
3455
|
+
for( int t = 0 ; !(t == (d*d)) ; t = (t+1) ) {
|
|
3456
|
+
if( ts_abs(a[t]) > scale ) {
|
|
3457
|
+
scale = ts_abs(a[t]) ;
|
|
3458
|
+
} else {
|
|
3459
|
+
0 ;
|
|
3460
|
+
} ;
|
|
3461
|
+
} ;
|
|
3462
|
+
for( int c = 0 ; !(c == d) ; c = (c+1) ) {
|
|
3463
|
+
int piv = c;
|
|
3464
|
+
double big = ts_abs(a[((c*d)+c)]);
|
|
3465
|
+
for( int r = (c+1) ; !(r >= d) ; r = (r+1) ) {
|
|
3466
|
+
if( ts_abs(a[((r*d)+c)]) > big ) {
|
|
3467
|
+
big = ts_abs(a[((r*d)+c)]) ;
|
|
3468
|
+
piv = r ;
|
|
3469
|
+
} else {
|
|
3470
|
+
0 ;
|
|
3471
|
+
} ;
|
|
3472
|
+
} ;
|
|
3473
|
+
if( big <= (scale*(double(d)*2.220446049250313e-16)) ) {
|
|
3474
|
+
singular = 1 ;
|
|
3475
|
+
} else {
|
|
3476
|
+
if( !(piv == c) ) {
|
|
3477
|
+
for( int j = 0 ; !(j == d) ; j = (j+1) ) {
|
|
3478
|
+
double tmp = a[((c*d)+j)];
|
|
3479
|
+
a[ ((c*d)+j) ] = a[((piv*d)+j)] ;
|
|
3480
|
+
a[ ((piv*d)+j) ] = tmp ;
|
|
3481
|
+
;
|
|
3482
|
+
} ;
|
|
3483
|
+
double tb = b[c];
|
|
3484
|
+
b[ c ] = b[piv] ;
|
|
3485
|
+
b[ piv ] = tb ;
|
|
3486
|
+
;
|
|
3487
|
+
} else {
|
|
3488
|
+
0 ;
|
|
3489
|
+
} ;
|
|
3490
|
+
for( int r = (c+1) ; !(r >= d) ; r = (r+1) ) {
|
|
3491
|
+
double fct = (a[((r*d)+c)]/a[((c*d)+c)]);
|
|
3492
|
+
for( int j = c ; !(j == d) ; j = (j+1) ) {
|
|
3493
|
+
a[ ((r*d)+j) ] = (a[((r*d)+j)]-(fct*a[((c*d)+j)])) ;
|
|
3494
|
+
} ;
|
|
3495
|
+
b[ r ] = (b[r]-(fct*b[c])) ;
|
|
3496
|
+
;
|
|
3497
|
+
} ;
|
|
3498
|
+
} ;
|
|
3499
|
+
;
|
|
3500
|
+
} ;
|
|
3501
|
+
if( singular == 0 ) {
|
|
3502
|
+
for( int cc = 0 ; !(cc == d) ; cc = (cc+1) ) {
|
|
3503
|
+
int c = ((d-1)-cc);
|
|
3504
|
+
double t = b[c];
|
|
3505
|
+
for( int j = (c+1) ; !(j >= d) ; j = (j+1) ) {
|
|
3506
|
+
t = (t-(a[((c*d)+j)]*w[((s*d)+j)])) ;
|
|
3507
|
+
} ;
|
|
3508
|
+
w[ ((s*d)+c) ] = (t/a[((c*d)+c)]) ;
|
|
3509
|
+
;
|
|
3510
|
+
;
|
|
3511
|
+
} ;
|
|
3512
|
+
} else {
|
|
3513
|
+
0 ;
|
|
3514
|
+
} ;
|
|
3515
|
+
} else {
|
|
3516
|
+
singular = 1 ;
|
|
3517
|
+
} ;
|
|
3518
|
+
if( singular == 1 ) {
|
|
3519
|
+
ts_minnorm(x,y,idx,w,aa,v,ev,e,ab,s,ns,fitint,p,d) ;
|
|
3520
|
+
} else {
|
|
3521
|
+
0 ;
|
|
3522
|
+
} ;
|
|
3523
|
+
;
|
|
3524
|
+
return 0 ;
|
|
3525
|
+
}
|
|
3526
|
+
|
|
3527
|
+
|
|
3528
|
+
SCM2CPP_FN inline double
|
|
3529
|
+
ts_abs( double v )
|
|
3530
|
+
{
|
|
3531
|
+
if( v < 0.0 ) {
|
|
3532
|
+
return (0.0-v) ;
|
|
3533
|
+
}
|
|
3534
|
+
else {
|
|
3535
|
+
return v ;
|
|
3536
|
+
}
|
|
3537
|
+
}
|
|
3538
|
+
|
|
3539
|
+
|
|
3540
|
+
inline int
|
|
3541
|
+
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 )
|
|
3542
|
+
{ int d = (p+fitint);
|
|
3543
|
+
int it = 0;
|
|
3544
|
+
std::vector<double> a((d*d),0.0);
|
|
3545
|
+
std::vector<double> b(d,0.0);
|
|
3546
|
+
std::vector<double> aa((d*d),0.0);
|
|
3547
|
+
std::vector<double> v((d*d),0.0);
|
|
3548
|
+
std::vector<double> ev(d,0.0);
|
|
3549
|
+
std::vector<double> e(d,0.0);
|
|
3550
|
+
std::vector<double> ab(d,0.0);
|
|
3551
|
+
std::vector<double> old(d,0.0);
|
|
3552
|
+
std::vector<double> num(d,0.0);
|
|
3553
|
+
std::vector<double> qv(d,0.0);
|
|
3554
|
+
double eps = 2.220446049250313e-16;
|
|
3555
|
+
for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
|
|
3556
|
+
ts_fit(x,y,idx,w,a,b,aa,v,ev,e,ab,s,ns,fitint,p,d) ;
|
|
3557
|
+
} ;
|
|
3558
|
+
if( d == 1 ) {
|
|
3559
|
+
std::vector<double> v(nsub,0.0);
|
|
3560
|
+
if( nsub > 65536 ) {
|
|
3561
|
+
#pragma omp parallel for
|
|
3562
|
+
for( int s = 0 ; s < nsub ; s += 1 ) {
|
|
3563
|
+
v[ s ] = w[s] ;
|
|
3564
|
+
}
|
|
3565
|
+
} else {
|
|
3566
|
+
for( int s = 0 ; s < nsub ; s += 1 ) {
|
|
3567
|
+
v[ s ] = w[s] ;
|
|
3568
|
+
}
|
|
3569
|
+
} ;
|
|
3570
|
+
for( int s = 1 ; !(s == nsub) ; s = (s+1) ) {
|
|
3571
|
+
double key = v[s];
|
|
3572
|
+
int j = (s-1);
|
|
3573
|
+
for( ; !((j < 0 || v[j] <= key)) ; ) {
|
|
3574
|
+
v[ (j+1) ] = v[j] ;
|
|
3575
|
+
j = (j-1) ;
|
|
3576
|
+
} ;
|
|
3577
|
+
v[ (j+1) ] = key ;
|
|
3578
|
+
;
|
|
3579
|
+
} ;
|
|
3580
|
+
coef[ 0 ] = ( ( (nsub % 2) == 1 ) ? (v[(nsub / 2)]) : ((0.5*(v[((nsub / 2)-1)]+v[(nsub / 2)]))) ) ;
|
|
3581
|
+
it = 1 ;
|
|
3582
|
+
;
|
|
3583
|
+
} else {
|
|
3584
|
+
double tol2 = (tol*tol);
|
|
3585
|
+
int done = 0;
|
|
3586
|
+
if( (d*nsub) > 65536 ) {
|
|
3587
|
+
#pragma omp parallel for
|
|
3588
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3589
|
+
double acc = 0.0;
|
|
3590
|
+
for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
|
|
3591
|
+
acc = (acc+w[((s*d)+j)]) ;
|
|
3592
|
+
} ;
|
|
3593
|
+
old[ j ] = (acc/double(nsub)) ;
|
|
3594
|
+
;
|
|
3595
|
+
}
|
|
3596
|
+
} else {
|
|
3597
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3598
|
+
double acc = 0.0;
|
|
3599
|
+
for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
|
|
3600
|
+
acc = (acc+w[((s*d)+j)]) ;
|
|
3601
|
+
} ;
|
|
3602
|
+
old[ j ] = (acc/double(nsub)) ;
|
|
3603
|
+
;
|
|
3604
|
+
}
|
|
3605
|
+
} ;
|
|
3606
|
+
for( ; !((done == 1 || it >= maxiter)) ; ) {
|
|
3607
|
+
int inx = 0;
|
|
3608
|
+
double sinv = 0.0;
|
|
3609
|
+
if( d > 65536 ) {
|
|
3610
|
+
#pragma omp parallel for
|
|
3611
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3612
|
+
num[ j ] = 0.0 ;
|
|
3613
|
+
qv[ j ] = 0.0 ;
|
|
3614
|
+
}
|
|
3615
|
+
} else {
|
|
3616
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3617
|
+
num[ j ] = 0.0 ;
|
|
3618
|
+
qv[ j ] = 0.0 ;
|
|
3619
|
+
}
|
|
3620
|
+
} ;
|
|
3621
|
+
for( int s = 0 ; !(s == nsub) ; s = (s+1) ) {
|
|
3622
|
+
double dn = 0.0;
|
|
3623
|
+
if( d > 65536 ) {
|
|
3624
|
+
#pragma omp parallel for reduction(+:dn)
|
|
3625
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3626
|
+
double df = (w[((s*d)+j)]-old[j]);
|
|
3627
|
+
dn = (dn+(df*df)) ;
|
|
3628
|
+
;
|
|
3629
|
+
}
|
|
3630
|
+
} else {
|
|
3631
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3632
|
+
double df = (w[((s*d)+j)]-old[j]);
|
|
3633
|
+
dn = (dn+(df*df)) ;
|
|
3634
|
+
;
|
|
3635
|
+
}
|
|
3636
|
+
} ;
|
|
3637
|
+
dn = sqrt(dn) ;
|
|
3638
|
+
if( dn >= eps ) {
|
|
3639
|
+
sinv = (sinv+(1.0/dn)) ;
|
|
3640
|
+
if( d > 65536 ) {
|
|
3641
|
+
#pragma omp parallel for
|
|
3642
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3643
|
+
qv[ j ] = (qv[j]+((w[((s*d)+j)]-old[j])/dn)) ;
|
|
3644
|
+
num[ j ] = (num[j]+(w[((s*d)+j)]/dn)) ;
|
|
3645
|
+
}
|
|
3646
|
+
} else {
|
|
3647
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3648
|
+
qv[ j ] = (qv[j]+((w[((s*d)+j)]-old[j])/dn)) ;
|
|
3649
|
+
num[ j ] = (num[j]+(w[((s*d)+j)]/dn)) ;
|
|
3650
|
+
}
|
|
3651
|
+
} ;
|
|
3652
|
+
} else {
|
|
3653
|
+
inx = 1 ;
|
|
3654
|
+
} ;
|
|
3655
|
+
;
|
|
3656
|
+
} ;
|
|
3657
|
+
double qn = 0.0;
|
|
3658
|
+
if( d > 65536 ) {
|
|
3659
|
+
#pragma omp parallel for reduction(+:qn)
|
|
3660
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3661
|
+
qn = (qn+(qv[j]*qv[j])) ;
|
|
3662
|
+
}
|
|
3663
|
+
} else {
|
|
3664
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3665
|
+
qn = (qn+(qv[j]*qv[j])) ;
|
|
3666
|
+
}
|
|
3667
|
+
} ;
|
|
3668
|
+
qn = sqrt(qn) ;
|
|
3669
|
+
int usedir = ( ( qn > eps ) ? (1) : (0) );
|
|
3670
|
+
double qn2 = ( ( usedir == 1 ) ? (qn) : (1.0) );
|
|
3671
|
+
double moved = 0.0;
|
|
3672
|
+
struct let6253 {
|
|
3673
|
+
let6253() {}
|
|
3674
|
+
double operator()( double t5 )
|
|
3675
|
+
{
|
|
3676
|
+
if( t5 > 0.0 ) {
|
|
3677
|
+
return t5 ;
|
|
3678
|
+
}
|
|
3679
|
+
else {
|
|
3680
|
+
return 0.0 ;
|
|
3681
|
+
}
|
|
3682
|
+
}
|
|
3683
|
+
} ;
|
|
3684
|
+
|
|
3685
|
+
struct let6275 {
|
|
3686
|
+
let6275() {}
|
|
3687
|
+
double operator()( double t6 )
|
|
3688
|
+
{
|
|
3689
|
+
if( t6 < 1.0 ) {
|
|
3690
|
+
return t6 ;
|
|
3691
|
+
}
|
|
3692
|
+
else {
|
|
3693
|
+
return 1.0 ;
|
|
3694
|
+
}
|
|
3695
|
+
}
|
|
3696
|
+
} ;
|
|
3697
|
+
double wa = let6253()((1.0-(double(inx)/qn2)));
|
|
3698
|
+
double wb = let6275()((double(inx)/qn2));
|
|
3699
|
+
if( d > 65536 ) {
|
|
3700
|
+
#pragma omp parallel for reduction(+:moved)
|
|
3701
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3702
|
+
double nd = ( ( usedir == 1 ) ? ((num[j]/sinv)) : (1.0) );
|
|
3703
|
+
double nw = ((wa*nd)+(wb*old[j]));
|
|
3704
|
+
moved = (moved+((old[j]-nw)*(old[j]-nw))) ;
|
|
3705
|
+
coef[ j ] = nw ;
|
|
3706
|
+
;
|
|
3707
|
+
;
|
|
3708
|
+
}
|
|
3709
|
+
} else {
|
|
3710
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3711
|
+
double nd = ( ( usedir == 1 ) ? ((num[j]/sinv)) : (1.0) );
|
|
3712
|
+
double nw = ((wa*nd)+(wb*old[j]));
|
|
3713
|
+
moved = (moved+((old[j]-nw)*(old[j]-nw))) ;
|
|
3714
|
+
coef[ j ] = nw ;
|
|
3715
|
+
;
|
|
3716
|
+
;
|
|
3717
|
+
}
|
|
3718
|
+
} ;
|
|
3719
|
+
;
|
|
3720
|
+
if( moved < tol2 ) {
|
|
3721
|
+
done = 1 ;
|
|
3722
|
+
} else {
|
|
3723
|
+
if( d > 65536 ) {
|
|
3724
|
+
#pragma omp parallel for
|
|
3725
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3726
|
+
old[ j ] = coef[j] ;
|
|
3727
|
+
}
|
|
3728
|
+
} else {
|
|
3729
|
+
for( int j = 0 ; j < d ; j += 1 ) {
|
|
3730
|
+
old[ j ] = coef[j] ;
|
|
3731
|
+
}
|
|
3732
|
+
} ;
|
|
3733
|
+
it = (it+1) ;
|
|
3734
|
+
} ;
|
|
3735
|
+
;
|
|
3736
|
+
;
|
|
3737
|
+
;
|
|
3738
|
+
;
|
|
3739
|
+
} ;
|
|
3740
|
+
;
|
|
3741
|
+
} ;
|
|
3742
|
+
;
|
|
3743
|
+
return it ;
|
|
3744
|
+
}
|
|
3745
|
+
#endif // CLASSIFIERS_HPP
|