| 1 |
/* |
| 2 |
* This file is part of MPSolve 3.2.2 |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2020, Dipartimento di Matematica "L. Tonelli", Pisa. |
| 5 |
* License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher |
| 6 |
* |
| 7 |
* Authors: |
| 8 |
* Dario Andrea Bini <[email protected]> |
| 9 |
* Giuseppe Fiorentino <[email protected]> |
| 10 |
* Leonardo Robol <[email protected]> |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* @file |
| 16 |
* @brief File with the implementation of the driver routines |
| 17 |
* for MPSolve. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <math.h> |
| 21 |
#include <float.h> |
| 22 |
#include <mps/mps.h> |
| 23 |
|
| 24 |
/** |
| 25 |
* @brief Main routine of the program that implements the algorithm |
| 26 |
* in the standard polynomial version. |
| 27 |
* |
| 28 |
* The program is divided into many parts |
| 29 |
* - Check the correctness of data, scale coefficients if |
| 30 |
* needed, and select cases: the variable <code>which_case</code> is |
| 31 |
* <code>'f'</code> or <code>'d'</code> according to float or dpe case. |
| 32 |
* - Call msolve or dsolve according to the value of which_case. |
| 33 |
* - Allocate MP variables mfpc, mroot, drad (if needed). |
| 34 |
* - Start MPsolve loop |
| 35 |
* - prepare data according to the current precision |
| 36 |
* and to the data_type (density/sparsity/user) |
| 37 |
* - Call msolve with the current precision |
| 38 |
* - check for termination |
| 39 |
*/ |
| 40 |
MPS_PRIVATE void |
| 41 |
mps_standard_mpsolve (mps_context * s) |
| 42 |
{ |
| 43 |
int i, nzc; |
| 44 |
char which_case; |
| 45 |
mps_boolean d_after_f, computed; |
| 46 |
|
| 47 |
#ifndef DISABLE_DEBUG |
| 48 |
clock_t *my_timer = mps_start_timer (); |
| 49 |
#endif |
| 50 |
|
| 51 |
mps_allocate_data (s); |
| 52 |
|
| 53 |
if (s->DOLOG) |
| 54 |
s->debug_level |= MPS_DEBUG_TRACE; |
| 55 |
|
| 56 |
/* == 1 == Setup variables, i.e. copy coefficients |
| 57 |
into dpr, dpc and similar. */ |
| 58 |
mps_setup (s); |
| 59 |
|
| 60 |
s->lastphase = no_phase; |
| 61 |
computed = false; |
| 62 |
s->over_max = false; |
| 63 |
|
| 64 |
/* == 2 == Resume from pre-computed roots */ |
| 65 |
if (s->resume) |
| 66 |
{ |
| 67 |
mps_error (s, "Resume not supported yet"); |
| 68 |
#ifndef DISABLE_DEBUG |
| 69 |
mps_stop_timer (my_timer); |
| 70 |
#endif |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
/* == 3 == Check data and get starting phase */ |
| 75 |
if (s->skip_float) |
| 76 |
which_case = 'd'; |
| 77 |
else |
| 78 |
which_case = 'f'; |
| 79 |
|
| 80 |
/* This variable is true if we need a dpe phase after the |
| 81 |
* float phase */ |
| 82 |
d_after_f = false; |
| 83 |
|
| 84 |
/* Check if a dpe phase is needed and deflate polynomial */ |
| 85 |
mps_check_data (s, &which_case); |
| 86 |
|
| 87 |
/* Check for errors in check data */ |
| 88 |
if (mps_context_has_errors (s)) |
| 89 |
{ |
| 90 |
#ifndef DISABLE_DEBUG |
| 91 |
mps_stop_timer (my_timer); |
| 92 |
#endif |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
rdpe_set_2dl (s->eps_out, 1.0, -s->output_config->prec); |
| 97 |
|
| 98 |
if (s->DOLOG) |
| 99 |
fprintf (s->logstr, "Which_case = %c, skip_float= %d\n", which_case, |
| 100 |
s->skip_float); |
| 101 |
|
| 102 |
/* == 4 == Float phase */ |
| 103 |
if (which_case == 'f') |
| 104 |
{ |
| 105 |
if (s->DOLOG) |
| 106 |
fprintf (s->logstr, "Float phase ...\n"); |
| 107 |
mps_fsolve (s, &d_after_f); |
| 108 |
s->lastphase = float_phase; |
| 109 |
|
| 110 |
if (s->DOLOG) |
| 111 |
mps_dump (s); |
| 112 |
|
| 113 |
computed = mps_check_stop (s); |
| 114 |
if (computed && s->output_config->goal != MPS_OUTPUT_GOAL_APPROXIMATE) |
| 115 |
goto exit_sub; |
| 116 |
/* stop for COUNT and ISOLATE goals */ |
| 117 |
} |
| 118 |
|
| 119 |
/* == 5 == DPE phase */ |
| 120 |
if (which_case == 'd' || d_after_f) |
| 121 |
{ /* DPE phase */ |
| 122 |
if (s->DOLOG) |
| 123 |
fprintf (s->logstr, "DPE phase ...\n"); |
| 124 |
/* If we are arriving from a float phase copy the floating points |
| 125 |
* roots approximations in the DPE root approximations. */ |
| 126 |
if (d_after_f) |
| 127 |
for (i = 0; i < s->n; i++) |
| 128 |
{ |
| 129 |
rdpe_set_d (s->root[i]->drad, s->root[i]->frad); |
| 130 |
cdpe_set_x (s->root[i]->dvalue, s->root[i]->fvalue); |
| 131 |
} |
| 132 |
s->lastphase = dpe_phase; |
| 133 |
mps_dsolve (s, d_after_f); |
| 134 |
|
| 135 |
if (s->DOLOG) |
| 136 |
mps_dump (s); |
| 137 |
|
| 138 |
computed = mps_check_stop (s); |
| 139 |
if (computed && s->output_config->goal != MPS_OUTPUT_GOAL_APPROXIMATE) |
| 140 |
goto exit_sub; |
| 141 |
} |
| 142 |
|
| 143 |
/* == 6 == Allocate MP variables mfpc, mroot, drad, mfppc, mfppc1 |
| 144 |
* (the real input case is not implemented yet ) */ |
| 145 |
MPS_DEBUG (s, "Starting MP phase"); |
| 146 |
|
| 147 |
s->lastphase = mp_phase; |
| 148 |
|
| 149 |
/* ==== 6.1 initialize mp variables */ |
| 150 |
mps_mp_set_prec (s, 2 * DBL_MANT_DIG); |
| 151 |
|
| 152 |
/* Prepare data according to the current working precision */ |
| 153 |
mps_prepare_data (s, s->mpwp); |
| 154 |
|
| 155 |
/* ==== 6.2 set initial values for mp variables */ |
| 156 |
for (i = 0; i < s->n; i++) |
| 157 |
{ |
| 158 |
if (which_case == 'd' || d_after_f) |
| 159 |
mpc_set_cdpe (s->root[i]->mvalue, s->root[i]->dvalue); |
| 160 |
else |
| 161 |
{ |
| 162 |
mpc_set_cplx (s->root[i]->mvalue, s->root[i]->fvalue); |
| 163 |
rdpe_set_d (s->root[i]->drad, s->root[i]->frad); |
| 164 |
} |
| 165 |
} |
| 166 |
if (computed && s->output_config->goal == MPS_OUTPUT_GOAL_APPROXIMATE) |
| 167 |
{ |
| 168 |
MPS_DEBUG (s, "Exiting since the approximation are computed and the goal is MPS_OUTPUT_GOAL_APPROXIMATE"); |
| 169 |
goto exit_sub; |
| 170 |
} |
| 171 |
|
| 172 |
MPS_DEBUG (s, "s->mpwp = %ld, s->mpwp_max = %ld", s->mpwp, s->mpwp_max); |
| 173 |
MPS_DEBUG (s, "s->input_config->prec = %ld", s->active_poly->prec); |
| 174 |
|
| 175 |
/* == 7 == Start MPSolve loop */ |
| 176 |
s->mpwp = mps_context_get_minimum_precision (s); |
| 177 |
|
| 178 |
/* Poor man GMP - machine precision detection. We need that min_prec is contained |
| 179 |
* in the interval [ DBL_MANT_DIG , 2 * DBL_MANT_DIG ]. This is probably true on most |
| 180 |
* architectures with the instruction above, but we want to be sure. */ |
| 181 |
while (s->mpwp < DBL_MANT_DIG) |
| 182 |
s->mpwp <<= 1; |
| 183 |
while (s->mpwp > 2 * DBL_MANT_DIG) |
| 184 |
s->mpwp >>= 1; |
| 185 |
|
| 186 |
while (!computed && s->mpwp < s->mpwp_max) |
| 187 |
{ |
| 188 |
s->mpwp *= 2; |
| 189 |
|
| 190 |
if (s->mpwp > s->mpwp_max) |
| 191 |
{ |
| 192 |
s->mpwp = s->mpwp_max; |
| 193 |
s->over_max = true; |
| 194 |
} |
| 195 |
|
| 196 |
if (s->DOLOG) |
| 197 |
fprintf (s->logstr, "MAIN: mp_loop: mpwp=%ld\n", s->mpwp); |
| 198 |
|
| 199 |
/* == 7.1 == prepare data according to the current precision */ |
| 200 |
mps_mp_set_prec (s, s->mpwp); |
| 201 |
mps_prepare_data (s, s->mpwp); |
| 202 |
|
| 203 |
/* == 7.2 == Call msolve with the current precision */ |
| 204 |
if (s->DOLOG) |
| 205 |
fprintf (s->logstr, "MAIN: now call msolve nclust=%ld\n", s->clusterization->n); |
| 206 |
mps_msolve (s); |
| 207 |
s->lastphase = mp_phase; |
| 208 |
|
| 209 |
/* if (s->DOLOG) dump(logstr); */ |
| 210 |
|
| 211 |
if (s->DOLOG) |
| 212 |
{ /* count isolated zeros */ |
| 213 |
nzc = 0; |
| 214 |
for (i = 0; i < s->n; i++) |
| 215 |
{ |
| 216 |
if (s->root[i]->status == MPS_ROOT_STATUS_ISOLATED || |
| 217 |
s->root[i]->status == MPS_ROOT_STATUS_APPROXIMATED) |
| 218 |
nzc++; |
| 219 |
} |
| 220 |
fprintf (s->logstr, "MAIN: isolated %d roots\n", nzc); |
| 221 |
fprintf (s->logstr, "MAIN: after msolve check stop\n"); |
| 222 |
} |
| 223 |
|
| 224 |
/* == 7.3 == Check the stop condition */ |
| 225 |
computed = mps_check_stop (s); |
| 226 |
mps_mmodify (s, true); |
| 227 |
|
| 228 |
/* == 7.4 == reset the status vector */ |
| 229 |
for (i = 0; i < s->n; i++) |
| 230 |
if (s->root[i]->status == MPS_ROOT_STATUS_NEW_CLUSTERED) |
| 231 |
s->root[i]->status = MPS_ROOT_STATUS_CLUSTERED; |
| 232 |
} |
| 233 |
|
| 234 |
/* == 8 == Check for termination */ |
| 235 |
if (!computed) |
| 236 |
{ |
| 237 |
if (s->over_max) |
| 238 |
{ |
| 239 |
s->over_max = true; |
| 240 |
/* mps_error (s, "Reached the maximum working precision"); */ |
| 241 |
MPS_DEBUG (s, "Reached the maximum working precision"); |
| 242 |
goto exit_sub; |
| 243 |
} |
| 244 |
else |
| 245 |
{ |
| 246 |
/* mps_warn (s, "Reached the input precision"); */ |
| 247 |
MPS_DEBUG (s, "Reached the input precision"); |
| 248 |
goto exit_sub; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
exit_sub: |
| 253 |
|
| 254 |
/* == 9 == Check inclusion disks */ |
| 255 |
if (computed && s->clusterization->n < s->n) |
| 256 |
if (!mps_inclusion (s)) |
| 257 |
{ |
| 258 |
mps_error (s, "Unable to compute inclusion disks"); |
| 259 |
return; |
| 260 |
} |
| 261 |
|
| 262 |
/* == 10 == Refine roots */ |
| 263 |
if (computed && !s->over_max && s->output_config->goal == MPS_OUTPUT_GOAL_APPROXIMATE) |
| 264 |
{ |
| 265 |
s->lastphase = mp_phase; |
| 266 |
mps_improve (s); |
| 267 |
} |
| 268 |
|
| 269 |
/* == 11 == Check inclusions */ |
| 270 |
/* This step is disabled since it cause problems with the lar* kind of polynomials. |
| 271 |
* To be re-enabled a careful check of the necessary precision to avoid NULL DERIVATIVE |
| 272 |
* warnings should be implemented. |
| 273 |
* if (s->active_poly->prec > 0) |
| 274 |
* mps_validate_inclusions (s); |
| 275 |
*/ |
| 276 |
|
| 277 |
/* == 12 == Restore to highest used precision */ |
| 278 |
if (s->lastphase == mp_phase) |
| 279 |
mps_restore_data (s); |
| 280 |
|
| 281 |
#ifndef DISABLE_DEBUG |
| 282 |
{ |
| 283 |
unsigned long time = mps_stop_timer (my_timer); |
| 284 |
MPS_DEBUG (s, "Total time using MPSolve: %lu ms", time); |
| 285 |
} |
| 286 |
#endif |
| 287 |
|
| 288 |
/* Finally copy the roots ready for output */ |
| 289 |
mps_copy_roots (s); |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* @brief Setup vectors and variables |
| 294 |
*/ |
| 295 |
MPS_PRIVATE void |
| 296 |
mps_setup (mps_context * s) |
| 297 |
{ |
| 298 |
int i; |
| 299 |
mps_polynomial *p = s->active_poly; |
| 300 |
mpf_t mptemp; |
| 301 |
mpc_t mptempc; |
| 302 |
|
| 303 |
if (s->DOLOG) |
| 304 |
{ |
| 305 |
/* fprintf (s->logstr, "Goal = %5s\n", s->goal); */ |
| 306 |
/* fprintf (s->logstr, "Data type = %3s\n", s->data_type); */ |
| 307 |
fprintf (s->logstr, "Degree = %d\n", s->n); |
| 308 |
fprintf (s->logstr, "Input prec. = %ld digits\n", (long)(s->active_poly->prec |
| 309 |
* LOG10_2)); |
| 310 |
fprintf (s->logstr, "Output prec. = %ld digits\n", (long)(s->output_config->prec |
| 311 |
* LOG10_2)); |
| 312 |
} |
| 313 |
|
| 314 |
/* setup temporary vectors */ |
| 315 |
if (MPS_IS_MONOMIAL_POLY (p) && MPS_DENSITY_IS_SPARSE (s->active_poly->density)) |
| 316 |
{ |
| 317 |
mps_monomial_poly *mp = MPS_MONOMIAL_POLY (p); |
| 318 |
|
| 319 |
for (i = 0; i <= p->degree; i++) |
| 320 |
{ |
| 321 |
mp->fap[i] = 0.0; |
| 322 |
mp->fpr[i] = 0.0; |
| 323 |
rdpe_set (mp->dap[i], rdpe_zero); |
| 324 |
cplx_set (mp->fpc[i], cplx_zero); |
| 325 |
rdpe_set (mp->dpr[i], rdpe_zero); |
| 326 |
cdpe_set (mp->dpc[i], cdpe_zero); |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
/* Indexes of the first (and only) cluster start from |
| 331 |
* 0 and reach n */ |
| 332 |
mps_cluster_reset (s); |
| 333 |
|
| 334 |
/* set input and output epsilon */ |
| 335 |
rdpe_set_2dl (s->eps_in, 1.0, 1 - s->active_poly->prec); |
| 336 |
rdpe_set_2dl (s->eps_out, 1.0, 1 - s->output_config->prec); |
| 337 |
|
| 338 |
/* precision of each root */ |
| 339 |
for (i = 0; i < s->n; i++) |
| 340 |
s->root[i]->wp = 53; |
| 341 |
|
| 342 |
/* output order info */ |
| 343 |
for (i = 0; i < s->n; i++) |
| 344 |
s->order[i] = i; |
| 345 |
|
| 346 |
/* reset root counts */ |
| 347 |
s->count[0] = s->count[1] = s->count[2] = 0; |
| 348 |
|
| 349 |
/* compute DPE approximations */ |
| 350 |
if (MPS_IS_MONOMIAL_POLY (p)) |
| 351 |
{ |
| 352 |
mps_monomial_poly *mp = MPS_MONOMIAL_POLY (p); |
| 353 |
|
| 354 |
/* init temporary mp variables */ |
| 355 |
mpf_init2 (mptemp, DBL_MANT_DIG); |
| 356 |
mpc_init2 (mptempc, DBL_MANT_DIG); |
| 357 |
|
| 358 |
/* main loop */ |
| 359 |
s->skip_float = false; |
| 360 |
for (i = 0; i <= s->n; i++) |
| 361 |
{ |
| 362 |
if (MPS_DENSITY_IS_SPARSE (s->active_poly->density) && !mp->spar[i]) |
| 363 |
continue; |
| 364 |
|
| 365 |
if (MPS_STRUCTURE_IS_REAL (s->active_poly->structure)) |
| 366 |
{ |
| 367 |
if (MPS_STRUCTURE_IS_RATIONAL (s->active_poly->structure) || |
| 368 |
MPS_STRUCTURE_IS_INTEGER (s->active_poly->structure)) |
| 369 |
{ |
| 370 |
mpf_set_q (mptemp, mp->initial_mqp_r[i]); |
| 371 |
mpf_get_rdpe (mp->dpr[i], mptemp); |
| 372 |
/*#G GMP 2.0.2 bug begin */ |
| 373 |
if (rdpe_sgn (mp->dpr[i]) != mpq_sgn (mp->initial_mqp_r[i])) |
| 374 |
rdpe_neg_eq (mp->dpr[i]); |
| 375 |
/*#G GMP bug end */ |
| 376 |
} |
| 377 |
|
| 378 |
if (MPS_STRUCTURE_IS_FP (s->active_poly->structure)) |
| 379 |
mpf_get_rdpe (mp->dpr[i], mpc_Re (mp->mfpc[i])); |
| 380 |
|
| 381 |
cdpe_set_e (mp->dpc[i], mp->dpr[i], rdpe_zero); |
| 382 |
|
| 383 |
/* compute dap[i] and check for float phase */ |
| 384 |
rdpe_abs (mp->dap[i], mp->dpr[i]); |
| 385 |
rdpe_abs (mp->dap[i], mp->dpr[i]); |
| 386 |
if (rdpe_gt (mp->dap[i], rdpe_maxd) |
| 387 |
|| rdpe_lt (mp->dap[i], rdpe_mind)) |
| 388 |
s->skip_float = true; |
| 389 |
} |
| 390 |
else if (MPS_STRUCTURE_IS_COMPLEX (s->active_poly->structure)) |
| 391 |
{ |
| 392 |
if (MPS_STRUCTURE_IS_RATIONAL (s->active_poly->structure) || |
| 393 |
MPS_STRUCTURE_IS_INTEGER (s->active_poly->structure)) |
| 394 |
{ |
| 395 |
mpc_set_q (mptempc, mp->initial_mqp_r[i], mp->initial_mqp_i[i]); |
| 396 |
mpc_get_cdpe (mp->dpc[i], mptempc); |
| 397 |
/*#G GMP 2.0.2 bug begin */ |
| 398 |
if (rdpe_sgn (cdpe_Re (mp->dpc[i])) != mpq_sgn (mp->initial_mqp_r[i])) |
| 399 |
rdpe_neg_eq (cdpe_Re (mp->dpc[i])); |
| 400 |
if (rdpe_sgn (cdpe_Im (mp->dpc[i])) != mpq_sgn (mp->initial_mqp_i[i])) |
| 401 |
rdpe_neg_eq (cdpe_Im (mp->dpc[i])); |
| 402 |
/*#G GMP bug end */ |
| 403 |
} |
| 404 |
else if (MPS_STRUCTURE_IS_FP (s->active_poly->structure)) |
| 405 |
mpc_get_cdpe (mp->dpc[i], mp->mfpc[i]); |
| 406 |
|
| 407 |
/* compute dap[i] */ |
| 408 |
cdpe_mod (mp->dap[i], mp->dpc[i]); |
| 409 |
if (rdpe_gt (mp->dap[i], rdpe_maxd) |
| 410 |
|| rdpe_lt (mp->dap[i], rdpe_mind)) |
| 411 |
s->skip_float = true; |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
/* free temporary mp variables */ |
| 416 |
mpf_clear (mptemp); |
| 417 |
mpc_clear (mptempc); |
| 418 |
|
| 419 |
/* adjust input data type */ |
| 420 |
if (MPS_STRUCTURE_IS_FP (s->active_poly->structure) && s->skip_float) |
| 421 |
s->active_poly->structure = MPS_STRUCTURE_IS_REAL (s->active_poly->structure) ? |
| 422 |
MPS_STRUCTURE_REAL_BIGFLOAT : MPS_STRUCTURE_COMPLEX_BIGFLOAT; |
| 423 |
|
| 424 |
/* prepare floating point vectors */ |
| 425 |
if (!s->skip_float) |
| 426 |
for (i = 0; i <= MPS_POLYNOMIAL (p)->degree; i++) |
| 427 |
{ |
| 428 |
if (MPS_DENSITY_IS_SPARSE (s->active_poly->density) || !mp->spar[i]) |
| 429 |
continue; |
| 430 |
if (MPS_STRUCTURE_IS_REAL (s->active_poly->structure)) |
| 431 |
{ |
| 432 |
mp->fpr[i] = rdpe_get_d (mp->dpr[i]); |
| 433 |
mp->fap[i] = fabs (mp->fpr[i]); |
| 434 |
cplx_set_d (mp->fpc[i], mp->fpr[i], 0.0); |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
cdpe_get_x (mp->fpc[i], mp->dpc[i]); |
| 439 |
mp->fap[i] = cplx_mod (mp->fpc[i]); |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* @brief Check consistency of data and makes some basic adjustments. |
| 447 |
* |
| 448 |
* This routine check, for example, if there are zero roots in the polynomial |
| 449 |
* (i.e. no costant term) and deflates the polynomial if necessary (shifting |
| 450 |
* the coefficients). |
| 451 |
* |
| 452 |
* It sets the value of the parameter <code>which_case</code> to <code>'f'</code> |
| 453 |
* if a floating point phase is enough, or to <code>'d'</code> if |
| 454 |
* a <code>dpe</code> phase is needed. |
| 455 |
* |
| 456 |
* @param s The <code>mps_context</code> associated with the current computation. |
| 457 |
* @param which_case the address of the variable which_case; |
| 458 |
*/ |
| 459 |
MPS_PRIVATE void |
| 460 |
mps_check_data (mps_context * s, char *which_case) |
| 461 |
{ |
| 462 |
rdpe_t min_coeff, max_coeff, tmp; |
| 463 |
mps_monomial_poly *p = NULL; |
| 464 |
int i; |
| 465 |
|
| 466 |
/* case of user-defined polynomial */ |
| 467 |
if (! MPS_IS_MONOMIAL_POLY (s->active_poly)) |
| 468 |
{ |
| 469 |
if (s->output_config->multiplicity) |
| 470 |
mps_error (s, |
| 471 |
"Multiplicity detection not yet implemented for user polynomial"); |
| 472 |
if (s->output_config->root_properties) |
| 473 |
mps_error (s, |
| 474 |
"Real/imaginary detection not yet implemented for user polynomial"); |
| 475 |
*which_case = 'd'; |
| 476 |
return; |
| 477 |
} |
| 478 |
else |
| 479 |
p = MPS_MONOMIAL_POLY (s->active_poly); |
| 480 |
|
| 481 |
/* Check consistency of input */ |
| 482 |
if (rdpe_eq (p->dap[s->n], rdpe_zero)) |
| 483 |
{ |
| 484 |
mps_warn (s, "The leading coefficient is zero"); |
| 485 |
do |
| 486 |
(s->n)--; |
| 487 |
while (rdpe_eq (p->dap[s->n], rdpe_zero)); |
| 488 |
|
| 489 |
MPS_POLYNOMIAL (p)->degree = s->n; |
| 490 |
} |
| 491 |
|
| 492 |
/* Compute min_coeff */ |
| 493 |
if (rdpe_lt (p->dap[0], p->dap[s->n])) |
| 494 |
rdpe_set (min_coeff, p->dap[0]); |
| 495 |
else |
| 496 |
rdpe_set (min_coeff, p->dap[s->n]); |
| 497 |
|
| 498 |
/* Compute max_coeff and its logarithm */ |
| 499 |
rdpe_set (max_coeff, p->dap[0]); |
| 500 |
for (i = 1; i <= s->n; i++) |
| 501 |
if (rdpe_lt (max_coeff, p->dap[i])) |
| 502 |
rdpe_set (max_coeff, p->dap[i]); |
| 503 |
s->lmax_coeff = rdpe_log (max_coeff); |
| 504 |
|
| 505 |
/* Multiplicity and sep */ |
| 506 |
if (s->output_config->multiplicity) |
| 507 |
{ |
| 508 |
if (MPS_STRUCTURE_IS_INTEGER (s->active_poly->structure)) |
| 509 |
{ |
| 510 |
mps_compute_sep (s); |
| 511 |
} |
| 512 |
else if (MPS_STRUCTURE_IS_RATIONAL (s->active_poly->structure)) |
| 513 |
{ |
| 514 |
mps_warn (s, "The multiplicity option has not been yet implemented"); |
| 515 |
s->sep = 0.0; |
| 516 |
} |
| 517 |
else |
| 518 |
{ |
| 519 |
mps_warn (s, "The input polynomial has neither integer nor rational"); |
| 520 |
mps_warn (s, " coefficients: unable to compute multiplicities"); |
| 521 |
s->sep = 0.0; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
/* Real/Imaginary detection */ |
| 526 |
if (s->output_config->root_properties || |
| 527 |
s->output_config->search_set == MPS_SEARCH_SET_REAL || |
| 528 |
s->output_config->search_set == MPS_SEARCH_SET_IMAG) |
| 529 |
{ |
| 530 |
if (MPS_STRUCTURE_IS_INTEGER (s->active_poly->structure)) |
| 531 |
{ |
| 532 |
mps_compute_sep (s); |
| 533 |
} |
| 534 |
else if (MPS_STRUCTURE_IS_RATIONAL (s->active_poly->structure)) |
| 535 |
{ |
| 536 |
mps_error (s, |
| 537 |
"The real/imaginary option has not been yet implemented for rational input"); |
| 538 |
return; |
| 539 |
} |
| 540 |
else |
| 541 |
{ |
| 542 |
mps_error (s, "The input polynomial has neither integer nor rational " |
| 543 |
"coefficients: unable to perform real/imaginary options"); |
| 544 |
return; |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
/* Select cases (dpe or floating point) |
| 549 |
* First normalize the polynomial (only the float version) */ |
| 550 |
rdpe_div (tmp, max_coeff, min_coeff); |
| 551 |
rdpe_mul_eq_d (tmp, (double)(s->n + 1)); |
| 552 |
rdpe_mul_eq (tmp, rdpe_mind); |
| 553 |
rdpe_div_eq (tmp, rdpe_maxd); |
| 554 |
|
| 555 |
if (rdpe_lt (tmp, rdpe_one)) |
| 556 |
{ |
| 557 |
mpc_t m_min_coeff; |
| 558 |
cdpe_t c_min_coeff; |
| 559 |
|
| 560 |
/* if (n+1)*max_coeff/min_coeff < dhuge/dtiny - float case */ |
| 561 |
*which_case = 'f'; |
| 562 |
rdpe_mul_eq (min_coeff, max_coeff); |
| 563 |
rdpe_mul (tmp, rdpe_mind, rdpe_maxd); |
| 564 |
rdpe_div (min_coeff, tmp, min_coeff); |
| 565 |
rdpe_sqrt_eq (min_coeff); |
| 566 |
|
| 567 |
rdpe_set (cdpe_Re (c_min_coeff), min_coeff); |
| 568 |
rdpe_set (cdpe_Im (c_min_coeff), rdpe_zero); |
| 569 |
|
| 570 |
mpc_init2 (m_min_coeff, mpc_get_prec (p->mfpc[0])); |
| 571 |
mpc_set_cdpe (m_min_coeff, c_min_coeff); |
| 572 |
|
| 573 |
/* min_coeff = sqrt(dhuge*dtiny/(min_coeff*max_coeff)) |
| 574 |
* NOTE: This is enabled for floating point polynomials only |
| 575 |
* for the moment, but it may work nicely also for other representations. */ |
| 576 |
{ |
| 577 |
for (i = 0; i <= s->n; i++) |
| 578 |
{ |
| 579 |
/* Multiply the MP leading coefficient */ |
| 580 |
mpc_mul_eq (p->mfpc[i], m_min_coeff); |
| 581 |
|
| 582 |
rdpe_mul (tmp, p->dap[i], min_coeff); |
| 583 |
rdpe_set (p->dap[i], tmp); |
| 584 |
p->fap[i] = rdpe_get_d (tmp); |
| 585 |
|
| 586 |
mpc_get_cdpe (p->dpc[i], p->mfpc[i]); |
| 587 |
cdpe_get_x (p->fpc[i], p->dpc[i]); |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
mpc_clear (m_min_coeff); |
| 592 |
} |
| 593 |
else |
| 594 |
*which_case = 'd'; |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* @brief Compute the minimum distance that can separate two roots of the input |
| 599 |
* polynomial. |
| 600 |
*/ |
| 601 |
MPS_PRIVATE void |
| 602 |
mps_compute_sep (mps_context * s) |
| 603 |
{ |
| 604 |
s->sep = s->n * s->lmax_coeff; |
| 605 |
s->sep = -(s->sep) - s->n * (1 + log ((double)s->n)) / LOG2; |
| 606 |
if (s->DOLOG) |
| 607 |
fprintf (s->logstr, "Sep = %f\n", s->sep); |
| 608 |
} |