Author: brentworden Date: Fri Apr 1 12:45:21 2005 New Revision: 159727 URL: http://svn.apache.org/viewcvs?view=rev&rev=159727 Log: PR: 34230 Fixed bug in PolynomialSplineFunction to allow evaluation of the function at the last knot point.
Modified: jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/PolynomialSplineFunction.java jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/SplineInterpolatorTest.java jakarta/commons/proper/math/trunk/xdocs/changes.xml Modified: jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/PolynomialSplineFunction.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/PolynomialSplineFunction.java?view=diff&r1=159726&r2=159727 ============================================================================== --- jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/PolynomialSplineFunction.java (original) +++ jakarta/commons/proper/math/trunk/src/java/org/apache/commons/math/analysis/PolynomialSplineFunction.java Fri Apr 1 12:45:21 2005 @@ -36,7 +36,7 @@ * 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 @@ * <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 @@ * 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 [EMAIL PROTECTED] PolynomialSplineFunction} for details on the algorithm for * computing the value of the function. @@ -125,15 +125,21 @@ * @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]); } Modified: jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/SplineInterpolatorTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/SplineInterpolatorTest.java?view=diff&r1=159726&r2=159727 ============================================================================== --- jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/SplineInterpolatorTest.java (original) +++ jakarta/commons/proper/math/trunk/src/test/org/apache/commons/math/analysis/SplineInterpolatorTest.java Fri Apr 1 12:45:21 2005 @@ -67,7 +67,9 @@ TestUtils.assertEquals(polynomials[1].getCoefficients(), target, coefficientTolerance); // Check interpolation - assertEquals(0.4,f.value(0.4), interpolationTolerance); + 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 @@ TestUtils.assertEquals(polynomials[2].getCoefficients(), target, coefficientTolerance); // Check interpolation - assertEquals(1.4,f.value(1.4), interpolationTolerance); + 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 @@ } /** - * 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); } } Modified: jakarta/commons/proper/math/trunk/xdocs/changes.xml URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/math/trunk/xdocs/changes.xml?view=diff&r1=159726&r2=159727 ============================================================================== --- jakarta/commons/proper/math/trunk/xdocs/changes.xml (original) +++ jakarta/commons/proper/math/trunk/xdocs/changes.xml Fri Apr 1 12:45:21 2005 @@ -39,6 +39,10 @@ <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> --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]