File: igraph_sparsemat2.c

package info (click to toggle)
igraph 0.7.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 19,180 kB
  • ctags: 16,601
  • sloc: ansic: 188,037; sh: 26,731; cpp: 18,275; yacc: 1,164; makefile: 959; lex: 484; xml: 378
file content (259 lines) | stat: -rw-r--r-- 7,393 bytes parent folder | download | duplicates (3)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2009-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 <cs/cs.h>
#include <igraph.h>
#include <igraph_sparsemat.h>
#include <igraph_blas_internal.h>
#include <igraph_arpack_internal.h>

int igraph_matrix_dgemv(const igraph_matrix_t *m,
                        const igraph_vector_t *v,
                        igraph_vector_t *res,
                        igraph_real_t alpha,
                        igraph_real_t beta,
                        igraph_bool_t transpose_m) {
  
  int nrow=igraph_matrix_nrow(m);
  int ncol=igraph_matrix_ncol(m);
  long int vlen=igraph_vector_size(v);
  int one=1;
  char t = transpose_m ? 't' : 'n';
  long int input_len  = transpose_m ? nrow : ncol;
  long int output_len = transpose_m ? ncol : nrow;
  
  if (vlen != input_len) {
    IGRAPH_ERROR("Matrix and vector sizes are incompatible", IGRAPH_EINVAL);
  }
  
  if (beta != 0 && igraph_vector_size(res) != output_len) {
    IGRAPH_ERROR("Non-zero beta and bad `res' vector size, possible mistake",
                 IGRAPH_EINVAL);
  }
  
  IGRAPH_CHECK(igraph_vector_resize(res, output_len));
  
  igraphdgemv_(&t, &nrow, &ncol, &alpha, &MATRIX(*m,0,0), 
               &nrow, VECTOR(*v), &one, &beta, VECTOR(*res), &one);
  
  return 0;
}

int igraph_matrix_vector_prod(const igraph_matrix_t *m,
                              const igraph_vector_t *v,
                              igraph_vector_t *res) {
  return igraph_matrix_dgemv(m, v, res, 1.0, 0.0, /*transpose=*/ 0);
}

int my_dgemv(const igraph_matrix_t *m,
                       const igraph_vector_t *v,
                       igraph_vector_t *res,
                       igraph_real_t alpha,
                       igraph_real_t beta,
                       igraph_bool_t transpose_m) {
  
  int nrow=igraph_matrix_nrow(m);
  int ncol=igraph_matrix_ncol(m);
  long int vlen=igraph_vector_size(v);
  int one=1;
  char t = transpose_m ? 't' : 'n';
  long int input_len  = transpose_m ? nrow : ncol;
  long int output_len = transpose_m ? ncol : nrow;
  
  if (vlen != input_len) {
    IGRAPH_ERROR("Matrix and vector sizes are incompatible", IGRAPH_EINVAL);
  }
  
  if (beta != 0 && igraph_vector_size(res) != output_len) {
    IGRAPH_ERROR("Non-zero beta and bad `res' vector size, possible mistake",
                IGRAPH_EINVAL);
  }
  
  IGRAPH_CHECK(igraph_vector_resize(res, output_len));
  
  igraphdgemv_(&t, &nrow, &ncol, &alpha, &MATRIX(*m,0,0), 
              &nrow, VECTOR(*v), &one, &beta, VECTOR(*res), &one);
  
  return 0;
}

int my_gaxpy(const igraph_matrix_t *m,
	     const igraph_vector_t *v,
	     igraph_vector_t *res) {
  return my_dgemv(m, v, res, 1.0, 0.0, /*transpose=*/ 0);
}

int my_dgemm(const igraph_matrix_t *m1,
	     const igraph_matrix_t *m2,
	     igraph_matrix_t *res) {

  long int m1_r=igraph_matrix_nrow(m1);
  long int m1_c=igraph_matrix_ncol(m1);
  long int m2_r=igraph_matrix_nrow(m2);
  long int m2_c=igraph_matrix_ncol(m2);
  long int i, j, k;
  
  if (m1_c != m2_r) { 
    IGRAPH_ERROR("Cannot multiply matrices, invalid dimensions", IGRAPH_EINVAL);
  }
  
  IGRAPH_CHECK(igraph_matrix_resize(res, m1_r, m2_c));
  igraph_matrix_null(res);

  for (i=0; i<m1_r; i++) {
    for (j=0; j<m2_c; j++) {
      for (k=0; k<m1_c /* which is also m2_r*/; k++) {
	MATRIX(*res, i, j) += MATRIX(*m1, i, k) * MATRIX(*m2, k, j);
      }
    }
  }

  return 0;
}

