-
Notifications
You must be signed in to change notification settings - Fork 15
/
ssvq_cimp.cpp
255 lines (191 loc) · 5.65 KB
/
ssvq_cimp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**********************************************************
*
* C++ mex of the core part of pli_ssvq
*
**********************************************************/
#include <light_mat/matlab/matlab_port.h>
using namespace lmat;
using namespace lmat::matlab;
typedef cref_matrix<double, 0, 0> cmat_t;
typedef cref_matrix<double, 0, 1> cvec_t;
typedef ref_matrix<double, 0, 0> mat_t;
typedef ref_matrix<double, 0, 1> vec_t;
LMAT_ENSURE_INLINE
inline void set_center(mat_t& C, index_t k, const cvec_t& x)
{
vec_t c = C.column(k);
c = x;
}
LMAT_ENSURE_INLINE
inline double calc_cost(const cvec_t& cen, const cvec_t& x)
{
double s = 0;
const index_t d = cen.nrows();
for (index_t i = 0; i < d; ++i)
{
double dx = cen[i] - x[i];
s += dx * dx;
}
return s;
}
LMAT_ENSURE_INLINE
inline index_t find_closest_center(
const mat_t& C, const index_t k, const cvec_t& x, double& cost)
{
index_t best_i = 0;
cost = calc_cost(C.column(0), x);
for (index_t i = 1; i < k; ++i)
{
double cc = calc_cost(C.column(i), x);
if (cc < cost)
{
cost = cc;
best_i = i;
}
}
return best_i;
}
LMAT_ENSURE_INLINE
inline void update_center(vec_t& c, double& cw, const cvec_t& x, double xw)
{
cw += xw;
double a = xw / cw;
double b = 1.0 - a;
const index_t d = c.nrows();
for (index_t i = 0; i < d; ++i)
{
c[i] = a * x[i] + b * c[i];
}
}
LMAT_ENSURE_INLINE
inline void see_print(const index_t vb_intv, index_t& vbcount, index_t i)
{
if (vb_intv)
{
if (--vbcount == 0)
{
vbcount = vb_intv;
mexPrintf(" %ld samples processed\n", long(i));
}
}
}
void ssvq(
mat_t& C, /* centers */
vec_t& w, /* center weights */
const double cbnd, /* cost bound */
const cmat_t& X, /* samples */
const cvec_t& xw, /* sample weights */
const cvec_t& u, /* random number sequence */
index_t& k, /* number of centers */
index_t& i, /* index of last processed sample */
const index_t vb_intv /* displaying interval */
)
{
const index_t kmax = C.ncolumns();
const index_t n = X.ncolumns();
index_t vbcount = 0;
if (vb_intv)
{
vbcount = vb_intv - (i % vb_intv);
}
if (k == 0 && i < n)
{
cvec_t x = X.column(i);
w[k] = xw[i];
set_center(C, k++, x);
++i;
see_print(vb_intv, vbcount, i);
}
while (i < n)
{
cvec_t x = X.column(i);
double cost;
index_t s = find_closest_center(C, k, x, cost);
bool to_add = !(xw[i] * cost < u[i] * cbnd);
if (xw[i] * cost < u[i] * cbnd)
{
// update to s-th center
vec_t cen = C.column(s);
update_center(cen, w[s], x, xw[i]);
}
else
{
if (k >= kmax) return;
// add new center
w[k] = xw[i];
set_center(C, k++, x);
}
++i;
see_print(vb_intv, vbcount, i);
}
}
/**
* Input
* [0] C0: initial set of centers
* [1] w0: initial center weights
* [2] cbnd: cost bound
*
* [3] X: sample matrix
* [4] xw: sample weights
* [5] u: rand numbers (one for a sample)
*
* [6] kmax: maximum number of centers
* [7] i0: The index of starting sample (i.e. # processed samples)
* [8] vb_intv: interval for displaying
*
* Output
* [0] C: updated centers
* [1] w: updated center weights
* [2] i: the (zero-based) index of last processed samples
*/
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// input arguments
const_marray _C0 = prhs[0];
const_marray _w0 = prhs[1];
const_marray _cbnd = prhs[2];
const_marray _X = prhs[3];
const_marray _xw = prhs[4];
const_marray _u = prhs[5];
const_marray _kmax = prhs[6];
const_marray _i0 = prhs[7];
const_marray _vb_intv = prhs[8];
// extract inputs
double cbnd = _cbnd.get_scalar();
cmat_t X = view2d<double>(_X);
cvec_t xw = view_as_col<double>(_xw);
cvec_t u = view_as_col<double>(_u);
const index_t kmax = (index_t)(_kmax.get_scalar());
index_t i = (index_t)(_i0.get_scalar());
const index_t vb_intv = (index_t)(_vb_intv.get_scalar());
const index_t d = X.nrows();
// prepare outputs
marray _C = marray::numeric_matrix<double>(d, kmax);
marray _w = marray::numeric_matrix<double>(1, kmax);
mat_t C = view2d<double>(_C);
vec_t w = view_as_col<double>(_w);
index_t k = 0;
if (_C0.is_empty())
{
k = _C0.ncolumns();
copy_vec(d * k, _C0.data<double>(), C.ptr_data());
copy_vec(k, _w0.data<double>(), w.ptr_data());
}
// call main function
ssvq(C, w, cbnd, X, xw, u, k, i, vb_intv);
// output
if (k < kmax)
{
marray _Ctmp = _C;
marray _wtmp = _w;
_C = marray::numeric_matrix<double>(d, k);
_w = marray::numeric_matrix<double>(1, k);
copy_vec(d * k, C.ptr_data(), _C.data<double>());
copy_vec(k, w.ptr_data(), _w.data<double>());
_Ctmp.destroy();
_wtmp.destroy();
}
plhs[0] = _C;
plhs[1] = _w;
plhs[2] = marray::from_double_scalar(double(i));
}