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"
20 #include "NUM2.h"
21
22 #include "oo_DESTROY.h"
23 #include "FunctionSeries_def.h"
24 #include "oo_COPY.h"
25 #include "FunctionSeries_def.h"
26 #include "oo_EQUAL.h"
27 #include "FunctionSeries_def.h"
28 #include "oo_CAN_WRITE_AS_ENCODING.h"
29 #include "FunctionSeries_def.h"
30 #include "oo_WRITE_TEXT.h"
31 #include "FunctionSeries_def.h"
32 #include "oo_WRITE_BINARY.h"
33 #include "FunctionSeries_def.h"
34 #include "oo_READ_TEXT.h"
35 #include "FunctionSeries_def.h"
36 #include "oo_READ_BINARY.h"
37 #include "FunctionSeries_def.h"
38 #include "oo_DESCRIPTION.h"
39 #include "FunctionSeries_def.h"
40
41 Thing_implement (FunctionSeries, Function, 0);
42
43 double structFunctionSeries :: v_evaluate (double /* x */) {
44 return undefined;
45 }
46
47 dcomplex structFunctionSeries :: v_evaluate_z (dcomplex /* z */) {
48 return { undefined, undefined };
49 }
50
51 void structFunctionSeries :: v_evaluateTerms (double /* x */, VEC terms) {
52 Melder_assert (terms.size == numberOfCoefficients);
53 terms <<= undefined;
54 }
55
56 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 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 (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_init (FunctionSeries me, double xmin, double xmax, integer numberOfCoefficients) {
90 my coefficients = zero_VEC (numberOfCoefficients);
91 my numberOfCoefficients = numberOfCoefficients;
92 my xmin = xmin;
93 my xmax = xmax;
94 }
95
96 autoFunctionSeries FunctionSeries_create (double xmin, double xmax, integer numberOfCoefficients) {
97 try {
98 autoFunctionSeries me = Thing_new (FunctionSeries);
99 FunctionSeries_init (me.get(), xmin, xmax, numberOfCoefficients);
100 return me;
101 } catch (MelderError) {
102 Melder_throw (U"FunctionSeries not created.");
103 }
104 }
105
106 void FunctionSeries_initWithCoefficients (FunctionSeries me, double xmin, double xmax, constVECVU const& coefficients, bool allowTrailingZeros) {
107 integer numberOfCoefficients = coefficients.size;
108 if (! allowTrailingZeros)
109 while (coefficients [numberOfCoefficients] == 0.0 && numberOfCoefficients > 1)
110 numberOfCoefficients --;
111 FunctionSeries_init (me, xmin, xmax, numberOfCoefficients);
112 my coefficients.all() <<= coefficients.part (1, numberOfCoefficients);
113 }
114
115 integer FunctionSeries_getDegree (FunctionSeries me) {
116 return my v_getDegree ();
117 }
118
119 void FunctionSeries_setDomain (FunctionSeries me, double xmin, double xmax) {
120 my xmin = xmin;
121 my xmax = xmax;
122 }
123
124 double FunctionSeries_evaluate (FunctionSeries me, double x) {
125 return my v_evaluate (x);
126 }
127
128 dcomplex FunctionSeries_evaluate_z (FunctionSeries me, dcomplex z) {
129 return my v_evaluate_z (z);
130 }
131
132 void FunctionSeries_evaluateTerms (FunctionSeries me, double x, VEC terms) {
133 my v_evaluateTerms (x, terms);
134 }
135
136 void FunctionSeries_getExtrema (FunctionSeries me, double x1, double x2, double *xmin, double *ymin, double *xmax, double *ymax) {
137 if (x2 <= x1) {
138 x1 = my xmin;
139 x2 = my xmax;
140 }
141 my v_getExtrema (x1, x2, xmin, ymin, xmax, ymax);
142 }
143
144 double FunctionSeries_getMinimum (FunctionSeries me, double x1, double x2) {
145 double ymin;
146 FunctionSeries_getExtrema (me, x1, x2, nullptr, & ymin, nullptr, nullptr);
147 return ymin;
148 }
149
150 double FunctionSeries_getXOfMinimum (FunctionSeries me, double x1, double x2) {
151 double xmin;
152 FunctionSeries_getExtrema (me, x1, x2, & xmin, nullptr, nullptr, nullptr);
153 return xmin;
154 }
155
156 double FunctionSeries_getMaximum (FunctionSeries me, double x1, double x2) {
157 double ymax;
158 FunctionSeries_getExtrema (me, x1, x2, nullptr, nullptr, nullptr, & ymax);
159 return ymax;
160 }
161
162 double FunctionSeries_getXOfMaximum (FunctionSeries me, double x1, double x2) {
163 double xmax;
164 FunctionSeries_getExtrema (me, x1, x2, nullptr, nullptr, & xmax, nullptr);
165 return xmax;
166 }
167
168 static void Graphics_polyline_clipTopBottom (Graphics g, VEC x, VEC y, double ymin, double ymax) {
169 Melder_assert (x.size == y.size);
170 integer index = 1;
171
172 if (x.size < 2)
173 return;
174 double x1 = x [1], y1 = y [1];
175 double xb = x1, yb = y1;
176
177 for (integer i = 2; i < x.size; i ++) {
178 const double x2 = x [i], y2 = y [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 [i - 1];
189 yb = y [i - 1];
190 index = i - 1;
191 y [i - 1] = ymax;
192 x [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 [i], ye = y [i];
200 y [i] = ymin;
201 x [i] = xcros_min;
202
203 Graphics_polyline (g, i - index + 1,
204 x.asArgumentToFunctionThatExpectsZeroBasedArray() + index,
205 y.asArgumentToFunctionThatExpectsZeroBasedArray() + index);
206
207 x [index] = xb;
208 y [index] = yb;
209 x [i] = xe;
210 y [i] = ye;
211 }
212 if (y1 < ymin && y2 > ymin) {
213 // Line enters from below: start new segment. Save start values
214
215 xb = x [i - 1];
216 yb = y [i - 1];
217 index = i - 1;
218 y [i - 1] = ymin;
219 x [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 [i], ye = y [i];
225 y [i] = ymax;
226 x [i] = xcros_max;
227
228 Graphics_polyline (g, i - index + 1,
229 x.asArgumentToFunctionThatExpectsZeroBasedArray() + index,
230 y.asArgumentToFunctionThatExpectsZeroBasedArray() + index);
231
232 x [index] = xb;
233 y [index] = yb;
234 x [i] = xe;
235 y [i] = ye;
236 }
237 } else {
238 index = i;
239 }
240 y1 = y2;
241 x1 = x2;
242 }
243 if (index < x.size - 1) {
244 Graphics_polyline (g, x.size - index,
245 x.asArgumentToFunctionThatExpectsZeroBasedArray() + index,
246 y.asArgumentToFunctionThatExpectsZeroBasedArray() + index);
247 x [index] = xb;
248 y [index] = yb;
249 }
250 }
251
252 void FunctionSeries_draw (FunctionSeries me, Graphics g, double xmin, double xmax, double ymin, double ymax, int extrapolate, bool garnish) {
253 integer numberOfPoints = 1000;
254
255 autoVEC x = raw_VEC (numberOfPoints);
256 autoVEC y = raw_VEC (numberOfPoints);
257
258 Function_unidirectionalAutowindow (me, & xmin, & xmax);
259 double fxmin = xmin, fxmax = xmax;
260 if (! extrapolate) {
261 if (xmax < my xmin || xmin > my xmax)
262 return;
263 if (xmin < my xmin)
264 fxmin = my xmin;
265 if (xmax > my xmax)
266 fxmax = my xmax;
267 }
268
269 if (ymax <= ymin) {
270 double x1, x2;
271 FunctionSeries_getExtrema (me, fxmin, fxmax, & x1, & ymin, & x2, & ymax);
272 }
273
274 Graphics_setInner (g);
275 Graphics_setWindow (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 (integer i = 1; i <= numberOfPoints; i ++) {
281 x [i] = fxmin + (i - 1.0) * dx;
282 y [i] = FunctionSeries_evaluate (me, x [i]);
283 }
284 Graphics_polyline_clipTopBottom (g, x.get(), y.get(), ymin, ymax);
285 Graphics_unsetInner (g);
286
287 if (garnish) {
288 Graphics_drawInnerBox (g);
289 Graphics_marksBottom (g, 2, true, true, false);
290 Graphics_marksLeft (g, 2, true, true, false);
291 }
292 }
293
294 void FunctionSeries_drawBasisFunction (FunctionSeries me, Graphics g, integer index, double xmin, double xmax, double ymin, double ymax, int extrapolate, bool garnish) {
295 if (index < 1 || index > my numberOfCoefficients)
296 return;
297 autoFunctionSeries thee = Data_copy (me);
298
299 thy coefficients.all() <<= 0.0;
300 thy coefficients [index] = 1.0;
301 thy numberOfCoefficients = index;
302 FunctionSeries_draw (thee.get(), g, xmin, xmax, ymin, ymax, extrapolate, garnish);
303 }
304
305 void FunctionSeries_setCoefficient (FunctionSeries me, integer index, double value) {
306 Melder_require (index > 0 && index <= my numberOfCoefficients,
307 U"Index out of range [1, ", my numberOfCoefficients, U"].");
308 Melder_require (value == 0.0 && index < my numberOfCoefficients,
309 U"You should not remove the highest degree term.");
310 my coefficients [index] = value;
311 }
312
313 /* end of file FunctionSeries.cpp */