Calling this constructor is equivalent to call {@link + * #MicrosphereInterpolator(int, int) + * MicrosphereInterpolator(MicrosphereInterpolator.DEFAULT_MICROSPHERE_ELEMENTS, + * MicrosphereInterpolator.DEFAULT_BRIGHTNESS_EXPONENT)}.

+

+ Microsphere interpolation is a robust multidimensional interpolation algorithm. + It has been described in William Dudziak's MS thesis. +

+ * y = 2 x1 - 3 x2 + 5 + */ + @Test + public void testLinearFunction2D() throws MathException { + MultivariateRealFunction f = new MultivariateRealFunction() { + public double value(double[] x) { + if (x.length != 2) { + throw new IllegalArgumentException(); + } + return 2 * x[0] - 3 * x[1] + 5; + } + }; + + MultivariateRealInterpolator interpolator = new MicrosphereInterpolator(); + + // Interpolating points in [-1, 1][-1, 1] by steps of 1. + final int n = 9; + final int dim = 2; + double[][] x = new double[n][dim]; + double[] y = new double[n]; + int index = 0; + for (int i = -1; i <= 1; i++) { + for (int j = -1; j <= 1; j++) { + x[index][0] = i; + x[index][1] = j; + y[index] = f.value(x[index]); + ++index; + } + } + + MultivariateRealFunction p = interpolator.interpolate(x, y); + + double[] c = new double[dim]; + double expected, result; + + c[0] = 0; + c[1] = 0; + expected = f.value(c); + result = p.value(c); + Assert.assertEquals("On sample point", expected, result, Math.ulp(1d)); + + c[0] = 0 + 1e-5; + c[1] = 1 - 1e-5; + expected = f.value(c); + result = p.value(c); + Assert.assertEquals("1e-5 away from sample point", expected, result, 1e-4); + } + + /** + * Test of interpolator for a quadratic function. + *

+ * y = 2 x12 - 3 x22 + * + 4 x1 x2 - 5 + */ + @Test + public void testParaboloid2D() throws MathException { + MultivariateRealFunction f = new MultivariateRealFunction() { + public double value(double[] x) { + if (x.length != 2) { + throw new IllegalArgumentException(); + } + return 2 * x[0] * x[0] - 3 * x[1] * x[1] + 4 * x[0] * x[1] - 5; + } + }; + + MultivariateRealInterpolator interpolator = new MicrosphereInterpolator(); + + // Interpolating points in [-10, 10][-10, 10] by steps of 2. + final int n = 121; + final int dim = 2; + double[][] x = new double[n][dim]; + double[] y = new double[n]; + int index = 0; + for (int i = -10; i <= 10; i += 2) { + for (int j = -10; j <= 10; j += 2) { + x[index][0] = i; + x[index][1] = j; + y[index] = f.value(x[index]); + ++index; + } + } + + MultivariateRealFunction p = interpolator.interpolate(x, y); + + double[] c = new double[dim]; + double expected, result; + + c[0] = 0; + c[1] = 0; + expected = f.value(c); + result = p.value(c); + Assert.assertEquals("On sample point", expected, result, Math.ulp(1d)); + + c[0] = 2 + 1e-5; + c[1] = 2 - 1e-5; + expected = f.value(c); + result = p.value(c); + Assert.assertEquals("1e-5 away from sample point", expected, result, 1e-3); + } +} Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/MicrosphereInterpolatorTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math/analysis/interpolation/MicrosphereInterpolatorTest.java ------------------------------------------------------------------------------ svn:keywords = Author Date Id Revision