File: scg2.c

package info (click to toggle)
igraph 0.8.5%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,284 kB
  • sloc: ansic: 97,287; cpp: 22,541; yacc: 1,150; makefile: 546; lex: 478; xml: 450; pascal: 82; sh: 9
file content (153 lines) | stat: -rw-r--r-- 5,503 bytes parent folder | download
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
/* -*- mode: C -*-  */
/*
   IGraph library.
   Copyright (C) 2011-2012  Gabor Csardi <csardi.gabor@gmail.com>
   334 Harvard st, Cambridge MA, 02139 USA

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA

*/

#include <igraph.h>

#include "../tests/test_utilities.inc"

int main() {

    igraph_t g;
    igraph_vector_t ev;
    igraph_t scg_graph;
    igraph_matrix_t scg_matrix;
    igraph_sparsemat_t scg_sparsemat;
    igraph_matrix_t L, R;
    igraph_sparsemat_t Lsparse, Rsparse;
    igraph_vector_t p;
    igraph_vector_t groups;
    igraph_vector_complex_t eval;
    igraph_matrix_complex_t evec;

    igraph_tree(&g, 10, /* children= */ 3, IGRAPH_TREE_UNDIRECTED);

    igraph_vector_init(&ev, 1);
    igraph_matrix_init(&L, 0, 0);
    igraph_matrix_init(&R, 0, 0);
    igraph_matrix_init(&scg_matrix, 0, 0);
    igraph_vector_init(&p, 0);
    igraph_vector_init(&groups, 0);
    igraph_vector_complex_init(&eval, 0);
    igraph_matrix_complex_init(&evec, 0, 0);

#define CALLSTO() do {                           \
        igraph_vector_resize(&p, 0);                     \
        igraph_vector_resize(&groups, 0);                    \
        igraph_vector_complex_resize(&eval, 0);              \
        igraph_matrix_complex_resize(&evec, 0, 0);               \
        igraph_scg_stochastic(&g, /*matrix=*/ 0, /*sparsemat=*/ 0, &ev,  \
                              /* intervals= */ 2, /* intervals_vector= */ 0, \
                              /* algorithm= */ IGRAPH_SCG_EXACT,         \
                              IGRAPH_SCG_NORM_ROW, &eval, &evec,         \
                              &groups, &p, /* use_arpack= */ 0,      \
                              /* maxiter= */ 0, &scg_graph, &scg_matrix,     \
                              &scg_sparsemat, &L, &R,            \
                              &Lsparse, &Rsparse);               \
    } while (0)

#define FIXSMALL(eps) do { \
    long int i, j, ncol, nrow; \
    ncol = igraph_vector_complex_size(&eval); \
    for (i = 0; i < ncol; i++) { \
        if (fabs((double)IGRAPH_REAL(VECTOR(eval)[i])) < eps) { \
            IGRAPH_REAL(VECTOR(eval)[i]) = 0; \
        } \
        if (fabs((double)IGRAPH_IMAG(VECTOR(eval)[i])) < eps) { \
            IGRAPH_IMAG(VECTOR(eval)[i]) = 0; \
        } \
    } \
    nrow = igraph_matrix_complex_nrow(&evec); \
    ncol = igraph_matrix_complex_ncol(&evec); \
    for (i = 0; i < nrow; i++) { \
        for (j = 0; j < ncol; j++) { \
            if (fabs((double)IGRAPH_REAL(MATRIX(evec, i, j))) < eps) { \
                IGRAPH_REAL(MATRIX(evec, i, j)) = 0; \
            } \
            if (fabs((double)IGRAPH_IMAG(MATRIX(evec, i, j))) < eps) { \
                IGRAPH_IMAG(MATRIX(evec, i, j)) = 0; \
            } \
        } \
    } \
    } while (0)

#define PRINTRES()                      \
    do {                              \
        printf("--------------------------------\n");       \
        igraph_vector_print(&groups);               \
        printf("---\n");                        \
        igraph_vector_complex_print(&eval);             \
        print_matrix_complex_first_row_positive(&evec);             \
        printf("---\n");                        \
        igraph_write_graph_edgelist(&scg_graph, stdout);        \
        printf("---\n");                        \
        igraph_sparsemat_print(&scg_sparsemat, stdout);     \
        printf("---\n");                        \
        igraph_sparsemat_print(&Lsparse, stdout);           \
        printf("---\n");                        \
        igraph_sparsemat_print(&Rsparse, stdout);           \
        printf("---\n");                        \
    } while (0)

    VECTOR(ev)[0] = 1;
    CALLSTO();
    FIXSMALL(1e-4);
    PRINTRES();
    igraph_destroy(&scg_graph);
    igraph_sparsemat_destroy(&scg_sparsemat);
    igraph_sparsemat_destroy(&Lsparse);
    igraph_sparsemat_destroy(&Rsparse);

    VECTOR(ev)[0] = 3;
    CALLSTO();
    FIXSMALL(1e-4);
    PRINTRES();
    igraph_destroy(&scg_graph);
    igraph_sparsemat_destroy(&scg_sparsemat);
    igraph_sparsemat_destroy(&Lsparse);
    igraph_sparsemat_destroy(&Rsparse);

    igraph_vector_resize(&ev, 2);
    VECTOR(ev)[0] = 1;
    VECTOR(ev)[1] = 3;
    CALLSTO();
    FIXSMALL(1e-4);
    PRINTRES();
    igraph_destroy(&scg_graph);
    igraph_sparsemat_destroy(&scg_sparsemat);
    igraph_sparsemat_destroy(&Lsparse);
    igraph_sparsemat_destroy(&Rsparse);

    igraph_matrix_complex_destroy(&evec);
    igraph_vector_complex_destroy(&eval);
    igraph_vector_destroy(&groups);
    igraph_vector_destroy(&p);
    igraph_matrix_destroy(&scg_matrix);
    igraph_matrix_destroy(&L);
    igraph_matrix_destroy(&R);
    igraph_vector_destroy(&ev);
    igraph_destroy(&g);

    /* -------------------------------------------------------------------- */

    return 0;
}