00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include <config.h>
00034 #endif
00035
00036 #include <irplib_polynomial.h>
00037 #include <math.h>
00038 #include <float.h>
00039 #include <stdint.h>
00040
00041
00042
00043
00044
00045 #define MAXDEGREE 6
00046
00047 #define irplib_polynomial_test_root_all(A, B, C, D, E) \
00048 irplib_polynomial_test_root_all_macro(A, B, C, D, E, __LINE__)
00049
00050
00051
00052
00053
00054 static cpl_error_code irplib_polynomial_multiply_1d_factor(cpl_polynomial *,
00055 const cpl_vector *,
00056 cpl_size);
00057 static void irplib_polynomial_solve_1d_all_test(void);
00058
00059 static void irplib_polynomial_test_root_all_macro(const cpl_vector *, cpl_size,
00060 double, double, double,
00061 unsigned);
00062
00063
00064
00065
00066 int main(void)
00067 {
00068
00069 cpl_test_init(PACKAGE_BUGREPORT, CPL_MSG_WARNING);
00070
00071 irplib_polynomial_solve_1d_all_test();
00072
00073 return cpl_test_end(0);
00074 }
00075
00076
00077
00084
00085 static void irplib_polynomial_solve_1d_all_test(void)
00086 {
00087
00088 cpl_polynomial * p2d = cpl_polynomial_new(2);
00089 cpl_polynomial * p1d = cpl_polynomial_new(1);
00090 cpl_vector * xtrue = cpl_vector_new(2);
00091 const cpl_size maxdegree = 4;
00092 cpl_size nreal = 0;
00093 cpl_size i;
00094 cpl_error_code code;
00095
00096 code = irplib_polynomial_solve_1d_all(NULL, xtrue, &nreal);
00097 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
00098
00099 code = irplib_polynomial_solve_1d_all(p1d, NULL, &nreal);
00100 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
00101
00102 code = irplib_polynomial_solve_1d_all(p1d, xtrue, NULL);
00103 cpl_test_eq_error(code, CPL_ERROR_NULL_INPUT);
00104
00105 code = irplib_polynomial_solve_1d_all(p2d, xtrue, &nreal);
00106 cpl_test_eq_error(code, CPL_ERROR_INVALID_TYPE);
00107
00108 code = irplib_polynomial_solve_1d_all(p1d, xtrue, &nreal);
00109 cpl_test_eq_error(code, CPL_ERROR_DATA_NOT_FOUND);
00110
00111
00112 i = 1;
00113 code = cpl_polynomial_set_coeff(p1d, &i, 1.0);
00114 cpl_test_eq_error(code, CPL_ERROR_NONE);
00115 code = irplib_polynomial_solve_1d_all(p1d, xtrue, &nreal);
00116 cpl_test_eq_error(code, CPL_ERROR_INCOMPATIBLE_INPUT);
00117
00118 cpl_polynomial_delete(p1d);
00119 cpl_polynomial_delete(p2d);
00120
00121 for (nreal = 1; nreal <= maxdegree; nreal++) {
00122
00123 double xreal = 0.0;
00124
00125
00126 cpl_vector_set_size(xtrue, nreal);
00127
00128 (void)cpl_vector_fill(xtrue, xreal);
00129
00130 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00131 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00132
00133
00134 xreal = 1.0;
00135
00136 (void)cpl_vector_fill(xtrue, xreal);
00137
00138 irplib_polynomial_test_root_all(xtrue, nreal, 1.0,
00139 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00140
00141
00142 for (i = 0; i < nreal; i++) {
00143 (void)cpl_vector_set(xtrue, i, 2.0 * (double)i - CPL_MATH_E);
00144 }
00145
00146 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00147 20.0 * DBL_EPSILON,
00148 300.0 * DBL_EPSILON);
00149
00150
00151 for (i = 0; i < nreal-1; i++) {
00152 (void)cpl_vector_set(xtrue, nreal-i-2, (double)(-i));
00153 }
00154 (void)cpl_vector_set(xtrue, nreal-1, (double)(nreal-1));
00155
00156 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00157 16.0*DBL_EPSILON, 600.0*DBL_EPSILON);
00158
00159 if (nreal < 2) continue;
00160
00161
00162
00163 (void)cpl_vector_fill(xtrue, 2.0);
00164 (void)cpl_vector_set(xtrue, nreal-2, -1.0);
00165 (void)cpl_vector_set(xtrue, nreal-1, 1.0);
00166
00167 irplib_polynomial_test_root_all(xtrue, nreal-2, CPL_MATH_PI,
00168 30.0*DBL_EPSILON, 25.0*DBL_EPSILON);
00169
00170 if (nreal < 3) continue;
00171 if (nreal > 4) {
00172
00173 (void)cpl_vector_fill(xtrue, 1.0);
00174 (void)cpl_vector_set(xtrue, nreal - 1 , 2.0);
00175
00176 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00177 DBL_EPSILON, DBL_EPSILON);
00178
00179 (void)cpl_vector_fill(xtrue, -1.0);
00180 (void)cpl_vector_set(xtrue, 0 , -2.0);
00181
00182 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00183 DBL_EPSILON, DBL_EPSILON);
00184
00185 (void)cpl_vector_fill(xtrue, 2.0);
00186 (void)cpl_vector_set(xtrue, 0, 1.0);
00187
00188 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00189 DBL_EPSILON, DBL_EPSILON);
00190 }
00191
00192 if (nreal > 3) continue;
00193
00194
00195 (void)cpl_vector_fill(xtrue, -2.0 * FLT_EPSILON);
00196 (void)cpl_vector_set(xtrue, 0, -1.0);
00197
00198 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00199 DBL_EPSILON, 2.0*DBL_EPSILON);
00200
00201
00202 #if defined SIZE_MAX && SIZE_MAX <= 4294967295
00203
00204 #else
00205 (void)cpl_vector_fill(xtrue, -0.2 * FLT_EPSILON);
00206
00207 (void)cpl_vector_set(xtrue, 0, -1.0);
00208
00209 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00210 FLT_EPSILON, 3.0*DBL_EPSILON);
00211 #endif
00212
00213
00214 if (nreal != 3) {
00215
00216 (void)cpl_vector_fill(xtrue, -2.0 * DBL_EPSILON);
00217 (void)cpl_vector_set(xtrue, 0, -1.0);
00218
00219 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00220 DBL_EPSILON, 2.0*DBL_EPSILON);
00221
00222
00223 (void)cpl_vector_set(xtrue, 0, -1.0);
00224 (void)cpl_vector_set(xtrue, 1, -2.0e-4 * FLT_EPSILON);
00225 (void)cpl_vector_set(xtrue, 2, 2.0e-4 * FLT_EPSILON);
00226
00227 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00228 FLT_EPSILON, 2.0*DBL_EPSILON);
00229 }
00230
00231
00232
00233 (void)cpl_vector_fill(xtrue, 2.0*DBL_EPSILON);
00234 (void)cpl_vector_set(xtrue, nreal - 2 , 3.0);
00235 (void)cpl_vector_set(xtrue, nreal - 1 , 2.0);
00236
00237 irplib_polynomial_test_root_all(xtrue, nreal - 2, CPL_MATH_PI,
00238 4.0 * DBL_EPSILON, DBL_EPSILON);
00239
00240
00241
00242 (void)cpl_vector_fill(xtrue, 3.0);
00243 (void)cpl_vector_set(xtrue, nreal - 2 , -1.0);
00244 (void)cpl_vector_set(xtrue, nreal - 1 , 2.0);
00245
00246 irplib_polynomial_test_root_all(xtrue, nreal - 2, CPL_MATH_PI,
00247 6.0*DBL_EPSILON, 220.0*DBL_EPSILON);
00248
00249
00250 }
00251
00252 #if MAXDEGREE > 2
00253
00254
00255 nreal = 3;
00256
00257 cpl_vector_set_size(xtrue, nreal);
00258
00259
00260 (void)cpl_vector_set(xtrue, 0, -2.0);
00261 (void)cpl_vector_set(xtrue, 1, 2.0 * DBL_EPSILON);
00262 (void)cpl_vector_set(xtrue, 2, 1.5);
00263
00264 irplib_polynomial_test_root_all(xtrue, nreal, 1.0,
00265 4.0*DBL_EPSILON, 30.0*DBL_EPSILON);
00266
00267 #if MAXDEGREE > 3
00268 nreal = 4;
00269
00270 cpl_vector_set_size(xtrue, nreal);
00271
00272
00273 (void)cpl_vector_set(xtrue, 0, -1.0);
00274 (void)cpl_vector_set(xtrue, 1, 1.0);
00275 (void)cpl_vector_set(xtrue, 2, 2.0);
00276 (void)cpl_vector_set(xtrue, 3, 2.0);
00277
00278 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00279 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00280
00281
00282 irplib_polynomial_test_root_all(xtrue, 2, CPL_MATH_PI,
00283 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00284
00285
00286
00287 (void)cpl_vector_set(xtrue, 0, -2.0);
00288 (void)cpl_vector_set(xtrue, 1, -1.0);
00289 (void)cpl_vector_set(xtrue, 2, 1.0);
00290 (void)cpl_vector_set(xtrue, 3, 2.0);
00291
00292 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00293 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00294
00295
00296 (void)cpl_vector_set(xtrue, 0, -1.0);
00297 (void)cpl_vector_set(xtrue, 1, 1.0);
00298 (void)cpl_vector_set(xtrue, 2, 0.0);
00299 (void)cpl_vector_set(xtrue, 3, 2.0);
00300
00301 irplib_polynomial_test_root_all(xtrue, 2, CPL_MATH_PI,
00302 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00303
00304
00305
00306 (void)cpl_vector_set(xtrue, 0, 1.0);
00307 (void)cpl_vector_set(xtrue, 1, 2.0);
00308 (void)cpl_vector_set(xtrue, 2, 1.0);
00309 (void)cpl_vector_set(xtrue, 3, 3.0);
00310
00311 irplib_polynomial_test_root_all(xtrue, 0, CPL_MATH_PI,
00312 10.0 * DBL_EPSILON, 10.0 * DBL_EPSILON);
00313
00314
00315 (void)cpl_vector_set(xtrue, 0, 0.0);
00316 (void)cpl_vector_set(xtrue, 1, 0.0);
00317 (void)cpl_vector_set(xtrue, 2, 0.0);
00318 (void)cpl_vector_set(xtrue, 3, 2.0);
00319
00320 irplib_polynomial_test_root_all(xtrue, 2, CPL_MATH_PI,
00321 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00322
00323 p1d = cpl_polynomial_new(1);
00324
00325 i = 0;
00326 cpl_polynomial_set_coeff(p1d, &i, -5.0);
00327 i = 1;
00328 cpl_polynomial_set_coeff(p1d, &i, -1.0);
00329 i = 2;
00330 cpl_polynomial_set_coeff(p1d, &i, -2.0);
00331 i = 4;
00332 cpl_polynomial_set_coeff(p1d, &i, 1.0);
00333
00334 code = irplib_polynomial_solve_1d_all(p1d, xtrue, &nreal);
00335 cpl_test_eq_error(code, CPL_ERROR_NONE);
00336
00337 cpl_msg_info(cpl_func, "Computed roots (%" CPL_SIZE_FORMAT " real): ",
00338 nreal);
00339 if (cpl_msg_get_level() <= CPL_MSG_INFO)
00340 cpl_vector_dump(xtrue, stderr);
00341 cpl_msg_info(cpl_func, "Residual: %g -> %g ", cpl_vector_get(xtrue, 0),
00342 cpl_polynomial_eval_1d(p1d, cpl_vector_get(xtrue, 0), NULL) );
00343 cpl_msg_info(cpl_func, "Residual: %g -> %g ", cpl_vector_get(xtrue, 1),
00344 cpl_polynomial_eval_1d(p1d, cpl_vector_get(xtrue, 1), NULL) );
00345
00346 cpl_polynomial_delete(p1d);
00347
00348 (void)cpl_vector_set(xtrue, 0, 0.0);
00349 (void)cpl_vector_set(xtrue, 1, 2.0);
00350 (void)cpl_vector_set(xtrue, 2, 1.0);
00351 (void)cpl_vector_set(xtrue, 3, 1.0);
00352
00353 irplib_polynomial_test_root_all(xtrue, 0, CPL_MATH_PI,
00354 2.0 * DBL_EPSILON, 2.0 * DBL_EPSILON);
00355
00356 (void)cpl_vector_set(xtrue, 0, -1.0);
00357 (void)cpl_vector_set(xtrue, 1, 2.0);
00358 (void)cpl_vector_set(xtrue, 2, 1.0);
00359 (void)cpl_vector_set(xtrue, 3, 3.0);
00360
00361 irplib_polynomial_test_root_all(xtrue, 0, CPL_MATH_PI,
00362 3.0 * DBL_EPSILON, 3.0 * DBL_EPSILON);
00363 #if MAXDEGREE > 4
00364 nreal = 5;
00365
00366 cpl_vector_set_size(xtrue, nreal);
00367
00368
00369 (void)cpl_vector_set(xtrue, 0, -1.0);
00370 (void)cpl_vector_set(xtrue, 1, 1.0);
00371 (void)cpl_vector_set(xtrue, 2, 2.0);
00372 (void)cpl_vector_set(xtrue, 3, 3.0);
00373 (void)cpl_vector_set(xtrue, 4, 4.0);
00374
00375 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00376 48.0 * DBL_EPSILON, 2800.0 * DBL_EPSILON);
00377
00378 irplib_polynomial_test_root_all(xtrue, nreal-2, CPL_MATH_PI,
00379 8.0 * DBL_EPSILON, 4000.0 * DBL_EPSILON);
00380
00381 irplib_polynomial_test_root_all(xtrue, nreal-4, CPL_MATH_PI,
00382 4.0 * DBL_EPSILON, 600.0 * DBL_EPSILON);
00383
00384 #if MAXDEGREE > 5
00385 nreal = 6;
00386
00387 cpl_vector_set_size(xtrue, nreal);
00388
00389
00390 (void)cpl_vector_set(xtrue, 0, -1.0);
00391 (void)cpl_vector_set(xtrue, 1, 1.0);
00392 (void)cpl_vector_set(xtrue, 2, 2.0);
00393 (void)cpl_vector_set(xtrue, 3, 3.0);
00394 (void)cpl_vector_set(xtrue, 4, 4.0);
00395 (void)cpl_vector_set(xtrue, 5, 5.0);
00396
00397 irplib_polynomial_test_root_all(xtrue, nreal, CPL_MATH_PI,
00398 240.0 * DBL_EPSILON, 50.0e3 * DBL_EPSILON);
00399
00400 irplib_polynomial_test_root_all(xtrue, nreal-2, CPL_MATH_PI,
00401 10.0 * DBL_EPSILON, 25.0e3 * DBL_EPSILON);
00402
00403 irplib_polynomial_test_root_all(xtrue, nreal-4, CPL_MATH_PI,
00404 12.0 * DBL_EPSILON, 1600.0 * DBL_EPSILON);
00405
00406
00407 (void)cpl_vector_set(xtrue, 0, 1.0);
00408 (void)cpl_vector_set(xtrue, 1, 1.0);
00409 (void)cpl_vector_set(xtrue, 2, 3.0);
00410 (void)cpl_vector_set(xtrue, 3, 3.0);
00411 (void)cpl_vector_set(xtrue, 4, 2.0);
00412 (void)cpl_vector_set(xtrue, 5, 1.0);
00413
00414 irplib_polynomial_test_root_all(xtrue, nreal-2, CPL_MATH_PI,
00415 0.05, 0.02);
00416
00417 #endif
00418 #endif
00419 #endif
00420 #endif
00421
00422 cpl_vector_delete(xtrue);
00423
00424 return;
00425 }
00426
00427
00438
00439 static
00440 cpl_error_code irplib_polynomial_multiply_1d_factor(cpl_polynomial * self,
00441 const cpl_vector * roots,
00442 cpl_size nreal)
00443 {
00444
00445 const cpl_size nroots = cpl_vector_get_size(roots);
00446 cpl_size i, degree;
00447
00448 cpl_ensure_code(self != NULL, CPL_ERROR_NULL_INPUT);
00449 cpl_ensure_code(roots != NULL, CPL_ERROR_NULL_INPUT);
00450 cpl_ensure_code(cpl_polynomial_get_dimension(self) == 1,
00451 CPL_ERROR_ILLEGAL_INPUT);
00452
00453 cpl_ensure_code(nreal >= 0, CPL_ERROR_ILLEGAL_INPUT);
00454 cpl_ensure_code(nreal <= nroots,
00455 CPL_ERROR_ILLEGAL_INPUT);
00456 cpl_ensure_code((cpl_vector_get_size(roots) - nreal) % 2 == 0,
00457 CPL_ERROR_ILLEGAL_INPUT);
00458
00459 i = 0;
00460 degree = cpl_polynomial_get_degree(self);
00461 cpl_ensure_code(degree > 0 || cpl_polynomial_get_coeff(self, &i) != 0.0,
00462 CPL_ERROR_DATA_NOT_FOUND);
00463
00464 for (i = 0; i < nreal; i++) {
00465 const double root = cpl_vector_get(roots, i);
00466 double prev = 0.0;
00467 cpl_size j;
00468
00469 degree++;
00470
00471 for (j = degree; j >= 0; j--) {
00472 double value = 0.0;
00473 double newval;
00474
00475 if (j > 0) {
00476 const cpl_size jj = j - 1;
00477 newval = value = cpl_polynomial_get_coeff(self, &jj);
00478 } else {
00479 newval = 0.0;
00480 }
00481
00482 if (j < degree) {
00483 newval -= root * prev;
00484 }
00485
00486 cpl_polynomial_set_coeff(self, &j, newval);
00487
00488 prev = value;
00489
00490 }
00491 }
00492
00493
00494
00495 for (; i < nroots; i += 2) {
00496 const double a = cpl_vector_get(roots, i);
00497 const double b = cpl_vector_get(roots, i+1);
00498 cpl_vector * aroot = cpl_vector_new(2);
00499 cpl_polynomial * copy = cpl_polynomial_duplicate(self);
00500
00501 cpl_vector_fill(aroot, a);
00502
00503 irplib_polynomial_multiply_1d_factor(self, aroot, 2);
00504
00505 cpl_polynomial_multiply_scalar(copy, copy, b * b);
00506
00507 cpl_polynomial_add(self, self, copy);
00508
00509 cpl_vector_delete(aroot);
00510 cpl_polynomial_delete(copy);
00511
00512 }
00513 cpl_test_assert(i == nroots);
00514
00515 return CPL_ERROR_NONE;
00516
00517 }
00518
00519
00532
00533 static void
00534 irplib_polynomial_test_root_all_macro(const cpl_vector * self, cpl_size nreal,
00535 double factor, double tolerance,
00536 double resitol, unsigned line)
00537 {
00538
00539 const cpl_size degree = cpl_vector_get_size(self);
00540 cpl_polynomial * p1d = cpl_polynomial_new(1);
00541 cpl_vector * roots = cpl_vector_new(degree);
00542 cpl_size i = 0;
00543 cpl_size jreal;
00544 cpl_error_code code;
00545
00546 code = cpl_polynomial_set_coeff(p1d, &i, factor);
00547 cpl_test_eq_error(code, CPL_ERROR_NONE);
00548
00549 code = irplib_polynomial_multiply_1d_factor(p1d, self, nreal);
00550 cpl_test_eq_error(code, CPL_ERROR_NONE);
00551
00552 code = irplib_polynomial_solve_1d_all(p1d, roots, &jreal);
00553 cpl_test_eq_error(code, CPL_ERROR_NONE);
00554
00555 cpl_test_eq(jreal, nreal);
00556 if (jreal != nreal) {
00557 cpl_msg_info(cpl_func, "1D-polynomial:");
00558 cpl_polynomial_dump(p1d, stderr);
00559 cpl_msg_error(cpl_func, "True roots (%" CPL_SIZE_FORMAT
00560 " real): (line=%u)", nreal, line);
00561 cpl_vector_dump(self, stderr);
00562 cpl_msg_error(cpl_func, "Computed roots (%" CPL_SIZE_FORMAT " real): ",
00563 jreal);
00564 cpl_vector_dump(roots, stderr);
00565 } else if (cpl_msg_get_level() < CPL_MSG_WARNING) {
00566 CPL_DIAG_PRAGMA_PUSH_IGN(-Wcast-qual)
00567 cpl_bivector * dump =
00568 cpl_bivector_wrap_vectors((cpl_vector*)self, roots);
00569 CPL_DIAG_PRAGMA_POP;
00570
00571 cpl_msg_warning(cpl_func, "Comparing %" CPL_SIZE_FORMAT " roots (%"
00572 CPL_SIZE_FORMAT " real): (line=%u)",
00573 degree, nreal, line);
00574 cpl_bivector_dump(dump, stderr);
00575 cpl_bivector_unwrap_vectors(dump);
00576 }
00577
00578 for (i = 0; i < jreal; i++) {
00579 const double root = cpl_vector_get(roots, i);
00580 const double residual = cpl_polynomial_eval_1d(p1d, root, NULL);
00581
00582 cpl_test_abs(root, cpl_vector_get(self, i), tolerance);
00583
00584 cpl_test_abs(residual, 0.0, resitol);
00585
00586 }
00587
00588 for (i = nreal; i < degree; i++) {
00589 const double root = cpl_vector_get(roots, i);
00590
00591 cpl_test_abs(root, cpl_vector_get(self, i), tolerance);
00592
00593
00594
00595 }
00596
00597 cpl_vector_delete(roots);
00598 cpl_polynomial_delete(p1d);
00599
00600 return;
00601 }