35 rep (
NONE), d (0), krts (0), p (NULL), rts (NULL) {
40 rep (
NONE), d (o), krts (0), p (NULL), rts (NULL) {
48 std::cout <<
"qf_poly (ax^2+bx+c), a = " << a <<
", b = " << b
49 <<
", c = " << c <<
", d° = " << deg <<
"\n";
53 if ((deg == 2) && (a == 0)) {
58 if ((deg == 1) && (a == 0)) {
89 std::cout <<
"Warning: qf_poly called with deg > 2.\n";
102 }
else if (dlt > 0) {
117 #ifdef _QF_POLY_DEBUG
118 std::cout <<
"qf_poly ax^2+bx+c: ";
125 rep (
COEFF), d (o), krts (0), rts (NULL) {
128 for (
int i = o;
i >= 0;
i--) p[
i] = coef[o -
i];
130 #ifdef _QF_POLY_DEBUG
131 std::cout <<
"qf_poly coeffs: ";
142 rep (
ROOTS), d (o), p (NULL) {
145 for (
int i = 0;
i < 2 * o;
i++)
149 #ifdef _QF_POLY_DEBUG
150 std::cout <<
"qf_poly (roots): ";
158 rep (P.rep), d (P.d), krts (0), p (NULL), rts (NULL) {
180 if (p != NULL)
delete[] p;
181 if (rts != NULL)
delete[] rts;
191 memcpy (rts, P.rts, sizeof (
qf_double_t) * (2 * d));
199 if (p != NULL)
delete[] p;
200 if (rts != NULL)
delete[] rts;
209 std::cout <<
"qf_poly::[] used on a NONE polynom.\n";
224 std::cout <<
"qf_poly::k () used on a NONE polynom.\n";
242 std::cout <<
"qf_poly::spl () used on a NONE polynom.\n";
269 std::cout <<
"qf_poly::unary - used on a NONE polynom.\n";
277 for (
unsigned i = 0;
i <= d;
i++)
292 if ((Q.rep ==
NONE) || (P.rep ==
NONE)) {
293 std::cout <<
"qf_poly::+ used on a NONE polynom.\n";
309 if ((rep ==
NONE) || (P.rep ==
NONE)) {
310 std::cout <<
"qf_poly::+= used on a NONE polynom.\n";
323 for (
unsigned i = 0;
i <= P.d;
i++)
329 for (
unsigned i = 0;
i <= d;
i++)
347 if ((P.rep ==
NONE) || (Q.rep ==
NONE)) {
348 std::cout <<
"qf_poly::- used on a NONE polynom.\n";
364 if ((rep ==
NONE) || (P.rep ==
NONE)) {
365 std::cout <<
"qf_poly::-= used on a NONE polynom.\n";
376 for (
unsigned i = 0;
i <= P.d;
i++)
381 memcpy (pp, P.p, sizeof (
qf_double_t) * (P.d + 1));
382 for (
unsigned i = 0;
i <= P.d;
i++)
384 pp[
i] = p[
i] - pp[
i];
403 if ((P.rep ==
NONE) || (Q.rep ==
NONE)) {
404 std::cout <<
"qf_poly::* used on a NONE polynom.\n";
416 std::cout <<
"qf_poly::* (scalar) used on a NONE polynom.\n";
427 if ((rep ==
NONE) || (P.rep ==
NONE)) {
428 std::cout <<
"qf_poly::*= () used on a NONE polynom.\n";
435 return ((*
this) *= P.p[0]);
437 return ((*
this) *= P.krts);
442 if (!(P.rep & COEFF)) P.
to_coeff ();
444 memset (q, 0,
sizeof (
qf_double_t) * (d + P.d + 1));
445 for (
unsigned i = 0;
i <= d;
i++)
446 for (
unsigned j = 0; j <= P.d; j++)
447 q[
i + j] += p[
i] * P.p[j];
454 if (!(P.rep & ROOTS)) P.
to_roots ();
457 memcpy (&rtsp[2 * d], P.rts, sizeof (
qf_double_t) * 2 * P.d);
470 std::cout <<
"qf_poly::*= (scalar) used on a NONE polynom.\n";
487 for (
unsigned i = 0;
i <= d;
i++)
516 for (
unsigned i = 0;
i <= d;
i++)
524 return !((*this) == P);
529 std::cout <<
"Warning qf_poly::is_null() on a NONE polynom.\n";
548 std::cout <<
"qf_poly::<< used on a NONE polynom.\n";
559 return qf_poly (p[d], 0, 0, 0);
564 for (
unsigned i = 0;
i <
n;
i++)
566 std::cout <<
"Warning: << by " << n <<
" asked for but only "
567 <<
i <<
" possible.\n";
573 memcpy (R.p, &(p[n]), sizeof (
qf_double_t) * (d - n + 1));
583 for (
unsigned i = 0, j = 0;
i < 2 * d;
i += 2) {
584 if ((rts[
i] == 0) && (rts[
i + 1] == 0) && (nbz != 0))
589 R.rts[j + 1] = rts[
i + 1];
598 std::cout <<
"Warning: << by " << n <<
" asked for but only "
599 << n - nbz <<
" possible.\n";
611 std::cout <<
"qf_poly::>> used on a NONE polynom.\n";
623 memcpy (&(R.p[n]), p, sizeof (
qf_double_t) * (d + 1));
629 memcpy (&(R.rts[2 * n]), rts, sizeof (
qf_double_t) * 2 * d);
642 std::cout <<
"qf_poly::odd () used on a NONE polynom.\n";
656 for (; i >= 0; i -= 2)
672 std::cout <<
"qf_poly::even () used on a NONE polynom.\n";
690 for (; i >= 1; i -= 2)
704 std::cout <<
"qf_poly::mnx () used on a NONE polynom.\n";
712 for (
unsigned i = 0;
i <= d;
i++)
713 P.p[
i] = ((
i % 2) == 0 ? p[
i] : -p[
i]);
719 for (
unsigned i = 0;
i < 2 * d;
i++)
722 P.krts = ((d % 2) == 0 ? krts : -krts);
732 std::cout <<
"qf_poly::hsq () used on a NONE polynom.\n";
746 std::cout <<
"qf_poly::sqr () used on a NONE polynom.\n";
757 std::cout <<
"Error! qf_poly::sqr () used on a non-square polynom.\n";
765 for (
unsigned i = 0;
i <= d / 2;
i++)
779 std::cout <<
"qf_poly::div () used on a NONE polynom.\n";
788 std::cout <<
"Warning: Div () called on a constant polynom.\n";
792 if ((d == 1) && (i != 0)) {
793 std::cout <<
"Div () real/complex error.\n";
823 std::cout <<
"Warning: Div () error. Specified factor not found.\n";
838 for (
unsigned k = 0, j = 0;
k < 2 * d;
k += 2) {
839 #ifdef _QF_POLY_DEBUG
840 std::cout <<
"Div: " << fabs (rts[
k] - r) <<
" "
841 << fabs (rts[
k + 1] - i) <<
"\n";
846 rtsp[j + 1] = rts[
k + 1];
861 std::cout <<
"Div () : factor not found! \n";
882 std::cout <<
"dN: " << dN <<
" dD : " << dD <<
'\n';
884 bool * Ln =
new bool[dN];
885 bool * Ld =
new bool[dD];
888 for (i = 0; i < dN; i++)
891 for (i = 0; i < dD; i++)
905 for (i = 0; i < 2 * dN; i += 2) {
906 for (j = 0; j < 2 * dD; j += 2) {
907 std::cout <<
"N.rts[" << i <<
"] = " << N.rts[
i] <<
", ";
908 std::cout <<
"D.rts[" << j <<
"] = " << D.rts[j] <<
"\n";
909 std::cout <<
"N.rts[" << i + 1 <<
"] = " << N.rts[i + 1] <<
", ";
910 std::cout <<
"D.rts[" << j + 1 <<
"] = " << D.rts[j + 1] <<
"\n";
912 (fabs (N.rts[i] - D.rts[j]) <
ROOT_TOL) &&
913 (fabs (N.rts[i + 1] - D.rts[j + 1]) <
ROOT_TOL)) {
918 std::cout <<
"Common root: (" << D.rts[j]
919 <<
", " << D.rts[j + 1] <<
"i)\n";
929 for (i = 0, j = 0; i < 2 * dN; i += 2) {
932 nrN[j + 1] = N.rts[i + 1];
942 for (i = 0, j = 0; i < 2 * D.d; i += 2) {
945 nrD[j + 1] = D.rts[i + 1];
957 std::cout <<
"ndN: " << ndN <<
" ndD : " << ndD <<
'\n';
967 std::cout <<
"qf_poly::hurw () used on a NONE polynom.\n";
977 for (
unsigned i = 0;
i < 2 * d;
i += 2) {
989 rtsp[j + 1] = rts[
i + 1];
1007 std::cout <<
"qf_poly::eval () used on a NONE polynom.\n";
1018 for (
int i = d - 1;
i >= 0;
i--)
1029 for (
unsigned i = 0;
i < 2 * d;
i += 2) {
1030 if (rts[
i + 1] == 0)
1037 r *= (a * a + n * a + m);
1047 return (
sqr ()).eval (c);
1050 #ifdef _QF_POLY_DEBUG
1055 std::cout << name <<
"(x) is not initalized.\n";
1060 std::cout << name <<
"(x) = ";
1065 std::cout << name <<
"(x) = ";
1071 #endif // _QF_POLY_DEBUG
1075 std::cout << p[0] <<
'\n';
1082 if (fabs (p[d]) != 1)
1083 std::cout << fabs (p[d]);
1090 std::cout <<
" x^" << d <<
' ';
1092 for (
unsigned i = d - 1;
i > 1;
i--) {
1101 if (fabs (p[
i]) != 1)
1102 std::cout << fabs (p[i]);
1104 std::cout <<
" x^" << i <<
' ';
1113 if (fabs (p[1]) != 1)
1114 std::cout << fabs (p[1]);
1126 std::cout << fabs (p[0]);
1137 std::cout << krts <<
' ';
1139 for (
unsigned i = 0;
i < 2 * d;
i += 2) {
1140 if (rts[
i + 1] == 0) {
1146 else if (rts[
i] < 0)
1147 std::cout <<
" + " << fabs (rts[
i]) <<
") ";
1150 std::cout <<
" - " << rts[
i] <<
") ";
1153 std::cout <<
"(X^2 ";
1159 std::cout <<
"- " << n <<
"X ";
1162 std::cout <<
"+ " << fabs (n) <<
"X ";
1164 std::cout <<
"+ " << m <<
") ";
1176 std::cout <<
"qf_poly::to_coeff () used on a NONE polynom.\n";
1191 if ((rts == NULL) || (d == 0))
1196 p[0] = -krts * rts[0];
1204 if (rts[2 * r + 1] == 0) {
1208 memcpy (&(q[1]), p,
sizeof (
qf_double_t) * (r + 1));
1210 for (
unsigned j = 0; j < r + 1; j++)
1211 q[j] -= rts[2 * r] * p[j];
1223 m = rts[2 * r] * rts[2 * r] + rts[2 * r + 1] * rts[2 * r + 1];
1224 n = -2 * rts[2 * r];
1226 q[0] = q[1] = s[0] = 0;
1228 memcpy (&(q[2]), p,
sizeof (
qf_double_t) * (r + 1));
1229 memcpy (&(s[1]), p,
sizeof (
qf_double_t) * (r + 1));
1231 for (
unsigned j = 0; j < r + 1; j++)
1232 q[j] += n * s[j] + m * p[j];
1234 q[r + 1] += n * s[r + 1];
1244 (*this).disp (
"qf_poly::to_coeff: ");
1254 std::cout <<
"qf_poly::to_roots () used on a NONE polynom.\n";
1278 status = qf_qrc (m, rts);
1281 for (
unsigned i = 0;
i < 2 * d;
i++) {
1298 for (i = 0; i < m.
n; i++)
1299 for (j = 0; j < m.
n; j++)
1302 for (i = 1; i < m.
n; i++)
1305 for (i = 0; i < m.
n; i++)
1306 m (i, m.
n - 1) = -p[
i] / p[m.
n];
1311 int not_converged = 1;
1315 while (not_converged) {
1321 for (i = 0; i < m.
n; i++) {
1324 col_norm = fabs (m (i + 1, i));
1330 for (j = 0; j < m.
n - 1; j++) {
1331 col_norm += fabs (m (j, m.
n - 1));
1337 row_norm = fabs (m (0, m.
n - 1));
1340 else if (i == m.
n - 1) {
1341 row_norm = fabs (m (i, i - 1));
1345 row_norm = fabs (m (i, i - 1)) + fabs (m (i, m.
n - 1));
1348 if ((col_norm == 0) || (row_norm == 0)) {
1352 g = row_norm /
RADIX;
1354 s = col_norm + row_norm;
1356 while (col_norm < g) {
1361 g = row_norm *
RADIX;
1363 while (col_norm > g) {
1368 if ((row_norm + col_norm) < 0.95 * s * f) {
1372 m (0, m.
n - 1) *= g;
1377 m (i, m.
n - 1) *= g;
1381 for (j = 0; j < m.
n; j++) {
1396 unsigned int iterations,
e,
i, j,
k, m;
1408 for (e = n; e >= 2; e--) {
1413 if (a1 <=
EPSILON * (a2 + a3))
1417 x = h (n - 1, n - 1);
1425 y = h (n - 2, n - 2);
1426 w = h (n - 2, n - 1) * h (n - 1, n - 2);
1431 y =
sqrt (fabs (q));
1458 if (iterations % 10 == 0 && iterations > 0) {
1462 for (i = 0; i <
n; i++) {
1466 s = fabs (h (n - 1, n - 2)) + fabs (h (n - 2, n - 3));
1469 w = -0.4375 * s *
s;
1474 for (m = n - 2; m >=
e; m--) {
1477 z = h (m - 1, m - 1);
1480 p = h (m - 1, m) + (r * s - w) / h (m, m - 1);
1481 q = h (m, m) - z - r -
s;
1483 s = fabs (p) + fabs (q) + fabs (r);
1491 a1 = fabs (h (m - 1, m - 2));
1492 a2 = fabs (h (m - 2, m - 2));
1493 a3 = fabs (h (m, m));
1495 if (a1 * (fabs (q) + fabs (r)) <=
EPSILON * fabs (p) * (a2 + a3))
1499 for (i = m + 2; i <=
n; i++) {
1500 h (i - 1, i - 3) = 0;
1502 for (i = m + 3; i <=
n; i++) {
1503 h (i - 1, i - 4) = 0;
1507 for (k = m; k <= n - 1; k++) {
1508 notlast = (k != n - 1);
1511 p = h (k - 1, k - 2);
1513 r = notlast ? h (k + 1, k - 2) : 0;
1514 x = fabs (p) + fabs (q) + fabs (r);
1524 s =
sqrt (p * p + q * q + r * r);
1530 h (k - 1, k - 2) = -s *
x;
1531 }
else if (e != m) {
1532 h (k - 1, k - 2) *= -1;
1543 for (j = k; j <=
n; j++) {
1544 p = h (k - 1, j - 1) + q * h (k, j - 1);
1547 p += r * h (k + 1, j - 1);
1548 h (k + 1, j - 1) -= p * z;
1551 h (k, j - 1) -= p * y;
1552 h (k - 1, j - 1) -= p *
x;
1554 j = (k + 3 <
n) ? (k + 3) :
n;
1557 for (i = e; i <= j; i++) {
1558 p = x * h (i - 1, k - 1) + y * h (i - 1, k);
1561 p += z * h (i - 1, k + 1);
1562 h (i - 1, k + 1) -= p * r;
1565 h (i - 1, k) -= p * q;
1566 h (i - 1, k - 1) -= p;
1570 goto next_iteration;