blob: 50f85b59e938776a3d1da1afdecc2b2c0457976f [file] [log] [blame]
caryclark@google.com235f56a2012-09-14 14:19:30 +00001// from http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
2/*
3 * Roots3And4.c
4 *
5 * Utility functions to find cubic and quartic roots,
6 * coefficients are passed like this:
7 *
8 * c[0] + c[1]*x + c[2]*x^2 + c[3]*x^3 + c[4]*x^4 = 0
9 *
10 * The functions return the number of non-complex roots and
11 * put the values into the s array.
12 *
13 * Author: Jochen Schwarze (schwarze@isa.de)
14 *
15 * Jan 26, 1990 Version for Graphics Gems
16 * Oct 11, 1990 Fixed sign problem for negative q's in SolveQuartic
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000017 * (reported by Mark Podlipec),
18 * Old-style function definitions,
19 * IsZero() as a macro
caryclark@google.com235f56a2012-09-14 14:19:30 +000020 * Nov 23, 1990 Some systems do not declare acos() and cbrt() in
21 * <math.h>, though the functions exist in the library.
22 * If large coefficients are used, EQN_EPS should be
23 * reduced considerably (e.g. to 1E-30), results will be
24 * correct but multiple roots might be reported more
25 * than once.
26 */
27
28#include <math.h>
29#include "CubicUtilities.h"
caryclark@google.com73ca6242013-01-17 21:02:47 +000030#include "QuadraticUtilities.h"
caryclark@google.com235f56a2012-09-14 14:19:30 +000031#include "QuarticRoot.h"
32
caryclark@google.com9f602912013-01-24 21:47:16 +000033int reducedQuarticRoots(const double t4, const double t3, const double t2, const double t1,
34 const double t0, const bool oneHint, double roots[4]) {
caryclark@google.com73ca6242013-01-17 21:02:47 +000035#if SK_DEBUG
caryclark@google.com73ca6242013-01-17 21:02:47 +000036 // create a string mathematica understands
caryclark@google.com9f602912013-01-24 21:47:16 +000037 // GDB set print repe 15 # if repeated digits is a bother
38 // set print elements 400 # if line doesn't fit
caryclark@google.com73ca6242013-01-17 21:02:47 +000039 char str[1024];
40 bzero(str, sizeof(str));
caryclark@google.com9f602912013-01-24 21:47:16 +000041 sprintf(str, "Solve[%1.19g x^4 + %1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]",
42 t4, t3, t2, t1, t0);
caryclark@google.com73ca6242013-01-17 21:02:47 +000043#endif
caryclark@google.combeda3892013-02-07 13:13:41 +000044 if (approximately_zero_when_compared_to(t4, t0) // 0 is one root
45 && approximately_zero_when_compared_to(t4, t1)
46 && approximately_zero_when_compared_to(t4, t2)
47 && approximately_zero_when_compared_to(t4, t3)) {
48 if (approximately_zero_when_compared_to(t3, t0)
49 && approximately_zero_when_compared_to(t3, t1)
50 && approximately_zero_when_compared_to(t3, t2)) {
caryclark@google.com9f602912013-01-24 21:47:16 +000051 return quadraticRootsReal(t2, t1, t0, roots);
52 }
53 return cubicRootsReal(t3, t2, t1, t0, roots);
caryclark@google.comd1688742012-09-18 20:08:37 +000054 }
caryclark@google.comf9502d72013-02-04 14:06:49 +000055 if (approximately_zero_when_compared_to(t0, t1) // 0 is one root
56 && approximately_zero_when_compared_to(t0, t2)
57 && approximately_zero_when_compared_to(t0, t3)
58 && approximately_zero_when_compared_to(t0, t4)) {
caryclark@google.com9f602912013-01-24 21:47:16 +000059 int num = cubicRootsReal(t4, t3, t2, t1, roots);
caryclark@google.comd1688742012-09-18 20:08:37 +000060 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000061 if (approximately_zero(roots[i])) {
caryclark@google.comd1688742012-09-18 20:08:37 +000062 return num;
63 }
64 }
caryclark@google.com9f602912013-01-24 21:47:16 +000065 roots[num++] = 0;
caryclark@google.comd1688742012-09-18 20:08:37 +000066 return num;
67 }
caryclark@google.com9f602912013-01-24 21:47:16 +000068 if (oneHint) {
caryclark@google.comaa358312013-01-29 20:28:49 +000069 SkASSERT(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root
caryclark@google.com9f602912013-01-24 21:47:16 +000070 int num = cubicRootsReal(t4, t4 + t3, -(t1 + t0), -t0, roots); // note that -C==A+B+D+E
caryclark@google.com6aea33f2012-10-09 14:11:58 +000071 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000072 if (approximately_equal(roots[i], 1)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +000073 return num;
74 }
75 }
caryclark@google.com9f602912013-01-24 21:47:16 +000076 roots[num++] = 1;
caryclark@google.com6aea33f2012-10-09 14:11:58 +000077 return num;
78 }
caryclark@google.com9f602912013-01-24 21:47:16 +000079 return -1;
caryclark@google.comd1688742012-09-18 20:08:37 +000080}
caryclark@google.com235f56a2012-09-14 14:19:30 +000081
caryclark@google.com9f602912013-01-24 21:47:16 +000082int quarticRootsReal(const double A, const double B, const double C, const double D,
caryclark@google.com235f56a2012-09-14 14:19:30 +000083 const double E, double s[4]) {
caryclark@google.comd1688742012-09-18 20:08:37 +000084 double u, v;
caryclark@google.com235f56a2012-09-14 14:19:30 +000085 /* normal form: x^4 + Ax^3 + Bx^2 + Cx + D = 0 */
86 const double invA = 1 / A;
87 const double a = B * invA;
88 const double b = C * invA;
89 const double c = D * invA;
90 const double d = E * invA;
91 /* substitute x = y - a/4 to eliminate cubic term:
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000092 x^4 + px^2 + qx + r = 0 */
caryclark@google.com235f56a2012-09-14 14:19:30 +000093 const double a2 = a * a;
94 const double p = -3 * a2 / 8 + b;
95 const double q = a2 * a / 8 - a * b / 2 + c;
96 const double r = -3 * a2 * a2 / 256 + a2 * b / 16 - a * c / 4 + d;
caryclark@google.com73ca6242013-01-17 21:02:47 +000097 int num;
caryclark@google.com235f56a2012-09-14 14:19:30 +000098 if (approximately_zero(r)) {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000099 /* no absolute term: y(y^3 + py + q) = 0 */
caryclark@google.com9f602912013-01-24 21:47:16 +0000100 num = cubicRootsReal(1, 0, p, q, s);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000101 s[num++] = 0;
102 } else {
103 /* solve the resolvent cubic ... */
caryclark@google.com9f602912013-01-24 21:47:16 +0000104 double cubicRoots[3];
105 int roots = cubicRootsReal(1, -p / 2, -r, r * p / 2 - q * q / 8, cubicRoots);
106 int index;
107 #if 0 && SK_DEBUG // enable to verify that any cubic root is as good as any other
108 double tries[3][4];
109 int nums[3];
110 for (index = 0; index < roots; ++index) {
111 /* ... and take one real solution ... */
112 const double z = cubicRoots[index];
113 /* ... to build two quadric equations */
114 u = z * z - r;
115 v = 2 * z - p;
116 if (approximately_zero_squared(u)) {
117 u = 0;
118 } else if (u > 0) {
119 u = sqrt(u);
120 } else {
121 SkDebugf("%s u=%1.9g <0\n", __FUNCTION__, u);
122 continue;
123 }
124 if (approximately_zero_squared(v)) {
125 v = 0;
126 } else if (v > 0) {
127 v = sqrt(v);
128 } else {
129 SkDebugf("%s v=%1.9g <0\n", __FUNCTION__, v);
130 continue;
131 }
132 nums[index] = quadraticRootsReal(1, q < 0 ? -v : v, z - u, tries[index]);
133 nums[index] += quadraticRootsReal(1, q < 0 ? v : -v, z + u, tries[index] + nums[index]);
134 /* resubstitute */
135 const double sub = a / 4;
136 for (int i = 0; i < nums[index]; ++i) {
137 tries[index][i] -= sub;
138 }
139 }
140 for (index = 0; index < roots; ++index) {
141 SkDebugf("%s", __FUNCTION__);
142 for (int idx2 = 0; idx2 < nums[index]; ++idx2) {
143 SkDebugf(" %1.9g", tries[index][idx2]);
144 }
145 SkDebugf("\n");
146 }
147 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000148 /* ... and take one real solution ... */
caryclark@google.com9f602912013-01-24 21:47:16 +0000149 double z;
150 num = 0;
151 int num2 = 0;
152 for (index = 0; index < roots; ++index) {
153 z = cubicRoots[index];
154 /* ... to build two quadric equations */
155 u = z * z - r;
156 v = 2 * z - p;
157 if (approximately_zero_squared(u)) {
158 u = 0;
159 } else if (u > 0) {
160 u = sqrt(u);
161 } else {
162 continue;
163 }
164 if (approximately_zero_squared(v)) {
165 v = 0;
166 } else if (v > 0) {
167 v = sqrt(v);
168 } else {
169 continue;
170 }
171 num = quadraticRootsReal(1, q < 0 ? -v : v, z - u, s);
172 num2 = quadraticRootsReal(1, q < 0 ? v : -v, z + u, s + num);
173 if (!((num | num2) & 1)) {
174 break; // prefer solutions without single quad roots
175 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000176 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000177 num += num2;
178 if (!num) {
179 return 0; // no valid cubic root
caryclark@google.com235f56a2012-09-14 14:19:30 +0000180 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000181 }
182 /* resubstitute */
183 const double sub = a / 4;
184 for (int i = 0; i < num; ++i) {
185 s[i] -= sub;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000186 }
187 // eliminate duplicates
caryclark@google.com73ca6242013-01-17 21:02:47 +0000188 for (int i = 0; i < num - 1; ++i) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000189 for (int j = i + 1; j < num; ) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000190 if (AlmostEqualUlps(s[i], s[j])) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000191 if (j < --num) {
192 s[j] = s[num];
193 }
194 } else {
195 ++j;
196 }
197 }
198 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000199 return num;
200}