[ 
https://issues.apache.org/jira/browse/MATH-985?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Johnathan Kool updated MATH-985:
--------------------------------

    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:

{code:title=SplineTest.java|borderStyle=solid}
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));
        }
}
{code}

and next, 2D which doesn't:

{code:title=BicubicSplineTest.java|borderStyle=solid}
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));
        }
}
{code}

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 guessing it might be a 
precision issue(?), but thought I should flag it.

  was:
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:

{code:title=SplineTest.java|borderStyle=solid}
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));
        }
}
{code}

and next, 2D which doesn't:

{code:title=BicubicSplineTest.java|borderStyle=solid}
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};
        double[][][]vals = new double[][][]{v2,v2,v2};
        
        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));
        }
}
{code}

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 guessing it might be a 
precision issue(?), but thought I should flag it.

    
> BicubicSpline interpolation returns unexpected values 
> (BicubicSplineInterpolator/BicubicSplineInterpolationFunction)
> --------------------------------------------------------------------------------------------------------------------
>
>                 Key: MATH-985
>                 URL: https://issues.apache.org/jira/browse/MATH-985
>             Project: Commons Math
>          Issue Type: Bug
>    Affects Versions: 3.1.1
>         Environment: Windows 7 64 bit, 64 bit JVM
>            Reporter: Johnathan Kool
>
> 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:
> {code:title=SplineTest.java|borderStyle=solid}
> 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));
>       }
> }
> {code}
> and next, 2D which doesn't:
> {code:title=BicubicSplineTest.java|borderStyle=solid}
> 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));
>       }
> }
> {code}
> 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 guessing it might be a 
> precision issue(?), but thought I should flag it.

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to