40 #define CUBIC_ZERO 1.0e-100 43 void cubic(
double a,
double b,
double c,
double d,
double *r1,
double *r2,
double *r3,
int *num)
56 double aa, p, q, D, u, v, phi;
61 if ( fabs(a) <=
norm ) {
62 if ( fabs(b) <=
norm ) {
63 if ( fabs(c) <=
norm ) {
64 if ( fabs(d) <=
norm ) {
78 if ( ( D = c * c - 4.0 * b * d ) < 0.0 ) {
84 if ( fabs(c) <
norm ) {
96 help = -( c +
sgn(c) * sqrt(D) ) / 2.0;
105 a = b / ( aa * 3.0 );
109 q = 2.0 * a * a * a - a * b + c;
110 D = q * q / 4.0 + p * p * p / 27.0;
118 * r2 =
cbrt(q / 2.0) - a;
119 * r1 = -2.0 * * r2 - a;
124 u = -q / 2.0 + sqrt(D);
131 p = sqrt(fabs(p) / 3.0);
132 help = ( -q / ( 2.0 * p * p * p ) );
133 if ( fabs(help) > 1.0 ) {
137 phi = acos(help) / 3.0;
138 double cp = cos(phi);
139 double sp = sqrt(3.0) * sin(phi);
140 * r1 = 2 * p * cp - a;
141 * r2 = -p * ( cp + sp ) - a;
142 * r3 = -p * ( cp - sp ) - a;
155 void cubic3r(
double a,
double b,
double c,
double d,
double *r1,
double *r2,
double *r3,
int *num)
168 double aa, r, q, p, D, phi;
182 if ( ( D = c * c - 4.0 * b * d ) < 0.0 ) {
200 help = -( c +
sgn(c) * sqrt(D) ) / 2.0;
214 q = ( a * a - 3.0 * b ) / 9.0;
215 r = ( 2.0 * a * a * a - 9.0 * a * b + 27.0 * c ) / 54.0;
229 help = r / sqrt(q * q * q);
230 if ( fabs(help) > 1.0 ) {
237 * r1 = -2.0 *p *cos(phi / 3.0) - a / 3.0;
238 * r2 = -2.0 *p *cos( ( phi + 2.0 *
M_PI ) / 3.0 ) - a / 3.0;
239 * r3 = -2.0 *p *cos( ( phi - 2.0 *
M_PI ) / 3.0 ) - a / 3.0;
267 return ( val + 1 > rank ? 1 : val + 1 );
277 double f1 = 0, f2 = 0, f3 = 0;
278 double sx = 0, sx2 = 0, sx3 = 0, sx4 = 0;
279 double detCi, Ci11, Ci12, Ci13, Ci22, Ci23, Ci33;
281 double yi, xi, xi2, xi3, xi4;
282 for (
int i = 0; i < n; ++i ) {
303 Ci11 = sx2 * sx4 - sx3 * sx3;
304 Ci12 = sx2 * sx3 - sx * sx4;
305 Ci13 = sx * sx3 - sx2 * sx2;
306 Ci22 = n * sx4 - sx2 * sx2;
307 Ci23 = sx * sx2 - n * sx3;
308 Ci33 = n * sx2 - sx * sx;
309 detCi = 1 / ( n * Ci11 + sx * Ci12 + sx2 * Ci13 );
310 a(0) = ( Ci11 * f1 + Ci12 * f2 + Ci13 * f3 ) * detCi;
311 a(1) = ( Ci12 * f1 + Ci22 * f2 + Ci23 * f3 ) * detCi;
312 a(2) = ( Ci13 * f1 + Ci23 * f2 + Ci33 * f3 ) * detCi;
313 }
else if ( n == 2 ) {
315 a(1) = ( y(1) - y(0) ) / ( x(1) - x(0) );
316 a(0) = y(0) - a(1) * x(0);
317 }
else if ( n == 1 ) {
329 }
else if ( i > 0 ) {
double sgn(double i)
Returns the signum of given value (if value is < 0 returns -1, otherwise returns 1) ...
void cubic(double a, double b, double c, double d, double *r1, double *r2, double *r3, int *num)
Solves cubic equation for real roots.
void cubic3r(double a, double b, double c, double d, double *r1, double *r2, double *r3, int *num)
Solves cubic equation for real roots, assuming that if cubic polynomial given then the only possibili...
int iperm(int val, int rank)
Returns iperm of val, in specific rank.
void ls2fit(const FloatArray &x, const FloatArray &y, FloatArray &a)
Least-square fit of 2nd degree polynomial .
Class representing vector of real numbers.
double cbrt(double x)
Returns the cubic root of x.
double norm(const FloatArray &x)
void zero()
Zeroes all coefficients of receiver.
double signum(double i)
Returns the signum of given value (i = 0 returns 0, i < 0 returns -1, i > 0 returns 1) ...
int giveSize() const
Returns the size of receiver.
the oofem namespace is to define a context or scope in which all oofem names are defined.
void resize(int s)
Resizes receiver towards requested size.