Debian Codegraph @

dwsys/FunctionSeries.cpp

raw symbols SCIP
1 /* FunctionSeries.cpp
2 *
3 * Copyright (C) 1993-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 "FunctionSeries.h"cxx . . $ `<file>/dwsys/FunctionSeries.h`/
20 #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/
21
22 #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/
23 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
24 #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/
25 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
26 #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/
27 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
28 #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/
29 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
30 #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/
31 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
32 #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/
33 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
34 #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/
35 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
36 #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/
37 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
38 #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/
39 #include "FunctionSeries_def.h"cxx . . $ `<file>/dwsys/FunctionSeries_def.h`/
40
41 Thing_implementcxx . . $ `sys/Thing.h:65:9`! (FunctionSeries, Function, 0);
42
43 double structFunctionSeries :: v_evaluate (double /* x */) {
44 return undefinedcxx . . $ undefined.;
45 }
46
47 dcomplexcxx . . $ dcomplex# structFunctionSeries :: v_evaluate_z (dcomplexcxx . . $ dcomplex# /* z */) {
48 return {cxx . . $ std/complex#complex(bc12c9ec15586e6a). undefinedcxx . . $ undefined., undefinedcxx . . $ undefined. };
49 }
50
51 void structFunctionSeries :: v_evaluateTerms (double /* x */, VECcxx . . $ VEC# terms) {
52 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (terms.size == numberOfCoefficients);
53 termscxx . . $ vectorview#vectorview(eac9d6ec4c4bfecd). <<=cxx . . $ `operator<<=`(c555d170d72ff256). undefinedcxx . . $ undefined.;
54 }
55
56 integercxx . . $ integer# structFunctionSeries :: v_getDegree () {
57 return numberOfCoefficients - 1;
58 }
59
60 void structFunctionSeries :: v_getExtrema (double x1, double x2, double *out_xmin, double *out_ymin, double *out_xmax, double *out_ymax) { // David, geen aparte naam hier nodig: ???
61 const integercxx . . $ integer# numberOfPoints = 1000;
62
63 // Melder_warning (L"defaultGetExtrema: extrema calculated by sampling the interval");
64
65 const double dx = (x2 - x1) / (numberOfPoints - 1);
66 double x = x1;
67 double xmn = x, xmx = xmn, ymn = v_evaluate (x), ymx = ymn; // xmin, xmax .. would shadow member
68 for (integercxx . . $ integer# i = 2; i <= numberOfPoints; i ++) {
69 x += dx;
70 const double y = v_evaluate (x);
71 if (y > ymx) {
72 ymx = y;
73 xmx = x;
74 } else if (y < ymn) {
75 ymn = y;
76 xmn = x;
77 }
78 }
79 if (out_xmin)
80 *out_xmin = xmn;
81 if (out_xmax)
82 *out_xmax = xmx;
83 if (out_ymin)
84 *out_ymin = ymn;
85 if (out_ymax)
86 *out_ymax = ymx;
87 }
88
89 void FunctionSeries_initcxx . . $ FunctionSeries_init(421c74ba8b5a8d9). (FunctionSeries me, double xmin, double xmax, integercxx . . $ integer# numberOfCoefficients) {
90 mycxx . . $ `melder/melder.h:44:9`! coefficients =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (numberOfCoefficients);
91 mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients = numberOfCoefficients;
92 mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. = xmin;
93 mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = xmax;
94 }
95
96 autoFunctionSeries FunctionSeries_createcxx . . $ FunctionSeries_create(2881c019678ef6de). (double xmin, double xmax, integercxx . . $ integer# numberOfCoefficients) {
97 try {
98 autoFunctionSeries me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (FunctionSeries);
99 FunctionSeries_initcxx . . $ FunctionSeries_init(421c74ba8b5a8d9). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), xmin, xmax, numberOfCoefficients);
100 return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).;
101 } catch (MelderErrorcxx . . $ MelderError#) {
102 Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"FunctionSeries not created.");
103 }
104 }
105
106 void FunctionSeries_initWithCoefficientscxx . . $ FunctionSeries_initWithCoefficients(1bec5b455a00e302). (FunctionSeries me, double xmin, double xmax, constVECVUcxx . . $ constVECVU# const& coefficients, bool allowTrailingZeros) {
107 integercxx . . $ integer# numberOfCoefficients = coefficients.sizecxx . . $ constvectorview#size.;
108 if (! allowTrailingZeros)
109 while (coefficients [cxx . . $ constvectorview#`operator[]`(e616e229aa94bfcf).numberOfCoefficients] == 0.0 && numberOfCoefficients > 1)
110 numberOfCoefficients --;
111 FunctionSeries_initcxx . . $ FunctionSeries_init(421c74ba8b5a8d9). (me, xmin, xmax, numberOfCoefficients);
112 mycxx . . $ `melder/melder.h:44:9`! coefficients.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(adea042513329390). coefficients.partcxx . . $ constvectorview#part(27d925039583bea4). (1, numberOfCoefficients);
113 }
114
115 integercxx . . $ integer# FunctionSeries_getDegreecxx . . $ FunctionSeries_getDegree(7a71d4389871b4de). (FunctionSeries me) {
116 return mycxx . . $ `melder/melder.h:44:9`! v_getDegree ();
117 }
118
119 void FunctionSeries_setDomaincxx . . $ FunctionSeries_setDomain(b59ca3cf33084f6f). (FunctionSeries me, double xmin, double xmax) {
120 mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. = xmin;
121 mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax. = xmax;
122 }
123
124 double FunctionSeries_evaluatecxx . . $ FunctionSeries_evaluate(edaf2589c46ec50f). (FunctionSeries me, double x) {
125 return mycxx . . $ `melder/melder.h:44:9`! v_evaluate (x);
126 }
127
128 dcomplexcxx . . $ dcomplex# FunctionSeries_evaluate_zcxx . . $ FunctionSeries_evaluate_z(656feef7db89d164). (FunctionSeries me, dcomplexcxx . . $ dcomplex# z) {
129 return mycxx . . $ `melder/melder.h:44:9`! v_evaluate_z (zcxx . . $ std/complex#complex(20ef1611af677bca).);
130 }
131
132 void FunctionSeries_evaluateTermscxx . . $ FunctionSeries_evaluateTerms(e6566cd39caaad45). (FunctionSeries me, double x, VECcxx . . $ VEC# terms) {
133 mycxx . . $ `melder/melder.h:44:9`! v_evaluateTerms (x, terms);
134 }
135
136 void FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (FunctionSeries me, double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax) {
137 if (x2 <= x1) {
138 x1 = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.;
139 x2 = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.;
140 }
141 mycxx . . $ `melder/melder.h:44:9`! v_getExtrema (x1, x2, xmin, ymin, xmax, ymax);
142 }
143
144 double FunctionSeries_getMinimumcxx . . $ FunctionSeries_getMinimum(da64249f0161698f). (FunctionSeries me, double x1, double x2) {
145 double ymin;
146 FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (me, x1, x2, nullptr, & ymin, nullptr, nullptr);
147 return ymin;
148 }
149
150 double FunctionSeries_getXOfMinimumcxx . . $ FunctionSeries_getXOfMinimum(da64249f0161698f). (FunctionSeries me, double x1, double x2) {
151 double xmin;
152 FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (me, x1, x2, & xmin, nullptr, nullptr, nullptr);
153 return xmin;
154 }
155
156 double FunctionSeries_getMaximumcxx . . $ FunctionSeries_getMaximum(da64249f0161698f). (FunctionSeries me, double x1, double x2) {
157 double ymax;
158 FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (me, x1, x2, nullptr, nullptr, nullptr, & ymax);
159 return ymax;
160 }
161
162 double FunctionSeries_getXOfMaximumcxx . . $ FunctionSeries_getXOfMaximum(da64249f0161698f). (FunctionSeries me, double x1, double x2) {
163 double xmax;
164 FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (me, x1, x2, nullptr, nullptr, & xmax, nullptr);
165 return xmax;
166 }
167
168 static void Graphics_polyline_clipTopBottomcxx . . $ Graphics_polyline_clipTopBottom(be110d8aae414f17). (Graphicscxx . . $ Graphics# g, VECcxx . . $ VEC# x, VECcxx . . $ VEC# y, double ymin, double ymax) {
169 Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (x.size == y.size);
170 integercxx . . $ integer# index = 1;
171
172 if (x.sizecxx . . $ vector#size. < 2)
173 return;
174 double x1 = x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1], y1 = y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).1];
175 double xb = x1, yb = y1;
176
177 for (integercxx . . $ integer# i = 2; i < x.sizecxx . . $ vector#size.; i ++) {
178 const double x2 = x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], y2 = y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
179
180 if (! ((y1 > ymax && y2 > ymax) || (y1 < ymin && y2 < ymin))) {
181 const double dxy = (x2 - x1) / (y1 - y2);
182 const double xcros_max = x1 - (ymax - y1) * dxy;
183 const double xcros_min = x1 - (ymin - y1) * dxy;
184 if (y1 > ymax && y2 < ymax) {
185 /*
186 Line enters from above: start new segment. Save start values.
187 */
188 xb = x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1];
189 yb = y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1];
190 index = i - 1;
191 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] = ymax;
192 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] = xcros_max;
193 }
194 if (y1 > ymin && y2 < ymin) {
195 /*
196 Line leaves at bottom: draw segment. Save end values and restore them
197 Origin of arrays for Graphics_polyline are at element 0 !!!
198 */
199 const double xe = x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], ye = y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
200 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = ymin;
201 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = xcros_min;
202
203 Graphics_polylinecxx . . $ Graphics_polyline(23a712ce109b96e0). (g, i - index + 1,
204 x.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ vector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).() + index,
205 y.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ vector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).() + index);
206
207 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = xb;
208 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = yb;
209 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = xe;
210 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = ye;
211 }
212 if (y1 < ymin && y2 > ymin) {
213 // Line enters from below: start new segment. Save start values
214
215 xb = x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1];
216 yb = y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1];
217 index = i - 1;
218 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] = ymin;
219 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i - 1] = xcros_min;
220 }
221 if (y1 < ymax && y2 > ymax) {
222 // Line leaves at top: draw segment. Save and restore
223
224 const double xe = x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i], ye = y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i];
225 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = ymax;
226 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = xcros_max;
227
228 Graphics_polylinecxx . . $ Graphics_polyline(23a712ce109b96e0). (g, i - index + 1,
229 x.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ vector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).() + index,
230 y.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ vector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).() + index);
231
232 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = xb;
233 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = yb;
234 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = xe;
235 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).i] = ye;
236 }
237 } else {
238 index = i;
239 }
240 y1 = y2;
241 x1 = x2;
242 }
243 if (index < x.sizecxx . . $ vector#size. - 1) {
244 Graphics_polylinecxx . . $ Graphics_polyline(23a712ce109b96e0). (g, x.sizecxx . . $ vector#size. - index,
245 x.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ vector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).() + index,
246 y.asArgumentToFunctionThatExpectsZeroBasedArraycxx . . $ vector#asArgumentToFunctionThatExpectsZeroBasedArray(5a2a78a048fb49a8).() + index);
247 x [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = xb;
248 y [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).index] = yb;
249 }
250 }
251
252 void FunctionSeries_drawcxx . . $ FunctionSeries_draw(b2da1ae81449e92). (FunctionSeries me, Graphicscxx . . $ Graphics# g, double xmin, double xmax, double ymin, double ymax, int extrapolate, bool garnish) {
253 integercxx . . $ integer# numberOfPoints = 1000;
254
255 autoVECcxx . . $ autoVEC# x = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfPoints);
256 autoVECcxx . . $ autoVEC# y = raw_VECcxx . . $ raw_VEC(e4395f764b859b1e). (numberOfPoints);
257
258 Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & xmin, & xmax);
259 double fxmin = xmin, fxmax = xmax;
260 if (! extrapolate) {
261 if (xmax < mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin. || xmin > mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.)
262 return;
263 if (xmin < mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.)
264 fxmin = mycxx . . $ `melder/melder.h:44:9`! xmincxx . . $ structFunction#xmin.;
265 if (xmax > mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.)
266 fxmax = mycxx . . $ `melder/melder.h:44:9`! xmaxcxx . . $ structFunction#xmax.;
267 }
268
269 if (ymax <= ymin) {
270 double x1, x2;
271 FunctionSeries_getExtremacxx . . $ FunctionSeries_getExtrema(e708ecf6762f71f6). (me, fxmin, fxmax, & x1, & ymin, & x2, & ymax);
272 }
273
274 Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g);
275 Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (g, xmin, xmax, ymin, ymax);
276 /*
277 Draw only the parts within [fxmin, fxmax] X [ymin, ymax].
278 */
279 const double dx = (fxmax - fxmin) / (numberOfPoints - 1);
280 for (integercxx . . $ integer# i = 1; i <= numberOfPoints; i ++) {
281 x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = fxmin + (i - 1.0) * dx;
282 y [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i] = FunctionSeries_evaluatecxx . . $ FunctionSeries_evaluate(edaf2589c46ec50f). (me, x [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).i]);
283 }
284 Graphics_polyline_clipTopBottomcxx . . $ Graphics_polyline_clipTopBottom(be110d8aae414f17). (g, x.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), y.getcxx . . $ autovector#get(e8d1da7fe68a6585).(), ymin, ymax);
285 Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g);
286
287 if (garnish) {
288 Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g);
289 Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false);
290 Graphics_marksLeftcxx . . $ Graphics_marksLeft(c3a11a8a55b243ff). (g, 2, true, true, false);
291 }
292 }
293
294 void FunctionSeries_drawBasisFunctioncxx . . $ FunctionSeries_drawBasisFunction(f6ad16e04dcc6d9c). (FunctionSeries me, Graphicscxx . . $ Graphics# g, integercxx . . $ integer# index, double xmin, double xmax, double ymin, double ymax, int extrapolate, bool garnish) {
295 if (index < 1 || index > mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients)
296 return;
297 autoFunctionSeries thee = Data_copycxx . . $ Data_copy(97a542a26794c80e). (me);
298
299 thycxx . . $ `melder/melder.h:45:9`! coefficients.allcxx . . $ autovector#all(a26726dad87555bb).() <<=cxx . . $ `operator<<=`(c555d170d72ff256). 0.0;
300 thycxx . . $ `melder/melder.h:45:9`! coefficients [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index] = 1.0;
301 thycxx . . $ `melder/melder.h:45:9`! numberOfCoefficients = index;
302 FunctionSeries_drawcxx . . $ FunctionSeries_draw(b2da1ae81449e92). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), g, xmin, xmax, ymin, ymax, extrapolate, garnish);
303 }
304
305 void FunctionSeries_setCoefficientcxx . . $ FunctionSeries_setCoefficient(52e2c46cb2df742d). (FunctionSeries me, integercxx . . $ integer# index, double value) {
306 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (index > 0 && index <= mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients,
307 U"Index out of range [1, ", mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients, U"].");
308 Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (value == 0.0 && index < mycxx . . $ `melder/melder.h:44:9`! numberOfCoefficients,
309 U"You should not remove the highest degree term.");
310 mycxx . . $ `melder/melder.h:44:9`! coefficients [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).index] = value;
311 }
312
313 /* end of file FunctionSeries.cpp */