File: igraph_psumtree.c

package info (click to toggle)
igraph 0.7.1-2.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,252 kB
  • sloc: ansic: 188,056; sh: 26,731; cpp: 18,275; yacc: 1,164; makefile: 959; lex: 484; xml: 405
file content (197 lines) | stat: -rw-r--r-- 5,193 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

/* -*- mode: C -*-  */
/* 
   IGraph library.
   Copyright (C) 2006-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 <igraph_psumtree.h>
#include <stdlib.h>

int print_vector(igraph_vector_t *v) {
  long int i, n=igraph_vector_size(v);
  for (i=0; i<n; i++) {
    printf("%li ", (long int) VECTOR(*v)[i]);
  }
  printf("\n");
  return 0;
}

int main() {
  igraph_psumtree_t tree;
  igraph_vector_t vec;
  long int i;
  igraph_real_t sum;

  /* Uniform random numbers */
  igraph_vector_init(&vec, 16);
  igraph_psumtree_init(&tree, 16);
  sum=igraph_psumtree_sum(&tree);
  if (sum != 0) {
    printf("Sum: %f instead of 0.\n", sum);
    return 1;
  }

  for (i=0; i<16; i++) {
    igraph_psumtree_update(&tree, i, 1);
  }
  if ((sum=igraph_psumtree_sum(&tree)) != 16) {
    printf("Sum: %f instead of 16.\n", sum);
    return 2;
  }
  
  for (i=0; i<16000; i++) {
    igraph_real_t r=((double)rand())/RAND_MAX * sum;
    long int idx;
    igraph_psumtree_search(&tree, &idx, r);
    VECTOR(vec)[idx] += 1;
  }  
  for (i=0; i<16; i++) {
    if (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200) {
      return 3;
    }
  }

  /* Nonuniform, even indices have twice as much chance */
  for (i=0; i<16; i+=2) {
    igraph_psumtree_update(&tree, i, 2);
  }
  if ((sum=igraph_psumtree_sum(&tree)) != 24) {
    printf("Sum: %f instead of 24.\n", sum);
    return 4;
  }
  
  igraph_vector_null(&vec);
  for (i=0; i<24000; i++) {
    igraph_real_t r=((double)rand())/RAND_MAX * sum;
    long int idx;
    igraph_psumtree_search(&tree, &idx, r);
    VECTOR(vec)[idx] += 1;
  }
  for (i=0; i<16; i++) {
    if (i%2 == 0 && (VECTOR(vec)[i] < 1800 || VECTOR(vec)[i] > 2200)) {
      return 5;
    }
    if (i%2 != 0 && (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200)) {
      return 6;
    }
  }
  
  /* Test zero probabilities */
  igraph_psumtree_update(&tree, 0, 0);
  igraph_psumtree_update(&tree, 5, 0);
  igraph_psumtree_update(&tree, 15, 0);
  sum=igraph_psumtree_sum(&tree);
  
  igraph_vector_null(&vec);
  for (i=0; i<20000; i++) {
    igraph_real_t r=((double)rand())/RAND_MAX * sum;
    long int idx;
    igraph_psumtree_search(&tree, &idx, r);
    VECTOR(vec)[idx] += 1;
  }
  if (VECTOR(vec)[0] != 0 || VECTOR(vec)[5] != 0 || VECTOR(vec)[15] != 0) {
    return 7;
  }

  igraph_vector_destroy(&vec);
  igraph_psumtree_destroy(&tree);

  /****************************************************/
  /* Non power-of-two vector size                     */
  /****************************************************/

  igraph_vector_init(&vec, 9);
  igraph_psumtree_init(&tree, 9);

  for (i=0; i<9; i++) {
    igraph_psumtree_update(&tree, i, 1);
  }
  sum=igraph_psumtree_sum(&tree);
  
  for (i=0; i<9000; i++) {
    igraph_real_t r=((double)rand())/RAND_MAX * sum;
    long int idx;
    igraph_psumtree_search(&tree, &idx, r);
    VECTOR(vec)[idx] += 1;
  }  
  for (i=0; i<9; i++) {
    if (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200) {
      return 8;
    }
  }

  /* Nonuniform, even indices have twice as much chance */
  for (i=0; i<9; i+=2) {
    igraph_psumtree_update(&tree, i, 2);
  }
  sum=igraph_psumtree_sum(&tree);
  
  igraph_vector_null(&vec);
  for (i=0; i<14000; i++) {
    igraph_real_t r=((double)rand())/RAND_MAX * sum;
    long int idx;
    igraph_psumtree_search(&tree, &idx, r);
    VECTOR(vec)[idx] += 1;
  }
  for (i=0; i<9; i++) {
    if (i%2 == 0 && (VECTOR(vec)[i] < 1800 || VECTOR(vec)[i] > 2200)) {
      return 9;
    }
    if (i%2 != 0 && (VECTOR(vec)[i] < 800 || VECTOR(vec)[i] > 1200)) {
      return 10;
    }
  }

  /* Test query */
  for (i=0; i<igraph_psumtree_size(&tree); i++) {
    if (i%2==0 && igraph_psumtree_get(&tree, i) != 2) {
      return 11;
    }
    if (i%2!=0 && igraph_psumtree_get(&tree, i) != 1) {
      return 12;
    }
  }
  
  /* Test zero probabilities */
  igraph_psumtree_update(&tree, 0, 0);
  igraph_psumtree_update(&tree, 5, 0);
  igraph_psumtree_update(&tree, 8, 0);
  sum=igraph_psumtree_sum(&tree);
  
  igraph_vector_null(&vec);
  for (i=0; i<9000; i++) {
    igraph_real_t r=((double)rand())/RAND_MAX * sum;
    long int idx;
    igraph_psumtree_search(&tree, &idx, r);
    VECTOR(vec)[idx] += 1;
  }
  if (VECTOR(vec)[0] != 0 || VECTOR(vec)[5] != 0 || VECTOR(vec)[8] != 0) {
    return 11;
  }

  igraph_vector_destroy(&vec);
  igraph_psumtree_destroy(&tree);

  if (!IGRAPH_FINALLY_STACK_EMPTY) return 13;
  
  return 0;
}