File: general.c

package info (click to toggle)
graphviz 2.42.2-7%2Bdeb12u1
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 95,764 kB
  • sloc: ansic: 1,051,543; cpp: 9,107; tcl: 4,897; makefile: 4,862; sh: 4,506; yacc: 4,190; xml: 2,970; cs: 1,921; objc: 1,157; lex: 625; java: 560; perl: 445; python: 255; awk: 241; javascript: 146; ruby: 64; php: 59; sed: 1
file content (367 lines) | stat: -rw-r--r-- 7,676 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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* $Id$Revision: */
/* vim:set shiftwidth=4 ts=8: */

/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: See CVS logs. Details at http://www.graphviz.org/
 *************************************************************************/

#include "general.h"
#include <errno.h>

#ifdef DEBUG
double _statistics[10];
#endif

real vector_median(int n, real *x){
  /* find the median value in a list of real */
  int *p = NULL;
  real res;
  vector_ordering(n, x, &p, TRUE);

  if ((n/2)*2 == n){
    res = 0.5*(x[p[n/2-1]] + x[p[n/2]]);
  } else {
    res = x[p[n/2]];
  }
  FREE(p);
  return res;
}
real vector_percentile(int n, real *x, real y){
  /* find the value such that y% of element of vector x is <= that value.
   y: a value between 0 and 1.
  */
  int *p = NULL, i;
  real res;
  vector_ordering(n, x, &p, TRUE);
  

  y = MIN(y, 1);
  y = MAX(0, y);

  i = n*y;
  res = x[p[i]];
  FREE(p); return res;
}

real drand(){
  return rand()/(real) RAND_MAX;
}

int irand(int n){
  /* 0, 1, ..., n-1 */
  assert(n > 1);
  /*return (int) MIN(floor(drand()*n),n-1);*/
  return rand()%n;
}

int *random_permutation(int n){
  int *p;
  int i, j, pp, len;
  if (n <= 0) return NULL;
  p = MALLOC(sizeof(int)*n);
  for (i = 0; i < n; i++) p[i] = i;

  len = n;
  while (len > 1){
    j = irand(len);
    pp = p[len-1];
    p[len-1] = p[j];
    p[j] = pp;
    len--;
  }
  return p;
}


real* vector_subtract_from(int n, real *x, real *y){
  /* y = x-y */
  int i;
  for (i = 0; i < n; i++) y[i] = y[i] - x[i];
  return y;
}
real* vector_subtract_to(int n, real *x, real *y){
  /* y = x-y */
  int i;
  for (i = 0; i < n; i++) y[i] = x[i] - y[i];
  return y;
}
real* vector_add_to(int n, real *x, real *y){
  /* y = x-y */
  int i;
  for (i = 0; i < n; i++) y[i] = x[i] + y[i];
  return y;
}

real vector_product(int n, real *x, real *y){
  real res = 0;
  int i;
  for (i = 0; i < n; i++) res += x[i]*y[i];
  return res;
}

real* vector_saxpy(int n, real *x, real *y, real beta){
  /* y = x+beta*y */
  int i;
  for (i = 0; i < n; i++) y[i] = x[i] + beta*y[i];
  return y;
}

real* vector_saxpy2(int n, real *x, real *y, real beta){
  /* x = x+beta*y */
  int i;
  for (i = 0; i < n; i++) x[i] = x[i] + beta*y[i];
  return x;
}

void vector_print(char *s, int n, real *x){
  int i;
    printf("%s{",s); 
    for (i = 0; i < n; i++) {
      if (i > 0) printf(",");
      printf("%f",x[i]); 
    }
    printf("}\n");
}

void vector_take(int n, real *v, int m, int *p, real **u){
  /* take m elements v[p[i]]],i=1,...,m and oput in u */
  int i;

  if (!*u) *u = MALLOC(sizeof(real)*m);

  for (i = 0; i < m; i++) {
    assert(p[i] < n && p[i] >= 0);
    (*u)[i] = v[p[i]];
  }
  
}

void vector_float_take(int n, float *v, int m, int *p, float **u){
  /* take m elements v[p[i]]],i=1,...,m and oput in u */
  int i;

  if (!*u) *u = MALLOC(sizeof(float)*m);

  for (i = 0; i < m; i++) {
    assert(p[i] < n && p[i] >= 0);
    (*u)[i] = v[p[i]];
  }
  
}

int comp_ascend(const void *s1, const void *s2){
  real *ss1, *ss2;
  ss1 = (real*) s1;
  ss2 = (real*) s2;

  if ((ss1)[0] > (ss2)[0]){
    return 1;
  } else if ((ss1)[0] < (ss2)[0]){
    return -1;
  }
  return 0;
}

int comp_descend(const void *s1, const void *s2){
  real *ss1, *ss2;
  ss1 = (real*) s1;
  ss2 = (real*) s2;

  if ((ss1)[0] > (ss2)[0]){
    return -1;
  } else if ((ss1)[0] < (ss2)[0]){
    return 1;
  }
  return 0;
}
int comp_descend_int(const void *s1, const void *s2){
  int *ss1, *ss2;
  ss1 = (int*) s1;
  ss2 = (int*) s2;

  if ((ss1)[0] > (ss2)[0]){
    return -1;
  } else if ((ss1)[0] < (ss2)[0]){
    return 1;
  }
  return 0;
}

