Graphviz 12.0.1~dev.20240716.0800
Loading...
Searching...
No Matches
general.c
Go to the documentation of this file.
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
11#include <cgraph/alloc.h>
12#include <math.h>
13#include <stdbool.h>
14#include <stddef.h>
15#include <sparse/general.h>
16#include <errno.h>
17
18#ifdef DEBUG
19double _statistics[10];
20#endif
21
22double drand(void){
23 return rand()/(double) RAND_MAX;
24}
25
26int irand(int n){
27 /* 0, 1, ..., n-1 */
28 assert(n > 1);
29 /*return (int) MIN(floor(drand()*n),n-1);*/
30 return rand()%n;
31}
32
34 int i, j, pp, len;
35 if (n <= 0) return NULL;
36 int *p = gv_calloc(n, sizeof(int));
37 for (i = 0; i < n; i++) p[i] = i;
38
39 len = n;
40 while (len > 1){
41 j = irand(len);
42 pp = p[len-1];
43 p[len-1] = p[j];
44 p[j] = pp;
45 len--;
46 }
47 return p;
48}
49
50
51double* vector_subtract_to(int n, double *x, double *y){
52 /* y = x-y */
53 int i;
54 for (i = 0; i < n; i++) y[i] = x[i] - y[i];
55 return y;
56}
57double vector_product(int n, double *x, double *y){
58 double res = 0;
59 int i;
60 for (i = 0; i < n; i++) res += x[i]*y[i];
61 return res;
62}
63
64double* vector_saxpy(int n, double *x, double *y, double beta){
65 /* y = x+beta*y */
66 int i;
67 for (i = 0; i < n; i++) y[i] = x[i] + beta*y[i];
68 return y;
69}
70
71double* vector_saxpy2(int n, double *x, double *y, double beta){
72 /* x = x+beta*y */
73 int i;
74 for (i = 0; i < n; i++) x[i] = x[i] + beta*y[i];
75 return x;
76}
77
78void vector_float_take(int n, float *v, int m, int *p, float **u){
79 /* take m elements v[p[i]]],i=1,...,m and oput in u */
80 int i;
81
82 if (!*u) *u = gv_calloc(m, sizeof(float));
83
84 for (i = 0; i < m; i++) {
85 assert(p[i] < n && p[i] >= 0);
86 (*u)[i] = v[p[i]];
87 }
88
89}
90
91static int comp_ascend(const void *s1, const void *s2){
92 const double *ss1 = s1;
93 const double *ss2 = s2;
94
95 if (ss1[0] > ss2[0]){
96 return 1;
97 } else if (ss1[0] < ss2[0]){
98 return -1;
99 }
100 return 0;
101}
102
103static int comp_ascend_int(const void *s1, const void *s2){
104 const int *ss1 = s1;
105 const int *ss2 = s2;
106
107 if (ss1[0] > ss2[0]){
108 return 1;
109 } else if (ss1[0] < ss2[0]){
110 return -1;
111 }
112 return 0;
113}
114
115void vector_ordering(int n, double *v, int **p){
116 /* give the position of the smallest, second smallest etc in vector v.
117 results in p. If *p == NULL, p is assigned.
118 */
119
120 int i;
121
122 if (!*p) *p = gv_calloc(n, sizeof(int));
123 double *u = gv_calloc(2 * n, sizeof(double));
124
125 for (i = 0; i < n; i++) {
126 u[2*i+1] = i;
127 u[2*i] = v[i];
128 }
129
130 qsort(u, n, sizeof(double)*2, comp_ascend);
131
132 for (i = 0; i < n; i++) (*p)[i] = (int) u[2*i+1];
133 free(u);
134}
135
136void vector_sort_int(int n, int *v){
137 qsort(v, n, sizeof(int), comp_ascend_int);
138}
139
140double distance_cropped(double *x, int dim, int i, int j){
141 double dist = distance(x, dim, i, j);
142 return fmax(dist, MINDIST);
143}
144
145double distance(double *x, int dim, int i, int j){
146 int k;
147 double dist = 0.;
148 for (k = 0; k < dim; k++) dist += (x[i*dim+k] - x[j*dim + k])*(x[i*dim+k] - x[j*dim + k]);
149 dist = sqrt(dist);
150 return dist;
151}
152
153double point_distance(double *p1, double *p2, int dim){
154 int i;
155 double dist;
156 dist = 0;
157 for (i = 0; i < dim; i++) dist += (p1[i] - p2[i])*(p1[i] - p2[i]);
158 return sqrt(dist);
159}
160
161char *strip_dir(char *s){
162 bool first = true;
163 if (!s) return s;
164 for (size_t i = strlen(s); ; i--) {
165 if (first && s[i] == '.') {/* get rid of .mtx */
166 s[i] = '\0';
167 first = false;
168 }
169 if (s[i] == '/') return &s[i+1];
170 if (i == 0) {
171 break;
172 }
173 }
174 return s;
175}
Memory allocation wrappers that exit on failure.
static void * gv_calloc(size_t nmemb, size_t size)
Definition alloc.h:26
#define MINDIST
Definition circular.c:16
static double dist(int dim, double *x, double *y)
int * random_permutation(int n)
Definition general.c:33
double * vector_saxpy(int n, double *x, double *y, double beta)
Definition general.c:64
double drand(void)
Definition general.c:22
double * vector_saxpy2(int n, double *x, double *y, double beta)
Definition general.c:71
char * strip_dir(char *s)
Definition general.c:161
void vector_ordering(int n, double *v, int **p)
Definition general.c:115
double distance(double *x, int dim, int i, int j)
Definition general.c:145
double vector_product(int n, double *x, double *y)
Definition general.c:57
static int comp_ascend_int(const void *s1, const void *s2)
Definition general.c:103
void vector_float_take(int n, float *v, int m, int *p, float **u)
Definition general.c:78
void vector_sort_int(int n, int *v)
Definition general.c:136
int irand(int n)
Definition general.c:26
double * vector_subtract_to(int n, double *x, double *y)
Definition general.c:51
double distance_cropped(double *x, int dim, int i, int j)
Definition general.c:140
double point_distance(double *p1, double *p2, int dim)
Definition general.c:153
static int comp_ascend(const void *s1, const void *s2)
Definition general.c:91
static double len(glCompPoint p)
Definition glutils.c:150
void free(void *)
node NULL
Definition grammar.y:149
NEATOPROCS_API void s1(graph_t *, node_t *)
Definition stuff.c:671
Definition grammar.c:93