caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 1 | // 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.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 17 | * (reported by Mark Podlipec), |
| 18 | * Old-style function definitions, |
| 19 | * IsZero() as a macro |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 20 | * 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.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 30 | #include "QuadraticUtilities.h" |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 31 | #include "QuarticRoot.h" |
| 32 | |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 33 | int 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.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 35 | #if SK_DEBUG |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 36 | // create a string mathematica understands |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 37 | // GDB set print repe 15 # if repeated digits is a bother |
| 38 | // set print elements 400 # if line doesn't fit |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 39 | char str[1024]; |
| 40 | bzero(str, sizeof(str)); |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 41 | 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.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 43 | #endif |
caryclark@google.com | beda389 | 2013-02-07 13:13:41 +0000 | [diff] [blame] | 44 | 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.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 51 | return quadraticRootsReal(t2, t1, t0, roots); |
| 52 | } |
| 53 | return cubicRootsReal(t3, t2, t1, t0, roots); |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 54 | } |
caryclark@google.com | f9502d7 | 2013-02-04 14:06:49 +0000 | [diff] [blame] | 55 | 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.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 59 | int num = cubicRootsReal(t4, t3, t2, t1, roots); |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 60 | for (int i = 0; i < num; ++i) { |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 61 | if (approximately_zero(roots[i])) { |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 62 | return num; |
| 63 | } |
| 64 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 65 | roots[num++] = 0; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 66 | return num; |
| 67 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 68 | if (oneHint) { |
caryclark@google.com | aa35831 | 2013-01-29 20:28:49 +0000 | [diff] [blame] | 69 | SkASSERT(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 70 | int num = cubicRootsReal(t4, t4 + t3, -(t1 + t0), -t0, roots); // note that -C==A+B+D+E |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 71 | for (int i = 0; i < num; ++i) { |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 72 | if (approximately_equal(roots[i], 1)) { |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 73 | return num; |
| 74 | } |
| 75 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 76 | roots[num++] = 1; |
caryclark@google.com | 6aea33f | 2012-10-09 14:11:58 +0000 | [diff] [blame] | 77 | return num; |
| 78 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 79 | return -1; |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 80 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 81 | |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 82 | int quarticRootsReal(const double A, const double B, const double C, const double D, |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 83 | const double E, double s[4]) { |
caryclark@google.com | d168874 | 2012-09-18 20:08:37 +0000 | [diff] [blame] | 84 | double u, v; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 85 | /* 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.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 92 | x^4 + px^2 + qx + r = 0 */ |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 93 | 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.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 97 | int num; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 98 | if (approximately_zero(r)) { |
skia.committer@gmail.com | 055c7c2 | 2012-09-15 02:01:41 +0000 | [diff] [blame] | 99 | /* no absolute term: y(y^3 + py + q) = 0 */ |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 100 | num = cubicRootsReal(1, 0, p, q, s); |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 101 | s[num++] = 0; |
| 102 | } else { |
| 103 | /* solve the resolvent cubic ... */ |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 104 | 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.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 148 | /* ... and take one real solution ... */ |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 149 | 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.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 176 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 177 | num += num2; |
| 178 | if (!num) { |
| 179 | return 0; // no valid cubic root |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 180 | } |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 181 | } |
| 182 | /* resubstitute */ |
| 183 | const double sub = a / 4; |
| 184 | for (int i = 0; i < num; ++i) { |
| 185 | s[i] -= sub; |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 186 | } |
| 187 | // eliminate duplicates |
caryclark@google.com | 73ca624 | 2013-01-17 21:02:47 +0000 | [diff] [blame] | 188 | for (int i = 0; i < num - 1; ++i) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 189 | for (int j = i + 1; j < num; ) { |
caryclark@google.com | 9f60291 | 2013-01-24 21:47:16 +0000 | [diff] [blame] | 190 | if (AlmostEqualUlps(s[i], s[j])) { |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 191 | if (j < --num) { |
| 192 | s[j] = s[num]; |
| 193 | } |
| 194 | } else { |
| 195 | ++j; |
| 196 | } |
| 197 | } |
| 198 | } |
caryclark@google.com | 235f56a | 2012-09-14 14:19:30 +0000 | [diff] [blame] | 199 | return num; |
| 200 | } |