Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D80F0FEDD for ; Fri, 31 May 2013 14:42:18 +0000 (UTC) Received: (qmail 67042 invoked by uid 500); 31 May 2013 14:42:18 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 66830 invoked by uid 500); 31 May 2013 14:42:17 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 66815 invoked by uid 99); 31 May 2013 14:42:16 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 May 2013 14:42:16 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 May 2013 14:42:13 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0F33823889E0; Fri, 31 May 2013 14:41:53 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1488256 - /commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java Date: Fri, 31 May 2013 14:41:52 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20130531144153.0F33823889E0@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: erans Date: Fri May 31 14:41:52 2013 New Revision: 1488256 URL: http://svn.apache.org/r1488256 Log: Unit tests. Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java?rev=1488256&r1=1488255&r2=1488256&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolatingFunctionTest.java Fri May 31 14:41:52 2013 @@ -19,6 +19,9 @@ package org.apache.commons.math3.analysi import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.MathIllegalArgumentException; import org.apache.commons.math3.analysis.BivariateFunction; +import org.apache.commons.math3.distribution.UniformRealDistribution; +import org.apache.commons.math3.random.RandomGenerator; +import org.apache.commons.math3.random.Well19937c; import org.junit.Assert; import org.junit.Test; import org.junit.Ignore; @@ -444,4 +447,165 @@ public final class BicubicSplineInterpol } } } + + /** + * Interpolating a plane. + *

+ * z = 2 x - 3 y + 5 + */ + @Test + public void testInterpolation1() { + final int sz = 21; + double[] xval = new double[sz]; + double[] yval = new double[sz]; + // Coordinate values + final double delta = 1d / (sz - 1); + for (int i = 0; i < sz; i++) { + xval[i] = -1 + 15 * i * delta; + yval[i] = -20 + 30 * i * delta; + } + + // Function values + BivariateFunction f = new BivariateFunction() { + public double value(double x, double y) { + return 2 * x - 3 * y + 5; + } + }; + double[][] zval = new double[xval.length][yval.length]; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + zval[i][j] = f.value(xval[i], yval[j]); + } + } + // Partial derivatives with respect to x + double[][] dZdX = new double[xval.length][yval.length]; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + dZdX[i][j] = 2; + } + } + // Partial derivatives with respect to y + double[][] dZdY = new double[xval.length][yval.length]; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + dZdY[i][j] = -3; + } + } + // Partial cross-derivatives + double[][] dZdXdY = new double[xval.length][yval.length]; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + dZdXdY[i][j] = 0; + } + } + + final BivariateFunction bcf + = new BicubicSplineInterpolatingFunction(xval, yval, zval, + dZdX, dZdY, dZdXdY); + double x, y; + double expected, result; + + final RandomGenerator rng = new Well19937c(1234567L); // "tol" depends on the seed. + final UniformRealDistribution distX + = new UniformRealDistribution(xval[0], xval[xval.length - 1]); + final UniformRealDistribution distY + = new UniformRealDistribution(yval[0], yval[yval.length - 1]); + + final int numSamples = 50; + final double tol = 6; + for (int i = 0; i < numSamples; i++) { + x = distX.sample(); + for (int j = 0; j < numSamples; j++) { + y = distY.sample(); +// System.out.println(x + " " + y + " " + f.value(x, y) + " " + bcf.value(x, y)); + Assert.assertEquals(f.value(x, y), bcf.value(x, y), tol); + } +// System.out.println(); + } + } + + /** + * Interpolating a paraboloid. + *

+ * z = 2 x2 - 3 y2 + 4 x y - 5 + */ + @Test + public void testInterpolation2() { + final int sz = 21; + double[] xval = new double[sz]; + double[] yval = new double[sz]; + // Coordinate values + final double delta = 1d / (sz - 1); + for (int i = 0; i < sz; i++) { + xval[i] = -1 + 15 * i * delta; + yval[i] = -20 + 30 * i * delta; + } + + // Function values + BivariateFunction f = new BivariateFunction() { + public double value(double x, double y) { + return 2 * x * x - 3 * y * y + 4 * x * y - 5; + } + }; + double[][] zval = new double[xval.length][yval.length]; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + zval[i][j] = f.value(xval[i], yval[j]); + } + } + // Partial derivatives with respect to x + double[][] dZdX = new double[xval.length][yval.length]; + BivariateFunction dfdX = new BivariateFunction() { + public double value(double x, double y) { + return 4 * (x + y); + } + }; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + dZdX[i][j] = dfdX.value(xval[i], yval[j]); + } + } + // Partial derivatives with respect to y + double[][] dZdY = new double[xval.length][yval.length]; + BivariateFunction dfdY = new BivariateFunction() { + public double value(double x, double y) { + return 4 * x - 6 * y; + } + }; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + dZdY[i][j] = dfdY.value(xval[i], yval[j]); + } + } + // Partial cross-derivatives + double[][] dZdXdY = new double[xval.length][yval.length]; + for (int i = 0; i < xval.length; i++) { + for (int j = 0; j < yval.length; j++) { + dZdXdY[i][j] = 4; + } + } + + BivariateFunction bcf = new BicubicSplineInterpolatingFunction(xval, yval, zval, + dZdX, dZdY, dZdXdY); + double x, y; + double expected, result; + + final RandomGenerator rng = new Well19937c(1234567L); // "tol" depends on the seed. + final UniformRealDistribution distX + = new UniformRealDistribution(rng, xval[0], xval[xval.length - 1]); + final UniformRealDistribution distY + = new UniformRealDistribution(rng, yval[0], yval[yval.length - 1]); + + final double tol = 224; + double max = 0; + for (int i = 0; i < sz; i++) { + x = distX.sample(); + for (int j = 0; j < sz; j++) { + y = distY.sample(); +// System.out.println(x + " " + y + " " + f.value(x, y) + " " + bcf.value(x, y)); + Assert.assertEquals(f.value(x, y), bcf.value(x, y), tol); + } +// System.out.println(); + } + } }