**0**

# Best way of solving a polynomial of the fourth degree?

###
#1
Members - Reputation: **100**

Posted 07 June 2007 - 07:52 PM

###
#2
Crossbones+ - Reputation: **1375**

Posted 07 June 2007 - 09:45 PM

###
#3
Members - Reputation: **538**

Posted 07 June 2007 - 10:06 PM

Quote:

Original post by Zipster

Considering that a quartic is the highest degree polynomial with a closed-form solution . . .

That is not true! You can go up to 4th degree, but as stated the solution will be very complicated. I'm sorry but I can't give you the solutions without my math book.

###
#4
Moderators - Reputation: **9533**

Posted 07 June 2007 - 10:41 PM

Quote:

Original post by dragongameQuote:

Original post by Zipster

Considering that a quartic is the highest degree polynomial with a closed-form solution . . .

That is not true! You can go up to 4th degree, but as stated the solution will be very complicated. I'm sorry but I can't give you the solutions without my math book.

quartic = fourth order

###
#5
Members - Reputation: **464**

Posted 08 June 2007 - 02:40 AM

#include <math.h>

#include <complex>

const double PI = 3.14159265358979323846;

//----------------------------------------------------------------------------

bool solveQuadratic(double &a, double &b, double &c, double &root)

{

if(a == 0.0 || abs(a/b) < 1.0e-6)

{

if(abs(b) < 1.0e-4)

return false;

else

{

root = -c/b;

return true;

}

}

double discriminant = b * b - 4.0 * a * c;

if(discriminant >= 0.0)

{

discriminant = sqrt(discriminant);

root = (b + discriminant) * -0.5 / a;

return true;

}

return false;

}

//----------------------------------------------------------------------------

bool solveCubic(double &a, double &b, double &c, double &d, double &root)

{

if(a == 0.0 || abs(a/b) < 1.0e-6)

return solveQuadratic(b, c, d, root);

double B = b/a, C = c/a, D = d/a;

double Q = (B*B - C*3.0)/9.0, QQQ = Q*Q*Q;

double R = (2.0*B*B*B - 9.0*B*C + 27.0*D)/54.0, RR = R*R;

// 3 real roots

if(RR<QQQ)

{

/* This sqrt and division is safe, since RR >= 0, so QQQ > RR, */

/* so QQQ > 0. The acos is also safe, since RR/QQQ < 1, and */

/* thus R/sqrt(QQQ) < 1. */

double theta = acos(R/sqrt(QQQ));

/* This sqrt is safe, since QQQ >= 0, and thus Q >= 0 */

double r1, r2, r3;

r1 = r2 = r3 = -2.0*sqrt(Q);

r1 *= cos(theta/3.0);

r2 *= cos((theta+2*PI)/3.0);

r3 *= cos((theta-2*PI)/3.0);

r1 -= B/3.0;

r2 -= B/3.0;

r3 -= B/3.0;

root = 1000000.0;

if(r1 >= 0.0) root = r1;

if(r2 >= 0.0 && r2 < root) root = r2;

if(r3 >= 0.0 && r3 < root) root = r3;

return true;

}

// 1 real root

else

{

double A2 = -pow(fabs®+sqrt(RR-QQQ),1.0/3.0);

if (A2!=0.0) {

if (R<0.0) A2 = -A2;

root = A2 + Q/A2;

}

root -= B/3.0;

return true;

}

}

//----------------------------------------------------------------------------

bool solveQuartic(double &a, double &b, double &c, double &d, double &e, double &root)

