1 /* -*- mode: C -*- */
2 /*
3 IGraph library.
4 Copyright (C) 2009-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
main()26 int main() {
27
28 igraph_sparsemat_t A, B, C, D;
29 igraph_t G, H;
30 igraph_vector_t vect;
31 long int i;
32
33 /* Create, compress, destroy */
34 igraph_sparsemat_init(&A, 100, 20, 50);
35 igraph_sparsemat_compress(&A, &B);
36 igraph_sparsemat_destroy(&B);
37 igraph_sparsemat_destroy(&A);
38
39 /* Convert a ring graph to a matrix, print it, compress, print again */
40 #define VC 10
41 igraph_ring(&G, VC, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1);
42 igraph_get_sparsemat(&G, &A);
43 igraph_destroy(&G);
44
45 igraph_sparsemat_compress(&A, &B);
46 igraph_sparsemat_print(&A, stdout);
47 igraph_sparsemat_print(&B, stdout);
48
49 /* Basic query, nrow, ncol, type, is_triplet, is_cc */
50 if (igraph_sparsemat_nrow(&A) != VC ||
51 igraph_sparsemat_ncol(&A) != VC ||
52 igraph_sparsemat_nrow(&B) != VC ||
53 igraph_sparsemat_ncol(&B) != VC) {
54 return 1;
55 }
56 if (!igraph_sparsemat_is_triplet(&A)) {
57 return 2;
58 }
59 if (!igraph_sparsemat_is_cc(&B)) {
60 return 3;
61 }
62 if (igraph_sparsemat_type(&A) != IGRAPH_SPARSEMAT_TRIPLET) {
63 return 4;
64 }
65 if (igraph_sparsemat_type(&B) != IGRAPH_SPARSEMAT_CC) {
66 return 5;
67 }
68
69 igraph_sparsemat_destroy(&A);
70 igraph_sparsemat_destroy(&B);
71 #undef VC
72
73 printf("------------------------\n");
74
75 /* Create unit matrices */
76 igraph_sparsemat_eye(&A, /*n=*/ 5, /*nzmax=*/ 5, /*value=*/ 1.0,
77 /*compress=*/ 0);
78 igraph_sparsemat_eye(&B, /*n=*/ 5, /*nzmax=*/ 5, /*value=*/ 1.0,
79 /*compress=*/ 1);
80 igraph_sparsemat_print(&A, stdout);
81 igraph_sparsemat_print(&B, stdout);
82 igraph_sparsemat_destroy(&A);
83 igraph_sparsemat_destroy(&B);
84
85 printf("------------------------\n");
86
87 /* Create diagonal matrices */
88 igraph_vector_init(&vect, 5);
89 for (i = 0; i < 5; i++) {
90 VECTOR(vect)[i] = i;
91 }
92 igraph_sparsemat_diag(&A, /*nzmax=*/ 5, /*values=*/ &vect, /*compress=*/ 0);
93 igraph_sparsemat_diag(&B, /*nzmax=*/ 5, /*values=*/ &vect, /*compress=*/ 1);
94 igraph_vector_destroy(&vect);
95 igraph_sparsemat_print(&A, stdout);
96 igraph_sparsemat_print(&B, stdout);
97 igraph_sparsemat_destroy(&A);
98 igraph_sparsemat_destroy(&B);
99
100 printf("------------------------\n");
101
102 /* Transpose matrices */
103 igraph_tree(&G, 10, /*children=*/ 2, IGRAPH_TREE_OUT);
104 igraph_get_sparsemat(&G, &A);
105 igraph_destroy(&G);
106 igraph_sparsemat_compress(&A, &B);
107 igraph_sparsemat_print(&B, stdout);
108 igraph_sparsemat_transpose(&B, &C, /*values=*/ 1);
109 igraph_sparsemat_print(&C, stdout);
110 igraph_sparsemat_destroy(&A);
111 igraph_sparsemat_destroy(&B);
112 igraph_sparsemat_destroy(&C);
113
114 printf("------------------------\n");
115
116 /* Add duplicate elements */
117 igraph_sparsemat_init(&A, 10, 10, /*nzmax=*/ 20);
118 for (i = 1; i < 10; i++) {
119 igraph_sparsemat_entry(&A, 0, i, 1.0);
120 }
121 for (i = 1; i < 10; i++) {
122 igraph_sparsemat_entry(&A, 0, i, 1.0);
123 }
124 igraph_sparsemat_print(&A, stdout);
125 igraph_sparsemat_compress(&A, &B);
126 igraph_sparsemat_print(&B, stdout);
127 igraph_sparsemat_dupl(&B);
128 igraph_sparsemat_print(&B, stdout);
129 igraph_sparsemat_destroy(&A);
130 igraph_sparsemat_destroy(&B);
131
132 printf("------------------------\n");
133
134 /* Drop zero elements */
135 igraph_sparsemat_init(&A, 10, 10, /*nzmax=*/ 20);
136 igraph_sparsemat_entry(&A, 7, 3, 0.0);
137 for (i = 1; i < 10; i++) {
138 igraph_sparsemat_entry(&A, 0, i, 1.0);
139 igraph_sparsemat_entry(&A, 0, i, 0.0);
140 }
141 igraph_sparsemat_entry(&A, 0, 0, 0.0);
142 igraph_sparsemat_print(&A, stdout);
143 igraph_sparsemat_compress(&A, &B);
144 igraph_sparsemat_print(&B, stdout);
145 igraph_sparsemat_dropzeros(&B);
146 igraph_sparsemat_print(&B, stdout);
147 igraph_sparsemat_destroy(&A);
148 igraph_sparsemat_destroy(&B);
149
150 printf("------------------------\n");
151
152 /* Add two matrices */
153
154 igraph_star(&G, 10, IGRAPH_STAR_OUT, /*center=*/ 0);
155 igraph_ring(&H, 10, /*directed=*/ 0, /*mutual=*/ 0, /*circular=*/ 1);
156 igraph_get_sparsemat(&G, &A);
157 igraph_get_sparsemat(&H, &B);
158 igraph_destroy(&G);
159 igraph_destroy(&H);
160 igraph_sparsemat_compress(&A, &C);
161 igraph_sparsemat_compress(&B, &D);
162 igraph_sparsemat_destroy(&A);
163 igraph_sparsemat_destroy(&B);
164 igraph_sparsemat_add(&C, &D, /*alpha=*/ 1.0, /*beta=*/ 2.0, &A);
165 igraph_sparsemat_destroy(&C);
166 igraph_sparsemat_destroy(&D);
167 igraph_sparsemat_print(&A, stdout);
168 igraph_sparsemat_destroy(&A);
169
170 return 0;
171 }
172