Submitted by: j3322ptm@yahoo.de HotFusionMan@Yahoo.com git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@140947 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0d757f3e99
commit
a235488095
|
@ -0,0 +1,134 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.analysis;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
/**
|
||||
* Represents a cubic spline function.
|
||||
* Spline functions map a certain interval of real numbers to real numbers.
|
||||
* A cubic spline consists of segments of cubic functions. For this class,
|
||||
* polynominal coefficents are used.
|
||||
* Arguments outside of the domain cause an IllegalArgumentException.
|
||||
*
|
||||
* @author pietsch at apache.org
|
||||
*
|
||||
*/
|
||||
public class CubicSplineFunction implements UnivariateRealFunction {
|
||||
// Spline segment interval delimiters.
|
||||
// Size is N+1 for N segments.
|
||||
private double xval[];
|
||||
|
||||
// The spline segment's polynominal coefficients.
|
||||
// The first index runs over the intervals, size is N.
|
||||
// The second index adresses the coefficients in the segment, with
|
||||
// index 0 being the absolute coefficient and index 3 the coefficient
|
||||
// for the third power.
|
||||
// The coefficients are setup so that x runs from 0 to xval[i+1]-xval[i].
|
||||
private double c[][];
|
||||
|
||||
public CubicSplineFunction(double xval[],double c[][]) {
|
||||
// TODO: should copy the arguments here, for safety. This could be a major overhead.
|
||||
this.xval=xval;
|
||||
this.c=c;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.UnivariateRealFunction#value(double)
|
||||
*/
|
||||
public double value(double x) throws MathException {
|
||||
if(x<xval[0]||x>xval[xval.length-1]) {
|
||||
throw new IllegalArgumentException("Argument outside domain");
|
||||
}
|
||||
int i=Arrays.binarySearch(xval,x);
|
||||
if(i<0) {
|
||||
i=-i-2;
|
||||
}
|
||||
x=x-xval[i];
|
||||
return ((c[i][3]*x+c[i][2])*x+c[i][1])*x+c[i][0];
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.UnivariateRealFunction#firstDerivative(double)
|
||||
*/
|
||||
public double firstDerivative(double x) throws MathException {
|
||||
if(x<xval[0]||x>xval[xval.length-1]) {
|
||||
throw new IllegalArgumentException("Argument outside domain");
|
||||
}
|
||||
int i=Arrays.binarySearch(xval,x);
|
||||
if(i<0) {
|
||||
i=-i-2;
|
||||
}
|
||||
x=x-xval[i];
|
||||
return (3*c[i][3]*x+2*c[i][2])*x+c[i][1];
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.UnivariateRealFunction#secondDerivative(double)
|
||||
*/
|
||||
public double secondDerivative(double x) throws MathException {
|
||||
if(x<xval[0]||x>xval[xval.length-1]) {
|
||||
throw new IllegalArgumentException("Argument outside domain");
|
||||
}
|
||||
int i=Arrays.binarySearch(xval,x);
|
||||
if(i<0) {
|
||||
i=-i-2;
|
||||
}
|
||||
x=x-xval[i];
|
||||
return 6*c[i][3]*x+2*c[i][2];
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,164 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.analysis;
|
||||
|
||||
/**
|
||||
* Computes a natural spline interpolation for the data set.
|
||||
*
|
||||
* @author pietsch at apache.org
|
||||
*
|
||||
*/
|
||||
public class SplineInterpolator implements UnivariateRealInterpolator {
|
||||
private double[][] c = null ;
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.apache.commons.math.UnivariateRealInterpolator#interpolate(double[], double[])
|
||||
*/
|
||||
public UnivariateRealFunction interpolate(double[] xval, double[] yval) {
|
||||
if (xval.length != yval.length) {
|
||||
throw new IllegalArgumentException("Dataset arrays must have same length.");
|
||||
}
|
||||
|
||||
if ( c == null )
|
||||
{
|
||||
// Number of intervals. The number of data points is N+1.
|
||||
int n = xval.length - 1;
|
||||
// Check whether the xval vector has ascending values.
|
||||
// Separation should be checked too (not implemented: which criteria?).
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (xval[i]>=xval[i+1]) {
|
||||
throw new IllegalArgumentException("Dataset must specify sorted, ascending x values.");
|
||||
}
|
||||
}
|
||||
// Vectors for the equation system. There are n-1 equations for the unknowns s[i] (1<=i<=N-1),
|
||||
// which are second order derivatives for the spline at xval[i]. At the end points, s[0]=s[N]=0.
|
||||
// Vectors are offset by -1, except the lower diagonal vector which is offset by -2. Layout:
|
||||
// d[0]*s[1]+u[0]*s[2] = b[0]
|
||||
// l[0]*s[1]+d[1]*s[2]+u[1]*s[3] = b[1]
|
||||
// l[1]*s[2]+d[2]*s[3]+u[2]*s[4] = b[2]
|
||||
// ...
|
||||
// l[N-4]*s[N-3]+d[N-3]*s[N-2]+u[N-3]*s[N-1] = b[N-3]
|
||||
// l[N-3]*s[N-2]+d[N-2]*s[N-1] = b[N-2]
|
||||
// Vector b is the right hand side of the system.
|
||||
double b[] = new double[n - 1];
|
||||
// Vector d is diagonal of the matrix and also holds the computed solution.
|
||||
double d[] = new double[n - 1];
|
||||
// u[] and l[] are not really needed, the computation can be folded into the
|
||||
// system solving loops.
|
||||
//double u[] = new double[n - 2]; // upper diagonal
|
||||
//double l[] = new double[n - 2]; // lower diagonal
|
||||
// Setup RHS and diagonal.
|
||||
for (int i = 0; i < n - 1; i++) {
|
||||
// TODO avoid recomputing the term
|
||||
// (yval[i + 2] - yval[i + 1]) / (xval[i + 2] - xval[i + 1])
|
||||
// take it from the previous loop pass. Note: the interesting part of performance
|
||||
// loss is the range check in the array access, not the computation itself.
|
||||
b[i] =
|
||||
6.0
|
||||
* ((yval[i + 2] - yval[i + 1]) / (xval[i + 2] - xval[i + 1])
|
||||
- (yval[i + 1] - yval[i]) / (xval[i + 1] - xval[i]));
|
||||
d[i] = 2.0 * (xval[i + 2] - xval[i]);
|
||||
}
|
||||
// Set up upper and lower diagonal. Keep the offsets in mind.
|
||||
//for (int i = 0; i < n - 2; i++) {
|
||||
//u[i] = xval[i + 2] - xval[i + 1];
|
||||
//l[i] = xval[i + 2] - xval[i + 1];
|
||||
//}
|
||||
// Solve the system: forward pass.
|
||||
for (int i = 0; i < n - 2; i++) {
|
||||
// TODO: This relies on compiler for CSE of delta/d[i]. Is this a reasonable assumption?
|
||||
double delta = xval[i + 2] - xval[i + 1];
|
||||
d[i + 1] -= delta * delta / d[i];
|
||||
b[i + 1] -= b[i] * delta / d[i];
|
||||
}
|
||||
// Solve the system: backward pass.
|
||||
d[n - 2] = b[n - 2] / d[n - 2];
|
||||
for (int i = n - 3; i >= 0; i--) {
|
||||
d[i] = (b[i] - (xval[i + 2] - xval[i + 1]) * d[i + 1]) / d[i];
|
||||
}
|
||||
// Compute coefficients as usual polynomial coefficients.
|
||||
// Not the best with respect to roundoff on evaluation, but simple.
|
||||
c = new double[n][4];
|
||||
c[0][3] = d[0] / (xval[1] - xval[0]) / 6.0;
|
||||
c[0][2] = 0.0;
|
||||
c[0][1] =
|
||||
(yval[1] - yval[0]) / (xval[1] - xval[0])
|
||||
- d[0] * (xval[1] - xval[0]) / 6.0;
|
||||
for (int i = 1; i < n - 2; i++) {
|
||||
// TODO: This relies on compiler for CSE of xval[i + 1] - xval[i]. Is this a reasonable assumption?
|
||||
c[i][3] = (d[i] - d[i - 1]) / (xval[i + 1] - xval[i]) / 6.0;
|
||||
c[i][2] = d[i - 1] / 2.0;
|
||||
c[i][1] =
|
||||
(yval[i + 1] - yval[i]) / (xval[i + 1] - xval[i])
|
||||
- d[i] * (xval[i + 1] - xval[i]) / 6.0
|
||||
- d[i
|
||||
- 1] * (xval[i + 1] - xval[i]) / 3.0;
|
||||
}
|
||||
// TODO: again, CSE aspects.
|
||||
c[n - 1][3] = -d[n - 2] / (xval[n] - xval[n - 1]) / 6.0;
|
||||
c[n - 1][2] = d[n - 2] / 2.0;
|
||||
c[n - 1][1] =
|
||||
(yval[n] - yval[n - 1]) / (xval[n] - xval[n - 1])
|
||||
- d[n
|
||||
- 2] * (xval[n] - xval[n - 1]) / 3.0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
c[i][0] = yval[i];
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: copy xval, unless copied in CubicSplineFunction constructor
|
||||
return new CubicSplineFunction(xval, c);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,75 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.analysis;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
/**
|
||||
* Interface for interpolating a data set.
|
||||
*
|
||||
* * @author pietsch at apache.org
|
||||
*
|
||||
*/
|
||||
public interface UnivariateRealInterpolator {
|
||||
|
||||
/**
|
||||
* Computes an interpolating function for the data set.
|
||||
* @param xval the arguments for the interpolation points
|
||||
* @param yval the values for the interpolation points
|
||||
* @return a function which interpolates the data set
|
||||
* @throws MathException if arguments violate assumptions made by the interpolationg algorithm
|
||||
*/
|
||||
public UnivariateRealFunction interpolate(double xval[], double yval[])
|
||||
throws MathException;
|
||||
}
|
|
@ -0,0 +1,360 @@
|
|||
/* ====================================================================
|
||||
* The Apache Software License, Version 1.1
|
||||
*
|
||||
* Copyright (c) 2003 The Apache Software Foundation. All rights
|
||||
* reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in
|
||||
* the documentation and/or other materials provided with the
|
||||
* distribution.
|
||||
*
|
||||
* 3. The end-user documentation included with the redistribution, if
|
||||
* any, must include the following acknowlegement:
|
||||
* "This product includes software developed by the
|
||||
* Apache Software Foundation (http://www.apache.org/)."
|
||||
* Alternately, this acknowlegement may appear in the software itself,
|
||||
* if and wherever such third-party acknowlegements normally appear.
|
||||
*
|
||||
* 4. The names "The Jakarta Project", "Commons", and "Apache Software
|
||||
* Foundation" must not be used to endorse or promote products derived
|
||||
* from this software without prior written permission. For written
|
||||
* permission, please contact apache@apache.org.
|
||||
*
|
||||
* 5. Products derived from this software may not be called "Apache"
|
||||
* nor may "Apache" appear in their names without prior written
|
||||
* permission of the Apache Software Foundation.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
|
||||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
||||
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
||||
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
* ====================================================================
|
||||
*
|
||||
* This software consists of voluntary contributions made by many
|
||||
* individuals on behalf of the Apache Software Foundation. For more
|
||||
* information on the Apache Software Foundation, please see
|
||||
* <http://www.apache.org/>.
|
||||
*/
|
||||
package org.apache.commons.math.analysis;
|
||||
|
||||
import org.apache.commons.math.MathException;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
/**
|
||||
* Test the interpolation framework.
|
||||
*
|
||||
* @author pietsch at apache.org
|
||||
*
|
||||
*/
|
||||
public class InterpolatorTest extends TestCase {
|
||||
|
||||
public InterpolatorTest(String name) {
|
||||
super(name);
|
||||
}
|
||||
|
||||
public static Test suite() {
|
||||
TestSuite suite = new TestSuite(InterpolatorTest.class);
|
||||
suite.setName("UnivariateRealInterpolator Tests");
|
||||
return suite;
|
||||
}
|
||||
|
||||
public void testInterpolateLinearDegenerateTwoSegment()
|
||||
throws MathException {
|
||||
System.out.println(" deg 2 seg");
|
||||
double xval[] = { 0.0, 0.5, 1.0 };
|
||||
double yval[] = { 0.0, 0.5, 1.0 };
|
||||
UnivariateRealInterpolator i = new SplineInterpolator();
|
||||
UnivariateRealFunction f = i.interpolate(xval, yval);
|
||||
double x;
|
||||
x = 0.0;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 0.5;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 1 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
}
|
||||
|
||||
public void testInterpolateLinearDegenerateThreeSegment()
|
||||
throws MathException {
|
||||
System.out.println(" deg 3 seg");
|
||||
double xval[] = { 0.0, 0.5, 1.0, 1.5 };
|
||||
double yval[] = { 0.0, 0.5, 1.0, 1.5 };
|
||||
UnivariateRealInterpolator i = new SplineInterpolator();
|
||||
UnivariateRealFunction f = i.interpolate(xval, yval);
|
||||
double x;
|
||||
x = 0.0;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 0.5 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 0.5;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 1 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 1;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 1.5 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
}
|
||||
|
||||
public void testInterpolateLinear() throws MathException {
|
||||
System.out.println(" triang 2 seg");
|
||||
double xval[] = { 0.0, 0.5, 1.0 };
|
||||
double yval[] = { 0.0, 0.5, 0.0 };
|
||||
UnivariateRealInterpolator i = new SplineInterpolator();
|
||||
UnivariateRealFunction f = i.interpolate(xval, yval);
|
||||
double x;
|
||||
x = 0.0;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 0.5 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 0.5;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 1 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
}
|
||||
|
||||
public void testInterpolateSin() throws MathException {
|
||||
System.out.println(" sin");
|
||||
double xval[] =
|
||||
{
|
||||
0.0,
|
||||
Math.PI / 6.0,
|
||||
Math.PI / 2.0,
|
||||
5.0 * Math.PI / 6.0,
|
||||
Math.PI,
|
||||
7.0 * Math.PI / 6.0,
|
||||
3.0 * Math.PI / 2.0,
|
||||
11.0 * Math.PI / 6.0,
|
||||
2.0 * Math.PI };
|
||||
double yval[] = { 0.0, 0.5, 1.0, 0.5, 0.0, -0.5, -1.0, -0.5, 0.0 };
|
||||
|
||||
System.out.println("n=" + xval.length);
|
||||
UnivariateRealInterpolator i = new SplineInterpolator();
|
||||
UnivariateRealFunction f = i.interpolate(xval, yval);
|
||||
double x;
|
||||
x = 0.0;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = Math.PI / 6.0 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = Math.PI / 6.0 + 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = Math.PI / 2 - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = Math.PI / 2 + 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = Math.PI - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = Math.PI + 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
x = 2.0 * Math.PI - 1E-6;
|
||||
System.out.println(
|
||||
"x="
|
||||
+ x
|
||||
+ " y="
|
||||
+ f.value(x)
|
||||
+ " y'="
|
||||
+ f.firstDerivative(x)
|
||||
+ " y''="
|
||||
+ f.secondDerivative(x));
|
||||
//assertEquals(0.5,f.value(Math.PI/6.0),)
|
||||
}
|
||||
|
||||
public void testIllegalArguments() throws MathException {
|
||||
// Data set arrays of different size.
|
||||
UnivariateRealInterpolator i = new SplineInterpolator();
|
||||
try {
|
||||
double xval[] = { 0.0, 1.0 };
|
||||
double yval[] = { 0.0, 1.0, 2.0 };
|
||||
UnivariateRealFunction f = i.interpolate(xval, yval);
|
||||
fail("Failed to detect data set array with different sizes.");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
}
|
||||
// X values not sorted.
|
||||
try {
|
||||
double xval[] = { 0.0, 1.0, 0.5 };
|
||||
double yval[] = { 0.0, 1.0, 2.0 };
|
||||
UnivariateRealFunction f = i.interpolate(xval, yval);
|
||||
fail("Failed to detect unsorted arguments.");
|
||||
} catch (IllegalArgumentException iae) {
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue