Statistics
| Branch: | Tag: | Revision:

sssimulator / Matrix / Test / matrix_test.c @ d6fa79fc

History | View | Annotate | Download (10.3 KB)

1
/*
2
 * This is SSSim: the Simple & Stupid Simulator
3
 *
4
 *  Copyright (c) 2015 Luca Baldesi
5
 *
6
 *  This is free software; see gpl-3.0.txt
7
 */
8

    
9
#include <assert.h>
10
#include <stdio.h>
11
#include <math.h>
12

    
13
#include <matrix.h>
14

    
15
void write_edgefile()
16
{
17
        FILE *efile;
18

    
19
        efile = fopen("net.edges", "w");
20
        fputs("0 1 1\n", efile);
21
        fputs("1 2 1\n", efile);
22
        fputs("2 3 1\n", efile);
23
        fputs("4 0 1\n", efile);
24
        fputs("5 0 1\n", efile);
25
        fputs("0 4 1\n", efile);
26
        fputs("0 5 1\n", efile);
27
        fputs("1 0 1\n", efile);
28
        fputs("2 1 1\n", efile);
29
        fputs("2 5 1\n", efile);
30
        fputs("3 2 1\n", efile);
31
        fputs("3 5 1\n", efile);
32
        fputs("4 5 1\n", efile);
33
        fputs("5 2 1\n", efile);
34
        fputs("5 3 1\n", efile);
35
        fputs("5 4 1\n", efile);
36
        fclose(efile);
37
}
38

    
39
void matrix_from_edgefile_test()
40
{
41
        struct matrix * m;
42
        FILE *efile;
43

    
44
        m = matrix_from_edgefile(NULL);
45
        assert(m == NULL);
46

    
47
        m = matrix_from_edgefile("non-existing_file");
48
        assert(m == NULL);
49

    
50
        efile = fopen("net.edges", "w");
51
        fputs("", efile);
52
        fclose(efile);
53
        m = matrix_from_edgefile("net.edges");
54
        assert(m == NULL);
55

    
56
        efile = fopen("net.edges", "a");
57
        fputs("# Hi, this is a comment!\n", efile);
58
        fclose(efile);
59
        m = matrix_from_edgefile("net.edges");
60
        assert(m == NULL);
61

    
62
        efile = fopen("net.edges", "a");
63
        fputs("0 1 0.3\n", efile);
64
        fputs("0 2 5.6\n", efile);
65
        fputs("1 2 39994\n", efile);
66
        fclose(efile);
67
        m = matrix_from_edgefile("net.edges");
68
        assert(m != NULL);
69
        assert(matrix_num_rows(m)  == 3);
70
        assert(matrix_num_cols(m) == 3);
71
        assert(matrix_element_get(m, 0, 0) == 0);
72
        assert(matrix_element_get(m, 0, 1) == 0.3);
73
        assert(matrix_element_get(m, 0, 2) == 5.6);
74
        assert(matrix_element_get(m, 1, 0) == 0);
75
        assert(matrix_element_get(m, 1, 1) == 0);
76
        assert(matrix_element_get(m, 1, 2) == 39994);
77
        assert(matrix_element_get(m, 2, 0) == 0);
78
        assert(matrix_element_get(m, 2, 1) == 0);
79
        assert(matrix_element_get(m, 2, 2) == 0);
80
        matrix_destroy(&m);
81

    
82
        fprintf(stderr,"%s successfully passed!\n",__func__);
83
}
84

    
85
void matrix_ones_test()
86
{
87
        struct matrix * m;
88

    
89
        m = matrix_ones(0, 0);
90
        assert(m == NULL);
91

    
92
        m = matrix_ones(1, 0);
93
        assert(m == NULL);
94

    
95
        m = matrix_ones(0, 1);
96
        assert(m == NULL);
97

    
98
        m = matrix_ones(1, 1);
99
        assert(m != NULL);
100
        assert(matrix_element_get(m, 0, 0) == 1);
101
        matrix_destroy(&m);
102

    
103
        m = matrix_ones(2, 2);
104
        assert(m != NULL);
105
        assert(matrix_element_get(m, 0, 0) == 1);
106
        assert(matrix_element_get(m, 0, 1) == 1);
107
        assert(matrix_element_get(m, 1, 0) == 1);
108
        assert(matrix_element_get(m, 1, 1) == 1);
109
        matrix_destroy(&m);
110

    
111
        fprintf(stderr,"%s successfully passed!\n",__func__);
112
}
113

    
114
void matrix_shrink_test()
115
{
116
        FILE *fp;
117
        struct matrix *m;
118
        matsize i;
119

    
120
        write_edgefile();
121
        m = matrix_from_edgefile("net.edges");
122

    
123
        matrix_shrink(NULL, 1);
124
        matrix_shrink(&m, 1);
125
        matrix_shrink(&m, 100);
126

    
127
        matrix_destroy(&m);
128

    
129
        m = matrix_ones(1, 5);
130
        matrix_shrink(&m, 2);
131
        assert(matrix_num_rows(m) == 1);
132
        assert(matrix_num_cols(m) == 4);
133
        for(i = 0; i < matrix_num_cols(m); i++)
134
                assert(matrix_element_get(m, 0, i) == 1);
135
        matrix_destroy(&m);
136

    
137
        m = matrix_ones(5, 1);
138
        matrix_shrink(&m, 2);
139
        assert(matrix_num_rows(m) == 4);
140
        assert(matrix_num_cols(m) == 1);
141
        for(i = 0; i < matrix_num_cols(m); i++)
142
                assert(matrix_element_get(m, i, 0) == 1);
143
        matrix_destroy(&m);
144

    
145
        m = matrix_ones(1, 5);
146
        matrix_shrink(&m, 0);
147
        assert(matrix_num_rows(m) == 1);
148
        assert(matrix_num_cols(m) == 4);
149
        for(i = 0; i < matrix_num_cols(m); i++)
150
                assert(matrix_element_get(m, 0, i) == 1);
151
        matrix_destroy(&m);
152

    
153
        m = matrix_ones(5, 1);
154
        matrix_shrink(&m, 0);
155
        assert(matrix_num_rows(m) == 4);
156
        assert(matrix_num_cols(m) == 1);
157
        for(i = 0; i < matrix_num_cols(m); i++)
158
                assert(matrix_element_get(m, i, 0) == 1);
159
        matrix_destroy(&m);
160

    
161
        fprintf(stderr,"%s successfully passed!\n",__func__);
162
}
163

    
164
void matrix_multiply_test()
165
{
166
        FILE *fp;
167
        struct matrix *m1, *m2, *m3;
168
        
169
        write_edgefile();
170
        m2 = matrix_from_edgefile("net.edges");
171

    
172
        m3 = matrix_multiply(NULL, NULL);
173
        assert(m3 == NULL);
174

    
175
        m3 = matrix_multiply(m2, NULL);
176
        assert(m3 == NULL);
177

    
178
        m3 = matrix_multiply(NULL, m2);
179
        assert(m3 == NULL);
180

    
181
        m1 = matrix_ones(6, 1);
182
        m3 = matrix_multiply(m2, m1);
183
        assert(m3 != NULL);
184
        assert(matrix_element_get(m3, 0, 0) == 3);
185
        assert(matrix_element_get(m3, 1, 0) == 2);
186
        assert(matrix_element_get(m3, 2, 0) == 3);
187
        assert(matrix_element_get(m3, 3, 0) == 2);
188
        assert(matrix_element_get(m3, 4, 0) == 2);
189
        assert(matrix_element_get(m3, 5, 0) == 4);
190

    
191
        matrix_destroy(&m3);
192
        m3 = matrix_multiply(m2, m2);
193
        assert(matrix_element_get(m3, 0, 0) == 3);
194
        assert(matrix_element_get(m3, 1, 0) == 0);
195
        assert(matrix_element_get(m3, 2, 0) == 2);
196
        assert(matrix_element_get(m3, 3, 0) == 1);
197
        assert(matrix_element_get(m3, 4, 0) == 1);
198
        assert(matrix_element_get(m3, 5, 0) == 1);
199
        assert(matrix_element_get(m3, 0, 1) == 0);
200
        assert(matrix_element_get(m3, 1, 1) == 2);
201
        assert(matrix_element_get(m3, 2, 1) == 0);
202
        assert(matrix_element_get(m3, 3, 1) == 1);
203
        assert(matrix_element_get(m3, 4, 1) == 1);
204
        assert(matrix_element_get(m3, 5, 1) == 2);
205
        assert(matrix_element_get(m3, 0, 2) == 2);
206
        assert(matrix_element_get(m3, 1, 2) == 0);
207
        assert(matrix_element_get(m3, 2, 2) == 3);
208
        assert(matrix_element_get(m3, 3, 2) == 1);
209
        assert(matrix_element_get(m3, 4, 2) == 1);
210
        assert(matrix_element_get(m3, 5, 2) == 1);
211
        assert(matrix_element_get(m3, 0, 3) == 1);
212
        assert(matrix_element_get(m3, 1, 3) == 1);
213
        assert(matrix_element_get(m3, 2, 3) == 1);
214
        assert(matrix_element_get(m3, 3, 3) == 2);
215
        assert(matrix_element_get(m3, 4, 3) == 1);
216
        assert(matrix_element_get(m3, 5, 3) == 1);
217
        assert(matrix_element_get(m3, 0, 4) == 1);
218
        assert(matrix_element_get(m3, 1, 4) == 1);
219
        assert(matrix_element_get(m3, 2, 4) == 1);
220
        assert(matrix_element_get(m3, 3, 4) == 1);
221
        assert(matrix_element_get(m3, 4, 4) == 2);
222
        assert(matrix_element_get(m3, 5, 4) == 1);
223
        assert(matrix_element_get(m3, 0, 5) == 1);
224
        assert(matrix_element_get(m3, 1, 5) == 2);
225
        assert(matrix_element_get(m3, 2, 5) == 1);
226
        assert(matrix_element_get(m3, 3, 5) == 1);
227
        assert(matrix_element_get(m3, 4, 5) == 1);
228
        assert(matrix_element_get(m3, 5, 5) == 4);
229

    
230
        matrix_destroy(&m2);
231
        matrix_destroy(&m1);
232
        matrix_destroy(&m3);
233
        fprintf(stderr,"%s successfully passed!\n",__func__);
234
}
235

    
236
void matrix_diag_test()
237
{
238
        struct matrix * m, *v;
239
        matsize i, j;
240

    
241
        m = matrix_diag(NULL);
242
        assert(m == NULL);
243

    
244
        v = matrix_ones(6, 1);
245
        for(i = 0; i < 6; i++)
246
                matrix_element_set(v, i, 0, i);
247

    
248
        m = matrix_diag(v);
249
        assert(m != NULL);
250
        for(i = 0; i < 6; i++)
251
                for(j = 0; j < 6; j++)
252
                {
253
                        if (i == j)
254
                                assert(matrix_element_get(m, i, j) == i);
255
                        else
256
                                assert(matrix_element_get(m, i, j) == 0);
257
                }
258
        matrix_destroy(&v);
259
        matrix_destroy(&m);
260

    
261
        v = matrix_ones(1, 6);
262
        for(i = 0; i < 6; i++)
263
                matrix_element_set(v, 0, i, i);
264

    
265
        m = matrix_diag(v);
266
        assert(m != NULL);
267
        for(i = 0; i < 6; i++)
268
                for(j = 0; j < 6; j++)
269
                {
270
                        if (i == j)
271
                                assert(matrix_element_get(m, i, j) == i);
272
                        else
273
                                assert(matrix_element_get(m, i, j) == 0);
274
                }
275
        matrix_destroy(&v);
276
        matrix_destroy(&m);
277
        fprintf(stderr,"%s successfully passed!\n",__func__);
278
}
279

    
280
void matrix_invdiag_test()
281
{
282
        struct matrix * m, *v;
283
        matsize i, j;
284

    
285
        m = matrix_invdiag(NULL);
286
        assert(m == NULL);
287

    
288
        v = matrix_ones(6, 1);
289
        for(i = 0; i < 6; i++)
290
                matrix_element_set(v, i, 0, pow(10, i));
291

    
292
        m = matrix_invdiag(v);
293
        assert(m != NULL);
294
        for(i = 0; i < 6; i++)
295
                for(j = 0; j < 6; j++)
296
                {
297
                        if (i == j)
298
                                assert(matrix_element_get(m, i, j) == (1/pow(10, i)));
299
                        else
300
                                assert(matrix_element_get(m, i, j) == 0);
301
                }
302
        matrix_destroy(&v);
303
        matrix_destroy(&m);
304

    
305
        v = matrix_ones(1, 6);
306
        for(i = 0; i < 6; i++)
307
                matrix_element_set(v, 0, i, pow(10, i));
308

    
309
        m = matrix_invdiag(v);
310
        assert(m != NULL);
311
        for(i = 0; i < 6; i++)
312
                for(j = 0; j < 6; j++)
313
                {
314
                        if (i == j)
315
                                assert(matrix_element_get(m, i, j) == (1/pow(10, i)));
316
                        else
317
                                assert(matrix_element_get(m, i, j) == 0);
318
                }
319
        matrix_destroy(&v);
320
        matrix_destroy(&m);
321

    
322
        fprintf(stderr,"%s successfully passed!\n",__func__);
323
}
324

    
325
void matrix_stochastify_test()
326
{
327
        int res;
328
        struct matrix * A;
329

    
330
        res = matrix_stochastify(NULL);
331
        assert(res < 0);
332

    
333
        write_edgefile();
334
        A = matrix_from_edgefile("net.edges");
335
        res = matrix_stochastify(A);
336
        assert(res == 0);
337
        assert(matrix_element_get(A, 0, 1) == 0.5);
338
        assert(matrix_element_get(A, 1, 1) == 0);
339
        assert(matrix_element_get(A, 2, 1) == 0.5);
340
        assert(matrix_element_get(A, 3, 1) == 0);
341
        assert(matrix_element_get(A, 4, 1) == 0);
342
        assert(matrix_element_get(A, 5, 1) == 0);
343
        assert(matrix_element_get(A, 0, 3) == 0);
344
        assert(matrix_element_get(A, 1, 3) == 0);
345
        assert(matrix_element_get(A, 2, 3) == 0.5);
346
        assert(matrix_element_get(A, 3, 3) == 0);
347
        assert(matrix_element_get(A, 4, 3) == 0);
348
        assert(matrix_element_get(A, 5, 3) == 0.5);
349
        assert(matrix_element_get(A, 0, 4) == 0.5);
350
        assert(matrix_element_get(A, 1, 4) == 0);
351
        assert(matrix_element_get(A, 2, 4) == 0);
352
        assert(matrix_element_get(A, 3, 4) == 0);
353
        assert(matrix_element_get(A, 4, 4) == 0);
354
        assert(matrix_element_get(A, 5, 4) == 0.5);
355
        assert(matrix_element_get(A, 0, 5) == 0.25);
356
        assert(matrix_element_get(A, 1, 5) == 0);
357
        assert(matrix_element_get(A, 2, 5) == 0.25);
358
        assert(matrix_element_get(A, 3, 5) == 0.25);
359
        assert(matrix_element_get(A, 4, 5) == 0.25);
360
        assert(matrix_element_get(A, 5, 5) == 0);
361

    
362
        matrix_destroy(&A);
363
        fprintf(stderr,"%s successfully passed!\n",__func__);
364
}
365

    
366
void matrix_eigencentrality_test()
367
{
368
        struct matrix * A, *x;
369
        matvalue v;
370

    
371
        x = matrix_eigencentrality(NULL, NULL);
372
        assert(x == NULL);
373
        
374
        x = matrix_eigencentrality(NULL, &v);
375
        assert(x == NULL);
376
        
377
        write_edgefile();
378
        A = matrix_from_edgefile("net.edges");
379

    
380
        // real 0.1875 0.125 0.1875 0.125 0.125 0.25
381
        // octave 0.18614 0.13045 0.18614 0.12870 0.12870 0.23986
382

    
383
        matrix_stochastify(A);
384
        x = matrix_eigencentrality(A, &v);
385
        assert(matrix_element_get(x, 0, 0) < 0.188);
386
        assert(matrix_element_get(x, 0, 0) > 0.186);
387
        assert(matrix_element_get(x, 1, 0) < 0.131);
388
        assert(matrix_element_get(x, 1, 0) > 0.124);
389
        assert(matrix_element_get(x, 2, 0) < 0.188);
390
        assert(matrix_element_get(x, 2, 0) > 0.186);
391
        assert(matrix_element_get(x, 3, 0) < 0.129);
392
        assert(matrix_element_get(x, 3, 0) > 0.124);
393
        assert(matrix_element_get(x, 4, 0) < 0.129);
394
        assert(matrix_element_get(x, 4, 0) > 0.124);
395
        assert(matrix_element_get(x, 5, 0) < 0.251);
396
        assert(matrix_element_get(x, 5, 0) > 0.23);
397
        assert(v > 0.9999);
398
        assert(v < 1.0001);
399

    
400
        matrix_destroy(&A);
401
        matrix_destroy(&x);
402
        fprintf(stderr,"%s successfully passed!\n",__func__);
403
}
404

    
405
int main(char ** argc,int argv)
406
{
407
        matrix_from_edgefile_test();
408
        matrix_ones_test();
409
        matrix_multiply_test();
410
        matrix_diag_test();
411
        matrix_invdiag_test();
412
        matrix_stochastify_test();
413
        matrix_eigencentrality_test();
414
        matrix_shrink_test();
415
        return 0;
416
}