igraph_bool_t check_same(const igraph_sparsemat_t *A, 
			 const igraph_matrix_t *M) {
  
  long int nrow=igraph_sparsemat_nrow(A);
  long int ncol=igraph_sparsemat_ncol(A);
  long int j, p, nzero=0;
  
  if (nrow != igraph_matrix_nrow(M) ||
      ncol != igraph_matrix_ncol(M)) { 
    return 0; 
  }
  
  for (j=0; j<A->cs->n; j++) {
    for (p=A->cs->p[j]; p < A->cs->p[j+1]; p++) {
      long int to=A->cs->i[p];
      igraph_real_t value=A->cs->x[p];
      if (value != MATRIX(*M, to, j)) { return 0; }
      nzero += 1;
    }
  }

  for (j=0; j<nrow; j++) {
    for (p=0; p<ncol; p++) {
      if (MATRIX(*M, j, p) != 0) { nzero -= 1; }
    }
  }

  return nzero == 0;
}

int main() {
  
  igraph_sparsemat_t A, B, C, D;
  igraph_vector_t v, w, x, y;
  igraph_matrix_t M, N, O;
  long int i;

  srand(1);

  /* Matrix-vector product */
#define NROW 10
#define NCOL 5
#define EDGES NROW*NCOL/3
  igraph_matrix_init(&M, NROW, NCOL);
  igraph_sparsemat_init(&A, NROW, NCOL, EDGES);
  for (i=0; i<EDGES; i++) {
    long int r=RNG_INTEGER(0, NROW-1);
    long int c=RNG_INTEGER(0, NCOL-1);
    igraph_real_t value=RNG_INTEGER(1,5);
    MATRIX(M, r, c) = MATRIX(M, r, c) + value;
    igraph_sparsemat_entry(&A, r, c, value);
  }
  igraph_sparsemat_compress(&A, &B);
  igraph_sparsemat_destroy(&A);
  
  igraph_vector_init(&v, NCOL);
  igraph_vector_init(&w, NCOL);
  for (i=0; i<NCOL; i++) {
    VECTOR(v)[i] = VECTOR(w)[i] = RNG_INTEGER(1,5);
  }
  
  igraph_vector_init(&x, NROW);
  igraph_vector_init(&y, NROW);
  my_gaxpy(&M, &v, &x);
  igraph_vector_null(&y);
  igraph_sparsemat_gaxpy(&B, &w, &y);
  
  if (!igraph_vector_all_e(&x, &y)) { return 1; }

  igraph_vector_destroy(&x);
  igraph_vector_destroy(&y);
  igraph_vector_destroy(&v);
  igraph_vector_destroy(&w);
  igraph_sparsemat_destroy(&B);
  igraph_matrix_destroy(&M);

#undef NROW
#undef NCOL
#undef EDGES

  /* Matrix-matrix product */
#define NROW_A 10
#define NCOL_A 7
#define EDGES_A NROW_A*NCOL_A/3
#define NROW_B 7
#define NCOL_B 9
#define EDGES_B NROW_B*NCOL_B/3
  igraph_matrix_init(&M, NROW_A, NCOL_A);
  igraph_sparsemat_init(&A, NROW_A, NCOL_A, EDGES_A);
  for (i=0; i<EDGES_A; i++) {
    long int r=RNG_INTEGER(0, NROW_A-1);
    long int c=RNG_INTEGER(0, NCOL_A-1);
    igraph_real_t value=RNG_INTEGER(1,5);
    MATRIX(M, r, c) = MATRIX(M, r, c) + value;
    igraph_sparsemat_entry(&A, r, c, value);
  }
  igraph_sparsemat_compress(&A, &C);
  igraph_sparsemat_destroy(&A);

  igraph_matrix_init(&N, NROW_B, NCOL_B);
  igraph_sparsemat_init(&B, NROW_B, NCOL_B, EDGES_B);
  for (i=0; i<EDGES_B; i++) {
    long int r=RNG_INTEGER(0, NROW_B-1);
    long int c=RNG_INTEGER(0, NCOL_B-1);
    igraph_real_t value=RNG_INTEGER(1,5);
    MATRIX(N, r, c) = MATRIX(N, r, c) + value;
    igraph_sparsemat_entry(&B, r, c, value);
  }
  igraph_sparsemat_compress(&B, &D);
  igraph_sparsemat_destroy(&B);

  igraph_matrix_init(&O, 0, 0);
  my_dgemm(&M, &N, &O);
  igraph_sparsemat_multiply(&C, &D, &A);

  if (! check_same(&A, &O)) { return 2; }
  
  igraph_sparsemat_destroy(&C);
  igraph_sparsemat_destroy(&D);
  igraph_sparsemat_destroy(&A);
  igraph_matrix_destroy(&M);
  igraph_matrix_destroy(&N);
  igraph_matrix_destroy(&O);
  
  return 0;
}