{

// When a or (a and b) are magnitudes of order smaller than C,D,E

// just ignore them entirely.

if(a == 0.0 || abs(a/b) < 1.0e-6 || abs(a/c) < 1.0e-6)

return solveCubic(b, c, d, e, root);

// Uses Ferrari's Method

double aa = a*a, aaa=aa*a, bb=b*b, bbb=bb*b;

double alpha = -3.0*bb/(8.0*aa) + c/a, alpha2 = alpha * alpha;

double beta = bbb/(8.0*aaa) + b*c/(-2.0*aa) + d/a;

double gamma = -3.0*bbb*b/(256.0*aaa*a) + c*bb/(16.0*aaa) + b*d/(-4.0*aa) + e/a;

if(beta == 0.0)

{

root = b/(-4.0*a) + sqrt(0.5 * (-alpha + sqrt(alpha2 + 4.0*gamma)));

return true;

}

else

{

std::complex<double> P = -alpha2/12.0 - gamma;

std::complex<double> Q = -alpha2*alpha/108.0 + alpha*gamma/3.0 - beta*beta/8.0;

std::complex<double> R = Q*0.5 + sqrt(Q*Q*0.25 + P*P*P/27.0);

std::complex<double> U = pow(R, 1.0/3.0);

std::complex<double> y = -5.0*alpha/6.0 - U;

if(U != 0.0) y += P/(3.0*U);

std::complex<double> W = sqrt(alpha + y + y);

std::complex<double> aRoot;

bool foundRealRoot = false;

double firstPart = b/(-4.0*a);

std::complex<double> secondPart = -3.0*alpha - 2.0*y;

std::complex<double> thirdPart = 2.0*beta/W;

aRoot = firstPart + 0.5 * (-W - sqrt(secondPart + thirdPart));

if(abs(aRoot.imag()) < 1.0e-10 && aRoot.real() >= 0.0)

{

root = aRoot.real();

foundRealRoot = true;

}

aRoot = firstPart + 0.5 * (-W + sqrt(secondPart + thirdPart));

if(abs(aRoot.imag()) < 1.0e-10 && aRoot.real() >= 0.0 &&

(!foundRealRoot || aRoot.real() < root))

{

root = aRoot.real();

foundRealRoot = true;

}

aRoot = firstPart + 0.5 * (W - sqrt(secondPart - thirdPart));

if(abs(aRoot.imag()) < 1.0e-10 && aRoot.real() >= 0.0 &&

(!foundRealRoot || aRoot.real() < root))

{

root = aRoot.real();

foundRealRoot = true;

}

aRoot = firstPart + 0.5 * (W + sqrt(secondPart - thirdPart));

if(abs(aRoot.imag()) < 1.0e-10 && aRoot.real() >= 0.0 &&

(!foundRealRoot || aRoot.real() < root))

{

root = aRoot.real();

foundRealRoot = true;

}

return foundRealRoot;

}

}

//----------------------------------------------------------------------------

That was such a pain to figure out, because I'm not particularly a math wiz. But it works. :-)

[EDIT] I realized that I should probably note that in any case where there are multiple roots, it tries to return the root that is closest to zero but above it. So if I had, say, root={-1, 1, 0.2}, then the algorithm returns 0.2. Assuming that ABCDE are terms that describe explicitly the movement of the two spheres over a given frame, then 0.2 is the most useful root, since it says that the spheres were able to use 20% of their motions.

Bonus note:

I'm not as sure how the math works out, but for my circle-circle test (2D collision test), I was able to reduce it to second order just by fixing one of the circles at the origin, and making its radius zero. The other circle had all of the motion and radius, so it ended up being a whenDoesCircleTouchOrigin test, which was much faster than 4th order.

###
#6
Members - Reputation: **145**

Posted 08 June 2007 - 03:15 AM

Quote:

Original post by dragongameQuote:

Original post by Zipster

Considering that a quartic is the highest degree polynomial with a closed-form solution . . .

That is not true! You can go up to 4th degree, but as stated the solution will be very complicated. I'm sorry but I can't give you the solutions without my math book.

Zipster is correct. Abel's impossibility theorem states that there is no general solution for polynomials equal or above fifth degree.

I think we're all talking about the same thing.

###
#7
Members - Reputation: **464**

Posted 08 June 2007 - 03:20 AM

