| 1 |
/* |
| 2 |
* This file is part of MPSolve 3.0 |
| 3 |
* |
| 4 |
* Copyright (C) 2001-2013, Dipartimento di Matematica "L. Tonelli", Pisa. |
| 5 |
* License: http://www.gnu.org/licenses/gpl.html GPL version 3 or higher |
| 6 |
* |
| 7 |
* Authors: |
| 8 |
* Leonardo Robol <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "quadratic-poly.h" |
| 12 |
|
| 13 |
#include <float.h> |
| 14 |
|
| 15 |
#define LOG2 0.69314718055994530941 |
| 16 |
|
| 17 |
/** |
| 18 |
* @param root The approximation whose Newton correction shall be computed. |
| 19 |
* @param corr The output value where the newton correction will be stored. |
| 20 |
* |
| 21 |
* This sample computes the 'Quadratic polynomial by |
| 22 |
* means of the relation: p=1+x*p**2, starting with p=1 |
| 23 |
*/ |
| 24 |
void |
| 25 |
mps_quadratic_poly_fnewton (mps_context * ctx, mps_polynomial * poly, mps_approximation * root, cplx_t corr) |
| 26 |
{ |
| 27 |
cplx_t p, pp, pt, tmp; |
| 28 |
double ap, ax, eps; |
| 29 |
int i, m, n = poly->degree; |
| 30 |
|
| 31 |
cplx_t x; |
| 32 |
mps_approximation_get_fvalue (ctx, root, x); |
| 33 |
|
| 34 |
double rad = mps_approximation_get_frad (ctx, root); |
| 35 |
mps_boolean again = mps_approximation_get_again (ctx, root); |
| 36 |
|
| 37 |
m = (int) (log (n + 1.0) / LOG2); |
| 38 |
if ((1 << m) <= n) |
| 39 |
m++; |
| 40 |
eps = (DBL_EPSILON * 4.0) * n; |
| 41 |
ax = cplx_mod (x); |
| 42 |
|
| 43 |
cplx_set (p, cplx_one); |
| 44 |
cplx_set (pp, cplx_zero); |
| 45 |
ap = 1.0; |
| 46 |
for (i = 1; i <= m; i++) |
| 47 |
{ |
| 48 |
cplx_sqr (tmp, p); |
| 49 |
cplx_mul (pt, x, tmp); |
| 50 |
cplx_add_eq (pt, cplx_one); |
| 51 |
cplx_mul_eq (pp, x); |
| 52 |
cplx_mul_eq (pp, p); |
| 53 |
cplx_mul_eq_d (pp, 2.0); |
| 54 |
cplx_add_eq (pp, tmp); |
| 55 |
cplx_set (p, pt); |
| 56 |
ap = ap * ax + cplx_mod (p); |
| 57 |
} |
| 58 |
ap = ap * ax; |
| 59 |
cplx_div (corr, p, pp); |
| 60 |
|
| 61 |
mps_approximation_set_again (ctx, root, cplx_mod (p) > eps * ap * 3); |
| 62 |
mps_approximation_set_frad (ctx, root, n * (cplx_mod (p) + 3 * ap * eps) / cplx_mod (pp)); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @brief User-defined program for the computation of \f$p\f$, \f$p'\f$. |
| 67 |
* |
| 68 |
* @param s The current mps_context |
| 69 |
* @param poly The mps_polynomial being solved. |
| 70 |
* @param root The approximation whose Newton correction shall be computed. |
| 71 |
* @param corr The output value where the newton correction will be stored. |
| 72 |
* |
| 73 |
* This sample computes the 'Quadratic polynomial by |
| 74 |
* means of the relation: p=1+x*p**2, starting with p=1 |
| 75 |
*/ |
| 76 |
void |
| 77 |
mps_quadratic_poly_dnewton (mps_context * ctx, mps_polynomial * poly, mps_approximation * root, cdpe_t corr) |
| 78 |
{ |
| 79 |
cdpe_t p, pp, pt, tmp; |
| 80 |
rdpe_t ap, ax, eps, temp, apeps, atmp; |
| 81 |
int i, m, n = poly->degree; |
| 82 |
cdpe_t x; |
| 83 |
rdpe_t drad; |
| 84 |
mps_boolean again; |
| 85 |
|
| 86 |
mps_approximation_get_dvalue (ctx, root, x); |
| 87 |
mps_approximation_get_drad (ctx, root, drad); |
| 88 |
again = mps_approximation_get_again (ctx, root); |
| 89 |
|
| 90 |
m = (int) (log (n + 1.0) / LOG2); |
| 91 |
if ((1 << m) <= n) |
| 92 |
m++; |
| 93 |
rdpe_set_d (eps, DBL_EPSILON); |
| 94 |
rdpe_mul_eq_d (eps, (double) 4 * n); |
| 95 |
cdpe_mod (ax, x); |
| 96 |
|
| 97 |
cdpe_set (p, cdpe_one); |
| 98 |
cdpe_set (pp, cdpe_zero); |
| 99 |
rdpe_set (ap, rdpe_one); |
| 100 |
for (i = 1; i <= m; i++) |
| 101 |
{ |
| 102 |
cdpe_sqr (tmp, p); |
| 103 |
cdpe_mul (pt, x, tmp); |
| 104 |
cdpe_add_eq (pt, cdpe_one); |
| 105 |
cdpe_mul_eq (pp, x); |
| 106 |
cdpe_mul_eq (pp, p); |
| 107 |
cdpe_mul_eq_d (pp, 2.0); |
| 108 |
cdpe_add_eq (pp, tmp); |
| 109 |
cdpe_set (p, pt); |
| 110 |
rdpe_mul_eq (ap, ax); |
| 111 |
cdpe_mod (atmp, p); |
| 112 |
rdpe_add_eq (ap, atmp); |
| 113 |
} |
| 114 |
rdpe_mul_eq (ap, ax); |
| 115 |
cdpe_div (corr, p, pp); |
| 116 |
|
| 117 |
cdpe_mod (temp, p); |
| 118 |
rdpe_mul (apeps, ap, eps); |
| 119 |
rdpe_mul_eq_d (apeps, 3.0); |
| 120 |
|
| 121 |
mps_approximation_set_again (ctx, root, rdpe_gt (temp, apeps)); |
| 122 |
|
| 123 |
rdpe_add (drad, temp, apeps); |
| 124 |
rdpe_mul_eq_d (drad, (double) n); |
| 125 |
cdpe_mod (temp, pp); |
| 126 |
rdpe_div_eq (drad, temp); |
| 127 |
if (rdpe_eq (drad, rdpe_zero)) |
| 128 |
rdpe_mul (drad, ax, eps); |
| 129 |
|
| 130 |
mps_approximation_set_drad (ctx, root, drad); |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* @brief User-defined program for the computation of \f$p\f$, \f$p'\f$. |
| 135 |
* |
| 136 |
* @param s The current mps_context |
| 137 |
* @param poly The mps_polynomial being solved. |
| 138 |
* @param root The approximation whose Newton correction shall be computed. |
| 139 |
* @param corr The output value where the newton correction will be stored. |
| 140 |
* |
| 141 |
* This sample computes the 'Quadratic polynomial by |
| 142 |
* means of the relation: p=1+x*p**2, starting with p=1 |
| 143 |
*/ |
| 144 |
void |
| 145 |
mps_quadratic_poly_mnewton (mps_context * ctx, mps_polynomial * poly, |
| 146 |
mps_approximation * root, mpc_t corr, long int wp) |
| 147 |
{ |
| 148 |
int i, m, n = poly->degree; |
| 149 |
rdpe_t ap, ax, eps, temp, apeps, atmp, epsilon, drad; |
| 150 |
cdpe_t ctmp; |
| 151 |
mpc_t p, pp, pt, tmp, x; |
| 152 |
mps_boolean again; |
| 153 |
|
| 154 |
mpc_init2 (p, wp); |
| 155 |
mpc_init2 (pp, wp); |
| 156 |
mpc_init2 (pt, wp); |
| 157 |
mpc_init2 (tmp, wp); |
| 158 |
|
| 159 |
mpc_init2 (x, wp); |
| 160 |
mps_approximation_get_mvalue (ctx, root, x); |
| 161 |
mps_approximation_get_drad (ctx, root, drad); |
| 162 |
again = mps_approximation_get_again (ctx, root); |
| 163 |
|
| 164 |
rdpe_set_2dl (epsilon, 1.0, 2 - wp); |
| 165 |
|
| 166 |
m = (int) (log (n + 1.0) / LOG2); |
| 167 |
if ((1 << m) <= n) |
| 168 |
m++; |
| 169 |
rdpe_set (eps, epsilon); |
| 170 |
rdpe_mul_eq_d (eps, (double) 4 * n); |
| 171 |
mpc_get_cdpe (ctmp, x); |
| 172 |
cdpe_mod (ax, ctmp); |
| 173 |
|
| 174 |
mpc_set_ui (p, 1, 0); |
| 175 |
mpc_set_ui (pp, 0, 0); |
| 176 |
rdpe_set (ap, rdpe_one); |
| 177 |
for (i = 1; i <= m; i++) |
| 178 |
{ |
| 179 |
mpc_sqr (tmp, p); |
| 180 |
mpc_mul (pt, x, tmp); |
| 181 |
mpc_add_eq_ui (pt, 1, 0); |
| 182 |
mpc_mul_eq (pp, x); |
| 183 |
mpc_mul_eq (pp, p); |
| 184 |
mpc_mul_eq_ui (pp, 2); |
| 185 |
mpc_add_eq (pp, tmp); |
| 186 |
mpc_set (p, pt); |
| 187 |
rdpe_mul_eq (ap, ax); |
| 188 |
mpc_get_cdpe (ctmp, p); |
| 189 |
cdpe_mod (atmp, ctmp); |
| 190 |
rdpe_add_eq (ap, atmp); |
| 191 |
} |
| 192 |
rdpe_mul_eq (ap, ax); |
| 193 |
mpc_div (corr, p, pp); |
| 194 |
|
| 195 |
mpc_get_cdpe (ctmp, p); |
| 196 |
cdpe_mod (temp, ctmp); |
| 197 |
rdpe_mul (apeps, ap, eps); |
| 198 |
rdpe_mul_eq_d (apeps, 3.0); |
| 199 |
mps_approximation_set_again (ctx, root, rdpe_gt (temp, apeps)); |
| 200 |
|
| 201 |
rdpe_add (drad, temp, apeps); |
| 202 |
rdpe_mul_eq_d (drad, (double) n); |
| 203 |
mpc_get_cdpe (ctmp, pp); |
| 204 |
cdpe_mod (temp, ctmp); |
| 205 |
rdpe_div_eq (drad, temp); |
| 206 |
if (rdpe_eq (drad, rdpe_zero)) |
| 207 |
rdpe_mul (drad, ax, eps); |
| 208 |
|
| 209 |
mps_approximation_set_drad (ctx, root, drad); |
| 210 |
mps_approximation_set_again (ctx, root, again); |
| 211 |
|
| 212 |
mpc_clear (tmp); |
| 213 |
mpc_clear (pt); |
| 214 |
mpc_clear (pp); |
| 215 |
mpc_clear (p); |
| 216 |
mpc_clear (x); |
| 217 |
} |