PR: 34230

Fixed bug in PolynomialSplineFunction to allow evaluation of the function at the last knot point.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@159727 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Brent Worden 2005-04-01 20:45:21 +00:00
parent 5fc7e64e55
commit ee310159d9
3 changed files with 23 additions and 9 deletions

View File

@ -36,7 +36,7 @@ import org.apache.commons.math.FunctionEvaluationException;
* centered on the knot points to compute the spline function values. See below.
* <p>
* The domain of the polynomial spline function is
* <code>[smallest knot, largest knot)</code>. Attempts to evaluate the
* <code>[smallest knot, largest knot]</code>. Attempts to evaluate the
* function at values outside of this range generate IllegalArgumentExceptions.
* <p>
* The value of the polynomial spline function for an argument <code>x</code>
@ -44,7 +44,7 @@ import org.apache.commons.math.FunctionEvaluationException;
* <ol>
* <li>The knot array is searched to find the segment to which <code>x</code>
* belongs. If <code>x</code> is less than the smallest knot point or greater
* than or equal to the largest one, an <code>IllegalArgumentException</code>
* than the largest one, an <code>IllegalArgumentException</code>
* is thrown.</li>
* <li> Let <code>j</code> be the index of the largest knot point that is less
* than or equal to <code>x</code>. The value returned is <br>
@ -116,7 +116,7 @@ public class PolynomialSplineFunction implements UnivariateRealFunction, Seriali
* Compute the value for the function.
* <p>
* Throws FunctionEvaluationException if v is outside of the domain of the
* function. The domain is [smallest knot, largest knot).
* function. The domain is [smallest knot, largest knot].
* <p>
* See {@link PolynomialSplineFunction} for details on the algorithm for
* computing the value of the function.
@ -125,16 +125,22 @@ public class PolynomialSplineFunction implements UnivariateRealFunction, Seriali
* @return the value
* @throws FunctionEvaluationException if v is outside of the domain of
* of the spline function (less than the smallest knot point or greater
* than or equal to the largest knot point)
* than the largest knot point)
*/
public double value(double v) throws FunctionEvaluationException {
if (v < knots[0] || v >= knots[n]) {
if (v < knots[0] || v > knots[n]) {
throw new FunctionEvaluationException(v,"Argument outside domain");
}
int i = Arrays.binarySearch(knots, v);
if (i < 0) {
i = -i - 2;
}
//This will handle the case where v is the last knot value
//There are only n-1 polynomials, so if v is the last knot
//then we will use the last polynomial to calculate the value.
if ( i >= polynomials.length ) {
i--;
}
return polynomials[i].value(v - knots[i]);
}

View File

@ -67,7 +67,9 @@ public class SplineInterpolatorTest extends TestCase {
TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance);
// Check interpolation
assertEquals(0.0,f.value(0.0), interpolationTolerance);
assertEquals(0.4,f.value(0.4), interpolationTolerance);
assertEquals(1.0,f.value(1.0), interpolationTolerance);
}
public void testInterpolateLinearDegenerateThreeSegment()
@ -88,7 +90,9 @@ public class SplineInterpolatorTest extends TestCase {
TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance);
// Check interpolation
assertEquals(0,f.value(0), interpolationTolerance);
assertEquals(1.4,f.value(1.4), interpolationTolerance);
assertEquals(1.5,f.value(1.5), interpolationTolerance);
}
public void testInterpolateLinear() throws Exception {
@ -179,11 +183,11 @@ public class SplineInterpolatorTest extends TestCase {
}
/**
* verifies that f(x[i]) = y[i] for i = 0..n -1 where n is common length -- skips last point.
* verifies that f(x[i]) = y[i] for i = 0..n-1 where n is common length.
*/
protected void verifyInterpolation(UnivariateRealFunction f, double x[], double y[])
throws Exception{
for (int i = 0; i < x.length - 1; i++) {
for (int i = 0; i < x.length; i++) {
assertEquals(f.value(x[i]), y[i], knotTolerance);
}
}

View File

@ -39,6 +39,10 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.1" date="In Development"
description="Jakarta Commons Math 1.1 - Development">
<action dev="brentworden" type="fix" due-to="Ben Litchfield">
Fixed bug in PolynomialSplineFunction to allow evaluation of the
function at the last knot point.
</action>
<action dev="brentworden" type="add">
Added Weibull distribution implementation.
</action>