Java 使用ApacheMath使用样条函数调整1D数组大小-如何?

Java 使用ApacheMath使用样条函数调整1D数组大小-如何?,java,math,apache-commons,spline,apache-commons-math,Java,Math,Apache Commons,Spline,Apache Commons Math,我正在寻找一个使用ApacheCommons-Math的样条函数调整1D数组大小的示例 我需要的是一种方法来扩展和/或收缩输入数组(double[]) 我找不到一个在线搜索的好例子。这里的技巧是,您需要两个数组来创建样条线,但您只有一个。因此,您需要制作一个阵列。您可以假设输入数组包含您的y值,并且新制作的数组包含您的x值,因此对于任何给定的x都有相应的y 免责声明,我尚未测试此代码,因此请确保进行相应调整 // To expand the array public static double[

我正在寻找一个使用ApacheCommons-Math的样条函数调整1D数组大小的示例

我需要的是一种方法来扩展和/或收缩输入数组(double[])


我找不到一个在线搜索的好例子。

这里的技巧是,您需要两个
数组来创建
样条线,但您只有一个。因此,您需要制作一个
阵列
。您可以假设输入
数组
包含您的
y
值,并且新制作的数组包含您的
x
值,因此对于任何给定的
x
都有相应的
y

免责声明,我尚未测试此代码,因此请确保进行相应调整

// To expand the array
public static double[] expand(double[] array, int newSize) {

    final int length = array.length;

    // let's calculate the new step size
    double step = (double) length / (newSize + 1);

    // fabricated array of x values
    double[] x = new double[length];
    for(int i = 0; i < length; ++i) {
        x[i] = i;
    }

    // using Linear interpolator but it can be any other interpolator
    LinearInterpolator li = new LinearInterpolator(); // or other interpolator
    PolynomialSplineFunction psf = li.interpolate(x, array);

    double[] expandedArray = new double[newSize];
    double xi = x[0];
    for (int i = 0; i < newSize - 1; ++i) {
       expandedArray[i] = psf.value(xi);
       xi += step;
    }
    expandedArray[newSize - 1] = array[length - 1];
    return expandedArray;
}
// To shrink the array
public static double[] shrink(double[] array, int newSize) {

    final int length = array.length;

    // let's calculate the new step size
    double step = (double) length / (newSize - 1);

    // fabricated array of x values
    double[] x = new double[length];
    for(int i = 0; i < length; ++i) {
        x[i] = i;
    }

    // using Linear interpolator but it can be any other interpolator
    LinearInterpolator li = new LinearInterpolator(); // or other interpolator
    PolynomialSplineFunction psf = li.interpolate(x, array);

    double[] expandedArray = new double[newSize];
    double xi = x[0];
    for (int i = 0; i < newSize - 1; ++i) {
       expandedArray[i] = psf.value(xi);
       xi += step;
    }
    expandedArray[newSize - 1] = array[length - 1];
    return expandedArray;
}