1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package com.irurueta.numerical.polynomials.estimators;
17
18 import com.irurueta.algebra.AlgebraException;
19 import com.irurueta.algebra.Matrix;
20 import com.irurueta.algebra.Utils;
21 import com.irurueta.numerical.LockedException;
22 import com.irurueta.numerical.NotReadyException;
23 import com.irurueta.numerical.polynomials.Polynomial;
24 import com.irurueta.numerical.robust.WeightSelection;
25 import com.irurueta.sorting.SortingException;
26
27 import java.util.List;
28
29
30
31
32
33
34
35
36
37 @SuppressWarnings("DuplicatedCode")
38 public class WeightedPolynomialEstimator extends PolynomialEstimator {
39
40
41
42
43 public static final int DEFAULT_MAX_EVALUATIONS = 50;
44
45
46
47
48
49 public static final boolean DEFAULT_SORT_WEIGHTS = true;
50
51
52
53
54 private int maxEvaluations = DEFAULT_MAX_EVALUATIONS;
55
56
57
58
59
60 private boolean sortWeights = DEFAULT_SORT_WEIGHTS;
61
62
63
64
65 private double[] weights;
66
67
68
69
70 public WeightedPolynomialEstimator() {
71 super();
72 }
73
74
75
76
77
78
79
80 public WeightedPolynomialEstimator(final int degree) {
81 super(degree);
82 }
83
84
85
86
87
88
89
90
91
92
93
94 public WeightedPolynomialEstimator(
95 final List<PolynomialEvaluation> evaluations, final double[] weights) {
96 super();
97 internalSetEvaluationsAndWeights(evaluations, weights);
98 }
99
100
101
102
103
104
105 public WeightedPolynomialEstimator(final PolynomialEstimatorListener listener) {
106 super(listener);
107 }
108
109
110
111
112
113
114
115
116
117
118
119
120 public WeightedPolynomialEstimator(
121 final int degree, final List<PolynomialEvaluation> evaluations, final double[] weights) {
122 super(degree);
123 internalSetEvaluationsAndWeights(evaluations, weights);
124 }
125
126
127
128
129
130
131
132
133 public WeightedPolynomialEstimator(final int degree, final PolynomialEstimatorListener listener) {
134 super(degree, listener);
135 }
136
137
138
139
140
141
142
143
144
145
146
147
148 public WeightedPolynomialEstimator(
149 final List<PolynomialEvaluation> evaluations, final double[] weights,
150 final PolynomialEstimatorListener listener) {
151 super(listener);
152 internalSetEvaluationsAndWeights(evaluations, weights);
153 }
154
155
156
157
158
159
160
161
162
163
164
165
166
167 public WeightedPolynomialEstimator(
168 final int degree, final List<PolynomialEvaluation> evaluations, final double[] weights,
169 final PolynomialEstimatorListener listener) {
170 super(degree, listener);
171 internalSetEvaluationsAndWeights(evaluations, weights);
172 }
173
174
175
176
177
178
179
180
181
182
183 @Override
184 public void setEvaluations(final List<PolynomialEvaluation> evaluations) {
185 throw new IllegalArgumentException("evaluations and weights must be provided at once");
186 }
187
188
189
190
191
192
193
194
195
196
197
198
199
200 public void setEvaluationsAndWeights(
201 final List<PolynomialEvaluation> evaluations, final double[] weights) throws LockedException {
202 if (isLocked()) {
203 throw new LockedException();
204 }
205 internalSetEvaluationsAndWeights(evaluations, weights);
206 }
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222 public void setDegreeEvaluationsAndWeights(
223 final int degree, final List<PolynomialEvaluation> evaluations, final double[] weights)
224 throws LockedException {
225 setDegree(degree);
226 setEvaluationsAndWeights(evaluations, weights);
227 }
228
229
230
231
232
233
234
235
236
237 public double[] getWeights() {
238 return weights;
239 }
240
241
242
243
244
245
246
247 public boolean areWeightsAvailable() {
248 return weights != null;
249 }
250
251
252
253
254
255
256
257 public int getMaxEvaluations() {
258 return maxEvaluations;
259 }
260
261
262
263
264
265
266
267
268
269
270
271
272 public void setMaxEvaluations(final int maxEvaluations) throws LockedException {
273 if (isLocked()) {
274 throw new LockedException();
275 }
276 if (maxEvaluations < getMinNumberOfEvaluations()) {
277 throw new IllegalArgumentException();
278 }
279 this.maxEvaluations = maxEvaluations;
280 }
281
282
283
284
285
286
287
288 public boolean isSortWeightsEnabled() {
289 return sortWeights;
290 }
291
292
293
294
295
296
297
298
299 public void setSortWeightsEnabled(final boolean sortWeights) throws LockedException {
300 if (isLocked()) {
301 throw new LockedException();
302 }
303
304 this.sortWeights = sortWeights;
305 }
306
307
308
309
310
311
312
313 @Override
314 public boolean isReady() {
315 return super.isReady() && areWeightsAvailable() && evaluations.size() == weights.length;
316 }
317
318
319
320
321
322
323
324
325
326 @Override
327 public Polynomial estimate() throws LockedException, NotReadyException, PolynomialEstimationException {
328 if (isLocked()) {
329 throw new LockedException();
330 }
331 if (!isReady()) {
332 throw new NotReadyException();
333 }
334
335 try {
336 locked = true;
337 if (listener != null) {
338 listener.onEstimateStart(this);
339 }
340
341 final var selection = WeightSelection.selectWeights(weights, sortWeights, maxEvaluations);
342 final var selected = selection.getSelected();
343 final var nEvaluations = selection.getNumSelected();
344
345
346 final var a = new Matrix(nEvaluations, degree + 1);
347 final var b = new Matrix(nEvaluations, 1);
348
349 var index = 0;
350 var counter = 0;
351 double weight;
352 for (final var evaluation : evaluations) {
353 if (selected[index]) {
354 weight = weights[index];
355
356 switch (evaluation.getType()) {
357 case DIRECT_EVALUATION:
358 fillDirectEvaluation((DirectPolynomialEvaluation) evaluation, a, b, counter);
359 break;
360 case DERIVATIVE_EVALUATION:
361 fillDerivativeEvaluation((DerivativePolynomialEvaluation) evaluation, a, b, counter);
362 break;
363 case INTEGRAL_EVALUATION:
364 fillIntegralEvaluation((IntegralPolynomialEvaluation) evaluation, a, b, counter);
365 break;
366 case INTEGRAL_INTERVAL:
367 fillIntegralIntervalEvaluation((IntegralIntervalPolynomialEvaluation) evaluation, a, b,
368 counter);
369 break;
370 default:
371 continue;
372 }
373
374 normalize(a, b, counter, weight);
375 counter++;
376 }
377
378 index++;
379 }
380
381 final var params = Utils.solve(a, b);
382
383 final var result = new Polynomial(params.toArray());
384
385 if (listener != null) {
386 listener.onEstimateEnd(this);
387 }
388
389 return result;
390 } catch (final AlgebraException | SortingException e) {
391 throw new PolynomialEstimationException(e);
392 } finally {
393 locked = false;
394 }
395 }
396
397
398
399
400
401
402 @Override
403 public PolynomialEstimatorType getType() {
404 return PolynomialEstimatorType.WEIGHTED_POLYNOMIAL_ESTIMATOR;
405 }
406
407
408
409
410
411
412
413
414
415
416 private static void normalize(final Matrix a, final Matrix b, final int row, final double weight) {
417 var sqrNorm = 0.0;
418 for (var i = 0; i < a.getColumns(); i++) {
419 sqrNorm += Math.pow(a.getElementAt(row, i), 2.0);
420 }
421 sqrNorm += Math.pow(b.getElementAtIndex(row), 2.0);
422
423 final var norm = Math.sqrt(sqrNorm);
424 final var factor = weight / norm;
425
426 for (var i = 0; i < a.getColumns(); i++) {
427 a.setElementAt(row, i, a.getElementAt(row, i) * factor);
428 }
429 b.setElementAtIndex(row, b.getElementAtIndex(row) * factor);
430 }
431
432
433
434
435
436
437
438
439
440 private void internalSetEvaluationsAndWeights(
441 final List<PolynomialEvaluation> evaluations, final double[] weights) {
442 if (weights == null || evaluations == null || weights.length != evaluations.size()) {
443 throw new IllegalArgumentException();
444 }
445 this.evaluations = evaluations;
446 this.weights = weights;
447 }
448 }