Graphviz 16.1.3~dev.20261121.1645
Loading...
Searching...
No Matches
solvers.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 <pathplan/solvers.h>
15#include <stdbool.h>
16
17static int solve1(double *coeff, double *roots);
18static int solve2(double *coeff, double *roots);
19
20#ifndef M_PI
21#define M_PI 3.14159265358979323846
22#endif
23
24static bool aeq0(double a) {
25 const double EPS = 1e-7;
26 return fabs(a) < EPS;
27}
28
29int solve3(double *coeff, double *roots) {
30 double a, b, c, d;
31 int rootn, i;
32 double p, q, disc, b_over_3a, c_over_a, d_over_a;
33 double r, theta, temp, alpha, beta;
34
35 a = coeff[3], b = coeff[2], c = coeff[1], d = coeff[0];
36 if (aeq0(a))
37 return solve2(coeff, roots);
38 b_over_3a = b / (3 * a);
39 c_over_a = c / a;
40 d_over_a = d / a;
41
42 p = b_over_3a * b_over_3a;
43 q = 2 * b_over_3a * p - b_over_3a * c_over_a + d_over_a;
44 p = c_over_a / 3 - p;
45 disc = q * q + 4 * p * p * p;
46
47 if (disc < 0) {
48 r = .5 * sqrt(-disc + q * q);
49 theta = atan2(sqrt(-disc), -q);
50 temp = 2 * cbrt(r);
51 roots[0] = temp * cos(theta / 3);
52 roots[1] = temp * cos((theta + M_PI + M_PI) / 3);
53 roots[2] = temp * cos((theta - M_PI - M_PI) / 3);
54 rootn = 3;
55 } else {
56 alpha = .5 * (sqrt(disc) - q);
57 beta = -q - alpha;
58 roots[0] = cbrt(alpha) + cbrt(beta);
59 if (disc > 0)
60 rootn = 1;
61 else
62 roots[1] = roots[2] = -.5 * roots[0], rootn = 3;
63 }
64
65 for (i = 0; i < rootn; i++)
66 roots[i] -= b_over_3a;
67
68 return rootn;
69}
70
71static int solve2(double *coeff, double *roots) {
72 double a, b, c;
73 double disc, b_over_2a, c_over_a;
74
75 a = coeff[2], b = coeff[1], c = coeff[0];
76 if (aeq0(a))
77 return solve1(coeff, roots);
78 b_over_2a = b / (2 * a);
79 c_over_a = c / a;
80
81 disc = b_over_2a * b_over_2a - c_over_a;
82 if (disc < 0) {
83 return 0;
84 } else if (disc > 0) {
85 roots[0] = -b_over_2a + sqrt(disc);
86 roots[1] = -2 * b_over_2a - roots[0];
87 return 2;
88 }
89 roots[0] = -b_over_2a;
90 return 1;
91}
92
93static int solve1(double *coeff, double *roots) {
94 double a, b;
95
96 a = coeff[1], b = coeff[0];
97 if (aeq0(a)) {
98 if (aeq0(b))
99 return 4;
100 else
101 return 0;
102 }
103 roots[0] = -b / a;
104 return 1;
105}
#define EPS
Definition emit.c:437
static Dtdisc_t disc
Definition exparse.y:209
#define alpha
Definition shapes.c:4034
static bool aeq0(double a)
Definition solvers.c:24
static int solve1(double *coeff, double *roots)
Definition solvers.c:93
int solve3(double *coeff, double *roots)
Definition solvers.c:29
static int solve2(double *coeff, double *roots)
Definition solvers.c:71
#define M_PI
Definition solvers.c:21