| 1 |
/* ChebyshevSeries.cpp |
| 2 |
* |
| 3 |
* Copyright (C) 2020 David Weenink |
| 4 |
* |
| 5 |
* This code is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 8 |
* your option) any later version. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "ChebyshevSeries.h" |
| 20 |
#include "NUM2.h" |
| 21 |
|
| 22 |
/****** ChebyshevSeries ******************************************/ |
| 23 |
|
| 24 |
Thing_implement (ChebyshevSeries, FunctionSeries, 0); |
| 25 |
|
| 26 |
/* |
| 27 |
p(x) = sum (k=1..numberOfCoefficients, c [k]*T [k](x')) - c [1] / 2; |
| 28 |
Numerical evaluation via Clenshaw's recurrence equation (NRC: 5.8.11) |
| 29 |
d [m+1] = d [m] = 0; |
| 30 |
d [j] = 2 x' d [j+1] - d [j+2] + c [j]; |
| 31 |
p(x) = d [0] = x' d [1] - d [2] + c [0] / 2; |
| 32 |
x' = (2 * x - xmin - xmax) / (xmax - xmin) |
| 33 |
*/ |
| 34 |
double structChebyshevSeries :: v_evaluate (double x) { |
| 35 |
if (x < our xmin || x > our xmax) |
| 36 |
return undefined; |
| 37 |
|
| 38 |
double d1 = 0.0, d2 = 0.0; |
| 39 |
if (numberOfCoefficients > 1) { |
| 40 |
/* |
| 41 |
Transform x from domain [xmin, xmax] to domain [-1, 1] |
| 42 |
*/ |
| 43 |
x = (2.0 * x - our xmin - our xmax) / (our xmax - our xmin); |
| 44 |
const double twox = 2.0 * x; |
| 45 |
for (integer i = our numberOfCoefficients; i > 1; i --) { |
| 46 |
const double tmp = d1; |
| 47 |
d1 = twox * d1 - d2 + our coefficients [i]; |
| 48 |
d2 = tmp; |
| 49 |
} |
| 50 |
} |
| 51 |
return x * d1 - d2 + our coefficients [1]; |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
T [n](x) = 2*x*T [n-1] - T [n-2](x) n >= 2 |
| 56 |
*/ |
| 57 |
void structChebyshevSeries :: v_evaluateTerms (double x, VEC terms) { |
| 58 |
Melder_assert (terms.size == numberOfCoefficients); |
| 59 |
if (x < our xmin || x > our xmax) { |
| 60 |
terms <<= undefined; |
| 61 |
return; |
| 62 |
} |
| 63 |
terms [1] = 1.0; |
| 64 |
if (numberOfCoefficients > 1) { |
| 65 |
/* |
| 66 |
Transform x from domain [xmin, xmax] to domain [-1, 1] |
| 67 |
*/ |
| 68 |
terms [2] = x = (2.0 * x - xmin - xmax) / (xmax - xmin); |
| 69 |
|
| 70 |
for (integer i = 3; i <= numberOfCoefficients; i ++) |
| 71 |
terms [i] = 2.0 * x * terms [i - 1] - terms [i - 2]; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
void structChebyshevSeries :: v_getExtrema (double x1, double x2, double *out_xmin, double *out_ymin, double *out_xmax, double *out_ymax) { |
| 76 |
try { |
| 77 |
autoPolynomial p = ChebyshevSeries_to_Polynomial (this); |
| 78 |
FunctionSeries_getExtrema (p.get(), x1, x2, out_xmin, out_ymin, out_xmax, out_ymax); |
| 79 |
} catch (MelderError) { |
| 80 |
Melder_throw (this, U"Extrema cannot be calculated"); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
autoChebyshevSeries ChebyshevSeries_create (double lxmin, double lxmax, integer numberOfPolynomials) { |
| 85 |
try { |
| 86 |
autoChebyshevSeries me = Thing_new (ChebyshevSeries); |
| 87 |
FunctionSeries_init (me.get(), lxmin, lxmax, numberOfPolynomials); |
| 88 |
return me; |
| 89 |
} catch (MelderError) { |
| 90 |
Melder_throw (U"ChebyshevSeries not created."); |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
autoChebyshevSeries ChebyshevSeries_createWithCoefficients (double lxmin, double lxmax, constVECVU const& coefficients) { |
| 95 |
try { |
| 96 |
autoChebyshevSeries me = Thing_new (ChebyshevSeries); |
| 97 |
FunctionSeries_initWithCoefficients (me.get(), lxmin, lxmax, coefficients, false); |
| 98 |
return me; |
| 99 |
} catch (MelderError) { |
| 100 |
Melder_throw (U"ChebyshevSeries not created from string."); |
| 101 |
}; |
| 102 |
} |
| 103 |
|
| 104 |
autoPolynomial ChebyshevSeries_to_Polynomial (ChebyshevSeries me) { |
| 105 |
try { |
| 106 |
const double xmin = -1.0, xmax = 1.0; |
| 107 |
|
| 108 |
autoPolynomial thee = Polynomial_create (xmin, xmax, my numberOfCoefficients - 1); |
| 109 |
|
| 110 |
thy coefficients [1] = my coefficients [1] /* * p [1] */; |
| 111 |
if (my numberOfCoefficients == 1) |
| 112 |
return thee; |
| 113 |
|
| 114 |
thy coefficients [2] = my coefficients [2]; |
| 115 |
if (my numberOfCoefficients > 2) { |
| 116 |
autoVEC pn = zero_VEC (my numberOfCoefficients); |
| 117 |
autoVEC pnm1 = zero_VEC (my numberOfCoefficients); |
| 118 |
autoVEC pnm2 = zero_VEC (my numberOfCoefficients); |
| 119 |
|
| 120 |
// Start the recursion: T [2] = x; T [1] = 1; |
| 121 |
|
| 122 |
pnm1 [2] = 1.0; |
| 123 |
pnm2 [1] = 1.0; |
| 124 |
const double a = 2.0, b = 0.0, c = -1.0; |
| 125 |
for (integer n = 2; n <= my numberOfCoefficients - 1; n ++) { |
| 126 |
NUMpolynomial_recurrence (pn.part (1, n + 1), a, b, c, pnm1.get (), pnm2.get()); |
| 127 |
if (my coefficients [n + 1] != 0.0) |
| 128 |
for (integer j = 1; j <= n + 1; j ++) |
| 129 |
thy coefficients [j] += my coefficients [n + 1] * pn [j]; |
| 130 |
autoVEC pn_old = std::move (pn); |
| 131 |
pn = std::move (pnm2); |
| 132 |
pnm2 = std::move (pnm1); |
| 133 |
pnm1 = std::move (pn_old); |
| 134 |
} |
| 135 |
} |
| 136 |
if (my xmin != xmin || my xmax != xmax) |
| 137 |
thee = Polynomial_scaleX (thee.get(), my xmin, my xmax); |
| 138 |
return thee; |
| 139 |
} catch (MelderError) { |
| 140 |
Melder_throw (me, U"; not converted to Polynomial."); |
| 141 |
}; |
| 142 |
} |
| 143 |
|
| 144 |
/* End of file ChebyshevSeries.cpp */ |