Debian Codegraph @

dwsys/LegendreSeries.cpp

raw symbols SCIP
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"
20 #include "NUM2.h"
21
22 Thing_implement (LegendreSeries, FunctionSeries, 0);
23
24 double structLegendreSeries :: v_evaluate (double x) {
25 if (x < our xmin || x > our xmax)
26 return undefined;
27 /*
28 Transform x from domain [xmin, xmax] to domain [-1, 1]
29 */
30 double p = our coefficients [1];
31 double pim1 = x = (2 * x - our xmin - our xmax) / (our xmax - our xmin);
32 if (numberOfCoefficients > 1) {
33 const double twox = 2.0 * x;
34 double pim2 = 1, f2 = x, d = 1.0;
35 p += our coefficients [2] * pim1;
36 for (integer i = 3; i <= our numberOfCoefficients; i ++) {
37 const double f1 = d ++;
38 f2 += twox;
39 const double pi = (f2 * pim1 - f1 * pim2) / d;
40 p += our coefficients [i] * pi;
41 pim2 = pim1;
42 pim1 = pi;
43 }
44 }
45 return p;
46 }
47
48 void structLegendreSeries :: v_evaluateTerms (double x, VEC terms) {
49 Melder_assert (terms.size == numberOfCoefficients);
50 if (x < our xmin || x > our xmax) {
51 terms <<= undefined;
52 return;
53 }
54
55 // Transform x from domain [xmin, xmax] to domain [-1, 1]
56
57 x = (2.0 * x - our xmin - our xmax) / (our xmax - our xmin);
58
59 terms [1] = 1.0;
60 if (our numberOfCoefficients > 1) {
61 const double twox = 2.0 * x;
62 double f2 = x, d = 1.0;
63 terms [2] = x;
64 for (integer i = 3; i <= numberOfCoefficients; i ++) {
65 const double f1 = d ++;
66 f2 += twox;
67 terms [i] = (f2 * terms [i - 1] - f1 * terms [i - 2]) / d;
68 }
69 }
70 }
71
72 void structLegendreSeries :: v_getExtrema (double x1, double x2, double *out_xmin, double *out_ymin, double *out_xmax, double *out_ymax) {
73 try {
74 autoPolynomial p = LegendreSeries_to_Polynomial (this);
75 FunctionSeries_getExtrema (p.get(), x1, x2, out_xmin, out_ymin, out_xmax, out_ymax);
76 } catch (MelderError) {
77 Melder_clearError ();
78 structFunctionSeries :: v_getExtrema (x1, x2, out_xmin, out_ymin, out_xmax, out_ymax);
79 }
80 }
81
82 autoLegendreSeries LegendreSeries_create (double xmin, double xmax, integer numberOfPolynomials) {
83 try {
84 autoLegendreSeries me = Thing_new (LegendreSeries);
85 FunctionSeries_init (me.get(), xmin, xmax, numberOfPolynomials);
86 return me;
87 } catch (MelderError) {
88 Melder_throw (U"LegendreSeries not created.");
89 }
90 }
91
92 autoLegendreSeries LegendreSeries_createWithCoefficients (double xmin, double xmax, constVECVU const& coefficients) {
93 try {
94 autoLegendreSeries me = Thing_new (LegendreSeries);
95 FunctionSeries_initWithCoefficients (me.get(), xmin, xmax, coefficients, false);
96 return me;
97 } catch (MelderError) {
98 Melder_throw (U"LegendreSeries not created from string.");
99 }
100 }
101
102 autoLegendreSeries LegendreSeries_getDerivative (LegendreSeries me) {
103 try {
104 autoLegendreSeries thee = LegendreSeries_create (my xmin, my xmax, my numberOfCoefficients - 1);
105
106 for (integer n = 1; n <= my numberOfCoefficients - 1; n ++) {
107 /*
108 P [n]'(x) = Sum (k=0..nonNegative, (2n - 4k - 1) P [n-2k-1](x))
109 */
110 integer n2 = n - 1;
111 for (integer k = 0; n2 >= 0; k ++, n2 -= 2)
112 thy coefficients [n2 + 1] += (2 * n - 4 * k - 1) * my coefficients [n + 1];
113 }
114 return thee;
115 } catch (MelderError) {
116 Melder_throw (me, U": no derivative created.");
117 }
118 }
119
120 autoPolynomial LegendreSeries_to_Polynomial (LegendreSeries me) {
121 try {
122 const double xmin = -1, xmax = 1;
123 autoPolynomial thee = Polynomial_create (xmin, xmax, my numberOfCoefficients - 1);
124
125 thy coefficients [1] = my coefficients [1]; /* * p [1] */
126 if (my numberOfCoefficients == 1)
127 return thee;
128
129 thy coefficients [2] = my coefficients [2]; /* * p [2] */
130 if (my numberOfCoefficients > 2) {
131 autoVEC pn = zero_VEC (my numberOfCoefficients);
132 autoVEC pnm1 = zero_VEC (my numberOfCoefficients);
133 autoVEC pnm2 = zero_VEC (my numberOfCoefficients);
134
135 // Start the recursion: P [1] = x; P [0] = 1;
136
137 pnm1 [2] = 1.0;
138 pnm2 [1] = 1.0;
139 for (integer n = 2; n <= my 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_recurrence (pn.part (1, n + 1), a, 0, c, pnm1.get(), pnm2.get());
143 if (my coefficients [n + 1] != 0.0)
144 for (integer j = 1; j <= n + 1; j ++)
145 thy coefficients [j] += my coefficients [n + 1] * pn [j];
146 autoVEC pn_old = std::move (pn);
147 pn = std::move (pnm2);
148 pnm2 = std::move (pnm1);
149 pnm1 = std::move (pn_old);
150 }
151 }
152 if (my xmin != xmin || my xmax != xmax)
153 thee = Polynomial_scaleX (thee.get(), my xmin, my xmax);
154 return thee;
155 } catch (MelderError) {
156 Melder_throw (me, U": not converted to Polynomial.");
157 }
158 }
159
160 /* end of file LegendreSeries.cpp */