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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.analysis;
19
import org.apache.commons.math.MathException;
20
import junit.framework.TestCase;
23
* Testcase for Neville interpolator.
25
* The error of polynomial interpolation is
26
* f(z) - p(z) = f^(n)(zeta) * (z-x[0])(z-x[1])...(z-x[n-1]) / n!
27
* where f^(n) is the n-th derivative of the approximated function and
28
* zeta is some point in the interval determined by x[] and z.
30
* Since zeta is unknown, f^(n)(zeta) cannot be calculated. But we can bound
31
* it and use the absolute value upper bound for estimates. For reference,
32
* see <b>Introduction to Numerical Analysis</b>, ISBN 038795452X, chapter 2.
34
* @version $Revision$ $Date$
36
public final class NevilleInterpolatorTest extends TestCase {
39
* Test of interpolator for the sine function.
41
* |sin^(n)(zeta)| <= 1.0, zeta in [0, 2*PI]
43
public void testSinFunction() throws MathException {
44
UnivariateRealFunction f = new SinFunction();
45
UnivariateRealInterpolator interpolator = new NevilleInterpolator();
46
double x[], y[], z, expected, result, tolerance;
48
// 6 interpolating points on interval [0, 2*PI]
50
double min = 0.0, max = 2 * Math.PI;
53
for (int i = 0; i < n; i++) {
54
x[i] = min + i * (max - min) / n;
57
double derivativebound = 1.0;
58
UnivariateRealFunction p = interpolator.interpolate(x, y);
60
z = Math.PI / 4; expected = f.value(z); result = p.value(z);
61
tolerance = Math.abs(derivativebound * partialerror(x, z));
62
assertEquals(expected, result, tolerance);
64
z = Math.PI * 1.5; expected = f.value(z); result = p.value(z);
65
tolerance = Math.abs(derivativebound * partialerror(x, z));
66
assertEquals(expected, result, tolerance);
70
* Test of interpolator for the exponential function.
72
* |expm1^(n)(zeta)| <= e, zeta in [-1, 1]
74
public void testExpm1Function() throws MathException {
75
UnivariateRealFunction f = new Expm1Function();
76
UnivariateRealInterpolator interpolator = new NevilleInterpolator();
77
double x[], y[], z, expected, result, tolerance;
79
// 5 interpolating points on interval [-1, 1]
81
double min = -1.0, max = 1.0;
84
for (int i = 0; i < n; i++) {
85
x[i] = min + i * (max - min) / n;
88
double derivativebound = Math.E;
89
UnivariateRealFunction p = interpolator.interpolate(x, y);
91
z = 0.0; expected = f.value(z); result = p.value(z);
92
tolerance = Math.abs(derivativebound * partialerror(x, z));
93
assertEquals(expected, result, tolerance);
95
z = 0.5; expected = f.value(z); result = p.value(z);
96
tolerance = Math.abs(derivativebound * partialerror(x, z));
97
assertEquals(expected, result, tolerance);
99
z = -0.5; expected = f.value(z); result = p.value(z);
100
tolerance = Math.abs(derivativebound * partialerror(x, z));
101
assertEquals(expected, result, tolerance);
105
* Test of parameters for the interpolator.
107
public void testParameters() throws Exception {
108
UnivariateRealInterpolator interpolator = new NevilleInterpolator();
111
// bad abscissas array
112
double x[] = { 1.0, 2.0, 2.0, 4.0 };
113
double y[] = { 0.0, 4.0, 4.0, 2.5 };
114
UnivariateRealFunction p = interpolator.interpolate(x, y);
116
fail("Expecting MathException - bad abscissas array");
117
} catch (MathException ex) {
123
* Returns the partial error term (z-x[0])(z-x[1])...(z-x[n-1])/n!
125
protected double partialerror(double x[], double z) throws
126
IllegalArgumentException {
129
throw new IllegalArgumentException
130
("Interpolation array cannot be empty.");
133
for (int i = 0; i < x.length; i++) {
134
out *= (z - x[i]) / (i + 1);