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.math4.legacy.analysis.interpolation;

  18. import org.apache.commons.numbers.angle.Reduce;
  19. import org.apache.commons.numbers.arrays.SortInPlace;
  20. import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
  21. import org.apache.commons.math4.legacy.exception.MathIllegalArgumentException;
  22. import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
  23. import org.apache.commons.math4.legacy.exception.NumberIsTooSmallException;
  24. import org.apache.commons.math4.legacy.core.MathArrays;

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

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

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

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

  87.         MathArrays.checkOrder(xval);
  88.         final double offset = xval[0];
  89.         final Reduce reduce = new Reduce(offset, period);

  90.         final int len = xval.length + extend * 2;
  91.         final double[] x = new double[len];
  92.         final double[] y = new double[len];
  93.         for (int i = 0; i < xval.length; i++) {
  94.             final int index = i + extend;
  95.             x[index] = reduce.applyAsDouble(xval[i]);
  96.             y[index] = yval[i];
  97.         }

  98.         // Wrap to enable interpolation at the boundaries.
  99.         for (int i = 0; i < extend; i++) {
  100.             int index = xval.length - extend + i;
  101.             x[i] = reduce.applyAsDouble(xval[index]) - period;
  102.             y[i] = yval[index];

  103.             index = len - extend + i;
  104.             x[index] = reduce.applyAsDouble(xval[i]) + period;
  105.             y[index] = yval[i];
  106.         }

  107.         SortInPlace.ASCENDING.apply(x, y);

  108.         final UnivariateFunction f = interpolator.interpolate(x, y);
  109.         return new UnivariateFunction() {
  110.             /** {@inheritDoc} */
  111.             @Override
  112.             public double value(final double x) throws MathIllegalArgumentException {
  113.                 return f.value(reduce.applyAsDouble(x));
  114.             }
  115.         };
  116.     }
  117. }