blob: ea786e44076019df47bc08b40b061ff4b7d2d039 [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.com5e0500f2013-02-20 12:51:37 +000043 mathematica_ize(str, sizeof(str));
44#if ONE_OFF_DEBUG
45 SkDebugf("%s\n", str);
46#endif
caryclark@google.com73ca6242013-01-17 21:02:47 +000047#endif
caryclark@google.combeda3892013-02-07 13:13:41 +000048 if (approximately_zero_when_compared_to(t4, t0) // 0 is one root
49 && approximately_zero_when_compared_to(t4, t1)
caryclark@google.com5e0500f2013-02-20 12:51:37 +000050 && approximately_zero_when_compared_to(t4, t2)) {
caryclark@google.combeda3892013-02-07 13:13:41 +000051 if (approximately_zero_when_compared_to(t3, t0)
52 && approximately_zero_when_compared_to(t3, t1)
53 && approximately_zero_when_compared_to(t3, t2)) {
caryclark@google.com9f602912013-01-24 21:47:16 +000054 return quadraticRootsReal(t2, t1, t0, roots);
55 }
caryclark@google.com5e0500f2013-02-20 12:51:37 +000056 if (approximately_zero_when_compared_to(t4, t3)) {
57 return cubicRootsReal(t3, t2, t1, t0, roots);
58 }
caryclark@google.comd1688742012-09-18 20:08:37 +000059 }
caryclark@google.comf9502d72013-02-04 14:06:49 +000060 if (approximately_zero_when_compared_to(t0, t1) // 0 is one root
61 && approximately_zero_when_compared_to(t0, t2)
62 && approximately_zero_when_compared_to(t0, t3)
63 && approximately_zero_when_compared_to(t0, t4)) {
caryclark@google.com9f602912013-01-24 21:47:16 +000064 int num = cubicRootsReal(t4, t3, t2, t1, roots);
caryclark@google.comd1688742012-09-18 20:08:37 +000065 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000066 if (approximately_zero(roots[i])) {
caryclark@google.comd1688742012-09-18 20:08:37 +000067 return num;
68 }
69 }
caryclark@google.com9f602912013-01-24 21:47:16 +000070 roots[num++] = 0;
caryclark@google.comd1688742012-09-18 20:08:37 +000071 return num;
72 }
caryclark@google.com9f602912013-01-24 21:47:16 +000073 if (oneHint) {
caryclark@google.comaa358312013-01-29 20:28:49 +000074 SkASSERT(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root
caryclark@google.com9f602912013-01-24 21:47:16 +000075 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 +000076 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000077 if (approximately_equal(roots[i], 1)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +000078 return num;
79 }
80 }
caryclark@google.com9f602912013-01-24 21:47:16 +000081 roots[num++] = 1;
caryclark@google.com6aea33f2012-10-09 14:11:58 +000082 return num;
83 }
caryclark@google.com9f602912013-01-24 21:47:16 +000084 return -1;
caryclark@google.comd1688742012-09-18 20:08:37 +000085}
caryclark@google.com235f56a2012-09-14 14:19:30 +000086
caryclark@google.com5e0500f2013-02-20 12:51:37 +000087int quarticRootsReal(int firstCubicRoot, const double A, const double B, const double C,
88 const double D, const double E, double s[4]) {
caryclark@google.comd1688742012-09-18 20:08:37 +000089 double u, v;
caryclark@google.com235f56a2012-09-14 14:19:30 +000090 /* normal form: x^4 + Ax^3 + Bx^2 + Cx + D = 0 */
91 const double invA = 1 / A;
92 const double a = B * invA;
93 const double b = C * invA;
94 const double c = D * invA;
95 const double d = E * invA;
96 /* substitute x = y - a/4 to eliminate cubic term:
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000097 x^4 + px^2 + qx + r = 0 */
caryclark@google.com235f56a2012-09-14 14:19:30 +000098 const double a2 = a * a;
99 const double p = -3 * a2 / 8 + b;
100 const double q = a2 * a / 8 - a * b / 2 + c;
101 const double r = -3 * a2 * a2 / 256 + a2 * b / 16 - a * c / 4 + d;
caryclark@google.com73ca6242013-01-17 21:02:47 +0000102 int num;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000103 if (approximately_zero(r)) {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +0000104 /* no absolute term: y(y^3 + py + q) = 0 */
caryclark@google.com9f602912013-01-24 21:47:16 +0000105 num = cubicRootsReal(1, 0, p, q, s);
caryclark@google.com235f56a2012-09-14 14:19:30 +0000106 s[num++] = 0;
107 } else {
108 /* solve the resolvent cubic ... */
caryclark@google.com9f602912013-01-24 21:47:16 +0000109 double cubicRoots[3];
110 int roots = cubicRootsReal(1, -p / 2, -r, r * p / 2 - q * q / 8, cubicRoots);
111 int index;
112 #if 0 && SK_DEBUG // enable to verify that any cubic root is as good as any other
113 double tries[3][4];
114 int nums[3];
115 for (index = 0; index < roots; ++index) {
116 /* ... and take one real solution ... */
117 const double z = cubicRoots[index];
118 /* ... to build two quadric equations */
119 u = z * z - r;
120 v = 2 * z - p;
121 if (approximately_zero_squared(u)) {
122 u = 0;
123 } else if (u > 0) {
124 u = sqrt(u);
125 } else {
126 SkDebugf("%s u=%1.9g <0\n", __FUNCTION__, u);
127 continue;
128 }
129 if (approximately_zero_squared(v)) {
130 v = 0;
131 } else if (v > 0) {
132 v = sqrt(v);
133 } else {
134 SkDebugf("%s v=%1.9g <0\n", __FUNCTION__, v);
135 continue;
136 }
137 nums[index] = quadraticRootsReal(1, q < 0 ? -v : v, z - u, tries[index]);
138 nums[index] += quadraticRootsReal(1, q < 0 ? v : -v, z + u, tries[index] + nums[index]);
139 /* resubstitute */
140 const double sub = a / 4;
141 for (int i = 0; i < nums[index]; ++i) {
142 tries[index][i] -= sub;
143 }
144 }
145 for (index = 0; index < roots; ++index) {
146 SkDebugf("%s", __FUNCTION__);
147 for (int idx2 = 0; idx2 < nums[index]; ++idx2) {
148 SkDebugf(" %1.9g", tries[index][idx2]);
149 }
150 SkDebugf("\n");
151 }
152 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000153 /* ... and take one real solution ... */
caryclark@google.com9f602912013-01-24 21:47:16 +0000154 double z;
155 num = 0;
156 int num2 = 0;
caryclark@google.com5e0500f2013-02-20 12:51:37 +0000157 for (index = firstCubicRoot; index < roots; ++index) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000158 z = cubicRoots[index];
159 /* ... to build two quadric equations */
160 u = z * z - r;
161 v = 2 * z - p;
162 if (approximately_zero_squared(u)) {
163 u = 0;
164 } else if (u > 0) {
165 u = sqrt(u);
166 } else {
167 continue;
168 }
169 if (approximately_zero_squared(v)) {
170 v = 0;
171 } else if (v > 0) {
172 v = sqrt(v);
173 } else {
174 continue;
175 }
176 num = quadraticRootsReal(1, q < 0 ? -v : v, z - u, s);
177 num2 = quadraticRootsReal(1, q < 0 ? v : -v, z + u, s + num);
178 if (!((num | num2) & 1)) {
179 break; // prefer solutions without single quad roots
180 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000181 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000182 num += num2;
183 if (!num) {
184 return 0; // no valid cubic root
caryclark@google.com235f56a2012-09-14 14:19:30 +0000185 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000186 }
187 /* resubstitute */
188 const double sub = a / 4;
189 for (int i = 0; i < num; ++i) {
190 s[i] -= sub;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000191 }
192 // eliminate duplicates
caryclark@google.com73ca6242013-01-17 21:02:47 +0000193 for (int i = 0; i < num - 1; ++i) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000194 for (int j = i + 1; j < num; ) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000195 if (AlmostEqualUlps(s[i], s[j])) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000196 if (j < --num) {
197 s[j] = s[num];
198 }
199 } else {
200 ++j;
201 }
202 }
203 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000204 return num;
205}