| 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 |
* Leonardo Robol <[email protected]> |
| 9 |
*/ |
| 10 |
|
| 11 |
#include <mps/mps.h> |
| 12 |
|
| 13 |
void mps_chebyshev_poly_free (mps_context *, mps_polynomial *); |
| 14 |
long int mps_chebyshev_poly_raise_data (mps_context * ctx, mps_polynomial * poly, long int wp); |
| 15 |
void mps_chebyshev_get_leading_coefficient (mps_context * ctx, mps_polynomial * poly, mpc_t lc); |
| 16 |
|
| 17 |
/* This is implemented in chebyshev-evaluation.c */ |
| 18 |
mps_boolean mps_chebyshev_poly_meval (mps_context * ctx, mps_polynomial * poly, mpc_t x, mpc_t value, rdpe_t error); |
| 19 |
|
| 20 |
mps_chebyshev_poly * |
| 21 |
mps_chebyshev_poly_new (mps_context * ctx, int n, mps_structure structure) |
| 22 |
{ |
| 23 |
mps_chebyshev_poly * poly = mps_new (mps_chebyshev_poly); |
| 24 |
|
| 25 |
/* Call the base constructor */ |
| 26 |
MPS_POLYNOMIAL (poly)->degree = n; |
| 27 |
mps_polynomial_init (ctx, MPS_POLYNOMIAL (poly)); |
| 28 |
|
| 29 |
/* Our implementation is still not thread safe */ |
| 30 |
MPS_POLYNOMIAL (poly)->thread_safe = false; |
| 31 |
|
| 32 |
MPS_POLYNOMIAL (poly)->structure = structure; |
| 33 |
|
| 34 |
/* Chances are that these are not really needed. In this case we can |
| 35 |
* simply leave this values set to NULL. They will be allocated on |
| 36 |
* demand by mps_chebyshev_poly_set_coefficient_q(). */ |
| 37 |
poly->rational_real_coeffs = poly->rational_imag_coeffs = NULL; |
| 38 |
|
| 39 |
if (MPS_STRUCTURE_IS_INTEGER (structure) || MPS_STRUCTURE_IS_RATIONAL (structure)) |
| 40 |
{ |
| 41 |
poly->rational_real_coeffs = mps_newv (mpq_t, n + 1); |
| 42 |
poly->rational_imag_coeffs = mps_newv (mpq_t, n + 1); |
| 43 |
|
| 44 |
mpq_vinit (poly->rational_real_coeffs, n + 1); |
| 45 |
mpq_vinit (poly->rational_imag_coeffs, n + 1); |
| 46 |
} |
| 47 |
|
| 48 |
poly->fpc = cplx_valloc (n + 1); |
| 49 |
poly->dpc = cdpe_valloc (n + 1); |
| 50 |
poly->mfpc = mpc_valloc (n + 1); |
| 51 |
|
| 52 |
mpc_vinit2 (poly->mfpc, n + 1, ctx->mpwp); |
| 53 |
|
| 54 |
/* Construct the polynomial vtable in a proper way */ |
| 55 |
MPS_POLYNOMIAL (poly)->free = mps_chebyshev_poly_free; |
| 56 |
MPS_POLYNOMIAL (poly)->raise_data = mps_chebyshev_poly_raise_data; |
| 57 |
MPS_POLYNOMIAL (poly)->meval = mps_chebyshev_poly_meval; |
| 58 |
MPS_POLYNOMIAL (poly)->get_leading_coefficient = mps_chebyshev_get_leading_coefficient; |
| 59 |
|
| 60 |
/* Attach the typename to this polynomial */ |
| 61 |
MPS_POLYNOMIAL (poly)->type_name = MPS_CHEBYSHEV_POLY_TYPE_NAME; |
| 62 |
|
| 63 |
/* Compute the leading coefficient of the polynomial */ |
| 64 |
mpc_init2 (poly->lc, ctx->mpwp); |
| 65 |
if (n > 0) |
| 66 |
{ |
| 67 |
mpc_set_ui (poly->lc, 2U, 0U); |
| 68 |
mpc_pow_si (poly->lc, poly->lc, n - 1); |
| 69 |
} |
| 70 |
else |
| 71 |
{ |
| 72 |
mpc_set_ui (poly->lc, 1U, 0U); |
| 73 |
} |
| 74 |
|
| 75 |
pthread_mutex_init (&poly->precision_mutex, NULL); |
| 76 |
|
| 77 |
return poly; |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
mps_chebyshev_poly_free (mps_context * ctx, mps_polynomial * poly) |
| 82 |
{ |
| 83 |
mps_chebyshev_poly * cpoly = MPS_CHEBYSHEV_POLY (poly); |
| 84 |
|
| 85 |
mpc_vclear (cpoly->mfpc, poly->degree + 1); |
| 86 |
mpc_clear (cpoly->lc); |
| 87 |
|
| 88 |
cplx_vfree (cpoly->fpc); |
| 89 |
cdpe_vfree (cpoly->dpc); |
| 90 |
mpc_vfree (cpoly->mfpc); |
| 91 |
|
| 92 |
if (MPS_STRUCTURE_IS_INTEGER (poly->structure) || MPS_STRUCTURE_IS_RATIONAL (poly->structure)) |
| 93 |
{ |
| 94 |
mpq_vclear (cpoly->rational_real_coeffs, poly->degree + 1); |
| 95 |
mpq_vclear (cpoly->rational_imag_coeffs, poly->degree + 1); |
| 96 |
|
| 97 |
free (cpoly->rational_real_coeffs); |
| 98 |
free (cpoly->rational_imag_coeffs); |
| 99 |
} |
| 100 |
|
| 101 |
pthread_mutex_destroy(&cpoly->precision_mutex); |
| 102 |
|
| 103 |
free (poly); |
| 104 |
} |
| 105 |
|
| 106 |
long int |
| 107 |
mps_chebyshev_poly_raise_data (mps_context * ctx, mps_polynomial * poly, long int wp) |
| 108 |
{ |
| 109 |
int i; |
| 110 |
mps_chebyshev_poly * cpoly = MPS_CHEBYSHEV_POLY (poly); |
| 111 |
|
| 112 |
pthread_mutex_lock (&cpoly->precision_mutex); |
| 113 |
|
| 114 |
/* Check if raising precision is a worth operation, or if we are already |
| 115 |
* to a higher preicison. */ |
| 116 |
if (wp < mpc_get_prec (cpoly->mfpc[0])) |
| 117 |
{ |
| 118 |
pthread_mutex_unlock (&cpoly->precision_mutex); |
| 119 |
return mpc_get_prec (cpoly->mfpc[0]); |
| 120 |
} |
| 121 |
|
| 122 |
mpc_set_prec (cpoly->lc, wp); |
| 123 |
mpc_set_ui (cpoly->lc, 2U, 0U); |
| 124 |
mpc_pow_si (cpoly->lc, cpoly->lc, MAX (poly->degree - 1, 0)); |
| 125 |
|
| 126 |
/* Otherwise really increase it */ |
| 127 |
for (i = 0; i <= poly->degree; i++) |
| 128 |
{ |
| 129 |
mpc_set_prec (cpoly->mfpc[i], wp); |
| 130 |
} |
| 131 |
|
| 132 |
if (MPS_STRUCTURE_IS_INTEGER (poly->structure) || |
| 133 |
MPS_STRUCTURE_IS_RATIONAL (poly->structure)) |
| 134 |
{ |
| 135 |
/* Regenerate the coefficients starting from the exact input |
| 136 |
* of the user. */ |
| 137 |
for (i = 0; i <= poly->degree; i++) |
| 138 |
{ |
| 139 |
mpf_set_q (mpc_Re (cpoly->mfpc[i]), cpoly->rational_real_coeffs[i]); |
| 140 |
mpf_set_q (mpc_Im (cpoly->mfpc[i]), cpoly->rational_imag_coeffs[i]); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
pthread_mutex_unlock (&cpoly->precision_mutex); |
| 145 |
|
| 146 |
return mpc_get_prec (cpoly->mfpc[0]); |
| 147 |
} |
| 148 |
|
| 149 |
void mps_chebyshev_get_leading_coefficient (mps_context * ctx, mps_polynomial * poly, mpc_t lc) |
| 150 |
{ |
| 151 |
mps_chebyshev_poly * cpoly = MPS_CHEBYSHEV_POLY (poly); |
| 152 |
|
| 153 |
mpc_set (lc, cpoly->lc); |
| 154 |
mpc_mul_eq (lc, cpoly->mfpc[poly->degree]); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
void |
| 159 |
mps_chebyshev_poly_set_coefficient_f (mps_context * ctx, mps_chebyshev_poly * poly, |
| 160 |
int i, mpc_t coeff) |
| 161 |
{ |
| 162 |
if (mpc_get_prec (coeff) > mpc_get_prec (poly->mfpc[0])) |
| 163 |
{ |
| 164 |
mps_chebyshev_poly_raise_data (ctx, MPS_POLYNOMIAL (poly), mpc_get_prec (coeff)); |
| 165 |
} |
| 166 |
|
| 167 |
mpc_set (poly->mfpc[i], coeff); |
| 168 |
mpc_get_cdpe (poly->dpc[i], coeff); |
| 169 |
mpc_get_cplx (poly->fpc[i], coeff); |
| 170 |
} |
| 171 |
|
| 172 |
void |
| 173 |
mps_chebyshev_poly_set_coefficient_q (mps_context * ctx, mps_chebyshev_poly * poly, |
| 174 |
int i, mpq_t real_part, mpq_t imag_part) |
| 175 |
{ |
| 176 |
mps_chebyshev_poly * cpoly = MPS_CHEBYSHEV_POLY (poly); |
| 177 |
|
| 178 |
mpq_set (cpoly->rational_real_coeffs[i], real_part); |
| 179 |
mpq_set (cpoly->rational_imag_coeffs[i], imag_part); |
| 180 |
|
| 181 |
mpf_set_q (mpc_Re (cpoly->mfpc[i]), real_part); |
| 182 |
mpf_set_q (mpc_Im (cpoly->mfpc[i]), imag_part); |
| 183 |
} |
| 184 |
|
| 185 |
void |
| 186 |
mps_chebyshev_poly_set_coefficient_i (mps_context * ctx, mps_chebyshev_poly * poly, |
| 187 |
int i, long int real_coeff, long int imag_coeff) |
| 188 |
{ |
| 189 |
mps_chebyshev_poly * cpoly = MPS_CHEBYSHEV_POLY (poly); |
| 190 |
|
| 191 |
mpq_set_si (cpoly->rational_real_coeffs[i], real_coeff, 1L); |
| 192 |
mpq_set_si (cpoly->rational_imag_coeffs[i], imag_coeff, 1L); |
| 193 |
|
| 194 |
mpf_set_q (mpc_Re (cpoly->mfpc[i]), cpoly->rational_real_coeffs[i]); |
| 195 |
mpf_set_q (mpc_Im (cpoly->mfpc[i]), cpoly->rational_imag_coeffs[i]); |
| 196 |
} |
| 197 |
|