From: Matthew Fernandez Date: Wed, 26 Aug 2020 00:50:39 +0000 (-0700) Subject: use fabs() instead of ABS() for taking the absolute value of reals X-Git-Tag: 2.46.0~20^2^2~100^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=8414e62738bf7f52596eb83049ecef758582c760;p=graphviz use fabs() instead of ABS() for taking the absolute value of reals The type real is #defined to double, so we can use the same replacement as in b88ebe0548dd412ea415d6cfc410d91117270277 to simplify this code. --- diff --git a/cmd/tools/mm2gv.c b/cmd/tools/mm2gv.c index 9bd0baa9d..b4c3300ef 100644 --- a/cmd/tools/mm2gv.c +++ b/cmd/tools/mm2gv.c @@ -305,7 +305,7 @@ static Agraph_t *makeDotGraph(SparseMatrix A, char *name, int dim, } } else { for (j = ia[i]; j < ia[i + 1]; j++) { - if (val) color[j] = ABS(val[j]); + if (val) color[j] = fabs(val[j]); else color[j] = 1; if (i != ja[j]) { if (first) { diff --git a/lib/edgepaint/edge_distinct_coloring.c b/lib/edgepaint/edge_distinct_coloring.c index f5b4d175d..72916b2b9 100644 --- a/lib/edgepaint/edge_distinct_coloring.c +++ b/lib/edgepaint/edge_distinct_coloring.c @@ -8,6 +8,7 @@ * Contributors: See CVS logs. Details at http://www.graphviz.org/ *************************************************************************/ #include "general.h" +#include #include "time.h" #include "SparseMatrix.h" #include "node_distinct_coloring.h" @@ -142,7 +143,7 @@ static int splines_intersect(int dim, int u1, int v1, int u2, int v2, for (i = 0; i < ns1 - 1; i++){ for (j = 0; j < ns2 - 1; j++){ cos_a = intersection_angle(&(x1[dim*i]), &(x1[dim*(i + 1)]), &(x2[dim*j]), &(x2[dim*(j+1)])); - if (!check_edges_with_same_endpoint && cos_a >= -1) cos_a = ABS(cos_a); + if (!check_edges_with_same_endpoint && cos_a >= -1) cos_a = fabs(cos_a); if (cos_a > cos_critical) { return 1; } @@ -235,7 +236,7 @@ Agraph_t* edge_distinct_coloring(char *color_scheme, char *lightness, Agraph_t* for (j = i+1; j < nz2; j++){ u2 = irn[j]; v2 = jcn[j]; cos_a = intersection_angle(&(x[dim*u1]), &(x[dim*v1]), &(x[dim*u2]), &(x[dim*v2])); - if (!check_edges_with_same_endpoint && cos_a >= -1) cos_a = ABS(cos_a); + if (!check_edges_with_same_endpoint && cos_a >= -1) cos_a = fabs(cos_a); if (cos_a > cos_critical) { B = SparseMatrix_coordinate_form_add_entries(B, 1, &i, &j, &cos_a); } diff --git a/lib/edgepaint/intersection.c b/lib/edgepaint/intersection.c index 1c946e789..4e18a4772 100644 --- a/lib/edgepaint/intersection.c +++ b/lib/edgepaint/intersection.c @@ -8,6 +8,7 @@ * Contributors: See CVS logs. Details at http://www.graphviz.org/ *************************************************************************/ #include "general.h" +#include static real cross(real *u, real *v){ return u[0]*v[1] - u[1]*v[0]; @@ -144,7 +145,7 @@ real intersection_angle(real *p1, real *p2, real *q1, real *q2){ snorm = sqrt(snorm); b = cross(r, s); line_dist_close = (line_segments_distance(p1, p2, q1, q2) <= close*MAX(rnorm, snorm)); - if (ABS(b) <= epsilon*snorm*rnorm){/* parallel */ + if (fabs(b) <= epsilon*snorm*rnorm){/* parallel */ if (line_dist_close) {/* two parallel lines that are close */ return 1; } @@ -173,7 +174,7 @@ real intersection_angle(real *p1, real *p2, real *q1, real *q2){ } /* normal case of intersect or very close */ - return ABS(res); + return fabs(res); } return -2;/* no intersection, and lines are not even close */ } diff --git a/lib/mingle/agglomerative_bundling.c b/lib/mingle/agglomerative_bundling.c index c3cdd3a77..a3bed7f97 100644 --- a/lib/mingle/agglomerative_bundling.c +++ b/lib/mingle/agglomerative_bundling.c @@ -11,6 +11,7 @@ #include "types.h" #include "globals.h" #include "general.h" +#include #include "time.h" #include "SparseMatrix.h" #include "vector.h" @@ -276,7 +277,7 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_establish(Agglomera if (Verbose > 1) fprintf(stderr,"level %d->%d, edges %d -> %d, ink %f->%f , gain = %f, or %f\n", grid->level, cgrid->level, grid->n, cgrid->n, grid->total_ink, grand_total_ink, grid->total_ink - grand_total_ink, grand_total_gain); - assert(ABS(grid->total_ink - cgrid->total_ink - grand_total_gain) <= 0.0001*grid->total_ink); + assert(fabs(grid->total_ink - cgrid->total_ink - grand_total_gain) <= 0.0001*grid->total_ink); cgrid = Agglomerative_Ink_Bundling_establish(cgrid, pick, angle_param, angle); grid->next = cgrid; @@ -524,7 +525,7 @@ static Agglomerative_Ink_Bundling Agglomerative_Ink_Bundling_aggressive_establis if (Verbose > 1) fprintf(stderr,"level %d->%d, edges %d -> %d, ink %f->%f , gain = %f, or %f\n", grid->level, cgrid->level, grid->n, cgrid->n, grid->total_ink, grand_total_ink, grid->total_ink - grand_total_ink, grand_total_gain); - assert(ABS(grid->total_ink - cgrid->total_ink - grand_total_gain) <= 0.0001*grid->total_ink); + assert(fabs(grid->total_ink - cgrid->total_ink - grand_total_gain) <= 0.0001*grid->total_ink); cgrid = Agglomerative_Ink_Bundling_aggressive_establish(cgrid, pick, angle_param, angle); grid->next = cgrid; @@ -623,7 +624,7 @@ static pedge* agglomerative_ink_bundling_internal(int dim, SparseMatrix A, pedge if (Verbose && DEBUG) fprintf(stderr,"calling ink2...\n"); ink1 = ink(edges, ia[i+1]-ia[i], pick, &ink0, &meet1, &meet2, angle_param, angle); if (Verbose && DEBUG) fprintf(stderr,"finish calling ink2...\n"); - assert(ABS(ink1 - cgrid->inks[i])<=MAX(TOL, TOL*ink1) && ink1 - ink0 <= TOL); + assert(fabs(ink1 - cgrid->inks[i])<=MAX(TOL, TOL*ink1) && ink1 - ink0 <= TOL); wgt_all = 0.; if (ia[i+1]-ia[i] > 1){ for (j = ia[i]; j < ia[i+1]; j++){ @@ -682,7 +683,7 @@ static pedge* agglomerative_ink_bundling_internal(int dim, SparseMatrix A, pedge if (Verbose && DEBUG) fprintf(stderr,"calling ink3...\n"); ink1 = ink(edges, ia[i+1]-ia[i], pick, &ink0, &meet1, &meet2, angle_param, angle); if (Verbose && DEBUG) fprintf(stderr,"done calling ink3...\n"); - assert(ABS(ink1 - cgrid->inks[i])<=MAX(TOL, TOL*ink1) && ink1 - ink0 <= TOL); + assert(fabs(ink1 - cgrid->inks[i])<=MAX(TOL, TOL*ink1) && ink1 - ink0 <= TOL); xx[i*4 + 0] = meet1.x; xx[i*4 + 1] = meet1.y; xx[i*4 + 2] = meet2.x; diff --git a/lib/mingle/edge_bundling.c b/lib/mingle/edge_bundling.c index 4cfb157c2..fb3d5c183 100644 --- a/lib/mingle/edge_bundling.c +++ b/lib/mingle/edge_bundling.c @@ -13,6 +13,7 @@ #include "types.h" #include "globals.h" #include "general.h" +#include #include "SparseMatrix.h" #include "edge_bundling.h" #include @@ -162,7 +163,7 @@ static real edge_compatibility_full(pedge e1, pedge e2){ ca = 0; for (i = 0; i < dim; i++) ca += (v1[i]-u1[i])*(v2[i]-u2[i]); - ca = ABS(ca/(len1*len2)); + ca = fabs(ca/(len1*len2)); assert(ca > -0.001); /* scale compatibility */ @@ -634,13 +635,6 @@ static pedge* force_directed_edge_bundling(SparseMatrix A, pedge* edges, int max return edges; } -static real absfun(real x){ - return ABS(x); -} - - - - static pedge* modularity_ink_bundling(int dim, int ne, SparseMatrix B, pedge* edges, real angle_param, real angle){ int *assignment = NULL, flag, nclusters; real modularity; @@ -658,7 +652,7 @@ static pedge* modularity_ink_bundling(int dim, int ne, SparseMatrix B, pedge* ed /* B may contain negative entries */ BB = SparseMatrix_copy(B); - BB = SparseMatrix_apply_fun(BB, absfun); + BB = SparseMatrix_apply_fun(BB, fabs); modularity_clustering(BB, TRUE, 0, use_value_for_clustering, &nclusters, &assignment, &modularity, &flag); SparseMatrix_delete(BB); @@ -735,7 +729,7 @@ static SparseMatrix check_compatibility(SparseMatrix A, int ne, pedge *edges, in fprintf(stderr,"compatibility=%f\n",dist); */ - if (ABS(dist) > tol){ + if (fabs(dist) > tol){ B = SparseMatrix_coordinate_form_add_entries(B, 1, &i, &jj, &dist); B = SparseMatrix_coordinate_form_add_entries(B, 1, &jj, &i, &dist); } diff --git a/lib/neatogen/overlap.c b/lib/neatogen/overlap.c index 7e7b48fc4..a6a10c4d3 100644 --- a/lib/neatogen/overlap.c +++ b/lib/neatogen/overlap.c @@ -20,6 +20,7 @@ #include "call_tri.h" #include "red_black_tree.h" #include "types.h" +#include #include "memory.h" #include "globals.h" #include @@ -43,8 +44,8 @@ static void ideal_distance_avoid_overlap(int dim, SparseMatrix A, real *x, real jj = ja[j]; if (jj == i) continue; dist = distance(x, dim, i, jj); - dx = ABS(x[i*dim] - x[jj*dim]); - dy = ABS(x[i*dim+1] - x[jj*dim+1]); + dx = fabs(x[i*dim] - x[jj*dim]); + dy = fabs(x[i*dim+1] - x[jj*dim+1]); wx = width[i*dim]+width[jj*dim]; wy = width[i*dim+1]+width[jj*dim+1]; if (dx < MACHINEACC*wx && dy < MACHINEACC*wy){ @@ -212,7 +213,7 @@ static SparseMatrix get_overlap_graph(int dim, int n, real *x, real *width, int fprintf(stderr," predecessor is node %d y = %f\n", ((scan_point *)newNode->key)->node, ((scan_point *)newNode->key)->x); #endif if (neighbor != k){ - if (ABS(0.5*(bsta+bsto) - 0.5*(bbsta+bbsto)) < 0.5*(bsto-bsta) + 0.5*(bbsto-bbsta)){/* if the distance of the centers of the interval is less than sum of width, we have overlap */ + if (fabs(0.5*(bsta+bsto) - 0.5*(bbsta+bbsto)) < 0.5*(bsto-bsta) + 0.5*(bbsto-bbsta)){/* if the distance of the centers of the interval is less than sum of width, we have overlap */ A = SparseMatrix_coordinate_form_add_entries(A, 1, &neighbor, &k, &one); #ifdef DEBUG_RBTREE fprintf(stderr,"====================================== %d %d\n",k,neighbor); diff --git a/lib/sfdpgen/sparse_solve.c b/lib/sfdpgen/sparse_solve.c index e4fe63dc0..1a81ab6ef 100644 --- a/lib/sfdpgen/sparse_solve.c +++ b/lib/sfdpgen/sparse_solve.c @@ -116,7 +116,7 @@ Operator Operator_uniform_stress_diag_precon_new(SparseMatrix A, real alpha){ for (i = 0; i < m; i++){ diag[i] = 1./(m-1); for (j = ia[i]; j < ia[i+1]; j++){ - if (i == ja[j] && ABS(a[j]) > 0) diag[i] = 1./((m-1)*alpha+a[j]); + if (i == ja[j] && fabs(a[j]) > 0) diag[i] = 1./((m-1)*alpha+a[j]); } } @@ -145,7 +145,7 @@ static Operator Operator_diag_precon_new(SparseMatrix A){ for (i = 0; i < m; i++){ diag[i] = 1.; for (j = ia[i]; j < ia[i+1]; j++){ - if (i == ja[j] && ABS(a[j]) > 0) diag[i] = 1./a[j]; + if (i == ja[j] && fabs(a[j]) > 0) diag[i] = 1./a[j]; } } diff --git a/lib/sfdpgen/spring_electrical.c b/lib/sfdpgen/spring_electrical.c index d0c4d4894..e44bc684d 100644 --- a/lib/sfdpgen/spring_electrical.c +++ b/lib/sfdpgen/spring_electrical.c @@ -339,7 +339,7 @@ static real get_angle(real *x, int dim, int i, int j){ for (k = 0; k < 2; k++){ y[k] = x[j*dim+k] - x[i*dim+k]; } - if (ABS(y[0]) <= ABS(y[1])*eps){ + if (fabs(y[0]) <= fabs(y[1])*eps){ if (y[1] > 0) return 0.5*PI; return 1.5*PI; } diff --git a/lib/sfdpgen/uniform_stress.c b/lib/sfdpgen/uniform_stress.c index 1fc19b208..20e6f9b39 100644 --- a/lib/sfdpgen/uniform_stress.c +++ b/lib/sfdpgen/uniform_stress.c @@ -12,6 +12,7 @@ *************************************************************************/ #include "general.h" +#include #include "SparseMatrix.h" #include "spring_electrical.h" #include "post_process.h" @@ -70,7 +71,7 @@ UniformStressSmoother UniformStressSmoother_new(int dim, SparseMatrix A, real *x for (j = ia[i]; j < ia[i+1]; j++){ k = ja[j]; if (k != i){ - dist = MAX(ABS(a[j]), epsilon); + dist = MAX(fabs(a[j]), epsilon); jd[nz] = jw[nz] = k; w[nz] = -1/(dist*dist); w[nz] = -1.; @@ -144,7 +145,7 @@ void uniform_stress(int dim, SparseMatrix A, real *x, int *flag){ /* make sure x is not all at the same point */ for (i = 1; i < n; i++){ for (k = 0; k < dim; k++) { - if (ABS(x[0*dim+k] - x[i*dim+k]) > MACHINEACC){ + if (fabs(x[0*dim+k] - x[i*dim+k]) > MACHINEACC){ samepoint = FALSE; i = n; break; diff --git a/lib/sparse/DotIO.c b/lib/sparse/DotIO.c index ad80d5882..68c9dde59 100644 --- a/lib/sparse/DotIO.c +++ b/lib/sparse/DotIO.c @@ -15,6 +15,7 @@ #include "general.h" #include "DotIO.h" #include "clustering.h" +#include #include "mq.h" /* #include "spring_electrical.h" */ #include "color_palette.h" @@ -566,7 +567,7 @@ makeDotGraph (SparseMatrix A, char *name, int dim, real *x, int with_color, int } } else { for (j = ia[i]; j < ia[i+1]; j++) { - color[j] = ABS(val[j]); + color[j] = fabs(val[j]); if (i != ja[j]){ if (first){ mindist = color[j]; diff --git a/lib/sparse/SparseMatrix.c b/lib/sparse/SparseMatrix.c index 2f89f7336..07565c9d0 100644 --- a/lib/sparse/SparseMatrix.c +++ b/lib/sparse/SparseMatrix.c @@ -219,7 +219,7 @@ int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only){ if (mask[jb[j]] < ia[i]) goto RETURN; } for (j = ib[i]; j < ib[i+1]; j++){ - if (ABS(b[j] - a[mask[jb[j]]]) > SYMMETRY_EPSILON) goto RETURN; + if (fabs(b[j] - a[mask[jb[j]]]) > SYMMETRY_EPSILON) goto RETURN; } } res = TRUE; @@ -237,8 +237,8 @@ int SparseMatrix_is_symmetric(SparseMatrix A, int test_pattern_symmetry_only){ if (mask[jb[j]] < ia[i]) goto RETURN; } for (j = ib[i]; j < ib[i+1]; j++){ - if (ABS(b[2*j] - a[2*mask[jb[j]]]) > SYMMETRY_EPSILON) goto RETURN; - if (ABS(b[2*j+1] - a[2*mask[jb[j]]+1]) > SYMMETRY_EPSILON) goto RETURN; + if (fabs(b[2*j] - a[2*mask[jb[j]]]) > SYMMETRY_EPSILON) goto RETURN; + if (fabs(b[2*j+1] - a[2*mask[jb[j]]+1]) > SYMMETRY_EPSILON) goto RETURN; } } res = TRUE; @@ -2285,7 +2285,7 @@ SparseMatrix SparseMatrix_normalize_by_row(SparseMatrix A){ for (i = 0; i < A->m; i++){ max = 0; for (j = A->ia[i]; j < A->ia[i+1]; j++){ - max = MAX(ABS(a[j]), max); + max = MAX(fabs(a[j]), max); } if (max != 0){ for (j = A->ia[i]; j < A->ia[i+1]; j++){ @@ -2413,7 +2413,7 @@ SparseMatrix SparseMatrix_crop(SparseMatrix A, real epsilon){ real *a = (real*) A->a; for (i = 0; i < A->m; i++){ for (j = sta; j < ia[i+1]; j++){ - if (ABS(a[j]) > epsilon){ + if (fabs(a[j]) > epsilon){ ja[nz] = ja[j]; a[nz++] = a[j]; } @@ -2445,7 +2445,7 @@ SparseMatrix SparseMatrix_crop(SparseMatrix A, real epsilon){ int *a = (int*) A->a; for (i = 0; i < A->m; i++){ for (j = sta; j < ia[i+1]; j++){ - if (ABS(a[j]) > epsilon){ + if (fabs(a[j]) > epsilon){ ja[nz] = ja[j]; a[nz++] = a[j]; } @@ -2688,14 +2688,14 @@ static int Dijkstra_internal(SparseMatrix A, int root, real *dist, int *nlist, i if (heap_id == UNVISITED){ ndata = MALLOC(sizeof(struct nodedata_struct)); - ndata->dist = ABS(a[j]) + ndata_min->dist; + ndata->dist = fabs(a[j]) + ndata_min->dist; ndata->id = jj; heap_ids[jj] = BinaryHeap_insert(h, ndata); //fprintf(stderr," set neighbor id=%d, dist=%f, hid = %d, a[%d]=%f, dist=%f\n",jj, ndata->dist, heap_ids[jj], jj, a[j], ndata->dist); } else { ndata = BinaryHeap_get_item(h, heap_id); - ndata->dist = MIN(ndata->dist, ABS(a[j]) + ndata_min->dist); + ndata->dist = MIN(ndata->dist, fabs(a[j]) + ndata_min->dist); assert(ndata->id == jj); BinaryHeap_reset(h, heap_id, ndata); //fprintf(stderr," reset neighbor id=%d, dist=%f, hid = %d, a[%d]=%f, dist=%f\n",jj, ndata->dist,heap_id, jj, a[j], ndata->dist); @@ -2760,7 +2760,7 @@ real SparseMatrix_pseudo_diameter_weighted(SparseMatrix A0, int root, int aggres } while (dist_max > dist0); *connectedQ = (!flag); - assert((dist_max - dist0)/MAX(1, MAX(ABS(dist0), ABS(dist_max))) < 1.e-10); + assert((dist_max - dist0)/MAX(1, MAX(fabs(dist0), fabs(dist_max))) < 1.e-10); *end1 = root; *end2 = list[nlist-1]; @@ -4131,7 +4131,7 @@ void SparseMatrix_page_rank(SparseMatrix A, real teleport_probablity, int weight for (i = 0; i < n; i++){ for (j = ia[i]; j < ia[i+1]; j++){ if (ja[j] == i) continue; - diag[i] += ABS(a[j]); + diag[i] += fabs(a[j]); } } } else { @@ -4176,7 +4176,7 @@ void SparseMatrix_page_rank(SparseMatrix A, real teleport_probablity, int weight */ res = 0; - for (i = 0; i < n; i++) res += ABS(x[i] - y[i]); + for (i = 0; i < n; i++) res += fabs(x[i] - y[i]); if (Verbose) fprintf(stderr,"page rank iter -- %d, res = %f\n",iter, res); memcpy(x, y, sizeof(real)*n); } while (res > epsilon);