blob: 6941935f43a547e46b3a9f18a2cb594bee84ea72 [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.com9f602912013-01-24 21:47:16 +000044 if (approximately_zero(t4)) {
45 if (approximately_zero(t3)) {
46 return quadraticRootsReal(t2, t1, t0, roots);
47 }
48 return cubicRootsReal(t3, t2, t1, t0, roots);
caryclark@google.comd1688742012-09-18 20:08:37 +000049 }
caryclark@google.com9f602912013-01-24 21:47:16 +000050 if (approximately_zero(t0)) { // 0 is one root
51 int num = cubicRootsReal(t4, t3, t2, t1, roots);
caryclark@google.comd1688742012-09-18 20:08:37 +000052 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000053 if (approximately_zero(roots[i])) {
caryclark@google.comd1688742012-09-18 20:08:37 +000054 return num;
55 }
56 }
caryclark@google.com9f602912013-01-24 21:47:16 +000057 roots[num++] = 0;
caryclark@google.comd1688742012-09-18 20:08:37 +000058 return num;
59 }
caryclark@google.com9f602912013-01-24 21:47:16 +000060 if (oneHint) {
61 assert(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root
62 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 +000063 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000064 if (approximately_equal(roots[i], 1)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +000065 return num;
66 }
67 }
caryclark@google.com9f602912013-01-24 21:47:16 +000068 roots[num++] = 1;
caryclark@google.com6aea33f2012-10-09 14:11:58 +000069 return num;
70 }
caryclark@google.com9f602912013-01-24 21:47:16 +000071 return -1;
caryclark@google.comd1688742012-09-18 20:08:37 +000072}
caryclark@google.com235f56a2012-09-14 14:19:30 +000073
caryclark@google.com9f602912013-01-24 21:47:16 +000074int quarticRootsReal(const double A, const double B, const double C, const double D,
caryclark@google.com235f56a2012-09-14 14:19:30 +000075 const double E, double s[4]) {
caryclark@google.comd1688742012-09-18 20:08:37 +000076 double u, v;
caryclark@google.com235f56a2012-09-14 14:19:30 +000077 /* normal form: x^4 + Ax^3 + Bx^2 + Cx + D = 0 */
78 const double invA = 1 / A;
79 const double a = B * invA;
80 const double b = C * invA;
81 const double c = D * invA;
82 const double d = E * invA;
83 /* substitute x = y - a/4 to eliminate cubic term:
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000084 x^4 + px^2 + qx + r = 0 */
caryclark@google.com235f56a2012-09-14 14:19:30 +000085 const double a2 = a * a;
86 const double p = -3 * a2 / 8 + b;
87 const double q = a2 * a / 8 - a * b / 2 + c;
88 const double r = -3 * a2 * a2 / 256 + a2 * b / 16 - a * c / 4 + d;
caryclark@google.com73ca6242013-01-17 21:02:47 +000089 int num;
caryclark@google.com235f56a2012-09-14 14:19:30 +000090 if (approximately_zero(r)) {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000091 /* no absolute term: y(y^3 + py + q) = 0 */
caryclark@google.com9f602912013-01-24 21:47:16 +000092 num = cubicRootsReal(1, 0, p, q, s);
caryclark@google.com235f56a2012-09-14 14:19:30 +000093 s[num++] = 0;
94 } else {
95 /* solve the resolvent cubic ... */
caryclark@google.com9f602912013-01-24 21:47:16 +000096 double cubicRoots[3];
97 int roots = cubicRootsReal(1, -p / 2, -r, r * p / 2 - q * q / 8, cubicRoots);
98 int index;
99 #if 0 && SK_DEBUG // enable to verify that any cubic root is as good as any other
100 double tries[3][4];
101 int nums[3];
102 for (index = 0; index < roots; ++index) {
103 /* ... and take one real solution ... */
104 const double z = cubicRoots[index];
105 /* ... to build two quadric equations */
106 u = z * z - r;
107 v = 2 * z - p;
108 if (approximately_zero_squared(u)) {
109 u = 0;
110 } else if (u > 0) {
111 u = sqrt(u);
112 } else {
113 SkDebugf("%s u=%1.9g <0\n", __FUNCTION__, u);
114 continue;
115 }
116 if (approximately_zero_squared(v)) {
117 v = 0;
118 } else if (v > 0) {
119 v = sqrt(v);
120 } else {
121 SkDebugf("%s v=%1.9g <0\n", __FUNCTION__, v);
122 continue;
123 }
124 nums[index] = quadraticRootsReal(1, q < 0 ? -v : v, z - u, tries[index]);
125 nums[index] += quadraticRootsReal(1, q < 0 ? v : -v, z + u, tries[index] + nums[index]);
126 /* resubstitute */
127 const double sub = a / 4;
128 for (int i = 0; i < nums[index]; ++i) {
129 tries[index][i] -= sub;
130 }
131 }
132 for (index = 0; index < roots; ++index) {
133 SkDebugf("%s", __FUNCTION__);
134 for (int idx2 = 0; idx2 < nums[index]; ++idx2) {
135 SkDebugf(" %1.9g", tries[index][idx2]);
136 }
137 SkDebugf("\n");
138 }
139 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000140 /* ... and take one real solution ... */
caryclark@google.com9f602912013-01-24 21:47:16 +0000141 double z;
142 num = 0;
143 int num2 = 0;
144 for (index = 0; index < roots; ++index) {
145 z = cubicRoots[index];
146 /* ... to build two quadric equations */
147 u = z * z - r;
148 v = 2 * z - p;
149 if (approximately_zero_squared(u)) {
150 u = 0;
151 } else if (u > 0) {
152 u = sqrt(u);
153 } else {
154 continue;
155 }
156 if (approximately_zero_squared(v)) {
157 v = 0;
158 } else if (v > 0) {
159 v = sqrt(v);
160 } else {
161 continue;
162 }
163 num = quadraticRootsReal(1, q < 0 ? -v : v, z - u, s);
164 num2 = quadraticRootsReal(1, q < 0 ? v : -v, z + u, s + num);
165 if (!((num | num2) & 1)) {
166 break; // prefer solutions without single quad roots
167 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000168 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000169 num += num2;
170 if (!num) {
171 return 0; // no valid cubic root
caryclark@google.com235f56a2012-09-14 14:19:30 +0000172 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000173 }
174 /* resubstitute */
175 const double sub = a / 4;
176 for (int i = 0; i < num; ++i) {
177 s[i] -= sub;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000178 }
179 // eliminate duplicates
caryclark@google.com73ca6242013-01-17 21:02:47 +0000180 for (int i = 0; i < num - 1; ++i) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000181 for (int j = i + 1; j < num; ) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000182 if (AlmostEqualUlps(s[i], s[j])) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000183 if (j < --num) {
184 s[j] = s[num];
185 }
186 } else {
187 ++j;
188 }
189 }
190 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000191 return num;
192}