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 2010/06/22 17:05:52 UTC
svn commit: r956914 - 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: Tue Jun 22 15:05:51 2010
New Revision: 956914
URL: http://svn.apache.org/viewvc?rev=956914&view=rev
Log:
MATH-378
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java (with props)
commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/LinearInterpolatorTest.java (with props)
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java
commons/proper/math/trunk/src/site/xdoc/changes.xml
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java?rev=956914&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java Tue Jun 22 15:05:51 2010
@@ -0,0 +1,73 @@
+/*
+ * 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.MathRuntimeException;
+import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
+import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
+import org.apache.commons.math.util.LocalizedFormats;
+
+/**
+ * Implements a linear function for interpolation of real univariate functions.
+ */
+public class LinearInterpolator implements UnivariateRealInterpolator {
+ /**
+ * Computes a linear interpolating function for the data set.
+ * @param x the arguments for the interpolation points
+ * @param y the values for the interpolation points
+ * @return a function which interpolates the data set
+ */
+ public PolynomialSplineFunction interpolate(double x[], double y[]) {
+ if (x.length != y.length) {
+ throw MathRuntimeException.createIllegalArgumentException(
+ LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, x.length, y.length);
+ }
+
+ if (x.length < 2) {
+ throw MathRuntimeException.createIllegalArgumentException(
+ LocalizedFormats.WRONG_NUMBER_OF_POINTS, 2, x.length);
+ }
+
+ // Number of intervals. The number of data points is n + 1.
+ int n = x.length - 1;
+
+ for (int i = 0; i < n; i++) {
+ if (x[i] >= x[i + 1]) {
+ throw MathRuntimeException.createIllegalArgumentException(
+ LocalizedFormats.NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS,
+ i, i+1, x[i], x[i+1]);
+ }
+ }
+
+ // Slope of the lines between the datapoints.
+ final double m[] = new double[n];
+ for (int i = 0; i < n; i++) {
+ m[i] = (y[i + 1] - y[i]) / (x[i + 1] - x[i]);
+ }
+
+ PolynomialFunction polynomials[] = new PolynomialFunction[n];
+ final double coefficients[] = new double[2];
+ for (int i = 0; i < n; i++) {
+ coefficients[0] = y[i];
+ coefficients[1] = m[i];
+ polynomials[i] = new PolynomialFunction(coefficients);
+ }
+
+ return new PolynomialSplineFunction(x, polynomials);
+ }
+
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/LinearInterpolator.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java?rev=956914&r1=956913&r2=956914&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/analysis/interpolation/SplineInterpolator.java Tue Jun 22 15:05:51 2010
@@ -71,7 +71,7 @@ public class SplineInterpolator implemen
int n = x.length - 1;
for (int i = 0; i < n; i++) {
- if (x[i] >= x[i + 1]) {
+ if (x[i] >= x[i + 1]) {
throw MathRuntimeException.createIllegalArgumentException(
LocalizedFormats.NOT_STRICTLY_INCREASING_NUMBER_OF_POINTS,
i, i+1, x[i], x[i+1]);
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=956914&r1=956913&r2=956914&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Tue Jun 22 15:05:51 2010
@@ -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="2.2" date="TBD" description="TBD">
+ <action dev="erans" type="add" issue="MATH-378" due-to="Matthew Rowles">
+ Implementation of linear interpolation.
+ </action>
<action dev="luc" type="fix" issue="MATH-361">
Improved localization of error messages.
</action>
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/LinearInterpolatorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/LinearInterpolatorTest.java?rev=956914&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/LinearInterpolatorTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/LinearInterpolatorTest.java Tue Jun 22 15:05:51 2010
@@ -0,0 +1,136 @@
+/*
+ * 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.MathException;
+import org.apache.commons.math.TestUtils;
+import org.apache.commons.math.analysis.UnivariateRealFunction;
+import org.apache.commons.math.analysis.polynomials.PolynomialFunction;
+import org.apache.commons.math.analysis.polynomials.PolynomialSplineFunction;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test the LinearInterpolator.
+ */
+public class LinearInterpolatorTest {
+
+ /** error tolerance for spline interpolator value at knot points */
+ protected double knotTolerance = 1E-12;
+
+ /** error tolerance for interpolating polynomial coefficients */
+ protected double coefficientTolerance = 1E-6;
+
+ /** error tolerance for interpolated values */
+ protected double interpolationTolerance = 1E-12;
+
+ @Test
+ public void testInterpolateLinearDegenerateTwoSegment()
+ throws Exception {
+ double x[] = { 0.0, 0.5, 1.0 };
+ double y[] = { 0.0, 0.5, 1.0 };
+ UnivariateRealInterpolator i = new LinearInterpolator();
+ UnivariateRealFunction f = i.interpolate(x, y);
+ verifyInterpolation(f, x, y);
+
+ // Verify coefficients using analytical values
+ PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
+ double target[] = {y[0], 1d};
+ TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
+ target = new double[]{y[1], 1d};
+ TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
+
+ // Check interpolation
+ Assert.assertEquals(0.0,f.value(0.0), interpolationTolerance);
+ Assert.assertEquals(0.4,f.value(0.4), interpolationTolerance);
+ Assert.assertEquals(1.0,f.value(1.0), interpolationTolerance);
+ }
+
+ @Test
+ public void testInterpolateLinearDegenerateThreeSegment()
+ throws Exception {
+ double x[] = { 0.0, 0.5, 1.0, 1.5 };
+ double y[] = { 0.0, 0.5, 1.0, 1.5 };
+ UnivariateRealInterpolator i = new LinearInterpolator();
+ UnivariateRealFunction f = i.interpolate(x, y);
+ verifyInterpolation(f, x, y);
+
+ // Verify coefficients using analytical values
+ PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
+ double target[] = {y[0], 1d};
+ TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
+ target = new double[]{y[1], 1d};
+ TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
+ target = new double[]{y[2], 1d};
+ TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
+
+ // Check interpolation
+ Assert.assertEquals(0,f.value(0), interpolationTolerance);
+ Assert.assertEquals(1.4,f.value(1.4), interpolationTolerance);
+ Assert.assertEquals(1.5,f.value(1.5), interpolationTolerance);
+ }
+
+ @Test
+ public void testInterpolateLinear() throws Exception {
+ double x[] = { 0.0, 0.5, 1.0 };
+ double y[] = { 0.0, 0.5, 0.0 };
+ UnivariateRealInterpolator i = new LinearInterpolator();
+ UnivariateRealFunction f = i.interpolate(x, y);
+ verifyInterpolation(f, x, y);
+
+ // Verify coefficients using analytical values
+ PolynomialFunction polynomials[] = ((PolynomialSplineFunction) f).getPolynomials();
+ double target[] = {y[0], 1d};
+ TestUtils.assertEquals(polynomials[0].getCoefficients(), target, coefficientTolerance);
+ target = new double[]{y[1], -1d};
+ TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
+ }
+
+ @Test
+ public void testIllegalArguments() throws MathException {
+ // Data set arrays of different size.
+ UnivariateRealInterpolator i = new LinearInterpolator();
+ try {
+ double xval[] = { 0.0, 1.0 };
+ double yval[] = { 0.0, 1.0, 2.0 };
+ i.interpolate(xval, yval);
+ Assert.fail("Failed to detect data set array with different sizes.");
+ } catch (IllegalArgumentException iae) {
+ // Expected.
+ }
+ // X values not sorted.
+ try {
+ double xval[] = { 0.0, 1.0, 0.5 };
+ double yval[] = { 0.0, 1.0, 2.0 };
+ i.interpolate(xval, yval);
+ Assert.fail("Failed to detect unsorted arguments.");
+ } catch (IllegalArgumentException iae) {
+ // Expected.
+ }
+ }
+
+ /**
+ * verifies that f(x[i]) = y[i] for i = 0..n-1 where n is common length.
+ */
+ protected void verifyInterpolation(UnivariateRealFunction f, double x[], double y[])
+ throws Exception{
+ for (int i = 0; i < x.length; i++) {
+ Assert.assertEquals(f.value(x[i]), y[i], knotTolerance);
+ }
+ }
+
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/LinearInterpolatorTest.java
------------------------------------------------------------------------------
svn:eol-style = native