Debian Codegraph @

lib/sparse/general.c

raw
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 "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
21 double _statistics[10];
22 #endif
23
24 double drand(void){
25 return rand()/(double) RAND_MAX;
26 }
27
28 double* vector_subtract_to(int n, double *x, double *y){
29 /* y = x-y */
30 int i;
31 for (i = 0; i < n; i++) y[i] = x[i] - y[i];
32 return y;
33 }
34 double vector_product(int n, double *x, double *y){
35 double res = 0;
36 int i;
37 for (i = 0; i < n; i++) res += x[i]*y[i];
38 return res;
39 }
40
41 double* vector_saxpy(int n, double *x, double *y, double beta){
42 /* y = x+beta*y */
43 int i;
44 for (i = 0; i < n; i++) y[i] = x[i] + beta*y[i];
45 return y;
46 }
47
48 double* vector_saxpy2(int n, double *x, double *y, double beta){
49 /* x = x+beta*y */
50 int i;
51 for (i = 0; i < n; i++) x[i] = x[i] + beta*y[i];
52 return x;
53 }
54
55 void vector_float_take(int n, float *v, int m, int *p, float **u){
56 /* take m elements v[p[i]]],i=1,...,m and oput in u */
57 int i;
58
59 if (!*u) *u = gv_calloc(m, sizeof(float));
60
61 for (i = 0; i < m; i++) {
62 assert(p[i] < n && p[i] >= 0);
63 (void)n;
64 (*u)[i] = v[p[i]];
65 }
66
67 }
68
69 static int comp_ascend(const void *s1, const void *s2){
70 const double *ss1 = s1;
71 const double *ss2 = s2;
72
73 if (ss1[0] > ss2[0]){
74 return 1;
75 } else if (ss1[0] < ss2[0]){
76 return -1;
77 }
78 return 0;
79 }
80
81 static int comp_ascend_int(const void *s1, const void *s2){
82 const int *ss1 = s1;
83 const int *ss2 = s2;
84
85 if (ss1[0] > ss2[0]){
86 return 1;
87 } else if (ss1[0] < ss2[0]){
88 return -1;
89 }
90 return 0;
91 }
92
93 void vector_ordering(int n, double *v, int **p){
94 /* give the position of the smallest, second smallest etc in vector v.
95 results in p. If *p == NULL, p is assigned.
96 */
97
98 int i;
99
100 if (!*p) *p = gv_calloc(n, sizeof(int));
101 double *u = gv_calloc(2 * n, sizeof(double));
102
103 for (i = 0; i < n; i++) {
104 u[2*i+1] = i;
105 u[2*i] = v[i];
106 }
107
108 qsort(u, n, sizeof(double)*2, comp_ascend);
109
110 for (i = 0; i < n; i++) (*p)[i] = (int) u[2*i+1];
111 free(u);
112 }
113
114 void vector_sort_int(int n, int *v){
115 qsort(v, n, sizeof(int), comp_ascend_int);
116 }
117
118 double distance_cropped(double *x, int dim, int i, int j){
119 double dist = distance(x, dim, i, j);
120 return fmax(dist, MINDIST);
121 }
122
123 double distance(double *x, int dim, int i, int j){
124 int k;
125 double dist = 0.;
126 for (k = 0; k < dim; k++) dist += (x[i*dim+k] - x[j*dim + k])*(x[i*dim+k] - x[j*dim + k]);
127 dist = sqrt(dist);
128 return dist;
129 }
130
131 double point_distance(double *p1, double *p2, int dim){
132 int i;
133 double dist;
134 dist = 0;
135 for (i = 0; i < dim; i++) dist += (p1[i] - p2[i])*(p1[i] - p2[i]);
136 return sqrt(dist);
137 }
138
139 char *strip_dir(char *s){
140 bool first = true;
141 if (!s) return s;
142 for (size_t i = strlen(s); ; i--) {
143 if (first && s[i] == '.') {/* get rid of .mtx */
144 s[i] = '\0';
145 first = false;
146 }
147 if (s[i] == '/') return &s[i+1];
148 if (i == 0) {
149 break;
150 }
151 }
152 return s;
153 }