[使用Apache Math用样条函数调整一维数组的大小-如何?

问题描述 投票:0回答:1

[我正在寻找有关使用样条函数和Apache Commons-Math调整一维数组大小的示例。

我需要的是扩展和/或缩小输入数组(double [])的方法。

我找不到试图在线搜索的好例子。

java math apache-commons spline apache-commons-math
1个回答
1
投票

这里的窍门是,您需要两个arrays才能创建一个spline,但只有一个。因此,您需要fabricate一个array。您可以假设输入array包含您的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 = (length * 1.0) / newSize;

    // fabricated array of x values
    int[] x = new int[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; ++i) {
       expandedArray[i] = psf.value(xi);
       xi += step;
    }
    return expandedArray ;
}

对于shrink数组,您可以decimate输入array,即,仅创建一个较小的新array,然后根据新步长取值,或使用上述interpolator

© www.soinside.com 2019 - 2024. All rights reserved.