blob: 389f68a0e9662e60b01c1c9d1556999bb7490a74 [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.comf9502d72013-02-04 14:06:49 +000050 if (approximately_zero_when_compared_to(t0, t1) // 0 is one root
51 && approximately_zero_when_compared_to(t0, t2)
52 && approximately_zero_when_compared_to(t0, t3)
53 && approximately_zero_when_compared_to(t0, t4)) {
caryclark@google.com9f602912013-01-24 21:47:16 +000054 int num = cubicRootsReal(t4, t3, t2, t1, roots);
caryclark@google.comd1688742012-09-18 20:08:37 +000055 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000056 if (approximately_zero(roots[i])) {
caryclark@google.comd1688742012-09-18 20:08:37 +000057 return num;
58 }
59 }
caryclark@google.com9f602912013-01-24 21:47:16 +000060 roots[num++] = 0;
caryclark@google.comd1688742012-09-18 20:08:37 +000061 return num;
62 }
caryclark@google.com9f602912013-01-24 21:47:16 +000063 if (oneHint) {
caryclark@google.comaa358312013-01-29 20:28:49 +000064 SkASSERT(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root
caryclark@google.com9f602912013-01-24 21:47:16 +000065 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 +000066 for (int i = 0; i < num; ++i) {
caryclark@google.com9f602912013-01-24 21:47:16 +000067 if (approximately_equal(roots[i], 1)) {
caryclark@google.com6aea33f2012-10-09 14:11:58 +000068 return num;
69 }
70 }
caryclark@google.com9f602912013-01-24 21:47:16 +000071 roots[num++] = 1;
caryclark@google.com6aea33f2012-10-09 14:11:58 +000072 return num;
73 }
caryclark@google.com9f602912013-01-24 21:47:16 +000074 return -1;
caryclark@google.comd1688742012-09-18 20:08:37 +000075}
caryclark@google.com235f56a2012-09-14 14:19:30 +000076
caryclark@google.com9f602912013-01-24 21:47:16 +000077int quarticRootsReal(const double A, const double B, const double C, const double D,
caryclark@google.com235f56a2012-09-14 14:19:30 +000078 const double E, double s[4]) {
caryclark@google.comd1688742012-09-18 20:08:37 +000079 double u, v;
caryclark@google.com235f56a2012-09-14 14:19:30 +000080 /* normal form: x^4 + Ax^3 + Bx^2 + Cx + D = 0 */
81 const double invA = 1 / A;
82 const double a = B * invA;
83 const double b = C * invA;
84 const double c = D * invA;
85 const double d = E * invA;
86 /* substitute x = y - a/4 to eliminate cubic term:
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000087 x^4 + px^2 + qx + r = 0 */
caryclark@google.com235f56a2012-09-14 14:19:30 +000088 const double a2 = a * a;
89 const double p = -3 * a2 / 8 + b;
90 const double q = a2 * a / 8 - a * b / 2 + c;
91 const double r = -3 * a2 * a2 / 256 + a2 * b / 16 - a * c / 4 + d;
caryclark@google.com73ca6242013-01-17 21:02:47 +000092 int num;
caryclark@google.com235f56a2012-09-14 14:19:30 +000093 if (approximately_zero(r)) {
skia.committer@gmail.com055c7c22012-09-15 02:01:41 +000094 /* no absolute term: y(y^3 + py + q) = 0 */
caryclark@google.com9f602912013-01-24 21:47:16 +000095 num = cubicRootsReal(1, 0, p, q, s);
caryclark@google.com235f56a2012-09-14 14:19:30 +000096 s[num++] = 0;
97 } else {
98 /* solve the resolvent cubic ... */
caryclark@google.com9f602912013-01-24 21:47:16 +000099 double cubicRoots[3];
100 int roots = cubicRootsReal(1, -p / 2, -r, r * p / 2 - q * q / 8, cubicRoots);
101 int index;
102 #if 0 && SK_DEBUG // enable to verify that any cubic root is as good as any other
103 double tries[3][4];
104 int nums[3];
105 for (index = 0; index < roots; ++index) {
106 /* ... and take one real solution ... */
107 const double z = cubicRoots[index];
108 /* ... to build two quadric equations */
109 u = z * z - r;
110 v = 2 * z - p;
111 if (approximately_zero_squared(u)) {
112 u = 0;
113 } else if (u > 0) {
114 u = sqrt(u);
115 } else {
116 SkDebugf("%s u=%1.9g <0\n", __FUNCTION__, u);
117 continue;
118 }
119 if (approximately_zero_squared(v)) {
120 v = 0;
121 } else if (v > 0) {
122 v = sqrt(v);
123 } else {
124 SkDebugf("%s v=%1.9g <0\n", __FUNCTION__, v);
125 continue;
126 }
127 nums[index] = quadraticRootsReal(1, q < 0 ? -v : v, z - u, tries[index]);
128 nums[index] += quadraticRootsReal(1, q < 0 ? v : -v, z + u, tries[index] + nums[index]);
129 /* resubstitute */
130 const double sub = a / 4;
131 for (int i = 0; i < nums[index]; ++i) {
132 tries[index][i] -= sub;
133 }
134 }
135 for (index = 0; index < roots; ++index) {
136 SkDebugf("%s", __FUNCTION__);
137 for (int idx2 = 0; idx2 < nums[index]; ++idx2) {
138 SkDebugf(" %1.9g", tries[index][idx2]);
139 }
140 SkDebugf("\n");
141 }
142 #endif
caryclark@google.com73ca6242013-01-17 21:02:47 +0000143 /* ... and take one real solution ... */
caryclark@google.com9f602912013-01-24 21:47:16 +0000144 double z;
145 num = 0;
146 int num2 = 0;
147 for (index = 0; index < roots; ++index) {
148 z = cubicRoots[index];
149 /* ... to build two quadric equations */
150 u = z * z - r;
151 v = 2 * z - p;
152 if (approximately_zero_squared(u)) {
153 u = 0;
154 } else if (u > 0) {
155 u = sqrt(u);
156 } else {
157 continue;
158 }
159 if (approximately_zero_squared(v)) {
160 v = 0;
161 } else if (v > 0) {
162 v = sqrt(v);
163 } else {
164 continue;
165 }
166 num = quadraticRootsReal(1, q < 0 ? -v : v, z - u, s);
167 num2 = quadraticRootsReal(1, q < 0 ? v : -v, z + u, s + num);
168 if (!((num | num2) & 1)) {
169 break; // prefer solutions without single quad roots
170 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000171 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000172 num += num2;
173 if (!num) {
174 return 0; // no valid cubic root
caryclark@google.com235f56a2012-09-14 14:19:30 +0000175 }
caryclark@google.com9f602912013-01-24 21:47:16 +0000176 }
177 /* resubstitute */
178 const double sub = a / 4;
179 for (int i = 0; i < num; ++i) {
180 s[i] -= sub;
caryclark@google.com235f56a2012-09-14 14:19:30 +0000181 }
182 // eliminate duplicates
caryclark@google.com73ca6242013-01-17 21:02:47 +0000183 for (int i = 0; i < num - 1; ++i) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000184 for (int j = i + 1; j < num; ) {
caryclark@google.com9f602912013-01-24 21:47:16 +0000185 if (AlmostEqualUlps(s[i], s[j])) {
caryclark@google.com235f56a2012-09-14 14:19:30 +0000186 if (j < --num) {
187 s[j] = s[num];
188 }
189 } else {
190 ++j;
191 }
192 }
193 }
caryclark@google.com235f56a2012-09-14 14:19:30 +0000194 return num;
195}