Graphviz 15.1.1~dev.20260628.0906
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 v2.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.html
7 *
8 * Contributors: Details at https://graphviz.org
9 *************************************************************************/
10
11#include "config.h"
12
13#include <math.h>
14#include <stdbool.h>
15#include <stddef.h>
16#include <sparse/general.h>
17#include <errno.h>
18#include <util/alloc.h>
19
20#ifdef DEBUG
21double _statistics[10];
22#endif
23
24double drand(void){
25 return rand()/(double) RAND_MAX;
26}
27
28double *vector_subtract_to(size_t n, double *x, double *y) {
29 /* y = x-y */
30 for (size_t i = 0; i < n; i++) y[i] = x[i] - y[i];
31 return y;
32}
33double vector_product(int n, double *x, double *y){
34 double res = 0;
35 int i;
36 for (i = 0; i < n; i++) res += x[i]*y[i];
37 return res;
38}
39
40double *vector_saxpy(size_t n, double *x, double *y, double beta) {
41 /* y = x+beta*y */
42 for (size_t i = 0; i < n; i++) y[i] = x[i] + beta * y[i];
43 return y;
44}
45
46double *vector_saxpy2(size_t n, double *x, double *y, double beta) {
47 /* x = x+beta*y */
48 for (size_t i = 0; i < n; i++) x[i] += beta * y[i];
49 return x;
50}
51
52void vector_float_take(size_t n, float *v, size_t m, int *p, float **u) {
53 /* take m elements v[p[i]]],i=1,...,m and oput in u */
54 if (!*u) *u = gv_calloc(m, sizeof(float));
55
56 for (size_t i = 0; i < m; i++) {
57 assert(p[i] >= 0 && (size_t)p[i] < n);
58 (void)n;
59 (*u)[i] = v[p[i]];
60 }
61
62}
63
64static int comp_ascend(const void *s1, const void *s2){
65 const double *ss1 = s1;
66 const double *ss2 = s2;
67
68 if (ss1[0] > ss2[0]){
69 return 1;
70 } else if (ss1[0] < ss2[0]){
71 return -1;
72 }
73 return 0;
74}
75
76static int comp_ascend_int(const void *s1, const void *s2){
77 const int *ss1 = s1;
78 const int *ss2 = s2;
79
80 if (ss1[0] > ss2[0]){
81 return 1;
82 } else if (ss1[0] < ss2[0]){
83 return -1;
84 }
85 return 0;
86}
87
88void vector_ordering(size_t n, double *v, int **p) {
89 /* give the position of the smallest, second smallest etc in vector v.
90 results in p. If *p == NULL, p is assigned.
91 */
92
93 if (!*p) *p = gv_calloc(n, sizeof(int));
94 double *u = gv_calloc(2 * n, sizeof(double));
95
96 for (size_t i = 0; i < n; i++) {
97 u[2 * i + 1] = (double)i;
98 u[2 * i] = v[i];
99 }
100
101 qsort(u, n, sizeof(double)*2, comp_ascend);
102
103 for (size_t i = 0; i < n; i++) (*p)[i] = (int)u[2 * i + 1];
104 free(u);
105}
106
107void vector_sort_int(int n, int *v){
108 qsort(v, n, sizeof(int), comp_ascend_int);
109}
110
111double distance_cropped(double *x, int dim, int i, int j){
112 double dist = distance(x, dim, i, j);
113 return fmax(dist, MINDIST);
114}
115
116double distance(double *x, int dim, int i, int j){
117 int k;
118 double dist = 0.;
119 for (k = 0; k < dim; k++) dist += (x[i*dim+k] - x[j*dim + k])*(x[i*dim+k] - x[j*dim + k]);
120 dist = sqrt(dist);
121 return dist;
122}
123
124double point_distance(double *p1, double *p2, int dim){
125 int i;
126 double dist;
127 dist = 0;
128 for (i = 0; i < dim; i++) dist += (p1[i] - p2[i])*(p1[i] - p2[i]);
129 return sqrt(dist);
130}
131
132char *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}
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:18
static double dist(int dim, double *x, double *y)
void vector_ordering(size_t n, double *v, int **p)
Definition general.c:88
double * vector_saxpy(size_t n, double *x, double *y, double beta)
y = x+beta*y
Definition general.c:40
double drand(void)
Definition general.c:24
char * strip_dir(char *s)
Definition general.c:132
double distance(double *x, int dim, int i, int j)
Definition general.c:116
double vector_product(int n, double *x, double *y)
Definition general.c:33
double * vector_subtract_to(size_t n, double *x, double *y)
y = x-y
Definition general.c:28
void vector_float_take(size_t n, float *v, size_t m, int *p, float **u)
Definition general.c:52
double * vector_saxpy2(size_t n, double *x, double *y, double beta)
x = x+beta*y
Definition general.c:46
static int comp_ascend_int(const void *s1, const void *s2)
Definition general.c:76
void vector_sort_int(int n, int *v)
Definition general.c:107
double distance_cropped(double *x, int dim, int i, int j)
Definition general.c:111
double point_distance(double *p1, double *p2, int dim)
Definition general.c:124
static int comp_ascend(const void *s1, const void *s2)
Definition general.c:64
void free(void *)
static const int dim
NEATOPROCS_API void s1(graph_t *, node_t *)
Definition stuff.c:651
Definition grammar.c:90