UnivariatePeriodicInterpolator.java

  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.  *      http://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. package org.apache.commons.math3.analysis.interpolation;

  18. import org.apache.commons.math3.analysis.UnivariateFunction;
  19. import org.apache.commons.math3.util.MathUtils;
  20. import org.apache.commons.math3.util.MathArrays;
  21. import org.apache.commons.math3.exception.MathIllegalArgumentException;
  22. import org.apache.commons.math3.exception.NonMonotonicSequenceException;
  23. import org.apache.commons.math3.exception.NumberIsTooSmallException;

  24. /**
  25.  * Adapter for classes implementing the {@link UnivariateInterpolator}
  26.  * interface.
  27.  * The data to be interpolated is assumed to be periodic. Thus values that are
  28.  * outside of the range can be passed to the interpolation function: They will
  29.  * be wrapped into the initial range before being passed to the class that
  30.  * actually computes the interpolation.
  31.  *
  32.  */
  33. public class UnivariatePeriodicInterpolator
  34.     implements UnivariateInterpolator {
  35.     /** Default number of extension points of the samples array. */
  36.     public static final int DEFAULT_EXTEND = 5;
  37.     /** Interpolator. */
  38.     private final UnivariateInterpolator interpolator;
  39.     /** Period. */
  40.     private final double period;
  41.     /** Number of extension points. */
  42.     private final int extend;

  43.     /**
  44.      * Builds an interpolator.
  45.      *
  46.      * @param interpolator Interpolator.
  47.      * @param period Period.
  48.      * @param extend Number of points to be appended at the beginning and
  49.      * end of the sample arrays in order to avoid interpolation failure at
  50.      * the (periodic) boundaries of the orginal interval. The value is the
  51.      * number of sample points which the original {@code interpolator} needs
  52.      * on each side of the interpolated point.
  53.      */
  54.     public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
  55.                                           double period,
  56.                                           int extend) {
  57.         this.interpolator = interpolator;
  58.         this.period = period;
  59.         this.extend = extend;
  60.     }

  61.     /**
  62.      * Builds an interpolator.
  63.      * Uses {@link #DEFAULT_EXTEND} as the number of extension points on each side
  64.      * of the original abscissae range.
  65.      *
  66.      * @param interpolator Interpolator.
  67.      * @param period Period.
  68.      */
  69.     public UnivariatePeriodicInterpolator(UnivariateInterpolator interpolator,
  70.                                           double period) {
  71.         this(interpolator, period, DEFAULT_EXTEND);
  72.     }

  73.     /**
  74.      * {@inheritDoc}
  75.      *
  76.      * @throws NumberIsTooSmallException if the number of extension points
  77.      * is larger than the size of {@code xval}.
  78.      */
  79.     public UnivariateFunction interpolate(double[] xval,
  80.                                           double[] yval)
  81.         throws NumberIsTooSmallException, NonMonotonicSequenceException {
  82.         if (xval.length < extend) {
  83.             throw new NumberIsTooSmallException(xval.length, extend, true);
  84.         }

  85.         MathArrays.checkOrder(xval);
  86.         final double offset = xval[0];

  87.         final int len = xval.length + extend * 2;
  88.         final double[] x = new double[len];
  89.         final double[] y = new double[len];
  90.         for (int i = 0; i < xval.length; i++) {
  91.             final int index = i + extend;
  92.             x[index] = MathUtils.reduce(xval[i], period, offset);
  93.             y[index] = yval[i];
  94.         }

  95.         // Wrap to enable interpolation at the boundaries.
  96.         for (int i = 0; i < extend; i++) {
  97.             int index = xval.length - extend + i;
  98.             x[i] = MathUtils.reduce(xval[index], period, offset) - period;
  99.             y[i] = yval[index];

  100.             index = len - extend + i;
  101.             x[index] = MathUtils.reduce(xval[i], period, offset) + period;
  102.             y[index] = yval[i];
  103.         }

  104.         MathArrays.sortInPlace(x, y);

  105.         final UnivariateFunction f = interpolator.interpolate(x, y);
  106.         return new UnivariateFunction() {
  107.             public double value(final double x) throws MathIllegalArgumentException {
  108.                 return f.value(MathUtils.reduce(x, period, offset));
  109.             }
  110.         };
  111.     }
  112. }