~ubuntu-branches/ubuntu/oneiric/commons-math/oneiric

« back to all changes in this revision

Viewing changes to src/test/java/org/apache/commons/math/analysis/interpolation/BicubicSplineInterpolatorTest.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan, Torsten Werner, Damien Raude-Morvan
  • Date: 2011-03-07 21:14:46 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20110307211446-4zea7og4eeyzhpai
Tags: 2.2-1
[ Torsten Werner ]
* Change maintainers into Maintainers.

[ Damien Raude-Morvan ]
* New upstream release (Closes: #617209).
* d/control: Bump Standards-Version to 3.9.1 (no changes needed).
* d/copyright: Refresh years, upgrade to DEP5 r166 and relicence my work
  under Apache-2.0.
* d/ant.properties: Set junit.jar to /usr/share/java/junit4.jar
  to ensure unit tests are launched.
* d/docs: Install upstream RELEASE-NOTES.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 *      http://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
package org.apache.commons.math.analysis.interpolation;
 
18
 
 
19
import org.apache.commons.math.MathException;
 
20
import org.apache.commons.math.DimensionMismatchException;
 
21
import org.apache.commons.math.analysis.BivariateRealFunction;
 
22
import org.junit.Assert;
 
23
import org.junit.Test;
 
24
 
 
25
/**
 
26
 * Testcase for the bicubic interpolator.
 
27
 * 
 
28
 * @version $Revision: 821626 $ $Date: 2009-10-04 23:57:30 +0200 (Sun, 04 Oct 2009) $ 
 
29
 */
 
30
public final class BicubicSplineInterpolatorTest {
 
31
    /**
 
32
     * Test preconditions.
 
33
     */
 
34
    @Test
 
35
    public void testPreconditions() throws MathException {
 
36
        double[] xval = new double[] {3, 4, 5, 6.5};
 
37
        double[] yval = new double[] {-4, -3, -1, 2.5};
 
38
        double[][] zval = new double[xval.length][yval.length];
 
39
 
 
40
        BivariateRealGridInterpolator interpolator = new BicubicSplineInterpolator();
 
41
        
 
42
        @SuppressWarnings("unused")
 
43
        BivariateRealFunction p = interpolator.interpolate(xval, yval, zval);
 
44
        
 
45
        double[] wxval = new double[] {3, 2, 5, 6.5};
 
46
        try {
 
47
            p = interpolator.interpolate(wxval, yval, zval);
 
48
            Assert.fail("an exception should have been thrown");
 
49
        } catch (IllegalArgumentException e) {
 
50
            // Expected
 
51
        }
 
52
 
 
53
        double[] wyval = new double[] {-4, -3, -1, -1};
 
54
        try {
 
55
            p = interpolator.interpolate(xval, wyval, zval);
 
56
            Assert.fail("an exception should have been thrown");
 
57
        } catch (IllegalArgumentException e) {
 
58
            // Expected
 
59
        }
 
60
 
 
61
        double[][] wzval = new double[xval.length][yval.length + 1];
 
62
        try {
 
63
            p = interpolator.interpolate(xval, yval, wzval);
 
64
            Assert.fail("an exception should have been thrown");
 
65
        } catch (DimensionMismatchException e) {
 
66
            // Expected
 
67
        }
 
68
        wzval = new double[xval.length - 1][yval.length];
 
69
        try {
 
70
            p = interpolator.interpolate(xval, yval, wzval);
 
71
            Assert.fail("an exception should have been thrown");
 
72
        } catch (DimensionMismatchException e) {
 
73
            // Expected
 
74
        }
 
75
    }
 
76
 
 
77
    /**
 
78
     * Test of interpolator for a plane.
 
79
     * <p>
 
80
     * z = 2 x - 3 y + 5
 
81
     */
 
82
    @Test
 
83
    public void testPlane() throws MathException {
 
84
        BivariateRealFunction f = new BivariateRealFunction() {
 
85
                public double value(double x, double y) {
 
86
                    return 2 * x - 3 * y + 5;
 
87
                }
 
88
            };
 
89
 
 
90
        BivariateRealGridInterpolator interpolator = new BicubicSplineInterpolator();
 
91
 
 
92
        double[] xval = new double[] {3, 4, 5, 6.5};
 
93
        double[] yval = new double[] {-4, -3, -1, 2, 2.5};
 
94
        double[][] zval = new double[xval.length][yval.length];
 
95
        for (int i = 0; i < xval.length; i++) {
 
96
            for (int j = 0; j < yval.length; j++) {
 
97
                zval[i][j] = f.value(xval[i], yval[j]);
 
98
            }
 
99
        }
 
100
 
 
101
        BivariateRealFunction p = interpolator.interpolate(xval, yval, zval);
 
102
        double x, y;
 
103
        double expected, result;
 
104
        
 
105
        x = 4;
 
106
        y = -3;
 
107
        expected = f.value(x, y);
 
108
        result = p.value(x, y);
 
109
        Assert.assertEquals("On sample point", expected, result, 1e-15);
 
110
 
 
111
        x = 4.5;
 
112
        y = -1.5;
 
113
        expected = f.value(x, y);
 
114
        result = p.value(x, y);
 
115
        Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.3);
 
116
 
 
117
        x = 3.5;
 
118
        y = -3.5;
 
119
        expected = f.value(x, y);
 
120
        result = p.value(x, y);
 
121
        Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.3);
 
122
    }
 
123
 
 
124
    /**
 
125
     * Test of interpolator for a paraboloid.
 
126
     * <p>
 
127
     * z = 2 x<sup>2</sup> - 3 y<sup>2</sup> + 4 x y - 5
 
128
     */
 
129
    @Test
 
130
    public void testParaboloid() throws MathException {
 
131
        BivariateRealFunction f = new BivariateRealFunction() {
 
132
                public double value(double x, double y) {
 
133
                    return 2 * x * x - 3 * y * y + 4 * x * y - 5;
 
134
                }
 
135
            };
 
136
 
 
137
        BivariateRealGridInterpolator interpolator = new BicubicSplineInterpolator();
 
138
 
 
139
        double[] xval = new double[] {3, 4, 5, 6.5};
 
140
        double[] yval = new double[] {-4, -3, -2, -1, 0.5, 2.5};
 
141
        double[][] zval = new double[xval.length][yval.length];
 
142
        for (int i = 0; i < xval.length; i++) {
 
143
            for (int j = 0; j < yval.length; j++) {
 
144
                zval[i][j] = f.value(xval[i], yval[j]);
 
145
            }
 
146
        }
 
147
 
 
148
        BivariateRealFunction p = interpolator.interpolate(xval, yval, zval);
 
149
        double x, y;
 
150
        double expected, result;
 
151
        
 
152
        x = 5;
 
153
        y = 0.5;
 
154
        expected = f.value(x, y);
 
155
        result = p.value(x, y);
 
156
        Assert.assertEquals("On sample point", expected, result, 1e-13);
 
157
 
 
158
        x = 4.5;
 
159
        y = -1.5;
 
160
        expected = f.value(x, y);
 
161
        result = p.value(x, y);
 
162
        Assert.assertEquals("half-way between sample points (middle of the patch)", expected, result, 0.2);
 
163
 
 
164
        x = 3.5;
 
165
        y = -3.5;
 
166
        expected = f.value(x, y);
 
167
        result = p.value(x, y);
 
168
        Assert.assertEquals("half-way between sample points (border of the patch)", expected, result, 0.2);
 
169
    }
 
170
}