GenSVM
test_gensvm_zv.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_zv.h"
29 
31 {
32  int n = 8,
33  m = 3,
34  K = 3;
35 
36  struct GenModel *model = gensvm_init_model();
37  model->n = n;
38  model->m = m;
39  model->K = K;
40  model->V = Calloc(double, (m+1)*(K-1));
41  matrix_set(model->V, model->K-1, 0, 0, 0.9025324416711976);
42  matrix_set(model->V, model->K-1, 0, 1, 0.9776784486541952);
43  matrix_set(model->V, model->K-1, 1, 0, 0.8336347240271171);
44  matrix_set(model->V, model->K-1, 1, 1, 0.1213543508830703);
45  matrix_set(model->V, model->K-1, 2, 0, 0.9401310852208050);
46  matrix_set(model->V, model->K-1, 2, 1, 0.7407478086613410);
47  matrix_set(model->V, model->K-1, 3, 0, 0.9053353815353901);
48  matrix_set(model->V, model->K-1, 3, 1, 0.8056059951641629);
49 
50  struct GenData *data = gensvm_init_data();
51  data->n = n;
52  data->m = m;
53  data->K = K;
54  data->Z = Calloc(double, n*(m+1));
55  matrix_set(data->Z, data->m+1, 0, 0, 1.0000000000000000);
56  matrix_set(data->Z, data->m+1, 0, 1, 0.4787662921736276);
57  matrix_set(data->Z, data->m+1, 0, 2, 0.7983044792882817);
58  matrix_set(data->Z, data->m+1, 0, 3, 0.4273006962165122);
59  matrix_set(data->Z, data->m+1, 1, 0, 1.0000000000000000);
60  matrix_set(data->Z, data->m+1, 1, 1, 0.7160319769123790);
61  matrix_set(data->Z, data->m+1, 1, 2, 0.5233066338418962);
62  matrix_set(data->Z, data->m+1, 1, 3, 0.4063256860579537);
63  matrix_set(data->Z, data->m+1, 2, 0, 1.0000000000000000);
64  matrix_set(data->Z, data->m+1, 2, 1, 0.3735389652435536);
65  matrix_set(data->Z, data->m+1, 2, 2, 0.8156214578257802);
66  matrix_set(data->Z, data->m+1, 2, 3, 0.6928367712901857);
67  matrix_set(data->Z, data->m+1, 3, 0, 1.0000000000000000);
68  matrix_set(data->Z, data->m+1, 3, 1, 0.3694690105850765);
69  matrix_set(data->Z, data->m+1, 3, 2, 0.8539671806454873);
70  matrix_set(data->Z, data->m+1, 3, 3, 0.5455108033084728);
71  matrix_set(data->Z, data->m+1, 4, 0, 1.0000000000000000);
72  matrix_set(data->Z, data->m+1, 4, 1, 0.8802158533820680);
73  matrix_set(data->Z, data->m+1, 4, 2, 0.0690778177684403);
74  matrix_set(data->Z, data->m+1, 4, 3, 0.4513353324958240);
75  matrix_set(data->Z, data->m+1, 5, 0, 1.0000000000000000);
76  matrix_set(data->Z, data->m+1, 5, 1, 0.7752402729955837);
77  matrix_set(data->Z, data->m+1, 5, 2, 0.3941285577056867);
78  matrix_set(data->Z, data->m+1, 5, 3, 0.2921042477960945);
79  matrix_set(data->Z, data->m+1, 6, 0, 1.0000000000000000);
80  matrix_set(data->Z, data->m+1, 6, 1, 0.6139038657913901);
81  matrix_set(data->Z, data->m+1, 6, 2, 0.4529743309354828);
82  matrix_set(data->Z, data->m+1, 6, 3, 0.7295983135133345);
83  matrix_set(data->Z, data->m+1, 7, 0, 1.0000000000000000);
84  matrix_set(data->Z, data->m+1, 7, 1, 0.7663625136928905);
85  matrix_set(data->Z, data->m+1, 7, 2, 0.3845759571625976);
86  matrix_set(data->Z, data->m+1, 7, 3, 0.2291505633226144);
87 
88  // start test code //
89  double *ZV = Calloc(double, n*(K-1));
90  double eps = 1e-14;
91  gensvm_calculate_ZV(model, data, ZV);
92 
93  mu_assert(fabs(matrix_get(ZV, K-1, 0, 0) - 2.4390099428102818) < eps,
94  "Incorrect ZV at 0, 0");
95  mu_assert(fabs(matrix_get(ZV, K-1, 0, 1) - 1.9713571175527906) < eps,
96  "Incorrect ZV at 0, 1");
97  mu_assert(fabs(matrix_get(ZV, K-1, 1, 0) - 2.3592794147310747) < eps,
98  "Incorrect ZV at 1, 0");
99  mu_assert(fabs(matrix_get(ZV, K-1, 1, 1) - 1.7795486953777246) < eps,
100  "Incorrect ZV at 1, 1");
101  mu_assert(fabs(matrix_get(ZV, K-1, 2, 0) - 2.6079682228282564) < eps,
102  "Incorrect ZV at 2, 0");
103  mu_assert(fabs(matrix_get(ZV, K-1, 2, 1) - 2.1853322915140310) < eps,
104  "Incorrect ZV at 2, 1");
105  mu_assert(fabs(matrix_get(ZV, K-1, 3, 0) - 2.5072459618750060) < eps,
106  "Incorrect ZV at 3, 0");
107  mu_assert(fabs(matrix_get(ZV, K-1, 3, 1) - 2.0945562119091297) < eps,
108  "Incorrect ZV at 3, 1");
109  mu_assert(fabs(matrix_get(ZV, K-1, 4, 0) - 2.1098629909184887) < eps,
110  "Incorrect ZV at 4, 0");
111  mu_assert(fabs(matrix_get(ZV, K-1, 4, 1) - 1.4992641640054902) < eps,
112  "Incorrect ZV at 4, 1");
113  mu_assert(fabs(matrix_get(ZV, K-1, 5, 0) - 2.1837844720035213) < eps,
114  "Incorrect ZV at 5, 0");
115  mu_assert(fabs(matrix_get(ZV, K-1, 5, 1) - 1.5990280274507829) < eps,
116  "Incorrect ZV at 5, 1");
117  mu_assert(fabs(matrix_get(ZV, K-1, 6, 0) - 2.5006904382610986) < eps,
118  "Incorrect ZV at 6, 0");
119  mu_assert(fabs(matrix_get(ZV, K-1, 6, 1) - 1.9754868722402175) < eps,
120  "Incorrect ZV at 6, 1");
121  mu_assert(fabs(matrix_get(ZV, K-1, 7, 0) - 2.1104087689101294) < eps,
122  "Incorrect ZV at 7, 0");
123  mu_assert(fabs(matrix_get(ZV, K-1, 7, 1) - 1.5401587391844891) < eps,
124  "Incorrect ZV at 7, 1");
125 
126  free(ZV);
127  // end test code //
128  gensvm_free_data(data);
129  gensvm_free_model(model);
130 
131  return NULL;
132 }
133 
135 {
136  int n = 8,
137  m = 3,
138  K = 3;
139 
140  struct GenModel *model = gensvm_init_model();
141  model->n = n;
142  model->m = m;
143  model->K = K;
144  model->V = Calloc(double, (m+1)*(K-1));
145  matrix_set(model->V, model->K-1, 0, 0, 0.9025324416711976);
146  matrix_set(model->V, model->K-1, 0, 1, 0.9776784486541952);
147  matrix_set(model->V, model->K-1, 1, 0, 0.8336347240271171);
148  matrix_set(model->V, model->K-1, 1, 1, 0.1213543508830703);
149  matrix_set(model->V, model->K-1, 2, 0, 0.9401310852208050);
150  matrix_set(model->V, model->K-1, 2, 1, 0.7407478086613410);
151  matrix_set(model->V, model->K-1, 3, 0, 0.9053353815353901);
152  matrix_set(model->V, model->K-1, 3, 1, 0.8056059951641629);
153 
154  struct GenData *data = gensvm_init_data();
155  data->n = 3;
156  data->m = m;
157  data->K = K;
158  data->Z = Calloc(double, data->n*(data->m+1));
159  matrix_set(data->Z, data->m+1, 0, 0, 1.0000000000000000);
160  matrix_set(data->Z, data->m+1, 0, 1, 0.4787662921736276);
161  matrix_set(data->Z, data->m+1, 0, 2, 0.7983044792882817);
162  matrix_set(data->Z, data->m+1, 0, 3, 0.4273006962165122);
163  matrix_set(data->Z, data->m+1, 1, 0, 1.0000000000000000);
164  matrix_set(data->Z, data->m+1, 1, 1, 0.7160319769123790);
165  matrix_set(data->Z, data->m+1, 1, 2, 0.5233066338418962);
166  matrix_set(data->Z, data->m+1, 1, 3, 0.4063256860579537);
167  matrix_set(data->Z, data->m+1, 2, 0, 1.0000000000000000);
168  matrix_set(data->Z, data->m+1, 2, 1, 0.3735389652435536);
169  matrix_set(data->Z, data->m+1, 2, 2, 0.8156214578257802);
170  matrix_set(data->Z, data->m+1, 2, 3, 0.6928367712901857);
171 
172  // start test code //
173  double *ZV = Calloc(double, data->n*(K-1));
174  double eps = 1e-14;
175  gensvm_calculate_ZV(model, data, ZV);
176 
177  mu_assert(fabs(matrix_get(ZV, K-1, 0, 0) - 2.4390099428102818) < eps,
178  "Incorrect ZV at 0, 0");
179  mu_assert(fabs(matrix_get(ZV, K-1, 0, 1) - 1.9713571175527906) < eps,
180  "Incorrect ZV at 0, 1");
181  mu_assert(fabs(matrix_get(ZV, K-1, 1, 0) - 2.3592794147310747) < eps,
182  "Incorrect ZV at 1, 0");
183  mu_assert(fabs(matrix_get(ZV, K-1, 1, 1) - 1.7795486953777246) < eps,
184  "Incorrect ZV at 1, 1");
185  mu_assert(fabs(matrix_get(ZV, K-1, 2, 0) - 2.6079682228282564) < eps,
186  "Incorrect ZV at 2, 0");
187  mu_assert(fabs(matrix_get(ZV, K-1, 2, 1) - 2.1853322915140310) < eps,
188  "Incorrect ZV at 2, 1");
189 
190  free(ZV);
191  // end test code //
192  gensvm_free_data(data);
193  gensvm_free_model(model);
194 
195  return NULL;
196 }
197 
199 {
200  int n = 8,
201  m = 3,
202  K = 3;
203 
204  struct GenModel *model = gensvm_init_model();
205  model->n = n;
206  model->m = m;
207  model->K = K;
208  model->V = Calloc(double, (m+1)*(K-1));
209  matrix_set(model->V, model->K-1, 0, 0, 0.9025324416711976);
210  matrix_set(model->V, model->K-1, 0, 1, 0.9776784486541952);
211  matrix_set(model->V, model->K-1, 1, 0, 0.8336347240271171);
212  matrix_set(model->V, model->K-1, 1, 1, 0.1213543508830703);
213  matrix_set(model->V, model->K-1, 2, 0, 0.9401310852208050);
214  matrix_set(model->V, model->K-1, 2, 1, 0.7407478086613410);
215  matrix_set(model->V, model->K-1, 3, 0, 0.9053353815353901);
216  matrix_set(model->V, model->K-1, 3, 1, 0.8056059951641629);
217 
218  struct GenData *data = gensvm_init_data();
219  data->n = n;
220  data->m = m;
221  data->K = K;
222  data->Z = Calloc(double, n*(m+1));
223  matrix_set(data->Z, data->m+1, 0, 0, 1.0000000000000000);
224  matrix_set(data->Z, data->m+1, 0, 1, 0.4787662921736276);
225  matrix_set(data->Z, data->m+1, 0, 2, 0.7983044792882817);
226  matrix_set(data->Z, data->m+1, 0, 3, 0.4273006962165122);
227  matrix_set(data->Z, data->m+1, 1, 0, 1.0000000000000000);
228  matrix_set(data->Z, data->m+1, 1, 1, 0.7160319769123790);
229  matrix_set(data->Z, data->m+1, 1, 2, 0.5233066338418962);
230  matrix_set(data->Z, data->m+1, 1, 3, 0.4063256860579537);
231  matrix_set(data->Z, data->m+1, 2, 0, 1.0000000000000000);
232  matrix_set(data->Z, data->m+1, 2, 1, 0.3735389652435536);
233  matrix_set(data->Z, data->m+1, 2, 2, 0.8156214578257802);
234  matrix_set(data->Z, data->m+1, 2, 3, 0.6928367712901857);
235  matrix_set(data->Z, data->m+1, 3, 0, 1.0000000000000000);
236  matrix_set(data->Z, data->m+1, 3, 1, 0.3694690105850765);
237  matrix_set(data->Z, data->m+1, 3, 2, 0.8539671806454873);
238  matrix_set(data->Z, data->m+1, 3, 3, 0.5455108033084728);
239  matrix_set(data->Z, data->m+1, 4, 0, 1.0000000000000000);
240  matrix_set(data->Z, data->m+1, 4, 1, 0.8802158533820680);
241  matrix_set(data->Z, data->m+1, 4, 2, 0.0690778177684403);
242  matrix_set(data->Z, data->m+1, 4, 3, 0.4513353324958240);
243  matrix_set(data->Z, data->m+1, 5, 0, 1.0000000000000000);
244  matrix_set(data->Z, data->m+1, 5, 1, 0.7752402729955837);
245  matrix_set(data->Z, data->m+1, 5, 2, 0.3941285577056867);
246  matrix_set(data->Z, data->m+1, 5, 3, 0.2921042477960945);
247  matrix_set(data->Z, data->m+1, 6, 0, 1.0000000000000000);
248  matrix_set(data->Z, data->m+1, 6, 1, 0.6139038657913901);
249  matrix_set(data->Z, data->m+1, 6, 2, 0.4529743309354828);
250  matrix_set(data->Z, data->m+1, 6, 3, 0.7295983135133345);
251  matrix_set(data->Z, data->m+1, 7, 0, 1.0000000000000000);
252  matrix_set(data->Z, data->m+1, 7, 1, 0.7663625136928905);
253  matrix_set(data->Z, data->m+1, 7, 2, 0.3845759571625976);
254  matrix_set(data->Z, data->m+1, 7, 3, 0.2291505633226144);
255 
256  // convert Z to sparse matrix
257  data->spZ = gensvm_dense_to_sparse(data->Z, data->n, data->m+1);
258  free(data->Z);
259  data->RAW = NULL;
260  data->Z = NULL;
261 
262  // start test code //
263  double *ZV = Calloc(double, n*(K-1));
264  double eps = 1e-14;
265  gensvm_calculate_ZV(model, data, ZV);
266 
267  mu_assert(fabs(matrix_get(ZV, K-1, 0, 0) - 2.4390099428102818) < eps,
268  "Incorrect ZV at 0, 0");
269  mu_assert(fabs(matrix_get(ZV, K-1, 0, 1) - 1.9713571175527906) < eps,
270  "Incorrect ZV at 0, 1");
271  mu_assert(fabs(matrix_get(ZV, K-1, 1, 0) - 2.3592794147310747) < eps,
272  "Incorrect ZV at 1, 0");
273  mu_assert(fabs(matrix_get(ZV, K-1, 1, 1) - 1.7795486953777246) < eps,
274  "Incorrect ZV at 1, 1");
275  mu_assert(fabs(matrix_get(ZV, K-1, 2, 0) - 2.6079682228282564) < eps,
276  "Incorrect ZV at 2, 0");
277  mu_assert(fabs(matrix_get(ZV, K-1, 2, 1) - 2.1853322915140310) < eps,
278  "Incorrect ZV at 2, 1");
279  mu_assert(fabs(matrix_get(ZV, K-1, 3, 0) - 2.5072459618750060) < eps,
280  "Incorrect ZV at 3, 0");
281  mu_assert(fabs(matrix_get(ZV, K-1, 3, 1) - 2.0945562119091297) < eps,
282  "Incorrect ZV at 3, 1");
283  mu_assert(fabs(matrix_get(ZV, K-1, 4, 0) - 2.1098629909184887) < eps,
284  "Incorrect ZV at 4, 0");
285  mu_assert(fabs(matrix_get(ZV, K-1, 4, 1) - 1.4992641640054902) < eps,
286  "Incorrect ZV at 4, 1");
287  mu_assert(fabs(matrix_get(ZV, K-1, 5, 0) - 2.1837844720035213) < eps,
288  "Incorrect ZV at 5, 0");
289  mu_assert(fabs(matrix_get(ZV, K-1, 5, 1) - 1.5990280274507829) < eps,
290  "Incorrect ZV at 5, 1");
291  mu_assert(fabs(matrix_get(ZV, K-1, 6, 0) - 2.5006904382610986) < eps,
292  "Incorrect ZV at 6, 0");
293  mu_assert(fabs(matrix_get(ZV, K-1, 6, 1) - 1.9754868722402175) < eps,
294  "Incorrect ZV at 6, 1");
295  mu_assert(fabs(matrix_get(ZV, K-1, 7, 0) - 2.1104087689101294) < eps,
296  "Incorrect ZV at 7, 0");
297  mu_assert(fabs(matrix_get(ZV, K-1, 7, 1) - 1.5401587391844891) < eps,
298  "Incorrect ZV at 7, 1");
299 
300  free(ZV);
301  // end test code //
302  gensvm_free_data(data);
303  gensvm_free_model(model);
304 
305  return NULL;
306 }
307 
309 {
310  int n = 8,
311  m = 3,
312  K = 3;
313 
314  struct GenModel *model = gensvm_init_model();
315  model->n = n;
316  model->m = m;
317  model->K = K;
318  model->V = Calloc(double, (m+1)*(K-1));
319  matrix_set(model->V, model->K-1, 0, 0, 0.9025324416711976);
320  matrix_set(model->V, model->K-1, 0, 1, 0.9776784486541952);
321  matrix_set(model->V, model->K-1, 1, 0, 0.8336347240271171);
322  matrix_set(model->V, model->K-1, 1, 1, 0.1213543508830703);
323  matrix_set(model->V, model->K-1, 2, 0, 0.9401310852208050);
324  matrix_set(model->V, model->K-1, 2, 1, 0.7407478086613410);
325  matrix_set(model->V, model->K-1, 3, 0, 0.9053353815353901);
326  matrix_set(model->V, model->K-1, 3, 1, 0.8056059951641629);
327 
328  struct GenData *data = gensvm_init_data();
329  data->n = 3;
330  data->m = m;
331  data->K = K;
332  data->Z = Calloc(double, data->n*(data->m+1));
333  matrix_set(data->Z, data->m+1, 0, 0, 1.0000000000000000);
334  matrix_set(data->Z, data->m+1, 0, 1, 0.4787662921736276);
335  matrix_set(data->Z, data->m+1, 0, 2, 0.7983044792882817);
336  matrix_set(data->Z, data->m+1, 0, 3, 0.4273006962165122);
337  matrix_set(data->Z, data->m+1, 1, 0, 1.0000000000000000);
338  matrix_set(data->Z, data->m+1, 1, 1, 0.7160319769123790);
339  matrix_set(data->Z, data->m+1, 1, 2, 0.5233066338418962);
340  matrix_set(data->Z, data->m+1, 1, 3, 0.4063256860579537);
341  matrix_set(data->Z, data->m+1, 2, 0, 1.0000000000000000);
342  matrix_set(data->Z, data->m+1, 2, 1, 0.3735389652435536);
343  matrix_set(data->Z, data->m+1, 2, 2, 0.8156214578257802);
344  matrix_set(data->Z, data->m+1, 2, 3, 0.6928367712901857);
345 
346  // convert Z to sparse matrix
347  data->spZ = gensvm_dense_to_sparse(data->Z, data->n, data->m+1);
348  free(data->Z);
349  data->RAW = NULL;
350  data->Z = NULL;
351 
352  // start test code //
353  double *ZV = Calloc(double, data->n*(data->K-1));
354  double eps = 1e-14;
355  gensvm_calculate_ZV(model, data, ZV);
356 
357  mu_assert(fabs(matrix_get(ZV, K-1, 0, 0) - 2.4390099428102818) < eps,
358  "Incorrect ZV at 0, 0");
359  mu_assert(fabs(matrix_get(ZV, K-1, 0, 1) - 1.9713571175527906) < eps,
360  "Incorrect ZV at 0, 1");
361  mu_assert(fabs(matrix_get(ZV, K-1, 1, 0) - 2.3592794147310747) < eps,
362  "Incorrect ZV at 1, 0");
363  mu_assert(fabs(matrix_get(ZV, K-1, 1, 1) - 1.7795486953777246) < eps,
364  "Incorrect ZV at 1, 1");
365  mu_assert(fabs(matrix_get(ZV, K-1, 2, 0) - 2.6079682228282564) < eps,
366  "Incorrect ZV at 2, 0");
367  mu_assert(fabs(matrix_get(ZV, K-1, 2, 1) - 2.1853322915140310) < eps,
368  "Incorrect ZV at 2, 1");
369 
370  free(ZV);
371  // end test code //
372  gensvm_free_data(data);
373  gensvm_free_model(model);
374 
375  return NULL;
376 }
377 
378 char *all_tests()
379 {
380  mu_suite_start();
385 
386  return NULL;
387 }
388 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
long K
number of classes for the workspace
Definition: gensvm_base.h:156
long m
number of features for the workspace
Definition: gensvm_base.h:154
#define mu_assert(test, message)
Definition: minunit.h:29
long K
number of classes
Definition: gensvm_base.h:58
char * test_zv_dense_2()
#define matrix_get(M, cols, i, j)
char * test_zv_sparse_2()
double * Z
Definition: gensvm_base.h:68
void gensvm_free_model(struct GenModel *model)
Free allocated GenModel struct.
Definition: gensvm_base.c:211
double * V
augmented weight matrix
Definition: gensvm_base.h:115
#define mu_run_test(test)
Definition: minunit.h:35
struct GenModel * gensvm_init_model(void)
Initialize a GenModel structure.
Definition: gensvm_base.c:102
A structure to represent the data.
Definition: gensvm_base.h:57
A structure to represent a single GenSVM model.
Definition: gensvm_base.h:92
void gensvm_calculate_ZV(struct GenModel *model, struct GenData *data, double *ZV)
Wrapper around sparse/dense versions of this function.
Definition: gensvm_zv.c:50
long n
number of instances in the dataset
Definition: gensvm_base.h:97
char * test_zv_sparse_1()
char * test_zv_dense_1()
RUN_TESTS(all_tests)
char * all_tests()
void gensvm_free_data(struct GenData *data)
Free allocated GenData struct.
Definition: gensvm_base.c:73
Header file for gensvm_zv.c.
long K
number of classes in the dataset
Definition: gensvm_base.h:95
long m
number of predictors (width of RAW)
Definition: gensvm_base.h:62
struct GenSparse * gensvm_dense_to_sparse(double *A, long rows, long cols)
Convert a dense matrix to a GenSparse structure if advantageous.
#define matrix_set(M, cols, i, j, val)
long n
number of instances
Definition: gensvm_base.h:60
struct GenData * gensvm_init_data(void)
Initialize a GenData structure.
Definition: gensvm_base.c:45
long n
number of instances for the workspace
Definition: gensvm_base.h:152
long m
number of predictor variables in the dataset
Definition: gensvm_base.h:99
#define mu_suite_start()
Definition: minunit.h:24
double * RAW
augmented raw data matrix
Definition: gensvm_base.h:73
struct GenSparse * spZ
sparse representation of the augmented data matrix
Definition: gensvm_base.h:71