general.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include <math.h>
  11. #include <stdbool.h>
  12. #include <stddef.h>
  13. #include <sparse/general.h>
  14. #include <errno.h>
  15. #include <util/alloc.h>
  16. #ifdef DEBUG
  17. double _statistics[10];
  18. #endif
  19. double drand(void){
  20. return rand()/(double) RAND_MAX;
  21. }
  22. int irand(int n){
  23. /* 0, 1, ..., n-1 */
  24. assert(n > 1);
  25. /*return (int) MIN(floor(drand()*n),n-1);*/
  26. return rand()%n;
  27. }
  28. int *random_permutation(int n){
  29. int i, j, pp, len;
  30. if (n <= 0) return NULL;
  31. int *p = gv_calloc(n, sizeof(int));
  32. for (i = 0; i < n; i++) p[i] = i;
  33. len = n;
  34. while (len > 1){
  35. j = irand(len);
  36. pp = p[len-1];
  37. p[len-1] = p[j];
  38. p[j] = pp;
  39. len--;
  40. }
  41. return p;
  42. }
  43. double* vector_subtract_to(int n, double *x, double *y){
  44. /* y = x-y */
  45. int i;
  46. for (i = 0; i < n; i++) y[i] = x[i] - y[i];
  47. return y;
  48. }
  49. double vector_product(int n, double *x, double *y){
  50. double res = 0;
  51. int i;
  52. for (i = 0; i < n; i++) res += x[i]*y[i];
  53. return res;
  54. }
  55. double* vector_saxpy(int n, double *x, double *y, double beta){
  56. /* y = x+beta*y */
  57. int i;
  58. for (i = 0; i < n; i++) y[i] = x[i] + beta*y[i];
  59. return y;
  60. }
  61. double* vector_saxpy2(int n, double *x, double *y, double beta){
  62. /* x = x+beta*y */
  63. int i;
  64. for (i = 0; i < n; i++) x[i] = x[i] + beta*y[i];
  65. return x;
  66. }
  67. void vector_float_take(int n, float *v, int m, int *p, float **u){
  68. /* take m elements v[p[i]]],i=1,...,m and oput in u */
  69. int i;
  70. if (!*u) *u = gv_calloc(m, sizeof(float));
  71. for (i = 0; i < m; i++) {
  72. assert(p[i] < n && p[i] >= 0);
  73. (*u)[i] = v[p[i]];
  74. }
  75. }
  76. static int comp_ascend(const void *s1, const void *s2){
  77. const double *ss1 = s1;
  78. const double *ss2 = s2;
  79. if (ss1[0] > ss2[0]){
  80. return 1;
  81. } else if (ss1[0] < ss2[0]){
  82. return -1;
  83. }
  84. return 0;
  85. }
  86. static int comp_ascend_int(const void *s1, const void *s2){
  87. const int *ss1 = s1;
  88. const int *ss2 = s2;
  89. if (ss1[0] > ss2[0]){
  90. return 1;
  91. } else if (ss1[0] < ss2[0]){
  92. return -1;
  93. }
  94. return 0;
  95. }
  96. void vector_ordering(int n, double *v, int **p){
  97. /* give the position of the smallest, second smallest etc in vector v.
  98. results in p. If *p == NULL, p is assigned.
  99. */
  100. int i;
  101. if (!*p) *p = gv_calloc(n, sizeof(int));
  102. double *u = gv_calloc(2 * n, sizeof(double));
  103. for (i = 0; i < n; i++) {
  104. u[2*i+1] = i;
  105. u[2*i] = v[i];
  106. }
  107. qsort(u, n, sizeof(double)*2, comp_ascend);
  108. for (i = 0; i < n; i++) (*p)[i] = (int) u[2*i+1];
  109. free(u);
  110. }
  111. void vector_sort_int(int n, int *v){
  112. qsort(v, n, sizeof(int), comp_ascend_int);
  113. }
  114. double distance_cropped(double *x, int dim, int i, int j){
  115. double dist = distance(x, dim, i, j);
  116. return fmax(dist, MINDIST);
  117. }
  118. double distance(double *x, int dim, int i, int j){
  119. int k;
  120. double dist = 0.;
  121. for (k = 0; k < dim; k++) dist += (x[i*dim+k] - x[j*dim + k])*(x[i*dim+k] - x[j*dim + k]);
  122. dist = sqrt(dist);
  123. return dist;
  124. }
  125. double point_distance(double *p1, double *p2, int dim){
  126. int i;
  127. double dist;
  128. dist = 0;
  129. for (i = 0; i < dim; i++) dist += (p1[i] - p2[i])*(p1[i] - p2[i]);
  130. return sqrt(dist);
  131. }
  132. char *strip_dir(char *s){
  133. bool first = true;
  134. if (!s) return s;
  135. for (size_t i = strlen(s); ; i--) {
  136. if (first && s[i] == '.') {/* get rid of .mtx */
  137. s[i] = '\0';
  138. first = false;
  139. }
  140. if (s[i] == '/') return &s[i+1];
  141. if (i == 0) {
  142. break;
  143. }
  144. }
  145. return s;
  146. }