###
#8
Members - Reputation: **100**

Posted 08 June 2007 - 03:40 AM

Quote:

Original post by pTymN

[EDIT] I realized that I should probably note that in any case where there are multiple roots, it tries to return the root that is closest to zero but above it. So if I had, say, root={-1, 1, 0.2}, then the algorithm returns 0.2. Assuming that ABCDE are terms that describe explicitly the movement of the two spheres over a given frame, then 0.2 is the most useful root, since it says that the spheres were able to use 20% of their motions.

I'm a little confused about that, because with my previous collision detection using position and velocity (second degree), although there were only two, I always took the - root (like from the +- sqrt()). I assumed when doing to the fourth degree, it would be only one of the roots that I'd use aswell.

Quote:

Original post by pTymN

Bonus note:

I'm not as sure how the math works out, but for my circle-circle test (2D collision test), I was able to reduce it to second order just by fixing one of the circles at the origin, and making its radius zero. The other circle had all of the motion and radius, so it ended up being a whenDoesCircleTouchOrigin test, which was much faster than 4th order.

Wait did you put acceleration in there too? Because I'm already doing what you said about the radius etc, and I can't see how you would reduce it to just the second degree.

thanks for the source!

btw I've managed to reduce the equation from:

ax^4 + bx^3 + cx^2 + dx + e = 0

to:

f = b/4a

x = u - f

g = -6f^2 + (c/a)

h = 8f^3 - 2(c/a)f + (d/a)

i = -3f^4 + (c/a)f^2 - (d/a)f + e/a

u^4 + gu^2 + hu + i = 0 //depressed quartic equation

From the wiki link i posted, but I lose them not long after that.

###
#9
Members - Reputation: **464**

Posted 08 June 2007 - 03:54 AM

Because my collision detection layer is not aware of "why" a particular corner or edge for an object moved, it doesn't know how linear or non-linear the motion of a particular boundary part was computed. The collision detection assumes that the object's motion is completely linear over the span of a frame, and that assumption has worked out very well after I changed the structure of the game object's code. I moved all accelerations into a method that is called after the collision detection phase, so when the collision detection tries scaling the object's motion down and retrying, it isn't returning linear time of collision for highly non-linear motions. The amount of error is huge, but the simulation looks great.

###
#10
Members - Reputation: **464**

Posted 08 June 2007 - 04:05 AM

Quote:

I'm a little confused about that, because with my previous collision detection using position and velocity (second degree), although there were only two, I always took the - root (like from the +- sqrt()). I assumed when doing to the fourth degree, it would be only one of the roots that I'd use aswell.

You know, I was only implementing this stuff to get something else working, but unlike quadratic, it does seem that the useful root isn't always the first or third one for quartic. When I got to the cubic code, I didn't even bother looking at that. If you do a more thorough investigation and find a useful pattern, I'd appreciate if you'd share your learnings. :-)

###
#11
Members - Reputation: **145**

Posted 08 June 2007 - 10:03 AM

There is a simpler solution than Ferrari's method.

Assume the equation: ax^4 + bx^3 + cx^2 + dx + e = 0

Simplify by dividing by a (this should always be done):

x^4 + Bx^3 + Cx^2 + Dx + E = 0

where

B = b/a

C = c/a

D = d/a

E = e/a

Solve this cubic equation for z: z^3 + 2Cz^2 + (C^2 - 4E)z - D^2 = 0

Solve the equations:

p = sqrt(z)

q = (c + z - d/p)/2

r = -sqrt(z)

s = (c + z + d/p)/2

Solve these quadratic equations to obtain the four roots:

x^2 + px + q = 0

x^2 + rx + s = 0

You'll have to understand that Ferrari discovered the first method, and it was complicated.

This is simpler, but I can't guarantee that it will have better performance, since you'll have to calculate the cubic and quadratic solutions.

If you're interested, the theory behind this can be found here.

###
#13
Members - Reputation: **145**

