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