简体   繁体   中英

How to use BicubicSplineInterpolator in Apache Commons Math?

I have three arrays x,y and value. for each x,y , f(x,y) = value;

I did not understand how to use the BicubicSplineInterpolator class. I need to find values for different x and y

Here is a link to the class http://commons.apache.org/proper/commons-math/javadocs/api-3.3/org/apache/commons/math3/analysis/interpolation/BicubicSplineInterpolator.html

TIA

From the doc, BicubicSplineInterpolator() requires the data points to form a grid-like pattern. Therefore, you should provide an array of x values (length = m) and an array of y values (length = m) and a matrix of function values (length= mxn).

I agree the docs are quite counter-intuitive. To make things worse, BicubicSplineInterpolator() is buggy, see https://issues.apache.org/jira/browse/MATH-1166 , use instead BicubicInterpolator as documented at http://commons.apache.org/proper/commons-math/changes-report.html .

The interpolation works with a regular grid (eg a 3x3 grid, and you have a value at each grid point).

You need to define the grid positions. (In this example, we have 0, 128, 256 in both x and y dimension. However these are just numbers, you can have on X eg temperature, and humidity on Y with different ranges.)

Then define a matrix with the actual values at each grid point, make an interpolator which returns an interpolating function, which can calculate any value at any x,y.

    final double[] xValues = new double[] {0,128,256};
    final double[] yValues = new double[] {0,128,256};

    final double[][] fValues = new double[][] {{1, 0, 1},
        {0, 0, 1},
        {0, 0, 1}};

        final BivariateGridInterpolator interpolator = new BicubicInterpolator();
        final BivariateFunction function = interpolator.interpolate(xValues, yValues,fValues);

        for (int y=0;y<255;y++) {
            for (int x=0;x<255;x++) {
                double value=function.value(x,  y);
                // do something with this
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM