1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2011-2012  Gabor Csardi <csardi.gabor@gmail.com>
5    334 Harvard st, Cambridge MA, 02139 USA
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301 USA
21 
22 */
23 
24 #include <igraph.h>
25 
26 #include "test_utilities.inc"
27 
main()28 int main() {
29 
30     igraph_t g;
31     igraph_vector_t ev;
32     igraph_t scg_graph;
33     igraph_matrix_t scg_matrix;
34     igraph_sparsemat_t scg_sparsemat;
35     igraph_matrix_t L, R;
36     igraph_sparsemat_t Lsparse, Rsparse;
37     igraph_vector_t p;
38     igraph_vector_t groups;
39     igraph_vector_complex_t eval;
40     igraph_matrix_complex_t evec;
41 
42     igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);
43 
44     igraph_vector_init(&ev, 1);
45     igraph_matrix_init(&L, 0, 0);
46     igraph_matrix_init(&R, 0, 0);
47     igraph_matrix_init(&scg_matrix, 0, 0);
48     igraph_vector_init(&p, 0);
49     igraph_vector_init(&groups, 0);
50     igraph_vector_complex_init(&eval, 0);
51     igraph_matrix_complex_init(&evec, 0, 0);
52 
53 #define CALLSTO() do {                           \
54         igraph_vector_resize(&p, 0);                     \
55         igraph_vector_resize(&groups, 0);                    \
56         igraph_vector_complex_resize(&eval, 0);              \
57         igraph_matrix_complex_resize(&evec, 0, 0);               \
58         igraph_scg_stochastic(&g, /*matrix=*/ 0, /*sparsemat=*/ 0, &ev,  \
59                               /* intervals= */ 2, /* intervals_vector= */ 0, \
60                               /* algorithm= */ IGRAPH_SCG_EXACT,         \
61                               IGRAPH_SCG_NORM_ROW, &eval, &evec,         \
62                               &groups, &p, /* use_arpack= */ 0,      \
63                               /* maxiter= */ 0, &scg_graph, &scg_matrix,     \
64                               &scg_sparsemat, &L, &R,            \
65                               &Lsparse, &Rsparse);               \
66     } while (0)
67 
68 #define FIXSMALL(eps) do { \
69     long int i, j, ncol, nrow; \
70     ncol = igraph_vector_complex_size(&eval); \
71     for (i = 0; i < ncol; i++) { \
72         if (fabs((double)IGRAPH_REAL(VECTOR(eval)[i])) < eps) { \
73             IGRAPH_REAL(VECTOR(eval)[i]) = 0; \
74         } \
75         if (fabs((double)IGRAPH_IMAG(VECTOR(eval)[i])) < eps) { \
76             IGRAPH_IMAG(VECTOR(eval)[i]) = 0; \
77         } \
78     } \
79     nrow = igraph_matrix_complex_nrow(&evec); \
80     ncol = igraph_matrix_complex_ncol(&evec); \
81     for (i = 0; i < nrow; i++) { \
82         for (j = 0; j < ncol; j++) { \
83             if (fabs((double)IGRAPH_REAL(MATRIX(evec, i, j))) < eps) { \
84                 IGRAPH_REAL(MATRIX(evec, i, j)) = 0; \
85             } \
86             if (fabs((double)IGRAPH_IMAG(MATRIX(evec, i, j))) < eps) { \
87                 IGRAPH_IMAG(MATRIX(evec, i, j)) = 0; \
88             } \
89         } \
90     } \
91     } while (0)
92 
93 #define PRINTRES()                      \
94     do {                              \
95         printf("--------------------------------\n");       \
96         igraph_vector_print(&groups);               \
97         printf("---\n");                        \
98         igraph_vector_complex_print(&eval);             \
99         print_matrix_complex_first_row_positive(&evec);             \
100         printf("---\n");                        \
101         igraph_write_graph_edgelist(&scg_graph, stdout);        \
102         printf("---\n");                        \
103         igraph_sparsemat_print(&scg_sparsemat, stdout);     \
104         printf("---\n");                        \
105         igraph_sparsemat_print(&Lsparse, stdout);           \
106         printf("---\n");                        \
107         igraph_sparsemat_print(&Rsparse, stdout);           \
108         printf("---\n");                        \
109     } while (0)
110 
111     VECTOR(ev)[0] = 1;
112     CALLSTO();
113     FIXSMALL(1e-4);
114     PRINTRES();
115     igraph_destroy(&scg_graph);
116     igraph_sparsemat_destroy(&scg_sparsemat);
117     igraph_sparsemat_destroy(&Lsparse);
118     igraph_sparsemat_destroy(&Rsparse);
119 
120     VECTOR(ev)[0] = 3;
121     CALLSTO();
122     FIXSMALL(1e-4);
123     PRINTRES();
124     igraph_destroy(&scg_graph);
125     igraph_sparsemat_destroy(&scg_sparsemat);
126     igraph_sparsemat_destroy(&Lsparse);
127     igraph_sparsemat_destroy(&Rsparse);
128 
129     igraph_vector_resize(&ev, 2);
130     VECTOR(ev)[0] = 1;
131     VECTOR(ev)[1] = 3;
132     CALLSTO();
133     FIXSMALL(1e-4);
134     PRINTRES();
135     igraph_destroy(&scg_graph);
136     igraph_sparsemat_destroy(&scg_sparsemat);
137     igraph_sparsemat_destroy(&Lsparse);
138     igraph_sparsemat_destroy(&Rsparse);
139 
140     igraph_matrix_complex_destroy(&evec);
141     igraph_vector_complex_destroy(&eval);
142     igraph_vector_destroy(&groups);
143     igraph_vector_destroy(&p);
144     igraph_matrix_destroy(&scg_matrix);
145     igraph_matrix_destroy(&L);
146     igraph_matrix_destroy(&R);
147     igraph_vector_destroy(&ev);
148     igraph_destroy(&g);
149 
150     /* -------------------------------------------------------------------- */
151 
152     VERIFY_FINALLY_STACK();
153 
154     return 0;
155 }
156