Posted 08 June 2007 - 12:20 PM

This is the correct method.

ax^4 + bx^3 + cx^2 + dx + e = 0 (=) Divide by a

x^4 + Bx^3 + Cx^2 + Dx + E = 0

B = b/a

C = c/a

D = d/a

E = e/a

(depressed quartic, like in Ferrari's method)

alpha = I = -3(B^2) / 8 + C

beta = J = (B^3)/8 - BC/2 + D

gamma = K = -3(B^4)/256 + C(B^2)/16 - BD/4 + E

Solve equation for z (one solution is enough):

z^3 + 2Iz^2 + (I^2 - 4K)z - J^2 = 0

p = sqrt(z)

r = -p

q = (I + z - J/p)/2

s = (I + z + J/p)/2

Solve (there are four u's, two for each equation):

u^2 + pu + q = 0

u^2 + ru + s = 0

Solution (x[]):

for each root u[i], x[i] = u[i] - B/4

[Edited by - Silver Phoenix on June 9, 2007 6:20:47 PM]

###
#14
Members - Reputation: **100**

Posted 09 June 2007 - 03:13 AM

Quote:

Original post by Silver Phoenix

Solve equation for z (one solution is enough):

z^3 + 2Iz^2 + (I^2 - 4K)z + J^2 = 0

I just kinda learned how to do the cubic equation, but I don't understand how there are only 3 roots, because I have the two +- from the quadratic equation,

but from that I'd assume you would be able to use the ++, --, +- and -+

eg heres the sqrt part of my cubic root calculations

u = (-B+-sqrt(BB+4AAA/27))/2

x = A/((3)cbrt(u)) - cbrt(u) - b/3a

btw x as in ax^3 + bx^2 + cx + d

thanks

[Edited by - johnnyBravo on June 9, 2007 9:13:46 AM]

###
#15
Members - Reputation: **145**

Posted 09 June 2007 - 12:38 PM

Quote:

Original post by johnnyBravo

I just kinda learned how to do the cubic equation, but I don't understand how there are only 3 roots, because I have the two +- from the quadratic equation,

but from that I'd assume you would be able to use the ++, --, +- and -+

A polynomial of degree N will always have no more than N roots. So a cubic equation cannot have more than 3 roots.

You only need one root from the three that the cubic equation gives. The final solution will always be equal (not taking precision loss into account).

For each different cubic solution there will be different p, q, r and s.

These different solutions give different second degree polynomials, but there's an invariants:

(u^2 + pu + q)*(u^2 + ru + s) = 0 (=)

(=) u^4 + (p + r)u^3 + (pr + q + s)u^2 + (ps + qr)u + qs = 0

For simplicity we will just focus on the simpler monomials: (p + r)u^3 and qs

If we take any two solutions from the cubic, then if one solution decreases p, the other will have to increase r, because (p + r) is always constant.

Actually, by definition r = -p => p + r = 0 (depressed quartic).

The same goes for qs. If two solution give q with opposite signs , then s will also have opposite signs for the two solutions.

Also remember that we solved a system of equations with p, q, r and s on the left side.

Quote:

Original post by johnnyBravo

btw x as in ax^3 + bx^2 + cx + d

x is the final solution. The solution for:

ax^4 + bx^3 + cx^2 + dx + e = 0

(or x^4 + Bx^3 + Cx^2 + Dx + E = 0, since they have the same roots)

Some notes:

There was a small but fatal mistake in the z equation. I already corrected it.

Compare edited with:

Quote:

Original

z^3 + 2Iz^2 + (I^2 - 4K)z + J^2 = 0

It is possible to simplify the cubic if K or J is zero.

If z is zero => p = sqrt(z) = 0 => J/p = J/0 (divide by zero)

###
#16
Crossbones+ - Reputation: **5674**

Posted 09 June 2007 - 01:42 PM

In Z(8),

x^2 - 1 = 0 has roots 1, 3, 5, and 7 (mod 8).

Of course, Z(8) has elements that are proper divisors of zero, i.e. a*b = 0 although a != 0 and b != 0. (namely, 2 and 4 (mod 8)).

###
#17
Members - Reputation: **100**

Posted 10 June 2007 - 02:33 AM

I ended up making classes out of the cubic and quartic, they even return compex number roots.

Btw I found some really good step by step examples for the cubic and quartic.

That place has a bunch of other things there too.

Quote:

Original post by pTymN

If you do a more thorough investigation and find a useful pattern, I'd appreciate if you'd share your learnings. :-)

I'm going to be working on that now, ill let u know how it goes.

###
#18
Members - Reputation: **464**

Posted 13 June 2007 - 12:14 PM

bool solveQuadraticOther(double a, double b, double c, double &root)

{

if(a == 0.0 || abs(a/b) < 1.0e-4)

{

if(abs(b) < 1.0e-4)

return false;

else

{

root = -c/b;

return true;

}

}

double discriminant = b * b - 4.0 * a * c;

if(discriminant >= 0.0)

{

discriminant = sqrt(discriminant);

root = (b - discriminant) * -0.5 / a;

return true;

}

return false;

}

//----------------------------------------------------------------------------

bool solveQuartic(double a, double b, double c, double d, double e, double &root)

{

// I switched to this method, and it seems to be more numerically stable.

// http://www.gamedev.net/community/forums/topic.asp?topic_id=451048

// When a or (a and b) are magnitudes of order smaller than C,D,E

// just ignore them entirely. This seems to happen because of numerical

// inaccuracies of the line-circle algorithm. I wanted a robust solver,

// so I put the fix here instead of there.

if(a == 0.0 || abs(a/b) < 1.0e-5 || abs(a/c) < 1.0e-5 || abs(a/d) < 1.0e-5)

return solveCubic(b, c, d, e, root);

double B = b/a, C = c/a, D = d/a, E = e/a;

double BB = B*B;

double I = -3.0*BB*0.125 + C;

double J = BB*B*0.125 - B*C*0.5 + D;

double K = -3*BB*BB/256.0 + C*BB/16.0 - B*D*0.25 + E;

double z;

bool foundRoot2 = false, foundRoot3 = false, foundRoot4 = false, foundRoot5 = false;

if(solveCubic(1.0, I+I, I*I - 4*K, -(J*J), z))

{

double value = z*z*z + z*z*(I+I) + z*(I*I - 4*K) - J*J;

double p = sqrt(z);

double r = -p;

double q = (I + z - J/p)*0.5;

double s = (I + z + J/p)*0.5;

bool foundRoot = false, foundARoot;

double aRoot;

foundRoot = solveQuadratic(1.0, p, q, root);

root -= B/4.0;

foundARoot = solveQuadratic(1.0, r, s, aRoot);

aRoot -= B/4.0;

if((foundRoot && foundARoot && ((aRoot < root && aRoot >= 0.0)

|| root < 0.0)) || (!foundRoot && foundARoot))

{

root = aRoot;

foundRoot = true;

}

foundARoot = solveQuadraticOther(1.0, p, q, aRoot);

aRoot -= B/4.0;

if((foundRoot && foundARoot && ((aRoot < root && aRoot >= 0.0)

|| root < 0.0)) || (!foundRoot && foundARoot))

{

root = aRoot;

foundRoot = true;

}

foundARoot = solveQuadraticOther(1.0, r, s, aRoot);

aRoot -= B/4.0;

if((foundRoot && foundARoot && ((aRoot < root && aRoot >= 0.0)

|| root < 0.0)) || (!foundRoot && foundARoot))

{

root = aRoot;

foundRoot = true;

}

return foundRoot;

}

return false;

}

This method is MUCH more stable than Ferrari's method. I highly encourage its use.

[Edited by - pTymN on June 13, 2007 11:14:04 PM]