30 cout <<
"Se a=0, a equacao nao e' do terceiro grau.";
37 Delta = b * b - 4 * a * c;
39 raiz[1] = (-1 * b +
sqrt(Delta)) / (2 * a);
40 raiz[2] = (-1 * b -
sqrt(Delta)) / (2 * a);
42 sort(&raiz[0], &raiz[3], greater<REAL>());
46 raiz[1] = (-1 * b) / (2 * a);
48 imagem =
fabs(Delta) / (2 * a);
53 raiz[1] = (-1 * b) / (2 * a);
62 REAL p = B - A * A / 3.;
63 REAL q = C - A * B / 3. + 2. * A * A * A / 27.;
64 REAL D = q * q / 4. + p * p * p / 27.;
65 REAL pi = 3.1415926535897932384626;
71 REAL r =
sqrt(q * q / 4. + M * M);
72 REAL t =
acos(-q / 2. / r);
73 raiz[0] = 2.*
pow( r, (REAL) (1. / 3.)) *
cos(t / 3.) - A / 3;
74 raiz[1] = 2.*
pow( r, (REAL) (1. / 3.)) *
cos((t + 2. * pi) / 3.) - A / 3.;
75 raiz[2] = 2.*
pow( r, (REAL) (1. / 3.)) *
cos((t + 4. * pi) / 3.) - A / 3.;
77 sort(&raiz[0], &raiz[3], greater<REAL>());
82 REAL u3 = -q / 2. +
sqrt(D);
84 u = -
pow((-u3), (REAL)(1./3.));
87 u =
pow(u3, (REAL)(1./3.));
89 REAL v3 = -q / 2. -
sqrt(D);
91 v = -
pow((-v3), (REAL)(1./3.));
94 v =
pow(v3, (REAL)(1./3.));
96 raiz[0] = u + v - A / 3.;
97 Delta = (A + raiz[0]) * (A + raiz[0]) + 4 * C / raiz[0];
98 REAL Real = -(A + raiz[0]) / 2.0;
100 REAL Imag =
sqrt(K) / 2.0L;
109 raiz[1] = Real + Imag;
110 raiz[2] = Real - Imag;
112 sort(&raiz[0], &raiz[3], greater<REAL>());
127 for (i = 0; i < 4; i++) {
128 if (
fabs(fCo[i]) > max) max =
fabs(fCo[i]);
130 max =
fabs(max * 1e-6L);
132 Tartaglia(fCo, fReal, fImagem);
140 for (i = 0; i < 4; i++) {
142 if (
fabs(fCo[i]) > max) max =
fabs(fCo[i]);
146 Tartaglia(fCo, fReal, fImagem);
152 for (i = 0; i < 3; i++)
153 if (fCo[i] == -99.999) {
154 cout <<
"TPZPolynomial::GetRoots.\nERRO: Coeficientes ainda nao foram especificados.\n";
176 for (i = 0; i < 4; i++)
177 if (
fabs(fCo[i]) < fTolerance) zero++;
179 for (i = 0; i < 3; i++) fReal[i] = 0.000;
183 if (
fabs(fCo[0]) < fTolerance &&
fabs(fCo[1]) < fTolerance) {
184 X[0] = fCo[2] / fCo[3];
188 sort(&X[0], &X[3], greater<REAL>());
194 for (i = 0; i < 3; i++) X[i] = -99.999;
199 der = 3.0 * fCo[3] * x * x + 2.0 * fCo[2] * x + fCo[1];
200 if (
fabs(der) < fTolerance) j = 10002;
201 else X[0] = x - ((fCo[3] * x * x * x + fCo[2] * x * x + fCo[1] * x + fCo[0]) / der);
202 tol =
fabs(x - X[0]);
203 if (tol <= fTolerance) j = 10001;
207 if (j == 10002 || j == 10000) {
212 der = 3.0 * fCo[3] * x * x + 2.0 * fCo[2] * x + fCo[1];
213 if (
fabs(der) <= fTolerance) j = 10002;
214 else X[0] = x - ((fCo[3] * x * x * x + fCo[2] * x * x + fCo[1] * x + fCo[0]) / der);
215 tol =
fabs(x - X[0]);
216 if (tol <= fTolerance) j = 1001;
220 if (j == 10002 || j == 10000) {
221 cout <<
"TPZPolynomial::SetRoots().\nErro no calculo das raizes!!!!! j= " << j <<
"\n";
226 teste = fCo[3] * X[0] * X[0] * X[0] + fCo[2] * X[0] + fCo[1] * X[0] + fCo[0];
227 cout <<
"Resultado da 1a raiz na equacao" << teste <<
"\n";
232 b2 = fCo[2] + X[0] * b3;
233 b1 = fCo[1] + X[0] * b2;
237 delta = b2 * b2 - 4.0 * b1 * b3;
239 cerr <<
"TPZPolynomial::SetRoots() - warning: Raizes nao reais!! Delta =" << delta <<
"\t" << X[0] <<
"\n";
240 cout << fCo[3] <<
"x >= + " << fCo[2] <<
"x <= + " << fCo[1] <<
"x + " << fCo[0] <<
" = 0.0\n";
241 cout << b3 <<
"x<= + " << b2 <<
"x + " << b1 <<
" = 0.0\n";
244 sort(&X[0], &X[3], greater<REAL>());
248 X[1] = (-b2 -
sqrt(delta)) / (2.0 * b3);
249 X[2] = (-b2 +
sqrt(delta)) / (2.0 * b3);
251 sort(&X[0], &X[3], greater<REAL>());
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ fabs
TPZVec< REAL > fReal
Roots of the polynomial.
void GetTolerance(REAL &tol)
Gets the tolerance value.
TPZVec< REAL > fCo
Polynomial coefficients.
void SetCoef(const REAL &c0, const REAL &c1, const REAL &c2, const REAL &c3)
Sets up four coefficients to polynomial.
expr_ expr_ expr_ expr_ acos
void SetTolerance(const REAL &tol)
REAL fTolerance
Tolerance value to computes.
TPZPlasticIntegrMem< REAL, 4 > teste
TPZPolynomial()
Default constructor.
Free store vector implementation.
int SetRoots()
Computes the roots of the polynomial and stores into the fReal.
expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ expr_ sqrt
int GetRoots(const TPZVec< REAL > &coef, TPZVec< REAL > &r)
On given coefficients computes the roots of the polynomial in r.
Contains declaration of the TPZPolynomial class which implements a polynomial.
void SetTolerance(const REAL &tol)
Sets the tolerance value to computes.
TPZFlopCounter pow(const TPZFlopCounter &orig, const TPZFlopCounter &xp)
Returns the power and increments the counter of the power.
Contains declaration of the TPZNumeric class which implements several methods to calculation.
TPZFlopCounter cos(const TPZFlopCounter &orig)
Returns the cosine in radians and increments the counter of the Cosine.
int Tartaglia(const TPZVec< REAL > &coef, TPZVec< REAL > &real, REAL &imagem)
Computes the roots of the cubic polynomial using Tartaglia method (until 3 degree) ...