MATH-991
Method to check whether a point is within the interpolation range. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1491625 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
25ad33cb9e
commit
1e6e14cb5f
|
@ -229,4 +229,19 @@ public class PolynomialSplineFunction implements UnivariateDifferentiableFunctio
|
|||
System.arraycopy(knots, 0, out, 0, n + 1);
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether a point is within the interpolation range.
|
||||
*
|
||||
* @param x Point.
|
||||
* @return {@code true} if {@code x} is a valid point.
|
||||
*/
|
||||
public boolean isValidPoint(double x) {
|
||||
if (x < knots[0] ||
|
||||
x > knots[n]) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -134,6 +134,41 @@ public class PolynomialSplineFunctionTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIsValidPoint() {
|
||||
final PolynomialSplineFunction spline =
|
||||
new PolynomialSplineFunction(knots, polynomials);
|
||||
final double xMin = knots[0];
|
||||
final double xMax = knots[knots.length - 1];
|
||||
|
||||
double x;
|
||||
|
||||
x = xMin;
|
||||
Assert.assertTrue(spline.isValidPoint(x));
|
||||
// Ensure that no exception is thrown.
|
||||
spline.value(x);
|
||||
|
||||
x = xMax;
|
||||
Assert.assertTrue(spline.isValidPoint(x));
|
||||
// Ensure that no exception is thrown.
|
||||
spline.value(x);
|
||||
|
||||
final double xRange = xMax - xMin;
|
||||
x = xMin + xRange / 3.4;
|
||||
Assert.assertTrue(spline.isValidPoint(x));
|
||||
// Ensure that no exception is thrown.
|
||||
spline.value(x);
|
||||
|
||||
final double small = 1e-8;
|
||||
x = xMin - small;
|
||||
Assert.assertFalse(spline.isValidPoint(x));
|
||||
// Ensure that an exception would have been thrown.
|
||||
try {
|
||||
spline.value(x);
|
||||
Assert.fail("OutOfRangeException expected");
|
||||
} catch (OutOfRangeException expected) {}
|
||||
}
|
||||
|
||||
/**
|
||||
* Do linear search to find largest knot point less than or equal to x.
|
||||
* Implementation does binary search.
|
||||
|
|
Loading…
Reference in New Issue