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.  *      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.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.analysis.interpolation;

  22. import org.hipparchus.analysis.UnivariateFunction;
  23. import org.hipparchus.exception.LocalizedCoreFormats;
  24. import org.hipparchus.exception.MathIllegalArgumentException;
  25. import org.hipparchus.util.MathArrays;
  26. import org.hipparchus.util.MathUtils;

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

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

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

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

  90.         MathArrays.checkOrder(xval);
  91.         final double offset = xval[0];

  92.         final int len = xval.length + extend * 2;
  93.         final double[] x = new double[len];
  94.         final double[] y = new double[len];
  95.         for (int i = 0; i < xval.length; i++) {
  96.             final int index = i + extend;
  97.             x[index] = MathUtils.reduce(xval[i], period, offset);
  98.             y[index] = yval[i];
  99.         }

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

  105.             index = len - extend + i;
  106.             x[index] = MathUtils.reduce(xval[i], period, offset) + period;
  107.             y[index] = yval[i];
  108.         }

  109.         MathArrays.sortInPlace(x, y);

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