int comp_ascend_int(const void *s1, const void *s2){
  int *ss1, *ss2;
  ss1 = (int*) s1;
  ss2 = (int*) s2;

  if ((ss1)[0] > (ss2)[0]){
    return 1;
  } else if ((ss1)[0] < (ss2)[0]){
    return -1;
  }
  return 0;
}


void vector_ordering(int n, real *v, int **p, int ascending){
  /* give the position of the lagest, second largest etc in vector v if ascending = FALSE

     or

     give the position of the smallest, second smallest etc  in vector v if ascending = TRUE.
     results in p. If *p == NULL, p is assigned.

     ascending: TRUE if v[p] is from small to large.
  */

  real *u;
  int i;

  if (!*p) *p = MALLOC(sizeof(int)*n);
  u = MALLOC(sizeof(real)*2*n);

  for (i = 0; i < n; i++) {
    u[2*i+1] = i;
    u[2*i] = v[i];
  }

  if (ascending){
    qsort(u, n, sizeof(real)*2, comp_ascend);
  } else {
    qsort(u, n, sizeof(real)*2, comp_descend);
  }

  for (i = 0; i < n; i++) (*p)[i] = (int) u[2*i+1];
  FREE(u);

}

void vector_sort_real(int n, real *v, int ascending){
  if (ascending){
    qsort(v, n, sizeof(real), comp_ascend);
  } else {
    qsort(v, n, sizeof(real), comp_descend);
  }
}
void vector_sort_int(int n, int *v, int ascending){
  if (ascending){
    qsort(v, n, sizeof(int), comp_ascend_int);
  } else {
    qsort(v, n, sizeof(int), comp_descend_int);
  }
}

int excute_system_command3(char *s1, char *s2, char *s3){
  char c[1000];

  strcpy(c, s1);
  strcat(c, s2);
  strcat(c, s3);
  return system(c);
}

int excute_system_command(char *s1, char *s2){
  char c[1000];

  strcpy(c, s1);
  strcat(c, s2);
  return system(c);
}

real distance_cropped(real *x, int dim, int i, int j){
  int k;
  real dist = 0.;
  for (k = 0; k < dim; k++) dist += (x[i*dim+k] - x[j*dim + k])*(x[i*dim+k] - x[j*dim + k]);
  dist = sqrt(dist);
  return MAX(dist, MINDIST);
}

real distance(real *x, int dim, int i, int j){
  int k;
  real dist = 0.;
  for (k = 0; k < dim; k++) dist += (x[i*dim+k] - x[j*dim + k])*(x[i*dim+k] - x[j*dim + k]);
  dist = sqrt(dist);
  return dist;
}

real point_distance(real *p1, real *p2, int dim){
  int i;
  real dist;
  dist = 0;
  for (i = 0; i < dim; i++) dist += (p1[i] - p2[i])*(p1[i] - p2[i]);
  return sqrt(dist);
}

char *strip_dir(char *s){
  int i, first = TRUE;
  if (!s) return s;
  for (i = strlen(s); i >= 0; i--) {
    if (first && s[i] == '.') {/* get rid of .mtx */
      s[i] = '\0';
      first = FALSE;
    }
    if (s[i] == '/') return (char*) &(s[i+1]);
  }
  return s;
}

void scale_to_box(real xmin, real ymin, real xmax, real ymax, int n, int dim, real *x){
  real min[3], max[3], min0[3], ratio = 1;
  int i, k;

  for (i = 0; i < dim; i++) {
    min[i] = x[i];
    max[i] = x[i];
  }

  for (i = 0; i < n; i++){
    for (k = 0; k < dim; k++) {
      min[k] = MIN(x[i*dim+k], min[k]);
      max[k] = MAX(x[i*dim+k], max[k]);
    }
  }

  if (max[0] - min[0] != 0) {
    ratio = (xmax-xmin)/(max[0] - min[0]);
  }
  if (max[1] - min[1] != 0) {
    ratio = MIN(ratio, (ymax-ymin)/(max[1] - min[1]));
  }
  
  min0[0] = xmin;
  min0[1] = ymin;
  min0[2] = 0;
  for (i = 0; i < n; i++){
    for (k = 0; k < dim; k++) {
      x[i*dim+k] = min0[k] + (x[i*dim+k] - min[k])*ratio;
    }
  }
  
  
}

int digitsQ(char *s){
  while (*s && *s - '0' >= 0 && *s - '0' <= 9) {
    s++;
  }
  if (*s) return 0;
  return 1;
}
int validQ_int_string(char *to_convert, int *v){
  /* check to see if this is a string is integer */
  char *p = to_convert;
  uint64_t val;
  errno = 0;
  val = strtoul(to_convert, &p, 10);
  if (errno != 0 ||// conversion failed (EINVAL, ERANGE)
      to_convert == p || // conversion failed (no characters consumed)
      *p != 0
      ) return 0;
  if (val > INT_MAX || val < INT_MIN) return 0;
  *v = (int) val;
  return 1;
}