Java:使用多项式样条函数 (Apache) 将数据集加倍

标签 java arrays interpolation spline

我正在尝试使用 SplineInterpolator PolynomialSplineFunction将数据集加倍。我认为我已经走得很远了(我可能错过了一些异常处理):

SplineInterpolator splineInterp;

public double[] doubledArray(double[] y){
    double[] yy = new double[y.length*2];
    // make a double version of y w/ -1 for "null" values
    for(int i = 0; i < yy.length; i++){
        if(i%2 == 0)
            yy[i] = y[i];
        else if(i == yy.length-1)
            yy[i] = yy[0];
        else
            yy[i] = -1;
    }
    // make a corresponding x array to satisfy SplineInterpolator.interpolate
    double[] x = new double[y.length];
    for(int i = 0; i < x.length; i++)
        x[i] = i;
    splineInterp = new SplineInterpolator();
    PolynomialSplineFunction polySplineF = splineInterp.interpolate(x, y);
    for(int i = 0; i < yy.length; i++){
        if(yy[i] == -1){
            yy[i] = polySplineF.value(i);
           // breaks down halfway through polySplineF.value expects and array of y.length
        }
    }
    return yy;
}

但是上面的代码最迟会在最后一个 for 循环中崩溃。那么,我的第一部分或多或少是正确的吗?有了多项式样条函数后,如何使用它来创建更大的数据集?

最佳答案

如果有人在家里跟随,这是我为此想出的实现:

private double[] multiplyArray(double[] y){
    // An array 2 or 4 or N times bigger than the original:
    double[] yy = new double[y.length*arrayMultiplier];
    // An array representing the indices of the original:
    double[] x = new double[y.length];
    for(int i = 0; i < x.length; i++)
        x[i] = i;
    // Get and instance of SplineInterpolator:
    SplineInterpolator splineInterp = new SplineInterpolator();
    // Use that instance's interpolate() function to a PolynomialSplineFunction
    // fitting your data, points y at indices x.
    PolynomialSplineFunction polySplineF = splineInterp.interpolate(x, y);

    // Use the PolynomialSplineFunction to fill in your larger array by supplying
    // index values divided by the arrayMultiplier
    for(int i = 0; i < yy.length; i++){
        yy[i] = polySplineF.value((double)(i/arrayMultiplier));
    }
    return yy;
}

如果有人需要的话,我还想出了如何进行可能更有用的填空用法。

关于Java:使用多项式样条函数 (Apache) 将数据集加倍,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497136/

相关文章:

java - 如何在android中为日历事件设置多个闹钟?

javascript - 在 Javascript 的 For 循环中从另一个数组填充数组

php - 如何根据值将数组拆分为两个不同的数组?

image - 非均匀插值

java - 如何实现多客户端与服务器聊天

java - 序列化:忽略一个包含空值的元素的列表属性

java - 创建数组时堆空间不足

nlp - 插值权重

python - 将价目表从较长的长度调整为较小的长度

java - Teamcity 构建计数器未传递到 Ant