You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2011/04/28 13:57:11 UTC

svn commit: r1097426 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/analysis/interpolation/ site/xdoc/ test/java/org/apache/commons/math/analysis/interpolation/

Author: erans
Date: Thu Apr 28 11:57:11 2011
New Revision: 1097426

URL: http://svn.apache.org/viewvc?rev=1097426&view=rev
Log:
MATH-562
Interpolator adapter for periodic data.

Added:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolator.java   (with props)
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolatorTest.java   (with props)
Modified:
    commons/proper/math/trunk/src/site/xdoc/changes.xml

Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolator.java?rev=1097426&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolator.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolator.java Thu Apr 28 11:57:11 2011
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.interpolation;
+
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.util.MathUtils;
+import org.apache.commons.math.exception.NumberIsTooSmallException;
+
+/**
+ * Adapter for class implementing the {@link UnivariateRealInterpolator}
+ * interface.
+ * The data to be interpolated is assumed to be periodic. Thus values that are
+ * outside of the range can be passed to the interpolation function: They will
+ * be wrapped into the initial range before being passed to the class that
+ * actually computes the interpolation.
+ *
+ * @version $Id$
+ */
+public class UnivariateRealPeriodicInterpolator
+    implements UnivariateRealInterpolator {
+    /** Default number of extension points of the samples array. */
+    public static final int DEFAULT_EXTEND = 5;
+    /** Interpolator. */
+    private final UnivariateRealInterpolator interpolator;
+    /** Period. */
+    private final double period;
+    /** Number of extension points. */
+    private final int extend;
+
+    /**
+     * Builds an interpolator.
+     *
+     * @param interpolator Interpolator.
+     * @param period Period.
+     * @param extend Number of points to be appended at the beginning and
+     * end of the sample arrays in order to avoid interpolation failure at
+     * the (periodic) boundaries of the orginal interval. The value is the
+     * number of sample points which the original {@code interpolator} needs
+     * on each side of the interpolated point.
+     */
+    public UnivariateRealPeriodicInterpolator(UnivariateRealInterpolator interpolator,
+                                              double period,
+                                              int extend) {
+        this.interpolator = interpolator;
+        this.period = period;
+        this.extend = extend;
+    }
+
+    /**
+     * Builds an interpolator.
+     * Uses {@link #DEFAULT_EXTEND} as the number of extension points on each side
+     * of the original abscissae range.
+     *
+     * @param interpolator Interpolator.
+     * @param period Period.
+     */
+    public UnivariateRealPeriodicInterpolator(UnivariateRealInterpolator interpolator,
+                                              double period) {
+        this(interpolator, period, DEFAULT_EXTEND);
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * @throws NumberIsTooSmallException if the number of extension points
+     * iss larger then the size of {@code xval}.
+     */
+    public UnivariateRealFunction interpolate(double[] xval,
+                                              double[] yval) {
+        if (xval.length < extend) {
+            throw new NumberIsTooSmallException(xval.length, extend, true);
+        }
+
+        MathUtils.checkOrder(xval);
+        final double offset = xval[0];
+
+        final int len = xval.length + extend * 2;
+        final double[] x = new double[len];
+        final double[] y = new double[len];
+        for (int i = 0; i < xval.length; i++) {
+            final int index = i + extend;
+            x[index] = MathUtils.reduce(xval[i], period, offset);
+            y[index] = yval[i];
+        }
+
+        // Wrap to enable interpolation at the boundaries.
+        for (int i = 0; i < extend; i++) {
+            int index = xval.length - extend + i;
+            x[i] = MathUtils.reduce(xval[index], period, offset) - period;
+            y[i] = yval[index];
+
+            index = len - extend + i;
+            x[index] = MathUtils.reduce(xval[i], period, offset) + period;
+            y[index] = yval[i];
+        }
+
+        MathUtils.sortInPlace(x, y);
+
+        final UnivariateRealFunction f = interpolator.interpolate(x, y);
+        return new UnivariateRealFunction() {
+            public double value(final double x) {
+                return f.value(MathUtils.reduce(x, period, offset));
+            }
+        };
+    }
+}

Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1097426&r1=1097425&r2=1097426&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Thu Apr 28 11:57:11 2011
@@ -52,6 +52,9 @@ The <action> type attribute can be add,u
     If the output is not quite correct, check for invisible trailing spaces!
      -->
     <release version="3.0" date="TBD" description="TBD">
+      <action dev="erans" type="fix" issue="MATH-562">
+        Added an interpolator adapter for data with known period.
+      </action>
       <action dev="luc" type="add" issue="MATH-541" >
         Added a "rectangular" Cholesky decomposition for positive semidefinite matrices.
       </action>

Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolatorTest.java?rev=1097426&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolatorTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolatorTest.java Thu Apr 28 11:57:11 2011
@@ -0,0 +1,163 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.math.analysis.interpolation;
+
+import java.util.Random;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.util.FastMath;
+import org.apache.commons.math.exception.NumberIsTooSmallException;
+import org.apache.commons.math.exception.NonMonotonousSequenceException;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+
+/**
+ * Test for {@link UnivariateRealPeriodicInterpolator}.
+ */
+public class UnivariateRealPeriodicInterpolatorTest {
+    private final Random rng = new Random(1224465L);
+
+    @Test
+    public void testSine() {
+        final int n = 30;
+        final double[] xval = new double[n];
+        final double[] yval = new double[n];
+        final double period = 12.3;
+        final double offset = 45.67;
+
+        double delta = 0;
+        for (int i = 0; i < n; i++) {
+            delta += rng.nextDouble() * period / n;
+            xval[i] = offset + delta;
+            yval[i] = FastMath.sin(xval[i]);
+        }
+
+        final UnivariateRealInterpolator inter = new LinearInterpolator();
+        final UnivariateRealFunction f = inter.interpolate(xval, yval);
+
+        final UnivariateRealInterpolator interP
+            = new UnivariateRealPeriodicInterpolator(new LinearInterpolator(),
+                                                     period, 1);
+        final UnivariateRealFunction fP = interP.interpolate(xval, yval);
+
+        // Comparing with original interpolation algorithm.
+        final double xMin = xval[0];
+        final double xMax = xval[n - 1];
+        for (int i = 0; i < n; i++) {
+            final double x = xMin + (xMax - xMin) * rng.nextDouble();
+            final double y = f.value(x);
+            final double yP = fP.value(x);
+
+            Assert.assertEquals("x=" + x, y, yP, Math.ulp(1d));
+        }
+
+        // Test interpolation outside the primary interval.
+        for (int i = 0; i < n; i++) {
+            final double xIn = offset + rng.nextDouble() * period;
+            final double xOut = xIn + rng.nextInt(123456789) * period;
+            final double yIn = fP.value(xIn);
+            final double yOut = fP.value(xOut);
+
+            Assert.assertEquals(yIn, yOut, 1e-7);
+        }
+    }
+
+    @Test
+    public void testLessThanOnePeriodCoverage() {
+        final int n = 30;
+        final double[] xval = new double[n];
+        final double[] yval = new double[n];
+        final double period = 12.3;
+        final double offset = 45.67;
+
+        double delta = period / 2;
+        for (int i = 0; i < n; i++) {
+            delta += period / (2 * n) * rng.nextDouble();
+            xval[i] = offset + delta;
+            yval[i] = FastMath.sin(xval[i]);
+        }
+
+        final UnivariateRealInterpolator interP
+            = new UnivariateRealPeriodicInterpolator(new LinearInterpolator(),
+                                                     period, 1);
+        final UnivariateRealFunction fP = interP.interpolate(xval, yval);
+
+        // Test interpolation outside the sample data interval.
+        for (int i = 0; i < n; i++) {
+            final double xIn = offset + rng.nextDouble() * period;
+            final double xOut = xIn + rng.nextInt(123456789) * period;
+            final double yIn = fP.value(xIn);
+            final double yOut = fP.value(xOut);
+
+            Assert.assertEquals(yIn, yOut, 1e-7);
+        }
+    }
+
+    @Test
+    public void testMoreThanOnePeriodCoverage() {
+        final int n = 30;
+        final double[] xval = new double[n];
+        final double[] yval = new double[n];
+        final double period = 12.3;
+        final double offset = 45.67;
+
+        double delta = period / 2;
+        for (int i = 0; i < n; i++) {
+            delta += 10 * period / n * rng.nextDouble();
+            xval[i] = offset + delta;
+            yval[i] = FastMath.sin(xval[i]);
+        }
+
+        final UnivariateRealInterpolator interP
+            = new UnivariateRealPeriodicInterpolator(new LinearInterpolator(),
+                                                     period, 1);
+        final UnivariateRealFunction fP = interP.interpolate(xval, yval);
+
+        // Test interpolation outside the sample data interval.
+        for (int i = 0; i < n; i++) {
+            final double xIn = offset + rng.nextDouble() * period;
+            final double xOut = xIn + rng.nextInt(123456789) * period;
+            final double yIn = fP.value(xIn);
+            final double yOut = fP.value(xOut);
+
+            Assert.assertEquals(yIn, yOut, 1e-6);
+        }
+    }
+
+    @Test(expected=NumberIsTooSmallException.class)
+    public void testTooFewSamples() {
+        final double[] xval = { 2, 3, 7 };
+        final double[] yval = { 1, 6, 5 };
+        final double period = 10;
+
+        final UnivariateRealInterpolator interpolator
+            = new UnivariateRealPeriodicInterpolator(new LinearInterpolator(), period);
+        final UnivariateRealFunction f = interpolator.interpolate(xval, yval);
+    }
+
+    @Test(expected=NonMonotonousSequenceException.class)
+    public void testUnsortedSamples() {
+        final double[] xval = { 2, 3, 7, 4, 6 };
+        final double[] yval = { 1, 6, 5, -1, -2 };
+        final double period = 10;
+
+        final UnivariateRealInterpolator interpolator
+            = new UnivariateRealPeriodicInterpolator(new LinearInterpolator(), period);
+        final UnivariateRealFunction f = interpolator.interpolate(xval, yval);
+    }
+}

Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/UnivariateRealPeriodicInterpolatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native