1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.hipparchus.analysis.interpolation;
23
24 import org.hipparchus.analysis.UnivariateFunction;
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalArgumentException;
27 import org.hipparchus.util.MathArrays;
28 import org.hipparchus.util.MathUtils;
29
30
31
32
33
34
35
36
37
38
39 public class UnivariatePeriodicInterpolator
40 implements UnivariateInterpolator {
41
42 public static final int DEFAULT_EXTEND = 5;
43
44 private final UnivariateInterpolator interpolator;
45
46 private final double period;
47
48 private final int extend;
49
50
51
52
53
54
55
56
57
58
59
60
61 public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
62 double period,
63 int extend) {
64 this.interpolator = interpolator;
65 this.period = period;
66 this.extend = extend;
67 }
68
69
70
71
72
73
74
75
76
77 public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
78 double period) {
79 this(interpolator, period, DEFAULT_EXTEND);
80 }
81
82
83
84
85
86
87
88 @Override
89 public UnivariateFunction interpolate(double[] xval,
90 double[] yval)
91 throws MathIllegalArgumentException {
92 if (xval.length < extend) {
93 throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL,
94 xval.length, extend);
95 }
96
97 MathArrays.checkOrder(xval);
98 final double offset = xval[0];
99
100 final int len = xval.length + extend * 2;
101 final double[] x = new double[len];
102 final double[] y = new double[len];
103 for (int i = 0; i < xval.length; i++) {
104 final int index = i + extend;
105 x[index] = MathUtils.reduce(xval[i], period, offset);
106 y[index] = yval[i];
107 }
108
109
110 for (int i = 0; i < extend; i++) {
111 int index = xval.length - extend + i;
112 x[i] = MathUtils.reduce(xval[index], period, offset) - period;
113 y[i] = yval[index];
114
115 index = len - extend + i;
116 x[index] = MathUtils.reduce(xval[i], period, offset) + period;
117 y[index] = yval[i];
118 }
119
120 MathArrays.sortInPlace(x, y);
121
122 final UnivariateFunction f = interpolator.interpolate(x, y);
123 return new UnivariateFunction() {
124
125 @Override
126 public double value(final double x) throws MathIllegalArgumentException {
127 return f.value(MathUtils.reduce(x, period, offset));
128 }
129 };
130 }
131 }