001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math3.analysis.interpolation;
018
019 import org.apache.commons.math3.analysis.UnivariateFunction;
020 import org.apache.commons.math3.util.MathUtils;
021 import org.apache.commons.math3.util.MathArrays;
022 import org.apache.commons.math3.exception.NumberIsTooSmallException;
023
024 /**
025 * Adapter for classes implementing the {@link UnivariateInterpolator}
026 * interface.
027 * The data to be interpolated is assumed to be periodic. Thus values that are
028 * outside of the range can be passed to the interpolation function: They will
029 * be wrapped into the initial range before being passed to the class that
030 * actually computes the interpolation.
031 *
032 * @version $Id: UnivariatePeriodicInterpolator.java 1379904 2012-09-01 23:54:52Z erans $
033 */
034 public class UnivariatePeriodicInterpolator
035 implements UnivariateInterpolator {
036 /** Default number of extension points of the samples array. */
037 public static final int DEFAULT_EXTEND = 5;
038 /** Interpolator. */
039 private final UnivariateInterpolator interpolator;
040 /** Period. */
041 private final double period;
042 /** Number of extension points. */
043 private final int extend;
044
045 /**
046 * Builds an interpolator.
047 *
048 * @param interpolator Interpolator.
049 * @param period Period.
050 * @param extend Number of points to be appended at the beginning and
051 * end of the sample arrays in order to avoid interpolation failure at
052 * the (periodic) boundaries of the orginal interval. The value is the
053 * number of sample points which the original {@code interpolator} needs
054 * on each side of the interpolated point.
055 */
056 public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
057 double period,
058 int extend) {
059 this.interpolator = interpolator;
060 this.period = period;
061 this.extend = extend;
062 }
063
064 /**
065 * Builds an interpolator.
066 * Uses {@link #DEFAULT_EXTEND} as the number of extension points on each side
067 * of the original abscissae range.
068 *
069 * @param interpolator Interpolator.
070 * @param period Period.
071 */
072 public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
073 double period) {
074 this(interpolator, period, DEFAULT_EXTEND);
075 }
076
077 /**
078 * {@inheritDoc}
079 *
080 * @throws NumberIsTooSmallException if the number of extension points
081 * iss larger then the size of {@code xval}.
082 */
083 public UnivariateFunction interpolate(double[] xval,
084 double[] yval)
085 throws NumberIsTooSmallException {
086 if (xval.length < extend) {
087 throw new NumberIsTooSmallException(xval.length, extend, true);
088 }
089
090 MathArrays.checkOrder(xval);
091 final double offset = xval[0];
092
093 final int len = xval.length + extend * 2;
094 final double[] x = new double[len];
095 final double[] y = new double[len];
096 for (int i = 0; i < xval.length; i++) {
097 final int index = i + extend;
098 x[index] = MathUtils.reduce(xval[i], period, offset);
099 y[index] = yval[i];
100 }
101
102 // Wrap to enable interpolation at the boundaries.
103 for (int i = 0; i < extend; i++) {
104 int index = xval.length - extend + i;
105 x[i] = MathUtils.reduce(xval[index], period, offset) - period;
106 y[i] = yval[index];
107
108 index = len - extend + i;
109 x[index] = MathUtils.reduce(xval[i], period, offset) + period;
110 y[index] = yval[i];
111 }
112
113 MathArrays.sortInPlace(x, y);
114
115 final UnivariateFunction f = interpolator.interpolate(x, y);
116 return new UnivariateFunction() {
117 public double value(final double x) {
118 return f.value(MathUtils.reduce(x, period, offset));
119 }
120 };
121 }
122 }