MATH-989
Added method to check whether a point is within the interpolation range. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1491606 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
b23c8567c3
commit
25ad33cb9e
|
@ -172,6 +172,24 @@ public class BicubicSplineInterpolatingFunction
|
||||||
return splines[i][j].value(xN, yN);
|
return splines[i][j].value(xN, yN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Indicates whether a point is within the interpolation range.
|
||||||
|
*
|
||||||
|
* @param x First coordinate.
|
||||||
|
* @param y Second coordinate.
|
||||||
|
* @return {@code true} if (x, y) is a valid point.
|
||||||
|
*/
|
||||||
|
public boolean isValidPoint(double x, double y) {
|
||||||
|
if (x < xval[0] ||
|
||||||
|
x > xval[xval.length - 1] ||
|
||||||
|
y < yval[0] ||
|
||||||
|
y > yval[yval.length - 1]) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param x x-coordinate.
|
* @param x x-coordinate.
|
||||||
* @param y y-coordinate.
|
* @param y y-coordinate.
|
||||||
|
|
|
@ -18,6 +18,7 @@ package org.apache.commons.math3.analysis.interpolation;
|
||||||
|
|
||||||
import org.apache.commons.math3.exception.DimensionMismatchException;
|
import org.apache.commons.math3.exception.DimensionMismatchException;
|
||||||
import org.apache.commons.math3.exception.MathIllegalArgumentException;
|
import org.apache.commons.math3.exception.MathIllegalArgumentException;
|
||||||
|
import org.apache.commons.math3.exception.OutOfRangeException;
|
||||||
import org.apache.commons.math3.analysis.BivariateFunction;
|
import org.apache.commons.math3.analysis.BivariateFunction;
|
||||||
import org.apache.commons.math3.distribution.UniformRealDistribution;
|
import org.apache.commons.math3.distribution.UniformRealDistribution;
|
||||||
import org.apache.commons.math3.random.RandomGenerator;
|
import org.apache.commons.math3.random.RandomGenerator;
|
||||||
|
@ -606,4 +607,64 @@ public final class BicubicSplineInterpolatingFunctionTest {
|
||||||
// System.out.println();
|
// System.out.println();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testIsValidPoint() {
|
||||||
|
final double xMin = -12;
|
||||||
|
final double xMax = 34;
|
||||||
|
final double yMin = 5;
|
||||||
|
final double yMax = 67;
|
||||||
|
final double[] xval = new double[] { xMin, xMax };
|
||||||
|
final double[] yval = new double[] { yMin, yMax };
|
||||||
|
final double[][] f = new double[][] { { 1, 2 },
|
||||||
|
{ 3, 4 } };
|
||||||
|
final double[][] dFdX = f;
|
||||||
|
final double[][] dFdY = f;
|
||||||
|
final double[][] dFdXdY = f;
|
||||||
|
|
||||||
|
final BicubicSplineInterpolatingFunction bcf
|
||||||
|
= new BicubicSplineInterpolatingFunction(xval, yval, f,
|
||||||
|
dFdX, dFdY, dFdXdY);
|
||||||
|
|
||||||
|
double x, y;
|
||||||
|
|
||||||
|
x = xMin;
|
||||||
|
y = yMin;
|
||||||
|
Assert.assertTrue(bcf.isValidPoint(x, y));
|
||||||
|
// Ensure that no exception is thrown.
|
||||||
|
bcf.value(x, y);
|
||||||
|
|
||||||
|
x = xMax;
|
||||||
|
y = yMax;
|
||||||
|
Assert.assertTrue(bcf.isValidPoint(x, y));
|
||||||
|
// Ensure that no exception is thrown.
|
||||||
|
bcf.value(x, y);
|
||||||
|
|
||||||
|
final double xRange = xMax - xMin;
|
||||||
|
final double yRange = yMax - yMin;
|
||||||
|
x = xMin + xRange / 3.4;
|
||||||
|
y = yMin + yRange / 1.2;
|
||||||
|
Assert.assertTrue(bcf.isValidPoint(x, y));
|
||||||
|
// Ensure that no exception is thrown.
|
||||||
|
bcf.value(x, y);
|
||||||
|
|
||||||
|
final double small = 1e-8;
|
||||||
|
x = xMin - small;
|
||||||
|
y = yMax;
|
||||||
|
Assert.assertFalse(bcf.isValidPoint(x, y));
|
||||||
|
// Ensure that an exception would have been thrown.
|
||||||
|
try {
|
||||||
|
bcf.value(x, y);
|
||||||
|
Assert.fail("OutOfRangeException expected");
|
||||||
|
} catch (OutOfRangeException expected) {}
|
||||||
|
|
||||||
|
x = xMin;
|
||||||
|
y = yMax + small;
|
||||||
|
Assert.assertFalse(bcf.isValidPoint(x, y));
|
||||||
|
// Ensure that an exception would have been thrown.
|
||||||
|
try {
|
||||||
|
bcf.value(x, y);
|
||||||
|
Assert.fail("OutOfRangeException expected");
|
||||||
|
} catch (OutOfRangeException expected) {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue