1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 /*
19 * This is not the original file distributed by the Apache Software Foundation
20 * It has been modified by the Hipparchus project
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 * Adapter for classes implementing the {@link UnivariateInterpolator}
32 * interface.
33 * The data to be interpolated is assumed to be periodic. Thus values that are
34 * outside of the range can be passed to the interpolation function: They will
35 * be wrapped into the initial range before being passed to the class that
36 * actually computes the interpolation.
37 *
38 */
39 public class UnivariatePeriodicInterpolator
40 implements UnivariateInterpolator {
41 /** Default number of extension points of the samples array. */
42 public static final int DEFAULT_EXTEND = 5;
43 /** Interpolator. */
44 private final UnivariateInterpolator interpolator;
45 /** Period. */
46 private final double period;
47 /** Number of extension points. */
48 private final int extend;
49
50 /**
51 * Builds an interpolator.
52 *
53 * @param interpolator Interpolator.
54 * @param period Period.
55 * @param extend Number of points to be appended at the beginning and
56 * end of the sample arrays in order to avoid interpolation failure at
57 * the (periodic) boundaries of the orginal interval. The value is the
58 * number of sample points which the original {@code interpolator} needs
59 * on each side of the interpolated point.
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 * Builds an interpolator.
71 * Uses {@link #DEFAULT_EXTEND} as the number of extension points on each side
72 * of the original abscissae range.
73 *
74 * @param interpolator Interpolator.
75 * @param period Period.
76 */
77 public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
78 double period) {
79 this(interpolator, period, DEFAULT_EXTEND);
80 }
81
82 /**
83 * {@inheritDoc}
84 *
85 * @throws MathIllegalArgumentException if the number of extension points
86 * is larger than the size of {@code xval}.
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 // Wrap to enable interpolation at the boundaries.
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 /** {@inheritDoc} */
125 @Override
126 public double value(final double x) throws MathIllegalArgumentException {
127 return f.value(MathUtils.reduce(x, period, offset));
128 }
129 };
130 }
131 }