1 /* -*- mode: C -*-  */
2 /*
3    IGraph library.
4    Copyright (C) 2006-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 
print_result(igraph_spmatrix_t * m,FILE * f)28 void print_result(igraph_spmatrix_t *m, FILE *f) {
29     print_spmatrix(m);
30     fprintf(f, "==================================================\n");
31 }
32 
main()33 int main() {
34     igraph_spmatrix_t m, m1;
35     igraph_spmatrix_iter_t mit;
36     igraph_real_t arr[12];
37     igraph_vector_t v;
38     long int i, j;
39     int order[] = { 1, 5, 8, 4, 0, 9, 6, 10, 11, 2, 3, 7 };
40 
41     /* igraph_spmatrix_init, igraph_spmatrix_destroy */
42     igraph_spmatrix_init(&m, 10, 10);
43     igraph_spmatrix_destroy(&m);
44 
45     igraph_spmatrix_init(&m, 0, 0);
46     igraph_spmatrix_destroy(&m);
47 
48     /* igraph_spmatrix_ncol, igraph_spmatrix_nrow */
49     igraph_spmatrix_init(&m, 10, 5);
50     if (igraph_spmatrix_nrow(&m) != 10) {
51         return 1;
52     }
53     if (igraph_spmatrix_ncol(&m) != 5) {
54         return 2;
55     }
56 
57     /* igraph_spmatrix_size, igraph_spmatrix_resize */
58     igraph_spmatrix_resize(&m, 6, 5);
59     if (igraph_spmatrix_size(&m) != 30) {
60         return 3;
61     }
62     if (igraph_spmatrix_nrow(&m) != 6) {
63         return 4;
64     }
65     if (igraph_spmatrix_ncol(&m) != 5) {
66         return 5;
67     }
68     igraph_spmatrix_resize(&m, 2, 4);
69     if (igraph_spmatrix_nrow(&m) != 2) {
70         return 6;
71     }
72     if (igraph_spmatrix_ncol(&m) != 4) {
73         return 7;
74     }
75     igraph_spmatrix_destroy(&m);
76 
77     /* igraph_spmatrix_get, igraph_spmatrix_set, igraph_spmatrix_null */
78     igraph_spmatrix_init(&m, 3, 4);
79     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
80         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
81             igraph_spmatrix_set(&m, i, j, (i + j) % 3);
82         }
83     }
84     print_result(&m, stdout);
85     igraph_spmatrix_null(&m);
86     print_result(&m, stdout);
87     /* now fill it in shuffled order */
88     for (i = 0; i < 12; i++) {
89         igraph_spmatrix_set(&m, order[i] / 4, order[i] % 4, (order[i] / 4 + order[i] % 4) % 3);
90     }
91     print_result(&m, stdout);
92     /* now decrease all elements by two in shuffled order */
93     for (i = 0; i < 12; i++) {
94         igraph_spmatrix_add_e(&m, order[i] / 4, order[i] % 4, -2);
95     }
96     print_result(&m, stdout);
97     /* now increase all elements by one in shuffled order */
98     for (i = 0; i < 12; i++) {
99         igraph_spmatrix_add_e(&m, order[i] / 4, order[i] % 4, 1);
100     }
101     print_result(&m, stdout);
102 
103     igraph_spmatrix_destroy(&m);
104 
105     /* igraph_matrix_add_cols, igraph_matrix_add_rows */
106     igraph_spmatrix_init(&m, 4, 3);
107     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
108         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
109             igraph_spmatrix_set(&m, i, j, (i + 1) * (j + 1));
110         }
111     }
112     igraph_spmatrix_add_cols(&m, 2);
113     igraph_spmatrix_add_rows(&m, 2);
114     if (igraph_spmatrix_ncol(&m) != 5) {
115         return 8;
116     }
117     if (igraph_spmatrix_nrow(&m) != 6) {
118         return 9;
119     }
120     print_result(&m, stdout);
121     igraph_spmatrix_destroy(&m);
122 
123     /* igraph_spmatrix_count_nonzero */
124     igraph_spmatrix_init(&m, 5, 3);
125     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
126         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
127             igraph_spmatrix_set(&m, i, j, i * j);
128         }
129     }
130     print_result(&m, stdout);
131     if (igraph_spmatrix_count_nonzero(&m) != 8) {
132         return 10;
133     }
134     igraph_spmatrix_destroy(&m);
135 
136     /* igraph_spmatrix_copy */
137     igraph_spmatrix_init(&m, 3, 4);
138     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
139         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
140             igraph_spmatrix_set(&m, i, j, i * j);
141         }
142     }
143     igraph_spmatrix_copy(&m1, &m);
144     print_result(&m1, stdout);
145     igraph_spmatrix_destroy(&m);
146     igraph_spmatrix_destroy(&m1);
147 
148     /* igraph_spmatrix_copy_to */
149     igraph_spmatrix_init(&m, 3, 4);
150     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
151         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
152             igraph_spmatrix_set(&m, i, j, i * j);
153         }
154     }
155     igraph_spmatrix_copy_to(&m, arr);
156     for (i = 0; i < 12; i++) {
157         printf(" %ld", (long)arr[i]);
158     }
159     printf("\n=========================\n");
160 
161     /* igraph_spmatrix_max */
162     arr[0] = igraph_spmatrix_max(&m, arr + 1, arr + 2);
163     for (i = 0; i < 3; i++) {
164         printf(" %ld", (long)arr[i]);
165     }
166     printf("\n=========================\n");
167 
168     igraph_spmatrix_destroy(&m);
169 
170     /* igraph_spmatrix_colsums */
171     igraph_spmatrix_init(&m, 3, 5);
172     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
173         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
174             igraph_spmatrix_set(&m, i, j, i + j - 4);
175         }
176     }
177     igraph_vector_init(&v, 0);
178     igraph_spmatrix_colsums(&m, &v);
179     print_vector_format(&v, stdout, "%g");
180     igraph_vector_destroy(&v);
181     igraph_spmatrix_destroy(&m);
182 
183     /* igraph_spmatrix_iter_t */
184     igraph_spmatrix_init(&m, 5, 5);
185     for (i = 0; i < igraph_spmatrix_nrow(&m); i++) {
186         for (j = 0; j < igraph_spmatrix_ncol(&m); j++) {
187             if (labs(i - j) == 1) {
188                 igraph_spmatrix_set(&m, i, j, (i + 1) * (j + 1));
189             }
190         }
191     }
192     igraph_spmatrix_iter_create(&mit, &m);
193     while (!igraph_spmatrix_iter_end(&mit)) {
194         printf("%ld %ld %ld\n", mit.ri, mit.ci, (long int)mit.value);
195         igraph_spmatrix_iter_next(&mit);
196     }
197     igraph_spmatrix_iter_destroy(&mit);
198     igraph_spmatrix_destroy(&m);
199     printf("=========================\n");
200 
201     VERIFY_FINALLY_STACK();
202 
203     return 0;
204 }
205