Solve quartic and cubic equations-.
More...
#include "pluto.h"
#include "complex.h"
Go to the source code of this file.
|
#define | swap(x, y) f = x; x = y; y = f; |
|
#define | ZQUARTIC(x) (ez + (x)*(dz + (x)*(cz + (x)*(bz + (x))))) |
|
|
int | QuarticSolve (double b, double c, double d, double e, double *z) |
|
int | CubicSolve (double b, double c, double d, double z[]) |
|
int | QuarticSolve2 (double b, double c, double d, double e, double *z) |
|
Solve quartic and cubic equations-.
- Author
- A. Mignone (migno.nosp@m.ne@p.nosp@m.h.uni.nosp@m.to.i.nosp@m.t)
- Date
- Sept 10, 2012
Definition in file quartic.old.c.
#define swap |
( |
|
x, |
|
|
|
y |
|
) |
| f = x; x = y; y = f; |
#define ZQUARTIC |
( |
|
x | ) |
(ez + (x)*(dz + (x)*(cz + (x)*(bz + (x))))) |
int CubicSolve |
( |
double |
b, |
|
|
double |
c, |
|
|
double |
d, |
|
|
double |
z[] |
|
) |
| |
Solve a cubic equation in the form
For its purpose, it is assumed that ALL roots are double. This makes things faster.
- Parameters
-
[in] | b | coefficient of the cubic |
[in] | c | coefficient of the cubic |
[in] | d | coefficient of the cubic |
[out] | z | vector containing the roots of the cubic. Roots should be sorted in increasing order. |
Reference: http://www.1728.com/cubic2.htm
- Returns
- Return 0 on success.
Definition at line 147 of file quartic.old.c.
170 double i, i2,
j,
k, m,
n, p;
171 static double one_3 = 1.0/3.0, one_27=1.0/27.0;
182 f =
c*(1.0 - 1.e-16) - b2*one_3;
183 g = b*(2.0*b2 - 9.0*
c)*one_27 + d;
216 k = (k < -1.0 ? -1.0:
k);
217 k = (k > 1.0 ? 1.0:
k);
222 n = sqrt(3.0)*sin(k);
225 z[0] = -j*(m +
n) + p;
226 z[1] = -j*(m -
n) + p;
int QuarticSolve |
( |
double |
b, |
|
|
double |
c, |
|
|
double |
d, |
|
|
double |
e, |
|
|
double * |
z |
|
) |
| |
Solve a quartic equation in the form
For its purpose, it is assumed that ALL roots are double. This makes things faster.
- Parameters
-
[in] | b | coefficient of the quartic |
[in] | c | coefficient of the quartic |
[in] | d | coefficient of the quartic |
[in] | e | coefficient of the quartic |
[out] | z | vector containing the (double) roots of the quartic |
Reference:
http://www.1728.com/quartic2.htm
- Returns
- Return 0 on success, 1 if cubic solver fails, 2 if NaN has been found and 3 if quartic is not satisfied.
Definition at line 21 of file quartic.old.c.
48 double a2, a1, a0, u[4];
50 static double three_256 = 3.0/256.0;
51 static double one_64 = 1.0/64.0;
56 g = d + b2*b*0.125 - b*
c*0.5;
57 h = e - b2*b2*three_256 + 0.0625*b2*
c - 0.25*b*d;
60 a1 = (f*f - 4.0*h)*0.0625;
71 z[0] = z[2] = - p -
s;
72 z[1] = z[3] = + p -
s;
82 z[0] = - p - q + r -
s;
84 z[2] = - p + q - r -
s;
98 if (z[1] < z[0]) {
swap(z[1],z[0]);}
99 else if (z[1] > z[3]) {
swap(z[1],z[3]);}
105 }
else if (z[2] < z[1]){
107 }
else if (z[2] > z[3]){
128 for (n = 0; n < 4; n++){
129 s = e + z[
n]*(d + z[
n]*(
c + z[
n]*(b + z[
n])));
134 if (fabs(s) > 1.e-6) {
int CubicSolve(double b, double c, double d, double z[])
int QuarticSolve2 |
( |
double |
b, |
|
|
double |
c, |
|
|
double |
d, |
|
|
double |
e, |
|
|
double * |
z |
|
) |
| |
Solve a quartic equation in the form
using Durand-Kerner method.
- Parameters
-
[in] | b | coefficient of the quartic |
[in] | c | coefficient of the quartic |
[in] | d | coefficient of the quartic |
[in] | e | coefficient of the quartic |
[out] | z | vector containing the (double) roots of the quartic |
Reference:
- Returns
- Return 0 on success, 1 if cubic solver fails, 2 if NaN has been found and 3 if quartic is not satisfied.
Definition at line 247 of file quartic.old.c.
272 double complex bz = b;
273 double complex cz =
c;
274 double complex dz = d;
275 double complex ez = e;
276 double complex p,
q, r,
s;
277 double complex dp, dq, dr, ds;
278 double acc = 1.e-5, err;
280 p = -1.0 + 0.000012*_Complex_I;
281 q = -0.25 + 0.00002*_Complex_I;
282 r = 0.25 + 0.00002*_Complex_I;
283 s = 1.0 - 0.000011*_Complex_I;
285 for (k = 0; k < 4096; k++){
290 dq =
ZQUARTIC(q)/( (q - p)*(q - r)*(q -
s) );
293 dr =
ZQUARTIC(r)/( (r - p)*(r - q)*(r -
s) );
296 ds =
ZQUARTIC(s)/( (s - p)*(s - q)*(s - r) );
299 err =
MAX(cabs(dp), cabs(dq));
300 err =
MAX(err, cabs(dr));
301 err =
MAX(err, cabs(ds));
303 if (err < acc)
break;
320 zmax =
MAX(z[0],z[1]);
321 zmax =
MAX(zmax,z[2]);
322 zmax =
MAX(zmax,z[3]);
324 zmin =
MIN(z[0],z[1]);
325 zmin =
MIN(zmin,z[2]);
326 zmin =
MIN(zmin,z[3]);