Uploaded image for project: 'Commons Math'
  1. Commons Math
  2. MATH-985

BicubicSpline interpolation returns unexpected values (BicubicSplineInterpolator/BicubicSplineInterpolationFunction)

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 3.1.1
    • Fix Version/s: 3.3
    • Labels:
      None
    • Environment:

      Windows 7 64 bit, 64 bit JVM

      Description

      I've been testing out the Tricubic spline functions, and have been getting some strange values. I dug down a bit, and it seems like they start at the Bicubic level. SplineInterpolator/PolynomialSplineFunction seem to be returning correct values. I set up a block of data that increases linearly, so you'd expect the interpolated values to follow the same trend, except the values tend to overshoot to half the distance between knot points, and then undershoot for the remaining half. Probably the easiest thing would be to show some tests. First - 1D which works fine:

      SplineTest.java
      import org.apache.commons.math3.analysis.interpolation.SplineInterpolator;
      import org.apache.commons.math3.analysis.polynomials.PolynomialSplineFunction;
      
      public class SplineTest {
      
      	double[] x = new double[]{.52,.54,.56,.58,.6};
      	double[] y = new double[]{76,77,78,79,80};
      	
      	public static void main(String[] args){
      		SplineTest st = new SplineTest();
      		st.go();
      	}
      	
      	public void go(){
      		SplineInterpolator si = new SplineInterpolator();
      		PolynomialSplineFunction sf = si.interpolate(x, y);
      		
      		System.out.println(sf.value(0.52));
      		System.out.println(sf.value(0.5225));
      		System.out.println(sf.value(0.525));
      		System.out.println(sf.value(0.5275));
      		System.out.println(sf.value(0.53));
      		System.out.println(sf.value(0.5325));
      		System.out.println(sf.value(0.535));
      		System.out.println(sf.value(0.5375));
      		System.out.println(sf.value(0.54));
      		System.out.println(sf.value(0.5425));
      		System.out.println(sf.value(0.545));
      		System.out.println(sf.value(0.5475));
      		System.out.println(sf.value(0.55));
      		System.out.println(sf.value(0.5525));
      		System.out.println(sf.value(0.555));
      		System.out.println(sf.value(0.5575));
      		System.out.println(sf.value(0.56));
      	}
      }
      

      and next, 2D which doesn't:

      BicubicSplineTest.java
      import org.apache.commons.math3.analysis.interpolation.BicubicSplineInterpolatingFunction;
      import org.apache.commons.math3.analysis.interpolation.BicubicSplineInterpolator;
      
      public class BicubicSplineTest {
      
      	double[] x = new double[]{0,1,2};
      	double[] y = new double[]{.52,.54,.56,.58,.6};
      	double[] v1 = new double[]{76,77,78,79,80};
      	double[][] v2 = new double[][]{v1,v1,v1};
      	
      	public static void main(String[] args){
      		BicubicSplineTest bt = new BicubicSplineTest();
      		bt.go();
      	}
      	
      	public void go(){
      		BicubicSplineInterpolator bi = new BicubicSplineInterpolator();
      		BicubicSplineInterpolatingFunction bf = bi.interpolate(x, y, v2);
      		
      		System.out.println(bf.value(1, 0.52));
      		System.out.println(bf.value(1, 0.5225));
      		System.out.println(bf.value(1, 0.525));
      		System.out.println(bf.value(1, 0.5275));
      		System.out.println(bf.value(1, 0.53));
      		System.out.println(bf.value(1, 0.5325));
      		System.out.println(bf.value(1, 0.535));
      		System.out.println(bf.value(1, 0.5375));
      		System.out.println(bf.value(1, 0.54));
      		System.out.println(bf.value(1, 0.5425));
      		System.out.println(bf.value(1, 0.545));
      		System.out.println(bf.value(1, 0.5475));
      		System.out.println(bf.value(1, 0.55));
      		System.out.println(bf.value(1, 0.5525));
      		System.out.println(bf.value(1, 0.555));
      		System.out.println(bf.value(1, 0.5575));
      		System.out.println(bf.value(1, 0.56));
      	}
      }
      

      The data points increase from 76 to 80 in a linear way. Incrementing by 1/8 the distance to the next point, the 1D spline returns:

      76.0
      76.125
      76.25
      76.375
      76.5
      76.625
      76.75
      76.875
      77.0
      77.125
      77.25
      77.375
      77.5
      77.625
      77.75
      77.875
      78.0

      The 2D spline returns:

      76.0
      80.14453124999996
      80.84375000000003
      79.24609375000007
      76.50000000000003
      73.75390625000007
      72.15625000000001
      72.85546874999996
      76.99999999999997
      81.14453124999993
      81.84374999999997
      80.24609375000006
      77.50000000000003
      74.75390625000009
      73.15625000000004
      73.85546874999997
      78.0

      Even though it's still effectively a 1D problem. I'm not sure exactly what's causing it - maybe something when multiplying the coefficients, but thought I should flag it.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                jkool Johnathan Kool
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: