int reducedQuarticRoots(const double t4, const double t3, const double t2, const double t1, const double t0, const bool oneHint, double roots[4]) { #if SK_DEBUG // create a string mathematica understands // GDB set print repe 15 # if repeated digits is a bother // set print elements 400 # if line doesn't fit char str[1024]; bzero(str, sizeof(str)); sprintf(str, "Solve[%1.19g x^4 + %1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]", t4, t3, t2, t1, t0); mathematica_ize(str, sizeof(str)); #if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA SkDebugf("%s\n", str); #endif #endif #if 0 && SK_DEBUG bool t4Or = approximately_zero_when_compared_to(t4, t0) // 0 is one root || approximately_zero_when_compared_to(t4, t1) || approximately_zero_when_compared_to(t4, t2); bool t4And = approximately_zero_when_compared_to(t4, t0) // 0 is one root && approximately_zero_when_compared_to(t4, t1) && approximately_zero_when_compared_to(t4, t2); if (t4Or != t4And) { SkDebugf("%s t4 or and\n", __FUNCTION__); } bool t3Or = approximately_zero_when_compared_to(t3, t0) || approximately_zero_when_compared_to(t3, t1) || approximately_zero_when_compared_to(t3, t2); bool t3And = approximately_zero_when_compared_to(t3, t0) && approximately_zero_when_compared_to(t3, t1) && approximately_zero_when_compared_to(t3, t2); if (t3Or != t3And) { SkDebugf("%s t3 or and\n", __FUNCTION__); } bool t0Or = approximately_zero_when_compared_to(t0, t1) // 0 is one root && approximately_zero_when_compared_to(t0, t2) && approximately_zero_when_compared_to(t0, t3) && approximately_zero_when_compared_to(t0, t4); bool t0And = approximately_zero_when_compared_to(t0, t1) // 0 is one root && approximately_zero_when_compared_to(t0, t2) && approximately_zero_when_compared_to(t0, t3) && approximately_zero_when_compared_to(t0, t4); if (t0Or != t0And) { SkDebugf("%s t0 or and\n", __FUNCTION__); } #endif if (approximately_zero_when_compared_to(t4, t0) // 0 is one root && approximately_zero_when_compared_to(t4, t1) && approximately_zero_when_compared_to(t4, t2)) { if (approximately_zero_when_compared_to(t3, t0) && approximately_zero_when_compared_to(t3, t1) && approximately_zero_when_compared_to(t3, t2)) { return quadraticRootsReal(t2, t1, t0, roots); } if (approximately_zero_when_compared_to(t4, t3)) { return cubicRootsReal(t3, t2, t1, t0, roots); } } if ((approximately_zero_when_compared_to(t0, t1) || approximately_zero(t1))// 0 is one root // && approximately_zero_when_compared_to(t0, t2) && approximately_zero_when_compared_to(t0, t3) && approximately_zero_when_compared_to(t0, t4)) { int num = cubicRootsReal(t4, t3, t2, t1, roots); for (int i = 0; i < num; ++i) { if (approximately_zero(roots[i])) { return num; } } roots[num++] = 0; return num; } if (oneHint) { SkASSERT(approximately_zero(t4 + t3 + t2 + t1 + t0)); // 1 is one root int num = cubicRootsReal(t4, t4 + t3, -(t1 + t0), -t0, roots); // note that -C==A+B+D+E for (int i = 0; i < num; ++i) { if (approximately_equal(roots[i], 1)) { return num; } } roots[num++] = 1; return num; } return -1; }
int cubicRootsReal(double A, double B, double C, double D, double s[3]) { #ifdef SK_DEBUG // create a string mathematica understands // GDB set print repe 15 # if repeated digits is a bother // set print elements 400 # if line doesn't fit char str[1024]; bzero(str, sizeof(str)); sprintf(str, "Solve[%1.19g x^3 + %1.19g x^2 + %1.19g x + %1.19g == 0, x]", A, B, C, D); mathematica_ize(str, sizeof(str)); #if ONE_OFF_DEBUG && ONE_OFF_DEBUG_MATHEMATICA SkDebugf("%s\n", str); #endif #endif if (approximately_zero(A) && approximately_zero_when_compared_to(A, B) && approximately_zero_when_compared_to(A, C) && approximately_zero_when_compared_to(A, D)) { // we're just a quadratic return quadraticRootsReal(B, C, D, s); } if (approximately_zero_when_compared_to(D, A) && approximately_zero_when_compared_to(D, B) && approximately_zero_when_compared_to(D, C)) { // 0 is one root int num = quadraticRootsReal(A, B, C, s); for (int i = 0; i < num; ++i) { if (approximately_zero(s[i])) { return num; } } s[num++] = 0; return num; } if (approximately_zero(A + B + C + D)) { // 1 is one root int num = quadraticRootsReal(A, A + B, -D, s); for (int i = 0; i < num; ++i) { if (AlmostEqualUlps(s[i], 1)) { return num; } } s[num++] = 1; return num; } double a, b, c; { double invA = 1 / A; a = B * invA; b = C * invA; c = D * invA; } double a2 = a * a; double Q = (a2 - b * 3) / 9; double R = (2 * a2 * a - 9 * a * b + 27 * c) / 54; double R2 = R * R; double Q3 = Q * Q * Q; double R2MinusQ3 = R2 - Q3; double adiv3 = a / 3; double r; double* roots = s; #if 0 if (approximately_zero_squared(R2MinusQ3) && AlmostEqualUlps(R2, Q3)) { if (approximately_zero_squared(R)) {/* one triple solution */ *roots++ = -adiv3; } else { /* one single and one double solution */ double u = cube_root(-R); *roots++ = 2 * u - adiv3; *roots++ = -u - adiv3; } } else #endif if (R2MinusQ3 < 0) // we have 3 real roots { double theta = acos(R / sqrt(Q3)); double neg2RootQ = -2 * sqrt(Q); r = neg2RootQ * cos(theta / 3) - adiv3; *roots++ = r; r = neg2RootQ * cos((theta + 2 * PI) / 3) - adiv3; if (!AlmostEqualUlps(s[0], r)) { *roots++ = r; } r = neg2RootQ * cos((theta - 2 * PI) / 3) - adiv3; if (!AlmostEqualUlps(s[0], r) && (roots - s == 1 || !AlmostEqualUlps(s[1], r))) { *roots++ = r; } } else // we have 1 real root { double sqrtR2MinusQ3 = sqrt(R2MinusQ3); double A = fabs(R) + sqrtR2MinusQ3; A = cube_root(A); if (R > 0) { A = -A; } if (A != 0) { A += Q / A; } r = A - adiv3; *roots++ = r; if (AlmostEqualUlps(R2, Q3)) { r = -A / 2 - adiv3; if (!AlmostEqualUlps(s[0], r)) { *roots++ = r; } } } return (int)(roots - s); }