@Test
public void testForwardFlat() {
QL.info("Testing forward-flat interpolation...");
final Array x = new Array(new double[] { 0.0, 1.0, 2.0, 3.0, 4.0 });
final Array y = new Array(new double[] { 5.0, 4.0, 3.0, 2.0, 1.0 });
final Interpolation f = new ForwardFlatInterpolation(x, y);
f.update();
final int N = x.size();
final double tolerance = 1.0e-12;
// at original points
for (int i=0; i<N; i++) {
final double p = x.get(i);
final double calculated = f.op(p);
final double expected = y.get(i);
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);
}
// at middle points
for (int i=0; i<N-1; i++) {
final double p = (x.get(i) + x.get(i+1))/2;
final double calculated = f.op(p);
final double expected = y.get(i);
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);
}
// outside the original range
f.enableExtrapolation();
{
double p = x.get(0) - 0.5;
double calculated = f.op(p);
double expected = y.get(0);
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);
p = x.get(N-1) + 0.5;
calculated = f.op(p);
expected = y.get(N-1);
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);
// primitive at original points
calculated = f.primitive(x.get(0));
expected = 0.0;
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);
}
double sum = 0.0;
for (int i=1; i<N; i++) {
sum += (x.get(i) - x.get(i-1)) * y.get(i-1);
final double calculated = f.primitive(x.get(i));
final double expected = sum;
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);
}
// primitive at middle points
sum = 0.0;
for (int i=0; i<N-1; i++) {
final double p = (x.get(i) + x.get(i+1))/2;
sum += (x.get(i+1) - x.get(i)) * y.get(i)/2;
final double calculated = f.primitive(p);
final double expected = sum;
sum += (x.get(i+1) - x.get(i)) * y.get(i)/2;
assertFalse("failed to reproduce expected datum"
+"\n expected value: "+expected
+"\n calculated value: "+calculated
+"\n error: "+Math.abs(calculated-expected),
Math.abs(calculated - expected) > tolerance);