| 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"cxx . . $ `<file>/dwsys/ChebyshevSeries.h`/ |
| 20 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 21 | |
| 22 | /****** ChebyshevSeries ******************************************/ |
| 23 | |
| 24 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (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 structChebyshevSeriescxx . . $ structChebyshevSeries# :: v_evaluatecxx . . $ structChebyshevSeries#v_evaluate(447c67c6676f75c3). (double x) { |
| 35 | if (x < ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin. || x > ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax.) |
| 36 | return undefinedcxx . . $ undefined.; |
| 37 | |
| 38 | double d1 = 0.0, d2 = 0.0; |
| 39 | if (numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. > 1) { |
| 40 | /* |
| 41 | Transform x from domain [xmin, xmax] to domain [-1, 1] |
| 42 | */ |
| 43 | x = (2.0 * x - ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin. - ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax.) / (ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax. - ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin.); |
| 44 | const double twox = 2.0 * x; |
| 45 | for (integercxx . . $ integer# i = ourcxx . . $ `melder/melder.h:42:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.; i > 1; i --) { |
| 46 | const double tmp = d1; |
| 47 | d1 = twox * d1 - d2 + ourcxx . . $ `melder/melder.h:42:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]; |
| 48 | d2 = tmp; |
| 49 | } |
| 50 | } |
| 51 | return x * d1 - d2 + ourcxx . . $ `melder/melder.h:42:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | T [n](x) = 2*x*T [n-1] - T [n-2](x) n >= 2 |
| 56 | */ |
| 57 | void structChebyshevSeriescxx . . $ structChebyshevSeries# :: v_evaluateTermscxx . . $ structChebyshevSeries#v_evaluateTerms(c7d2ecbf502d4cf4). (double x, VECcxx . . $ VEC# terms) { |
| 58 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (terms.size == numberOfCoefficients); |
| 59 | if (x < ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin. || x > ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax.) { |
| 60 | termscxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(c555d170d72ff256). undefinedcxx . . $ undefined.; |
| 61 | return; |
| 62 | } |
| 63 | terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 64 | if (numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. > 1) { |
| 65 | /* |
| 66 | Transform x from domain [xmin, xmax] to domain [-1, 1] |
| 67 | */ |
| 68 | terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = x = (2.0 * x - xmincxx . . $ structFunction#xmin. - xmaxcxx . . $ structFunction#xmax.) / (xmaxcxx . . $ structFunction#xmax. - xmincxx . . $ structFunction#xmin.); |
| 69 | |
| 70 | for (integercxx . . $ integer# i = 3; i <= numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.; i ++) |
| 71 | terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = 2.0 * x * terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] - terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 2]; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void structChebyshevSeriescxx . . $ structChebyshevSeries# :: v_getExtremacxx . . $ structChebyshevSeries#v_getExtrema(a54784ab049e7c7d). (double x1, double x2, double *out_xmin, double *out_ymin, double *out_xmax, double *out_ymax) { |
| 76 | try { |
| 77 | autoPolynomialcxx . . $ autoPolynomial# p = ChebyshevSeries_to_Polynomialcxx . . $ ChebyshevSeries_to_Polynomial(decf9667bc295c4d). (this); |
| 78 | FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), x1, x2, out_xmin, out_ymin, out_xmax, out_ymax); |
| 79 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 80 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (this, U"Extrema cannot be calculated"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | autoChebyshevSeriescxx . . $ autoChebyshevSeries# ChebyshevSeries_createcxx . . $ ChebyshevSeries_create(d7b2205d89dbf313). (double lxmin, double lxmax, integercxx . . $ integer# numberOfPolynomials) { |
| 85 | try { |
| 86 | autoChebyshevSeriescxx . . $ autoChebyshevSeries# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (ChebyshevSeries); |
| 87 | FunctionSeries_initcxx . . $ FunctionSeries_init(421c74ba8b5a8d9). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), lxmin, lxmax, numberOfPolynomials); |
| 88 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 89 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 90 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ChebyshevSeries not created."); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | autoChebyshevSeriescxx . . $ autoChebyshevSeries# ChebyshevSeries_createWithCoefficientscxx . . $ ChebyshevSeries_createWithCoefficients(e40dfba4ba1d2b5). (double lxmin, double lxmax, constVECVUcxx . . $ constVECVU# const& coefficients) { |
| 95 | try { |
| 96 | autoChebyshevSeriescxx . . $ autoChebyshevSeries# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (ChebyshevSeries); |
| 97 | FunctionSeries_initWithCoefficientscxx . . $ FunctionSeries_initWithCoefficients(1bec5b455a00e302). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), lxmin, lxmax, coefficients, false); |
| 98 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 99 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 100 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ChebyshevSeries not created from string."); |
| 101 | }; |
| 102 | } |
| 103 | |
| 104 | autoPolynomialcxx . . $ autoPolynomial# ChebyshevSeries_to_Polynomialcxx . . $ ChebyshevSeries_to_Polynomial(decf9667bc295c4d). (ChebyshevSeriescxx . . $ ChebyshevSeries# me) { |
| 105 | try { |
| 106 | const double xmin = -1.0, xmax = 1.0; |
| 107 | |
| 108 | autoPolynomialcxx . . $ autoPolynomial# thee = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (xmin, xmax, mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1); |
| 109 | |
| 110 | thycxx . . $ `melder/melder.h:45:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] /* * p [1] */; |
| 111 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. == 1) |
| 112 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 113 | |
| 114 | thycxx . . $ `melder/melder.h:45:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2]; |
| 115 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. > 2) { |
| 116 | autoVECcxx . . $ autoVEC# pn = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.); |
| 117 | autoVECcxx . . $ autoVEC# pnm1 = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.); |
| 118 | autoVECcxx . . $ autoVEC# pnm2 = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.); |
| 119 | |
| 120 | // Start the recursion: T [2] = x; T [1] = 1; |
| 121 | |
| 122 | pnm1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = 1.0; |
| 123 | pnm2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 124 | const double a = 2.0, b = 0.0, c = -1.0; |
| 125 | for (integercxx . . $ integer# n = 2; n <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1; n ++) { |
| 126 | NUMpolynomial_recurrencecxx . . $ NUMpolynomial_recurrence(627eaf5e3a164f6a). (pn.partcxx . . $ autovector#part(2d8d48654ca64e70). (1, n + 1), a, b, c, pnm1cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585). (), pnm2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 127 | if (mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).n + 1] != 0.0) |
| 128 | for (integercxx . . $ integer# j = 1; j <= n + 1; j ++) |
| 129 | thycxx . . $ `melder/melder.h:45:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j] += mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).n + 1] * pn [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).j]; |
| 130 | autoVECcxx . . $ autoVEC# pn_old = stdcxx . . $ autovector#autovector(d33c649a3aceae2f).::movecxx . . $ std/move(e35c19a1ba7baa26). (pn); |
| 131 | pn =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26). (pnm2); |
| 132 | pnm2 =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26). (pnm1); |
| 133 | pnm1 =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26). (pn_old); |
| 134 | } |
| 135 | } |
| 136 | if (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. != xmin || mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. != xmax) |
| 137 | thee =cxx . . $ autoSomeThing#`operator=`(80cc59814f7161e8). Polynomial_scaleXcxx . . $ Polynomial_scaleX(5d8bde0bd4bb491f). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.); |
| 138 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 139 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 140 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U"; not converted to Polynomial."); |
| 141 | }; |
| 142 | } |
| 143 | |
| 144 | /* End of file ChebyshevSeries.cpp */ |