1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.math4.legacy.analysis.interpolation;
18
19 import org.apache.commons.math4.legacy.analysis.BivariateFunction;
20 import org.apache.commons.statistics.distribution.ContinuousDistribution;
21 import org.apache.commons.statistics.distribution.UniformContinuousDistribution;
22 import org.apache.commons.math4.legacy.exception.DimensionMismatchException;
23 import org.apache.commons.math4.legacy.exception.InsufficientDataException;
24 import org.apache.commons.math4.legacy.exception.NonMonotonicSequenceException;
25 import org.apache.commons.math4.legacy.exception.NullArgumentException;
26 import org.apache.commons.rng.UniformRandomProvider;
27 import org.apache.commons.rng.simple.RandomSource;
28 import org.junit.Assert;
29 import org.junit.Test;
30
31
32
33
34 public final class PiecewiseBicubicSplineInterpolatorTest {
35
36
37
38 @Test
39 public void testPreconditions() {
40 double[] xval = new double[] { 3, 4, 5, 6.5, 7.5 };
41 double[] yval = new double[] { -4, -3, -1, 2.5, 3.5 };
42 double[][] zval = new double[xval.length][yval.length];
43
44 BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
45
46 try {
47 interpolator.interpolate( null, yval, zval );
48 Assert.fail( "Failed to detect x null pointer" );
49 } catch ( NullArgumentException iae ) {
50
51 }
52
53 try {
54 interpolator.interpolate( xval, null, zval );
55 Assert.fail( "Failed to detect y null pointer" );
56 } catch ( NullArgumentException iae ) {
57
58 }
59
60 try {
61 interpolator.interpolate( xval, yval, null );
62 Assert.fail( "Failed to detect z null pointer" );
63 } catch ( NullArgumentException iae ) {
64
65 }
66
67 try {
68 double xval1[] = { 0.0, 1.0, 2.0, 3.0 };
69 interpolator.interpolate( xval1, yval, zval );
70 Assert.fail( "Failed to detect insufficient x data" );
71 } catch ( InsufficientDataException iae ) {
72
73 }
74
75 try {
76 double yval1[] = { 0.0, 1.0, 2.0, 3.0 };
77 interpolator.interpolate( xval, yval1, zval );
78 Assert.fail( "Failed to detect insufficient y data" );
79 } catch ( InsufficientDataException iae ) {
80
81 }
82
83 try {
84 double zval1[][] = new double[4][4];
85 interpolator.interpolate( xval, yval, zval1 );
86 Assert.fail( "Failed to detect insufficient z data" );
87 } catch ( InsufficientDataException iae ) {
88
89 }
90
91 try {
92 double xval1[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
93 interpolator.interpolate( xval1, yval, zval );
94 Assert.fail( "Failed to detect data set array with different sizes." );
95 } catch ( DimensionMismatchException iae ) {
96
97 }
98
99 try {
100 double yval1[] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 };
101 interpolator.interpolate( xval, yval1, zval );
102 Assert.fail( "Failed to detect data set array with different sizes." );
103 } catch ( DimensionMismatchException iae ) {
104
105 }
106
107
108 try {
109 double xval1[] = { 0.0, 1.0, 0.5, 7.0, 3.5 };
110 interpolator.interpolate( xval1, yval, zval );
111 Assert.fail( "Failed to detect unsorted x arguments." );
112 } catch ( NonMonotonicSequenceException iae ) {
113
114 }
115
116
117 try {
118 double yval1[] = { 0.0, 1.0, 1.5, 0.0, 3.0 };
119 interpolator.interpolate( xval, yval1, zval );
120 Assert.fail( "Failed to detect unsorted y arguments." );
121 } catch ( NonMonotonicSequenceException iae ) {
122
123 }
124 }
125
126
127
128
129
130
131 @Test
132 public void testInterpolation1() {
133 final int sz = 21;
134 double[] xval = new double[sz];
135 double[] yval = new double[sz];
136
137 final double delta = 1d / (sz - 1);
138 for ( int i = 0; i < sz; i++ ){
139 xval[i] = -1 + 15 * i * delta;
140 yval[i] = -20 + 30 * i * delta;
141 }
142
143
144 BivariateFunction f = new BivariateFunction() {
145 @Override
146 public double value( double x, double y ) {
147 return 2 * x - 3 * y + 5;
148 }
149 };
150 double[][] zval = new double[xval.length][yval.length];
151 for ( int i = 0; i < xval.length; i++ ) {
152 for ( int j = 0; j < yval.length; j++ ) {
153 zval[i][j] = f.value(xval[i], yval[j]);
154 }
155 }
156
157 BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
158 BivariateFunction p = interpolator.interpolate(xval, yval, zval);
159 double x;
160 double y;
161
162 final UniformRandomProvider rng = RandomSource.WELL_19937_C.create(1234567L);
163 final ContinuousDistribution.Sampler distX = UniformContinuousDistribution.of(xval[0], xval[xval.length - 1]).createSampler(rng);
164 final ContinuousDistribution.Sampler distY = UniformContinuousDistribution.of(yval[0], yval[yval.length - 1]).createSampler(rng);
165
166 final int numSamples = 50;
167 final double tol = 2e-14;
168 for ( int i = 0; i < numSamples; i++ ) {
169 x = distX.sample();
170 for ( int j = 0; j < numSamples; j++ ) {
171 y = distY.sample();
172
173 Assert.assertEquals(f.value(x, y), p.value(x, y), tol);
174 }
175
176 }
177 }
178
179
180
181
182
183
184 @Test
185 public void testInterpolation2() {
186 final int sz = 21;
187 double[] xval = new double[sz];
188 double[] yval = new double[sz];
189
190 final double delta = 1d / (sz - 1);
191 for ( int i = 0; i < sz; i++ ) {
192 xval[i] = -1 + 15 * i * delta;
193 yval[i] = -20 + 30 * i * delta;
194 }
195
196
197 BivariateFunction f = new BivariateFunction() {
198 @Override
199 public double value( double x, double y ) {
200 return 2 * x * x - 3 * y * y + 4 * x * y - 5;
201 }
202 };
203 double[][] zval = new double[xval.length][yval.length];
204 for ( int i = 0; i < xval.length; i++ ) {
205 for ( int j = 0; j < yval.length; j++ ) {
206 zval[i][j] = f.value(xval[i], yval[j]);
207 }
208 }
209
210 BivariateGridInterpolator interpolator = new PiecewiseBicubicSplineInterpolator();
211 BivariateFunction p = interpolator.interpolate(xval, yval, zval);
212 double x;
213 double y;
214
215 final UniformRandomProvider rng = RandomSource.WELL_19937_C.create(1234567L);
216 final ContinuousDistribution.Sampler distX = UniformContinuousDistribution.of(xval[0], xval[xval.length - 1]).createSampler(rng);
217 final ContinuousDistribution.Sampler distY = UniformContinuousDistribution.of(yval[0], yval[yval.length - 1]).createSampler(rng);
218
219 final int numSamples = 50;
220 final double tol = 5e-13;
221 for ( int i = 0; i < numSamples; i++ ) {
222 x = distX.sample();
223 for ( int j = 0; j < numSamples; j++ ) {
224 y = distY.sample();
225
226 Assert.assertEquals(f.value(x, y), p.value(x, y), tol);
227 }
228
229 }
230 }
231 }