| 1 | /* LegendreSeries.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 "LegendreSeries.h"cxx . . $ `<file>/dwsys/LegendreSeries.h`/ |
| 20 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 21 | |
| 22 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (LegendreSeries, FunctionSeries, 0); |
| 23 | |
| 24 | double structLegendreSeriescxx . . $ structLegendreSeries# :: v_evaluatecxx . . $ structLegendreSeries#v_evaluate(447c67c6676f75c3). (double x) { |
| 25 | if (x < ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin. || x > ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax.) |
| 26 | return undefinedcxx . . $ undefined.; |
| 27 | /* |
| 28 | Transform x from domain [xmin, xmax] to domain [-1, 1] |
| 29 | */ |
| 30 | double p = ourcxx . . $ `melder/melder.h:42:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 31 | double pim1 = x = (2 * 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.); |
| 32 | if (numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. > 1) { |
| 33 | const double twox = 2.0 * x; |
| 34 | double pim2 = 1, f2 = x, d = 1.0; |
| 35 | p += ourcxx . . $ `melder/melder.h:42:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] * pim1; |
| 36 | for (integercxx . . $ integer# i = 3; i <= ourcxx . . $ `melder/melder.h:42:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.; i ++) { |
| 37 | const double f1 = d ++; |
| 38 | f2 += twox; |
| 39 | const double pi = (f2 * pim1 - f1 * pim2) / d; |
| 40 | p += ourcxx . . $ `melder/melder.h:42:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] * pi; |
| 41 | pim2 = pim1; |
| 42 | pim1 = pi; |
| 43 | } |
| 44 | } |
| 45 | return p; |
| 46 | } |
| 47 | |
| 48 | void structLegendreSeriescxx . . $ structLegendreSeries# :: v_evaluateTermscxx . . $ structLegendreSeries#v_evaluateTerms(c7d2ecbf502d4cf4). (double x, VECcxx . . $ VEC# terms) { |
| 49 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (terms.size == numberOfCoefficients); |
| 50 | if (x < ourcxx . . $ `melder/melder.h:42:9`! xmincxx . . $ structFunction#xmin. || x > ourcxx . . $ `melder/melder.h:42:9`! xmaxcxx . . $ structFunction#xmax.) { |
| 51 | termscxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(c555d170d72ff256). undefinedcxx . . $ undefined.; |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | // Transform x from domain [xmin, xmax] to domain [-1, 1] |
| 56 | |
| 57 | 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.); |
| 58 | |
| 59 | terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 60 | if (ourcxx . . $ `melder/melder.h:42:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. > 1) { |
| 61 | const double twox = 2.0 * x; |
| 62 | double f2 = x, d = 1.0; |
| 63 | terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).2] = x; |
| 64 | for (integercxx . . $ integer# i = 3; i <= numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.; i ++) { |
| 65 | const double f1 = d ++; |
| 66 | f2 += twox; |
| 67 | terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = (f2 * terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] - f1 * terms [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 2]) / d; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void structLegendreSeriescxx . . $ structLegendreSeries# :: v_getExtremacxx . . $ structLegendreSeries#v_getExtrema(a54784ab049e7c7d). (double x1, double x2, double *out_xmin, double *out_ymin, double *out_xmax, double *out_ymax) { |
| 73 | try { |
| 74 | autoPolynomialcxx . . $ autoPolynomial# p = LegendreSeries_to_Polynomialcxx . . $ LegendreSeries_to_Polynomial(8a88b583f10ad7c8). (this); |
| 75 | FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (p.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), x1, x2, out_xmin, out_ymin, out_xmax, out_ymax); |
| 76 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 77 | Melder_clearErrorcxx . . $ Melder_clearError(49f6e7a06ebc5aa8). (); |
| 78 | structFunctionSeriescxx . . $ structFunctionSeries# :: v_getExtremacxx . . $ structFunctionSeries#v_getExtrema(a54784ab049e7c7d). (x1, x2, out_xmin, out_ymin, out_xmax, out_ymax); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | autoLegendreSeriescxx . . $ autoLegendreSeries# LegendreSeries_createcxx . . $ LegendreSeries_create(8b24ab7125498910). (double xmin, double xmax, integercxx . . $ integer# numberOfPolynomials) { |
| 83 | try { |
| 84 | autoLegendreSeriescxx . . $ autoLegendreSeries# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (LegendreSeries); |
| 85 | FunctionSeries_initcxx . . $ FunctionSeries_init(421c74ba8b5a8d9). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), xmin, xmax, numberOfPolynomials); |
| 86 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 87 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 88 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"LegendreSeries not created."); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | autoLegendreSeriescxx . . $ autoLegendreSeries# LegendreSeries_createWithCoefficientscxx . . $ LegendreSeries_createWithCoefficients(c7bbc40308a62043). (double xmin, double xmax, constVECVUcxx . . $ constVECVU# const& coefficients) { |
| 93 | try { |
| 94 | autoLegendreSeriescxx . . $ autoLegendreSeries# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (LegendreSeries); |
| 95 | FunctionSeries_initWithCoefficientscxx . . $ FunctionSeries_initWithCoefficients(1bec5b455a00e302). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), xmin, xmax, coefficients, false); |
| 96 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 97 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 98 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"LegendreSeries not created from string."); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | autoLegendreSeriescxx . . $ autoLegendreSeries# LegendreSeries_getDerivativecxx . . $ LegendreSeries_getDerivative(e3d92eb9f8cd9f10). (LegendreSeriescxx . . $ LegendreSeries# me) { |
| 103 | try { |
| 104 | autoLegendreSeriescxx . . $ autoLegendreSeries# thee = LegendreSeries_createcxx . . $ LegendreSeries_create(8b24ab7125498910). (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin., mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax., mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1); |
| 105 | |
| 106 | for (integercxx . . $ integer# n = 1; n <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1; n ++) { |
| 107 | /* |
| 108 | P [n]'(x) = Sum (k=0..nonNegative, (2n - 4k - 1) P [n-2k-1](x)) |
| 109 | */ |
| 110 | integercxx . . $ integer# n2 = n - 1; |
| 111 | for (integercxx . . $ integer# k = 0; n2 >= 0; k ++, n2 -= 2) |
| 112 | thycxx . . $ `melder/melder.h:45:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).n2 + 1] += (2 * n - 4 * k - 1) * mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).n + 1]; |
| 113 | } |
| 114 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 115 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 116 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no derivative created."); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | autoPolynomialcxx . . $ autoPolynomial# LegendreSeries_to_Polynomialcxx . . $ LegendreSeries_to_Polynomial(8a88b583f10ad7c8). (LegendreSeriescxx . . $ LegendreSeries# me) { |
| 121 | try { |
| 122 | const double xmin = -1, xmax = 1; |
| 123 | autoPolynomialcxx . . $ autoPolynomial# thee = Polynomial_createcxx . . $ Polynomial_create(7da2dc008a3770b9). (xmin, xmax, mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1); |
| 124 | |
| 125 | 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] */ |
| 126 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. == 1) |
| 127 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 128 | |
| 129 | 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]; /* * p [2] */ |
| 130 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. > 2) { |
| 131 | autoVECcxx . . $ autoVEC# pn = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.); |
| 132 | autoVECcxx . . $ autoVEC# pnm1 = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.); |
| 133 | autoVECcxx . . $ autoVEC# pnm2 = zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients.); |
| 134 | |
| 135 | // Start the recursion: P [1] = x; P [0] = 1; |
| 136 | |
| 137 | pnm1 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).2] = 1.0; |
| 138 | pnm2 [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1] = 1.0; |
| 139 | for (integercxx . . $ integer# n = 2; n <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficientscxx . . $ structFunctionSeries#numberOfCoefficients. - 1; n ++) { |
| 140 | const double a = (2.0 * n - 1.0) / (double) n; |
| 141 | const double c = - (n - 1.0) / (double) n; |
| 142 | NUMpolynomial_recurrencecxx . . $ NUMpolynomial_recurrence(627eaf5e3a164f6a). (pn.partcxx . . $ autovector#part(2d8d48654ca64e70). (1, n + 1), a, 0, c, pnm1cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).(), pnm2cxx . . $ constvector#constvector(eac9d6ec4c4bfecd)..getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 143 | if (mycxx . . $ `melder/melder.h:44:9`! coefficientscxx . . $ structFunctionSeries#coefficients. [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).n + 1] != 0.0) |
| 144 | for (integercxx . . $ integer# j = 1; j <= n + 1; j ++) |
| 145 | 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]; |
| 146 | autoVECcxx . . $ autoVEC# pn_old = stdcxx . . $ autovector#autovector(d33c649a3aceae2f).::movecxx . . $ std/move(e35c19a1ba7baa26). (pn); |
| 147 | pn =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26). (pnm2); |
| 148 | pnm2 =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26). (pnm1); |
| 149 | pnm1 =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26). (pn_old); |
| 150 | } |
| 151 | } |
| 152 | if (mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. != xmin || mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. != xmax) |
| 153 | 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.); |
| 154 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 155 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 156 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": not converted to Polynomial."); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* end of file LegendreSeries.cpp */ |